List of usage examples for java.lang Class getDeclaredFields
@CallerSensitive public Field[] getDeclaredFields() throws SecurityException
From source file:com.sunway.cbm.util.web.ReflectionUtils.java
public static List<Field> getDeclaredFields(Class<?> clazz) { List<Field> fieldRes = new ArrayList<Field>(); Class<?> superclass = clazz.getSuperclass(); if (superclass != null) { fieldRes.addAll(getDeclaredFields(superclass)); }/*from w w w.java 2 s . c o m*/ Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { fieldRes.add(field); } return fieldRes; }
From source file:com.rockagen.commons.util.ClassUtil.java
/** * obtain fields list of specified class If recursively is true, obtain * fields from all class hierarchy/*from ww w . j a v a 2 s . com*/ * * @param clazz class * where fields are searching * @param recursively * param * @return array of fields */ public static Field[] getDeclaredFields(Class<?> clazz, boolean recursively) { List<Field> fields = new LinkedList<Field>(); Field[] declaredFields = clazz.getDeclaredFields(); Collections.addAll(fields, declaredFields); Class<?> superClass = clazz.getSuperclass(); if (superClass != null && recursively) { Field[] declaredFieldsOfSuper = getDeclaredFields(superClass, true); if (declaredFieldsOfSuper.length > 0) Collections.addAll(fields, declaredFieldsOfSuper); } return fields.toArray(new Field[fields.size()]); }
From source file:com.github.gekoh.yagen.util.FieldInfo.java
private static void addAttributeOverrides(Map<String, String> overrides, String path, Class type) { for (Field field : type.getDeclaredFields()) { String curPath = path + field.getName() + "."; Column column;//from ww w. j a v a2 s .c o m if (field.isAnnotationPresent(AttributeOverride.class)) { addAttributeOverride(overrides, addNamePrefixToAttributeOverride( formatAnnotation(field.getAnnotation(AttributeOverride.class)), curPath)); } else if (field.isAnnotationPresent(AttributeOverrides.class)) { for (AttributeOverride attributeOverride : field.getAnnotation(AttributeOverrides.class).value()) { addAttributeOverride(overrides, addNamePrefixToAttributeOverride(formatAnnotation(attributeOverride), curPath)); } } else if (((column = field.getAnnotation(Column.class)) != null && (!column.nullable() || column.unique())) || (field.isAnnotationPresent(Basic.class) && !field.getAnnotation(Basic.class).optional())) { String columnName = column != null ? column.name() : field.getName(); int length = column != null ? column.length() : 255; String override = "@javax.persistence.AttributeOverride(name=\"" + path + field.getName() + "\", column=" + "@javax.persistence.Column(name=\"" + columnName + "\", length=" + length + ", nullable=true, unique=false))"; addAttributeOverride(overrides, override); } if (field.isAnnotationPresent(Embedded.class)) { addAttributeOverrides(overrides, curPath, field.getType()); } } }
From source file:org.appverse.web.framework.backend.api.converters.helpers.ConverterUtils.java
/** * Obtain a list of field declared by a bean class and superclasses * /* w ww.j ava 2 s . com*/ * @param bean * Bean to get the fields * @return Array of fields declared by bean class and superclasses * @throws Exception */ private static Field[] getBeanFields(AbstractBean bean) throws Exception { // Fields added to AbstractBean by ConverterAspect String[] ignoreFields = ((Detachable) bean).getDetachableFields(); Class<?> clazz = bean.getClass(); // Get the all fields declared by the class Field[] fields = clazz.getDeclaredFields(); // For all superclasses until arrive to AbstractBean (including // AbstractBean)... while (clazz.getSuperclass() != null && AbstractBean.class.isAssignableFrom(clazz.getSuperclass())) { clazz = clazz.getSuperclass(); // Add the clazz declared fields to field list fields = (Field[]) ArrayUtils.addAll(fields, clazz.getDeclaredFields()); } // Remove from the list fields added by the converters aspect to // Abstract // bean for (Field field : fields) { if (ArrayUtils.contains(ignoreFields, field.getName())) { fields = (Field[]) ArrayUtils.removeElement(fields, field); } } return fields; }
From source file:net.radai.beanz.util.ReflectionUtil.java
public static Field findField(Class<?> clazz, String propName) { Class<?> c = clazz; while (c != null) { for (Field f : c.getDeclaredFields()) { if (!f.getName().equals(propName)) { continue; }//from w w w. j a va2s .c om int modifiers = f.getModifiers(); if (Modifier.isStatic(modifiers)) { continue; } return f; } c = c.getSuperclass(); } return null; }
From source file:Main.java
public static <T> ArrayList<HashMap<String, String>> prepareDataToSave( ArrayList<T> source, Class<T> classType) throws NoSuchMethodException, SecurityException, IllegalArgumentException, IllegalAccessException { ArrayList<HashMap<String, String>> destination = new ArrayList<HashMap<String, String>>(); ArrayList<Field> savedFieds = new ArrayList<Field>(); Field[] fields;// w ww. j a va2 s . c o m Object value = null; HashMap<String, String> aux; Field auxField; fields = classType.getDeclaredFields(); for (int j = 0; j < fields.length; j++) { Class<?> type = fields[j].getType(); if (!(Modifier.isStatic(fields[j].getModifiers()) || Modifier.isFinal(fields[j].getModifiers()) || type.isArray() || Collection.class.isAssignableFrom(type))) { savedFieds.add(fields[j]); } } if (classType == null || fields == null) { return null; } for (int i = 0; i < source.size(); i++) { aux = new HashMap<String, String>(); for (int j = 0; j < savedFieds.size(); j++) { auxField = savedFieds.get(j); auxField.setAccessible(true); value = auxField.get(source.get(i)); aux.put(auxField.getName(), value.toString()); } destination.add(aux); } return destination; }
From source file:com.evolveum.midpoint.prism.xjc.PrismForJAXBUtil.java
private static <T> Field getAnyField(Class<T> clazz) { for (Field field : clazz.getDeclaredFields()) { XmlAnyElement xmlAnyElementAnnotation = field.getAnnotation(XmlAnyElement.class); if (xmlAnyElementAnnotation != null) { return field; }// w ww . j a v a 2s . c o m } return null; }
From source file:microsoft.exchange.webservices.data.core.service.schema.ServiceObjectSchema.java
/** * Initialize schema property names.//from w w w. java 2 s .com */ public static void initializeSchemaPropertyNames() { synchronized (lockObject) { for (Class<?> type : ServiceObjectSchema.allSchemaTypes.getMember()) { Field[] fields = type.getDeclaredFields(); for (Field field : fields) { int modifier = field.getModifiers(); if (Modifier.isPublic(modifier) && Modifier.isStatic(modifier)) { Object o; try { o = field.get(null); if (o instanceof PropertyDefinition) { PropertyDefinition propertyDefinition = (PropertyDefinition) o; propertyDefinition.setName(field.getName()); } } catch (IllegalArgumentException e) { LOG.error(e); // Skip the field } catch (IllegalAccessException e) { LOG.error(e); // Skip the field } } } } } }
From source file:com.shenit.commons.utils.GsonUtils.java
/** * ???./*ww w.ja va 2s . co m*/ * @param clazz * @return */ public static Field<?>[] serializeFields(Class<?> clazz) { if (REGISTERED_FIELDS.containsKey(clazz)) return REGISTERED_FIELDS.get(clazz); java.lang.reflect.Field[] fields = clazz.getDeclaredFields(); List<Field<?>> fs = Lists.newArrayList(); SerializedName serializedName; JsonProperty jsonProp; IgnoreField ignore; for (java.lang.reflect.Field field : fields) { if (field == null) continue; serializedName = field.getAnnotation(SerializedName.class); jsonProp = field.getAnnotation(JsonProperty.class); ignore = field.getAnnotation(IgnoreField.class); if (ignore != null) continue; String name = null; if (serializedName != null) { name = serializedName.value(); } else if (jsonProp != null) { name = jsonProp.value(); } else { name = Modifier.isStatic(field.getModifiers()) ? null : field.getName(); } if (name == null) continue; Default defVal = field.getDeclaredAnnotation(Default.class); fs.add(new Field(name, field.getName(), field.getType(), defVal == null ? null : defVal.value())); } Field<?>[] fsArray = fs.toArray(new Field<?>[0]); REGISTERED_FIELDS.put(clazz, fsArray); return fsArray; }
From source file:com.hihframework.core.utils.ReflectUtil.java
public static final String[] getAllFieldsName(Object obj) { String[] retValue;/*from w ww .ja v a 2 s .c o m*/ Class<?> objClass = obj.getClass(); Field[] fields = objClass.getDeclaredFields(); if (fields != null && !(fields.length < 1)) { retValue = new String[fields.length]; for (int i = 0; i < fields.length; i++) { retValue[i] = fields[i].getName(); } return retValue; } else return null; }