Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you want to check the value of a variable at run time, you can use reflection to check the instantiated objects fields like this:</p> <pre><code>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&lt;?&gt; 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; } } </code></pre> <p>You can easily go from this implementation to one which uses getter/setter method invocations to check the value of inaccessible fields. If you want something less generic, or you want something which only checks specific fields, you can use c.getField(String) to get just that field, and then call field.get(object) for just that field.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload