Can we programmatically verify that this is a variable name in standard form or not?

Example

class MyClass {

    String name;
    String NAme;
    public static void main(String args[]) {

    }
}

The conclusion should be:

name is in standard form
NAme is not in standard form
+4
source share
1 answer

Its possible through reflection. You can get all declared fields using: -

public static void main(String args[]) {
    Field[] fields = MyClass.class.getDeclaredFields();
    for(Field field : fields ){
    field.setAccessible(true);
    String name = field.getName();
    //Check if name contains small letters or the logic to check if camel-case etc
    }
}

You cannot do this for local variables

+3
source

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


All Articles