I would like to know if its possible in Java, to use method's parameter literal name as value?
I have a method like this:
protected String resolve(String param, String prop) {
return Utils.isNullOrBlank(System.getProperty(prop))
? param
: System.getProperty(prop);
}
The problem is every param
is called say env
, browser
, and every prop
is called "myenv"
, "mybrowser"
.
So every time I call resolve method I need to write something like resolve(env, "myenv")
.
Is there possibility to make this method take only one argument, param
, take it literal name and add "my"
as prefix, instead of passing another prop
argument?
CodePudding user response:
No, there is no way to achieve this. Java methods cannot look at the code of the argument passed, to find the name of a variable or otherwise.
CodePudding user response:
Use string concatenation.
String part = "abc";
String full = part "de";
System.out.println(part);
System.out.println(full);
CodePudding user response:
You must use reflection to achieve this.
There is another question answered with it.
Reflection generic get field value
Field field = object.getClass().getDeclaredField(fieldName);