How to get variable names at runtime?

Usage example:

public void testMethod(String para1, String para2, String para3){


 if(para1==null){
         System.out.println("para1 cannot be null");

   }
 if(para2)...
}

As the null verification code above, we will repeat our request to write the same code to verify each parameter. But we cannot really expose a general method, say, checknull (String para), because we need to print the parameter name so that users know which one is wrong.

Perhaps in java there is no way to do this in java. Method parameter names should disappear after compilation, if I understand them correctly.

So how do you guys usually solve this problem?

+3
source share
10 answers

Paste the message. There is no other way to do this. And no, you cannot get the variable name.

Java assert, . :

public void testMethod(String para1, String para2, String para3) {
  assert para1 != null : "para1 is null";
  assert para2 != null : "para2 is null";
  assert para3 != null : "para3 is null";
}

-ea. : .

, <23 > , Error ( , Java), catch (Exception e) . , , , .

, ( null, , ), RuntimeException .

+4

, , :

package sandbox;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

public class ReflectionClassChecker {

    public static boolean checkAllPublic(Object someObject){
        System.out.println("Checking someObject " + someObject.toString());
        boolean hasNulls = false;

        Class<?> c = someObject.getClass();

        Field[] fields = c.getFields();

        for(Field field: fields){
            System.out.println("Checking field " + field.getName() + ".");
            if(isFieldPublic(field)){
                System.out.println("Field " + field.getName() + " is public, checking it for null.");
                Object value = getField(field, someObject) ; 
                if(value == null){
                    System.out.println("Field " + field.getName() + " is null.");
                    hasNulls = true;
                } else {
                    System.out.println("Field " + field + " has value " + value );
                }
            }
        }

        return hasNulls;
    }

    private static boolean isFieldPublic(Field field){
        int modifiers = field.getModifiers();
        boolean isPublic = Modifier.isPublic(modifiers);        
        return isPublic;
    }

    private static Object getField(Field field, Object someObject){
        Object value = null;
        try{
            value = field.get(someObject);
        } catch (IllegalAccessException ignore){
            System.out.println(ignore);
        }       
        return value;
    }
}

, getter/setter, . , -, , c.getField(String), , field.get(object) .

+2

, Java 7 , , . , Java 6 : (

+1

varargs Java 1.5. "Object... objects" .

public static void printSpaced (Object... objects) {  for (Object o: objects) {       System.out.print(o + ":" );               if (o == "x" ) {                       System.out.print( "x found" );               }       } }

+1

, , , , , NullPointerException ( Java, IllegalArgumentException).

...
if (para1 == null) {
    throw new NullPointerException("para1 cannot be null");
}
...

, - :

assertNotNull(Object param, String paramName) {...}
0

, , , , , Java, , . , - .

0

, , , . :

checkNull("parm1", parm1);
checkNull("parm2", parm2);
checkNull("parm3", parm3);

, , , parm1, parm2 parm3, - , customerName, balanceDue accountType.

, , , , . _ , , , . balanceDue , , . .. , , , .

, , . , , , .

0

, Paranamer ( ). .

, FindBugs JSR305 ( Java7, FindBugs FindBugs Maven) Java5 , . , ( ), ( , ), (, , AspectJ, ).

0

Use the parameter annotation and add a dynamic proxy (or Java 4 CGLIB style) to perform validation. See link text for how to use CGLIB

0
source

Why don't you transfer them as a card? So you can have a key representing the variable name and a value representing the actual String variable.

-1
source

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


All Articles