List of usage examples for java.lang Class getDeclaredFields
@CallerSensitive public Field[] getDeclaredFields() throws SecurityException
From source file:Main.java
public static Field getField(Class<?> clazz, String fieldName) { final Field[] fields = clazz.getDeclaredFields(); final Field field = getField(fields, fieldName); if (field == null) { final Class<?> superClass = clazz.getSuperclass(); if (Object.class.equals(superClass)) { return null; }// w w w .j a v a2 s . com return getField(superClass, fieldName); } return field; }
From source file:ReflectionTest.java
/** * Prints all fields of a class// www .j a v a 2 s . c om * @param cl a class */ public static void printFields(Class cl) { Field[] fields = cl.getDeclaredFields(); for (Field f : fields) { Class type = f.getType(); String name = f.getName(); System.out.print(" "); String modifiers = Modifier.toString(f.getModifiers()); if (modifiers.length() > 0) System.out.print(modifiers + " "); System.out.println(type.getName() + " " + name + ";"); } }
From source file:testReflection.java
public static Field[] getAllFields(Class klass) { List<Field> fields = new ArrayList<Field>(); fields.addAll(Arrays.asList(klass.getDeclaredFields())); if (klass.getSuperclass() != null) { fields.addAll(Arrays.asList(getAllFields(klass.getSuperclass()))); }/*from ww w .j ava2s . c o m*/ return fields.toArray(new Field[] {}); }
From source file:Main.java
/** * Finds a set of constant static byte field declarations in the class that have the given value * and whose name match the given pattern * @param cl class to search in/*ww w. ja v a 2s . com*/ * @param value value of constant static byte field declarations to match * @param regexPattern pattern to match against the name of the field * @return a set of the names of fields, expressed as a string */ private static String findConstByteInClass(Class<?> cl, byte value, String regexPattern) { Field[] fields = cl.getDeclaredFields(); Set<String> fieldSet = new HashSet<String>(); for (Field f : fields) { try { if (f.getType() == Byte.TYPE && (f.getModifiers() & Modifier.STATIC) != 0 && f.getName().matches(regexPattern) && f.getByte(null) == value) { fieldSet.add(f.getName()); } } catch (IllegalArgumentException e) { // ignore } catch (IllegalAccessException e) { // ignore } } return fieldSet.toString(); }
From source file:Main.java
private static void init() { try {//from ww w.jav a2s . c o m // read all com.android.internal.R Class clazz = Class.forName("com.android.internal.R$layout"); Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { // public static final if (Modifier.isPublic(field.getModifiers()) && Modifier.isStatic(field.getModifiers()) && Modifier.isFinal(field.getModifiers())) { try { int id = field.getInt(null); sSystemLayoutResIds.put(id, field.getName()); } catch (IllegalAccessException e) { } } } } catch (Exception e) { } }
From source file:Main.java
public static Object mergedObject(Object oldObject, Object newObject) throws Exception { Class<?> cls = newObject.getClass(); Field[] fields = cls.getDeclaredFields(); for (Field field : fields) { Class<?> clsType = field.getType(); String name = field.getName(); String method = name.substring(0, 1).toUpperCase() + name.substring(1, name.length()); String strGet = "get" + method; Method methodGet = cls.getDeclaredMethod(strGet); Object object = methodGet.invoke(newObject); if (object != null) { String strSet = "set" + method; Method methodSet = cls.getDeclaredMethod(strSet, clsType); Object objValue = typeConversion(clsType, object.toString()); methodSet.invoke(oldObject, objValue); }//from w w w.jav a 2 s . com } return oldObject; }
From source file:Main.java
public static List<Field> getRealFields(Class objectClass) { List<Field> result = new LinkedList<Field>(); for (Field item : objectClass.getDeclaredFields()) { if (!item.isSynthetic()) result.add(item);// w w w . j a va 2 s .c o m } return result; }
From source file:Main.java
/** * Get a Hashtable of all class member fields (including private) of the * given object/*from w ww . j av a 2 s . c om*/ * * @param objectInstance Object to get data from * @return Hashtable of all values * * @throws IllegalArgumentException * @throws IllegalAccessException */ public static Map<String, Object> getAllFields(Object objectInstance) throws IllegalArgumentException, IllegalAccessException // NOSONAR { HashMap<String, Object> map = new HashMap<String, Object>(); Class<? extends Object> clazz = objectInstance.getClass(); Field fields[] = clazz.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { Field f = fields[i]; f.setAccessible(true); String fieldName = f.getName(); Object fieldValue = f.get(objectInstance); map.put(fieldName, fieldValue); } return map; }
From source file:Main.java
static Unsafe getSMU() { try {//w w w . java 2s .c om return sun.misc.Unsafe.getUnsafe(); } catch (SecurityException tryReflectionInstead) { // ignore } try { return java.security.AccessController.doPrivileged((PrivilegedExceptionAction<Unsafe>) () -> { Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class; for (Field f : k.getDeclaredFields()) { f.setAccessible(true); Object x = f.get(null); if (k.isInstance(x)) return k.cast(x); } throw new NoSuchFieldError("the Unsafe"); }); } catch (java.security.PrivilegedActionException e) { throw new RuntimeException("Could not initialize intrinsics", e.getCause()); } }
From source file:com.vmware.qe.framework.datadriven.impl.injector.DataInjectorUsingReflection.java
private static Field getField(Class<?> clazz, String fieldName) { Class<?> tmpClass = clazz; do {//from ww w . j a v a 2 s . c o m for (Field field : tmpClass.getDeclaredFields()) { String candidateName = field.getName(); if (!candidateName.equals(fieldName)) { continue; } field.setAccessible(true); return field; } tmpClass = tmpClass.getSuperclass(); } while (clazz != null); throw new RuntimeException("Field '" + fieldName + "' not found on class " + clazz); }