How to print variable name and variable value in java

String activityState = "resume"; DebugLog(activityState) void DebugLog(String obj1) {} 

How to make DebugLog printable as follows:

 activityState : resume 

I used to write many print statements in many places during debugging. I will write statements like

 System.out.println("activityState : " + activityState); 

I want the method to print the name of the variable and the value of the variable. In C ++, this can be done as shown below:

 #define dbg(x) cout<< #x <<" --> " << x << endl ; 

Is there any way to do this?

Thanks in advance.

+6
source share
3 answers

There is no direct solution to get the variable name.

However, in a context where you have many fields and you do not want to manually print your state, you can use reflection.

Here is an example:

 class MyPojo { public static void main(String[] args) { System.out.println(new MyPojo()); } int i = 1; String s = "foo"; @Override public String toString() { StringBuilder result = new StringBuilder(); for (Field f: getClass().getDeclaredFields()) { try { result .append(f.getName()) .append(" : ") .append(f.get(this)) .append(System.getProperty("line.separator")); } catch (IllegalStateException ise) { result .append(f.getName()) .append(" : ") .append("[cannot retrieve value]") .append(System.getProperty("line.separator")); } // nope catch (IllegalAccessException iae) {} } return result.toString(); } } 

Output

 i : 1 s : foo 
+1
source

You can use java Reflection to get the variable name and value. Here is a sample code:

 public class Example{ String activityState = "resume"; public static void main(String[] args) { Example example = new Example(); Class<?> c = example.getClass(); Field field = c.getDeclaredField("activityState"); System.out.println(field.getName()); System.out.println(field.get(example)); } } 
0
source

Since this is for debugging, you can use instrumentation with aspectj , your code remains clean from the debug output pins, and you can discard the aspect if necessary.

Define a set(FieldPattern) point abbreviation set(FieldPattern) to catch all field assignments (join points)

 public aspect TestAssignmentAspect { pointcut assigmentPointCut() : set(* *); after() : assigmentPointCut() { System.out.printf("%s = %s%n", thisJoinPoint.getSignature().getName(), String.valueOf(Arrays.toString(thisJoinPoint.getArgs()))); } } 

Here is a test class

 public class Test { public static String activityState = "stopped"; public static void main(String[] args) { activityState = "start"; doSomething(); activityState = "pause"; doSomeOtherthing(); activityState = "resume"; System.out.printf("the end!%n"); } private static void doSomeOtherthing() { System.out.printf("doing some other thing...%n"); } private static void doSomething() { System.out.printf("doing something...%n"); } } 

If you run this example based on the format, the output will be

 activityState = [stopped] activityState = [start] doing something... activityState = [pause] doing some other thing... activityState = [resume] the end! 

Explanation

 pointcut assigmentPointCut() : set(* *); 

set binding points to fishing, binding points to any variable with any name, can also be in the example

 pointcut assigmentPointCut() : set(String activityState); 

advice , desired behavior when a given point crop matches

 after() : assigmentPointCut() { ... } 

Information about connecting to a point can be obtained using the special thisJoinPoint link.

0
source

Source: https://habr.com/ru/post/973214/


All Articles