List of usage examples for java.lang.reflect Field getName
public String getName()
From source file:com.bosscs.spark.commons.utils.AnnotationUtils.java
/** * Returns the field name as known by the datastore. If the provided field object DeepField annotation * specifies the fieldName property, the value of this property will be returned, otherwise the java field name * will be returned./*www.j a v a 2 s . c om*/ * * @param field the Field object associated to the property for which we want to resolve the name. * @return the field name. */ public static String deepFieldName(Field field) { HadoopField annotation = field.getAnnotation(HadoopField.class); if (StringUtils.isNotEmpty(annotation.fieldName())) { return annotation.fieldName(); } else { return field.getName(); } }
From source file:com.khepry.utilities.GenericUtilities.java
public static Map<Integer, String> getAllJdbcTypeNames() { Map<Integer, String> result = new HashMap<Integer, String>(); try {/*from w ww .ja v a 2 s .c om*/ for (Field field : Types.class.getFields()) { result.put((Integer) field.get(null), field.getName()); } } catch (IllegalArgumentException | IllegalAccessException ex) { Logger.getLogger(GenericUtilities.class.getName()).log(Level.SEVERE, null, ex); } return result; }
From source file:Main.java
/** * Converts a given object to a form encoded map * @param objName Name of the object/*from ww w .j a va 2s.c o m*/ * @param obj The object to convert into a map * @param objectMap The object map to populate * @param processed List of objects hashCodes that are already parsed * @throws InvalidObjectException */ private static void objectToMap(String objName, Object obj, Map<String, Object> objectMap, HashSet<Integer> processed) throws InvalidObjectException { //null values need not to be processed if (obj == null) return; //wrapper types are autoboxed, so reference checking is not needed if (!isWrapperType(obj.getClass())) { //avoid infinite recursion if (processed.contains(obj.hashCode())) return; processed.add(obj.hashCode()); } //process arrays if (obj instanceof Collection<?>) { //process array if ((objName == null) || (objName.isEmpty())) throw new InvalidObjectException("Object name cannot be empty"); Collection<?> array = (Collection<?>) obj; //append all elements in the array into a string int index = 0; for (Object element : array) { //load key value pair String key = String.format("%s[%d]", objName, index++); loadKeyValuePairForEncoding(key, element, objectMap, processed); } } else if (obj instanceof Map) { //process map Map<?, ?> map = (Map<?, ?>) obj; //append all elements in the array into a string for (Map.Entry<?, ?> pair : map.entrySet()) { String attribName = pair.getKey().toString(); String key = attribName; if ((objName != null) && (!objName.isEmpty())) { key = String.format("%s[%s]", objName, attribName); } loadKeyValuePairForEncoding(key, pair.getValue(), objectMap, processed); } } else { //process objects // invoke getter methods Method[] methods = obj.getClass().getMethods(); for (Method method : methods) { //is a getter? if ((method.getParameterTypes().length != 0) || (!method.getName().startsWith("get"))) continue; //get json attribute name Annotation getterAnnotation = method.getAnnotation(JsonGetter.class); if (getterAnnotation == null) continue; //load key name String attribName = ((JsonGetter) getterAnnotation).value(); String key = attribName; if ((objName != null) && (!objName.isEmpty())) { key = String.format("%s[%s]", objName, attribName); } try { //load key value pair Object value = method.invoke(obj); loadKeyValuePairForEncoding(key, value, objectMap, processed); } catch (Exception ex) { } } // load fields Field[] fields = obj.getClass().getFields(); for (Field field : fields) { //load key name String attribName = field.getName(); String key = attribName; if ((objName != null) && (!objName.isEmpty())) { key = String.format("%s[%s]", objName, attribName); } try { //load key value pair Object value = field.get(obj); loadKeyValuePairForEncoding(key, value, objectMap, processed); } catch (Exception ex) { } } } }
From source file:nl.minbzk.dwr.zoeken.enricher.notifier.DirectResultCache.java
/** * Since we don't really have a better way to quickly verify integrity, we resort to doing it this way. * //from www . j av a 2 s .co m * @return boolean */ public static boolean validateNotifierProfileModelIntegrity() { final List<String> notifierProfileInternalFields = Arrays.asList(new String[] { "uuid", "email", "emailLanguage", "query", "searchParameters", "notificationInterval", "notificationMaximum" }); for (Field nodeField : com.seajas.search.attender.bridge.wsdl.profile.NotifierProfile.class .getDeclaredFields()) if (!notifierProfileInternalFields.contains(nodeField.getName())) { logger.error("Unable to account for field '" + nodeField.getName() + "' in NotifierProfile"); return false; } return true; }
From source file:com.browseengine.bobo.serialize.JSONSerializer.java
private static void loadObject(Object retObj, Field f, JSONObject jsonObj) throws JSONSerializationException { String key = f.getName(); Class type = f.getType();// ww w .ja v a 2s . co m try { if (type.isPrimitive()) { if (type == Integer.TYPE) { f.setInt(retObj, jsonObj.getInt(key)); } else if (type == Long.TYPE) { f.setLong(retObj, jsonObj.getInt(key)); } else if (type == Short.TYPE) { f.setShort(retObj, (short) jsonObj.getInt(key)); } else if (type == Boolean.TYPE) { f.setBoolean(retObj, jsonObj.getBoolean(key)); } else if (type == Double.TYPE) { f.setDouble(retObj, jsonObj.getDouble(key)); } else if (type == Float.TYPE) { f.setFloat(retObj, (float) jsonObj.getDouble(key)); } else if (type == Character.TYPE) { char ch = jsonObj.getString(key).charAt(0); f.setChar(retObj, ch); } else if (type == Byte.TYPE) { f.setByte(retObj, (byte) jsonObj.getInt(key)); } else { throw new JSONSerializationException("Unknown primitive: " + type); } } else if (type == String.class) { f.set(retObj, jsonObj.getString(key)); } else if (JSONSerializable.class.isAssignableFrom(type)) { JSONObject jObj = jsonObj.getJSONObject(key); JSONSerializable serObj = deSerialize(type, jObj); f.set(retObj, serObj); } } catch (Exception e) { throw new JSONSerializationException(e.getMessage(), e); } }
From source file:Main.java
/** * get all fields for a class/*from w ww .j a v a 2 s . c o m*/ * * @param type * @return all fields indexed by their names */ private static Map<String, Field> getAllFields(Class<?> type) { Map<String, Field> fields = new HashMap<String, Field>(); Class<?> currentType = type; while (!currentType.equals(Object.class)) { for (Field field : currentType.getDeclaredFields()) { int mod = field.getModifiers(); /* * by convention, our fields should not have any modifier */ if (mod == 0 || Modifier.isProtected(mod) && !Modifier.isStatic(mod)) { fields.put(field.getName(), field); } } currentType = currentType.getSuperclass(); } return fields; }
From source file:com.aw.support.reflection.AttributeAccessor.java
/** * @param target/* ww w. ja va 2 s. co m*/ * @return */ public static void set(Object target, Field field, Object value) { field.setAccessible(true); try { field.set(target, value); } catch (IllegalAccessException e) { e.printStackTrace(); throw new IllegalArgumentException("Error setting the value of the attribute:<" + field.getName() + "> of object:<" + target + "> check: Attribute Type:<" + field.getType() + "> value Type: <" + value.getClass() + ">"); } }
From source file:com.test.edusys.common.utils.reflection.ReflectionUtils.java
/** * objectmap//from ww w .jav a 2 s . c o m * @param obj * @return * @throws Exception */ public static Map<String, Object> objectToMap(Object obj) throws Exception { if (obj == null) { return null; } Map<String, Object> map = new HashMap<String, Object>(); Field[] declaredFields = obj.getClass().getDeclaredFields(); for (Field field : declaredFields) { field.setAccessible(true); map.put(field.getName(), field.get(obj)); } return map; }
From source file:com.ginema.api.enricher.SensitiveDataExtractor.java
/** * Recursive method to enrich the object * // w ww. ja v a 2 s.co m * @param o * @param holder * @throws IllegalAccessException */ private static void enrichObjectTree(Object o, SensitiveDataHolder holder) throws Exception { for (Field f : o.getClass().getDeclaredFields()) { if (!ReflectionUtils.isPrimitive(f)) { Method getter = PropertyDescriptorHolder.getGetterMethod(o.getClass(), f.getName()); if (getter == null && !java.lang.reflect.Modifier.isStatic(f.getModifiers())) { throw new IllegalArgumentException("No getter found for property " + f.getName()); } if (getter == null) continue; Object value = getter.invoke(o, null); if (ClassUtils.isAssignable(f.getType(), SensitiveDataField.class)) { populateHolderMapByType(holder, (SensitiveDataField<?>) value); } checkAndEnrichObject(holder, value); checkAndEnrichCollection(holder, value); } } }
From source file:com.siberhus.ngai.core.CrudHelper.java
/** * /*from ww w . ja v a 2 s .co m*/ * @param entityClass * @return */ private synchronized final static EntityInfo getEntityInfo(Class<?> entityClass) { EntityInfo entityInfo = ENTITY_INFO_CACHE.get(entityClass); if (entityInfo != null) { return entityInfo; } entityInfo = new EntityInfo(); Entity entityAnnot = (Entity) entityClass.getAnnotation(Entity.class); if (!"".equals(entityAnnot.name())) { entityInfo.setEntityName(entityAnnot.name()); } else { entityInfo.setEntityName(entityClass.getSimpleName()); } Collection<Field> entityFields = ReflectUtil.getFields(entityClass); for (Field field : entityFields) { if (Modifier.isStatic(field.getModifiers())) { continue; } if (field.getName().equals("id")) { continue; } if (!field.isAccessible()) { field.setAccessible(true); } entityInfo.getFieldSet().add(field); } ENTITY_INFO_CACHE.put(entityClass, entityInfo); return entityInfo; }