List of usage examples for java.lang Class getDeclaredFields
@CallerSensitive public Field[] getDeclaredFields() throws SecurityException
From source file:com.abiquo.model.util.ModelTransformer.java
public static <T> void transform(final Class sourceClass, final Class<T> targetClass, final Object source, final T target) throws Exception { Field[] transportFields = sourceClass.getDeclaredFields(); Class superClass = sourceClass.getSuperclass(); while (!superClass.getSimpleName().equalsIgnoreCase("SingleResourceTransportDto")) { transportFields = (Field[]) ArrayUtils.addAll(transportFields, superClass.getDeclaredFields()); superClass = superClass.getSuperclass(); }/* w ww. j a v a 2 s . c om*/ for (Field field : transportFields) { int modifiers = field.getModifiers(); if (!Modifier.isTransient(modifiers) && !Modifier.isStatic(modifiers)) { String name = field.getName(); try { if (fieldExist(name, targetClass) && fieldExist(name, source.getClass()) || getterExist(name, source.getClass()) && setterExist(name, targetClass, field.getType())) { Object value = getter(name, source.getClass()).invoke(source, new Object[0]); if (setterExist(name, targetClass, field.getType())) { setter(name, targetClass, field.getType()).invoke(target, new Object[] { value }); } } } catch (InvocationTargetException e) { // Ignore invalid field } } } }
From source file:jfix.util.Reflections.java
/** * Returns all fields of a given class (including fields from superclasses). *///w ww. j ava 2 s . c o m public static Set<Field> getFields(Class<?> clazz) { Set<Field> result = new HashSet<>(); for (Class<?> superClass : getSuperClassesAndInterfaces(clazz)) { for (Field field : superClass.getDeclaredFields()) { field.setAccessible(true); result.add(field); } } return result; }
From source file:net.buffalo.protocal.util.ClassUtil.java
private static HashMap getFieldMap(Class cl) { HashMap fieldMap = new HashMap(); for (; cl != null; cl = cl.getSuperclass()) { Field[] fields = cl.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; if (Modifier.isTransient(field.getModifiers()) || Modifier.isStatic(field.getModifiers())) continue; field.setAccessible(true);//from ww w . j av a2 s . com fieldMap.put(field.getName(), field); } } return fieldMap; }
From source file:org.LexGrid.LexBIG.caCore.utils.LexEVSCaCoreUtils.java
public static ArrayList<Field> getAllFields(Class clazz) { ArrayList<Field> returnFields = new ArrayList<Field>(); while (clazz != null) { Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { returnFields.add(field);//ww w . ja v a 2 s. c o m } clazz = clazz.getSuperclass(); } return returnFields; }
From source file:arena.utils.ReflectionUtils.java
public static String[] getAttributeNamesUsingGetter(Class<?> entityClass) { // return BeanUtils.getPropertyDescriptors(entityClass); Class<?> clsMe = entityClass; List<String> voFieldNames = new ArrayList<String>(); while (clsMe != null) { Field[] fields = clsMe.getDeclaredFields(); for (int n = 0; n < fields.length; n++) { int modifiers = fields[n].getModifiers(); if (!Modifier.isPublic(modifiers) && !Modifier.isStatic(modifiers)) { String fieldName = fields[n].getName(); try { PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(entityClass, fieldName); if (pd.getReadMethod() != null) { voFieldNames.add(fieldName); }/*from w w w.j a v a 2 s . co m*/ } catch (Throwable err) { // skip } } } // Loop back to parent class clsMe = clsMe.getSuperclass(); } return voFieldNames.toArray(new String[voFieldNames.size()]); }
From source file:Main.java
/** * Get a list of the name of variables of the class received * @param klass Class to get the variable names * @return List<String>[] of length 3 with variable names: [0]: primitives, [1]: arrays, [2]: objects *//* ww w.j a v a2 s .c o m*/ public static List<String>[] getVariableNames(Class klass) { //array to return List<String>[] varNames = new List[3]; for (int i = 0; i < 3; i++) { varNames[i] = new ArrayList<>(); } //add all valid fields do { Field[] fields = klass.getDeclaredFields(); for (Field field : fields) { if (!Modifier.isTransient(field.getModifiers())) { //get the type Class type = field.getType(); if (type.isPrimitive() || (type == Integer.class) || (type == Float.class) || (type == Double.class) || (type == Boolean.class) || (type == String.class)) { varNames[0].add(field.getName()); } else if (type.isArray()) { varNames[1].add(field.getName()); } else { varNames[2].add(field.getName()); } } } klass = klass.getSuperclass(); } while (klass != null); //return array return varNames; }
From source file:NestUtil.java
/** * Get all fields of a class.//from ww w.jav a 2s . co m * * @param clazz The class. * @return All fields of a class. */ public static Collection<Field> getFields(Class<?> clazz) { // if (log.isDebugEnabled()) { // log.debug("getFields(Class<?>) - start"); //} Map<String, Field> fields = new HashMap<String, Field>(); while (clazz != null) { for (Field field : clazz.getDeclaredFields()) { if (!fields.containsKey(field.getName())) { fields.put(field.getName(), field); } } clazz = clazz.getSuperclass(); } Collection<Field> returnCollection = fields.values(); // if (log.isDebugEnabled()) { // log.debug("getFields(Class<?>) - end"); // } return returnCollection; }
From source file:lite.flow.util.ActivityInspector.java
static public ArrayList<Field> getOutputs(Class<?> clazz) { ArrayList<Field> outputs = new ArrayList<>(); for (Field field : clazz.getDeclaredFields()) { Class<?> decl = field.getType(); if (Output.class.isAssignableFrom(decl)) { outputs.add(field);/*from w w w.j a v a2 s. co m*/ } } return outputs; }
From source file:com.shrj.util.ReflectionUtils.java
public static String getAnnotatedFieldName(Class clazz, Class<? extends Annotation> annotation) { Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { if (field.isAnnotationPresent(annotation)) { return field.getName(); }/*from w w w . j a va 2s. co m*/ } return null; }
From source file:com.nridge.core.base.field.data.DataBeanBag.java
/** * Accepts a POJO containing one or more public fields and * creates a DataBag from them. The DataBeanObject1 test * class provides a reference example.//from w w w .j ava 2 s. co m * * @param anObject POJO instance. * * @return Data bag instance populated with field information. * * @throws NSException Thrown if object is null. * @throws NoSuchFieldException Thrown if field does not exist. * @throws IllegalAccessException Thrown if access is illegal. */ public static DataBag fromFieldsToBag(Object anObject) throws NSException, NoSuchFieldException, IllegalAccessException { DataField dataField; boolean isPublicAccess; if (anObject == null) throw new NSException("Object is null"); DataBag dataBag = new DataBag(anObject.toString()); Class<?> objClass = anObject.getClass(); Field[] fieldArray = objClass.getDeclaredFields(); for (Field objField : fieldArray) { isPublicAccess = Modifier.isPublic(objField.getModifiers()); if (!isPublicAccess) objField.setAccessible(true); dataField = reflectField(anObject, objField); dataBag.add(dataField); } return dataBag; }