0

I want to add all the abstracts of getter and setter of a class to the interface that I am implementing in that particular interface. I also want to generate a final variable that resembles class variable. This variable can be used as string to access class variable after deserializing.

Eg:

public class Abc implements IAbc{

private String oneVariable;

 public String getOneVariable(){
    return oneVariable;
 }
}

On implementing the above class with interface IAbc. IAbc should contain the following code:

public interface IAbc{
  public static final String ONE_VARIABLE = "oneVariable";

  public getOneVariable();

}

I have tried googling for the solution but could not get any. Also the methods in class should have the annotation @Override after this code is generated.

1 Answers1

0

TL;DR This is an interesting programming challenge, but I find very little use for this in real life scenarios.
Here the name of the first string variable is known before hand, you can directly store the final value in this instead of the round-about way of storing the name of second variable.


If understand correctly you are trying to access a (String) variable of a class whose name will be in another string variable. This is possible using java reflection.

Additionally you want this code to be placed in an interface such that it can be (re)used in all the classes implementing it.

import java.lang.reflect.Field;

interface Foo {

    public default String getStringVar() 
            throws NoSuchFieldException, IllegalAccessException {
        // get varName
        Class thisCls = this.getClass();
        Field varNameField = thisCls.getField("varName");
        String varName = (String) varNameField.get(this);

        // get String variable of name `varName`
        Field strField = thisCls.getField(varName);
        String value = (String) strField.get(this);

        return value;
    }
}


class FooImpl1 implements Foo {
    public final String varName = "str1";
    public String str1 = "some value";
}

class FooImpl2 implements Foo {
    public final String varName = "str2";
    public String str2 = "some other value";
}

class Main {
    public static void main(String[] args) 
            throws NoSuchFieldException, IllegalAccessException {
        System.out.println(new FooImpl1().getStringVar());
        System.out.println(new FooImpl2().getStringVar());
    }
}

Here I have two String members in classes implementing interface Foo. The first varName contains the variable name of the second String, second String variable contains the data.
In the interface using reflection I am first extracting the variable name stored in varName, then using this extracting the value of second String.

shanmuga
  • 4,329
  • 2
  • 21
  • 35
  • I don't want to write code for this I want a template that can be used in eclipse when I create a class variable and save. The corresponding interface will be updated as mentioned. The real world application is that this will only be use for isolated enity which has only one class per interface. This interface is used to access methods of a class(getter and setter). Also, final variable in the interface are used to access object when it is converted into map and you want to get that particular variable from map. – niraj dighe Jun 02 '18 at 08:23
  • Nope. Interface affects the class, not the other way around. That would be anti pattern. Thats why you couldn't find anything for it. Look at it this way, interfaces define certain structure the classes should have. Compiler checks the if this if classes adhere to this structure. If you allowed classes to affect interface, it would form circular dependency. – shanmuga Jun 02 '18 at 17:18
  • Actually It won't because the template for which class I require this only has one class per interface. – niraj dighe Jun 05 '18 at 07:00