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.
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.
Here I have two String members in classes implementing interface
Foo
. The firstvarName
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.