List of usage examples for java.lang Class getDeclaredFields
@CallerSensitive public Field[] getDeclaredFields() throws SecurityException
From source file:com.smart.utils.ReflectionUtils.java
/** * ?classString,list,listString/*from w w w.ja va2 s.c o m*/ * * @param clz * @return */ public static List<String> getAllFieldNames(Class clz) { List result = new ArrayList(); Map allFields = new HashMap(); // ??? List classFamilyTree = new LinkedList(); classFamilyTree.add(clz); Class tempSuperClass = clz; while ((tempSuperClass = tempSuperClass.getSuperclass()) != null) { classFamilyTree.add(tempSuperClass); } // Map???field?????? for (int i = classFamilyTree.size() - 1; i >= 0; i--) { Class tempClass = (Class) classFamilyTree.get(i); Field[] fieldsOfTempClass = tempClass.getDeclaredFields(); for (int j = 0; j < fieldsOfTempClass.length; j++) { Field field = fieldsOfTempClass[j]; allFields.put(field.getName(), field.getName()); } } // map?list for (Iterator iter = allFields.entrySet().iterator(); iter.hasNext();) { Map.Entry entry = (Map.Entry) iter.next(); result.add(entry.getValue()); } return result; }
From source file:com.smart.utils.ReflectionUtils.java
/** * ?classFieldlistlistField//from w w w . ja v a2 s. c o m * * @param clz * @return */ public static List<Field> getAllFields(Class clz) { List result = new ArrayList(); Map allFields = new HashMap(); // ??? List classFamilyTree = new LinkedList(); classFamilyTree.add(clz); Class tempSuperClass = clz; while ((tempSuperClass = tempSuperClass.getSuperclass()) != null) { classFamilyTree.add(tempSuperClass); } // Map???field?????? for (int i = classFamilyTree.size() - 1; i >= 0; i--) { Class tempClass = (Class) classFamilyTree.get(i); Field[] fieldsOfTempClass = tempClass.getDeclaredFields(); for (int j = 0; j < fieldsOfTempClass.length; j++) { Field field = fieldsOfTempClass[j]; allFields.put(field.getName(), field); } } // map?list for (Iterator iter = allFields.entrySet().iterator(); iter.hasNext();) { Map.Entry entry = (Map.Entry) iter.next(); result.add(entry.getValue()); } return result; }
From source file:Mopex.java
/** * Returns an array of fields that are the declared instance variables of * cls. An instance variable is a field that is not static. * /*from w ww . ja v a 2 s .co m*/ * @return java.lang.reflect.Field[] * @param cls * java.lang.Class */ //start extract getDeclaredIVS public static Field[] getDeclaredIVs(Class cls) { Field[] fields = cls.getDeclaredFields(); // Count the IVs int numberOfIVs = 0; for (int i = 0; i < fields.length; i++) { if (!Modifier.isStatic(fields[i].getModifiers())) numberOfIVs++; } Field[] declaredIVs = new Field[numberOfIVs]; // Populate declaredIVs int j = 0; for (int i = 0; i < fields.length; i++) { if (!Modifier.isStatic(fields[i].getModifiers())) declaredIVs[j++] = fields[i]; } return declaredIVs; }
From source file:com.datumbox.common.persistentstorage.factories.MongoDBStructureFactory.java
public static List<Field> getAllFields(List<Field> fields, Class<?> type) { fields.addAll(Arrays.asList(type.getDeclaredFields())); if (type.getSuperclass() != null) { fields = getAllFields(fields, type.getSuperclass()); }/*from ww w. j av a2s . co m*/ return fields; }
From source file:com.igormaznitsa.upom.UPomModel.java
private static Field findDeclaredFieldForName(final Class klazz, final String fieldName) { Field result = null;/*from w ww .j a v a 2s .c o m*/ Class curr = klazz; while (curr.getName().startsWith(MAVEN_MODEL_PACKAGE_PREFIX)) { for (final Field m : curr.getDeclaredFields()) { if (m.getName().equalsIgnoreCase(fieldName)) { result = m; break; } } if (result != null) { result.setAccessible(true); break; } curr = klazz.getSuperclass(); } return result; }
From source file:com.alibaba.jstorm.cluster.StormConfig.java
public static HashMap<String, Object> getClassFields(Class<?> cls) throws IllegalArgumentException, IllegalAccessException { Field[] list = cls.getDeclaredFields(); HashMap<String, Object> rtn = new HashMap<String, Object>(); for (Field f : list) { String name = f.getName(); rtn.put(name, f.get(null).toString()); }/*from w w w . j a va 2 s . c om*/ return rtn; }
From source file:gr.abiss.calipso.jpasearch.specifications.GenericSpecifications.java
public static Map<String, String[]> getSimpleSearchTerms(Class<?> clazz, String[] values) { Map<String, String[]> simpleSearchTerms = new HashMap<String, String[]>(); Class<?> tmpClass = clazz; // try the cache first List<Field> simpleSearchFieldList = SIMPLE_SEARCH_FIELDs_CACHE.get(clazz); if (simpleSearchFieldList == null) { simpleSearchFieldList = new LinkedList<Field>(); while (tmpClass != null) { for (Field tmpField : tmpClass.getDeclaredFields()) { String fieldName = tmpField.getName(); // if string and not excluded if (isAcceptableSimpleSearchFieldClass(tmpField) && !tmpField.isAnnotationPresent(Transient.class) && !tmpField.isAnnotationPresent(JsonIgnore.class)) { simpleSearchTerms.put(fieldName, values); simpleSearchFieldList.add(tmpField); }//w w w.j a v a 2 s . c o m } tmpClass = tmpClass.getSuperclass(); } // update the cache SIMPLE_SEARCH_FIELDs_CACHE.put(clazz, simpleSearchFieldList); } else { // use the caches field list for (Field tmpField : simpleSearchFieldList) { simpleSearchTerms.put(tmpField.getName(), values); } } return simpleSearchTerms; }
From source file:ml.shifu.shifu.container.meta.MetaFactory.java
/** * Iterate each property of Object, get the value and validate * //from w ww.j a va 2 s. c om * @param isGridSearch * - if grid search, ignore validation in train#params as they are set all as list * @param ptag * - the prefix of key to search @MetaItem * @param obj * - the object to validate * @return ValidateResult * If all items are OK, the ValidateResult.status will be true; * Or the ValidateResult.status will be false, ValidateResult.causes will contain the reasons * @throws Exception * any exception in validaiton */ public static ValidateResult iterateCheck(boolean isGridSearch, String ptag, Object obj) throws Exception { ValidateResult result = new ValidateResult(true); if (obj == null) { return result; } Class<?> cls = obj.getClass(); Field[] fields = cls.getDeclaredFields(); Class<?> parentCls = cls.getSuperclass(); if (!parentCls.equals(Object.class)) { Field[] pfs = parentCls.getDeclaredFields(); fields = (Field[]) ArrayUtils.addAll(fields, pfs); } for (Field field : fields) { if (!field.isSynthetic() && !Modifier.isStatic(field.getModifiers()) && !isJsonIngoreField(field)) { Method method = cls.getMethod("get" + getMethodName(field.getName())); Object value = method.invoke(obj); encapsulateResult(result, validate(isGridSearch, ptag + ITEM_KEY_SEPERATOR + field.getName(), value)); } } return result; }
From source file:com.mmj.app.lucene.solr.client.SolrClient.java
public static Object toBean(SolrDocument record, Class<?> clazz) { Object o = null;/*from w ww . jav a 2 s . c om*/ try { o = clazz.newInstance(); } catch (InstantiationException e1) { e1.printStackTrace(); } catch (IllegalAccessException e1) { e1.printStackTrace(); } Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { Object value = record.get(field.getName()); ConvertUtils.register(new DateConverter(null), java.util.Date.class); try { BeanUtils.setProperty(o, field.getName(), value); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } return o; }
From source file:ml.shifu.shifu.container.meta.MetaFactory.java
/** * Validate the ModelConfig object, to make sure each item follow the constrain * // ww w. j av a 2 s . co m * @param modelConfig * - object to validate * @return ValidateResult * If all items are OK, the ValidateResult.status will be true; * Or the ValidateResult.status will be false, ValidateResult.causes will contain the reasons * @throws Exception * any exception in validaiton */ public static ValidateResult validate(ModelConfig modelConfig) throws Exception { ValidateResult result = new ValidateResult(true); GridSearch gs = new GridSearch(modelConfig.getTrain().getParams(), modelConfig.getTrain().getGridConfigFileContent()); Class<?> cls = modelConfig.getClass(); Field[] fields = cls.getDeclaredFields(); for (Field field : fields) { // skip log instance if (field.getName().equalsIgnoreCase("log") || field.getName().equalsIgnoreCase("logger")) { continue; } if (!field.isSynthetic()) { Method method = cls.getMethod("get" + getMethodName(field.getName())); Object value = method.invoke(modelConfig); if (value instanceof List) { List<?> objList = (List<?>) value; for (Object obj : objList) { encapsulateResult(result, iterateCheck(gs.hasHyperParam(), field.getName(), obj)); } } else { encapsulateResult(result, iterateCheck(gs.hasHyperParam(), field.getName(), value)); } } } return result; }