private static void ensureSpringRulesAreNotPresent(Class<?> testClass) { for (Field field : testClass.getFields()) { Assert.state(!SpringClassRule.class.isAssignableFrom(field.getType()), () -> String.format( "Detected SpringClassRule field in test class [%s], " + "but SpringClassRule cannot be used with the SpringJUnit4ClassRunner.", testClass.getName())); Assert.state(!SpringMethodRule.class.isAssignableFrom(field.getType()), () -> String.format( "Detected SpringMethodRule field in test class [%s], " + "but SpringMethodRule cannot be used with the SpringJUnit4ClassRunner.", testClass.getName())); } }
private void readAndroidRStyleClass() { try { Class<?> rStyleCls = Class.forName(ANDROID_R_STYLE_CLS); for (Field f : rStyleCls.getFields()) { styleMap.put(f.getInt(f.getType()), f.getName()); } } catch (Exception th) { LOG.error("Android R class loading failed", th); } }
public static List<Field> callGetFields(Class thiz) throws SecurityException { return Arrays.asList(thiz.getFields()); }
public void listRaw(){ Field[] fields=R.raw.class.getFields(); for(int count=0; count < fields.length; count++){ Log.i("Raw Asset: ", fields[count].getName()); } }
/** * Go through all of the paths via reflection, and print them out in a TSV format. * This is useful for command line scripts. * * @param args Ignored. */ public static void main(String[] args) throws IllegalAccessException { for (Field field : DefaultPaths.class.getFields()) { System.out.println(field.getName() + "\t" + field.get(null)); } }
/** * Go through all of the paths via reflection, and print them out in a TSV format. * This is useful for command line scripts. * * @param args Ignored. */ public static void main(String[] args) throws IllegalAccessException { for (Field field : LanguageInfo.class.getFields()) { System.out.println(field.getName() + "\t" + field.get(null)); } }
private int countFieldsInClass(Class<?> clazz) { int fieldCount = 0; for(Field field : clazz.getFields()) { // get all fields if( !Modifier.isStatic(field.getModifiers()) && !Modifier.isTransient(field.getModifiers()) ) { fieldCount++; } } return fieldCount; }
/** Returns an array of {@link Field} containing the public fields of the class represented by the supplied Class. */ static public Field[] getFields (Class c) { java.lang.reflect.Field[] fields = c.getFields(); Field[] result = new Field[fields.length]; for (int i = 0, j = fields.length; i < j; i++) { result[i] = new Field(fields[i]); } return result; }
/** Returns an array of {@link Field} containing the public fields of the class represented by the supplied Class. */ static public Field[] getFields (Class c) { java.lang.reflect.Field[] fields = c.getFields(); Field[] result = new Field[fields.length]; for (int i = 0, j = fields.length; i < j; i++) { result[i] = new Field(fields[i]); } return result; }
for (Field f: MyClass.class.getFields()) { Column column = f.getAnnotation(Column.class); if (column != null) System.out.println(column.columnName()); }
public static void main(String... strings) throws IllegalArgumentException, IllegalAccessException { for (Field f : SectionItem.class.getFields()) { if (f.getType().equals(int.class)) { if (0 != (f.getModifiers() & Modifier.STATIC)) { System.out.printf("%s(0x%04x,0,0),//\n", f.getName(), f.get(null)); } } } }
private static String getSqlTypeName(int sqlType) { try { Integer val = new Integer(sqlType); for (Field field : Types.class.getFields()) { if (val.equals(field.get(null))) { return field.getName(); } } } catch (IllegalAccessException e) { throw new RuntimeException("Could not get sqlTypeName ", e); } throw new RuntimeException("Unknown sqlType " + sqlType); }
/** * Given the class, list up its {@link PropertyType}s from its public fields/getters. */ private Map<String, PropertyType> buildPropertyTypes(Class<?> clazz) { Map<String, PropertyType> r = new HashMap<String, PropertyType>(); for (Field f : clazz.getFields()) r.put(f.getName(),new PropertyType(f)); for (Method m : clazz.getMethods()) if(m.getName().startsWith("get")) r.put(Introspector.decapitalize(m.getName().substring(3)),new PropertyType(m)); return r; }
/** * Create a new Constants converter class wrapping the given class. * <p>All <b>public</b> static final variables will be exposed, whatever their type. * @param clazz the class to analyze * @throws IllegalArgumentException if the supplied {@code clazz} is {@code null} */ public Constants(Class<?> clazz) { Assert.notNull(clazz, "Class must not be null"); this.className = clazz.getName(); Field[] fields = clazz.getFields(); for (Field field : fields) { if (ReflectionUtils.isPublicStaticFinal(field)) { String name = field.getName(); try { Object value = field.get(null); this.fieldCache.put(name, value); } catch (IllegalAccessException ex) { // just leave this field and continue } } } }
private static Field getField(Class<?> cls, String fieldName) { Field result = null; if (CLASS_FIELD_CACHE.containsKey(cls) && CLASS_FIELD_CACHE.get(cls).containsKey(fieldName)) { return CLASS_FIELD_CACHE.get(cls).get(fieldName); } try { result = cls.getDeclaredField(fieldName); result.setAccessible(true); } catch (NoSuchFieldException e) { for (Field field : cls.getFields()) { if (fieldName.equals(field.getName()) && ReflectUtils.isPublicInstanceField(field)) { result = field; break; } } } if (result != null) { ConcurrentMap<String, Field> fields = CLASS_FIELD_CACHE.get(cls); if (fields == null) { fields = new ConcurrentHashMap<String, Field>(); CLASS_FIELD_CACHE.putIfAbsent(cls, fields); } fields = CLASS_FIELD_CACHE.get(cls); fields.putIfAbsent(fieldName, result); } return result; }
private static Field getField(Class<?> cls, String fieldName) { Field result = null; if (CLASS_FIELD_CACHE.containsKey(cls) && CLASS_FIELD_CACHE.get(cls).containsKey(fieldName)) { return CLASS_FIELD_CACHE.get(cls).get(fieldName); } try { result = cls.getDeclaredField(fieldName); result.setAccessible(true); } catch (NoSuchFieldException e) { for (Field field : cls.getFields()) { if (fieldName.equals(field.getName()) && ReflectUtils.isPublicInstanceField(field)) { result = field; break; } } } if (result != null) { ConcurrentMap<String, Field> fields = CLASS_FIELD_CACHE.get(cls); if (fields == null) { fields = new ConcurrentHashMap<String, Field>(); CLASS_FIELD_CACHE.putIfAbsent(cls, fields); } fields = CLASS_FIELD_CACHE.get(cls); fields.putIfAbsent(fieldName, result); } return result; }
/** * Find a field of a certain name on a specified class. */ @Nullable protected Field findField(String name, Class<?> clazz, boolean mustBeStatic) { Field[] fields = clazz.getFields(); for (Field field : fields) { if (field.getName().equals(name) && (!mustBeStatic || Modifier.isStatic(field.getModifiers()))) { return field; } } // We'll search superclasses and implemented interfaces explicitly, // although it shouldn't be necessary - however, see SPR-10125. if (clazz.getSuperclass() != null) { Field field = findField(name, clazz.getSuperclass(), mustBeStatic); if (field != null) { return field; } } for (Class<?> implementedInterface : clazz.getInterfaces()) { Field field = findField(name, implementedInterface, mustBeStatic); if (field != null) { return field; } } return null; }
public FieldGetter takeOver(Class<?> targetClass, String fieldName) { java.lang.reflect.Field[] fieldArray = targetClass.getFields(); for (java.lang.reflect.Field field : fieldArray) { if (field.getName().equals(fieldName)) { return new RealFieldGetter(field); } } return null; }
static List<OptionWithMetaInfo> extractConfigOptions(Class<?> clazz) { try { List<OptionWithMetaInfo> configOptions = new ArrayList<>(8); Field[] fields = clazz.getFields(); for (Field field : fields) { if (isConfigOption(field) && shouldBeDocumented(field)) { configOptions.add(new OptionWithMetaInfo((ConfigOption<?>) field.get(null), field)); } } return configOptions; } catch (Exception e) { throw new RuntimeException("Failed to extract config options from class " + clazz + '.', e); } }
private ParsedQuickSilver(Class<? extends SearchableModelObject> clazz) { QuickSilver qs; for (Method m : clazz.getMethods()) { qs = m.getAnnotation(QuickSilver.class); if(qs!=null) { String url = stripGetPrefix(m); if(qs.value().length==0) getters.add(new MethodGetter(url,splitName(url),m)); else { for (String name : qs.value()) getters.add(new MethodGetter(url,name,m)); } } } for (Field f : clazz.getFields()) { qs = f.getAnnotation(QuickSilver.class); if(qs!=null) { if(qs.value().length==0) getters.add(new FieldGetter(f.getName(),splitName(f.getName()),f)); else { for (String name : qs.value()) getters.add(new FieldGetter(f.getName(),name,f)); } } } }