List of usage examples for java.lang.reflect Field getName
public String getName()
From source file:com.github.juanmf.java2plant.Parser.java
protected static void addAggregation(Set<Relation> relations, Class<?> fromType, Field f) { Class<?> delegateType = f.getType(); String varName = f.getName(); String message = varName + ": " + TypesHelper.getSimpleName(f.getGenericType().toString()); String toCardinal = "1"; String toName = delegateType.getName(); if (isMulti(delegateType)) { toCardinal = "*"; if (!delegateType.isArray()) { Set<String> typeVars = getTypeParams(f); for (String type : typeVars) { Relation aggregation = new Aggregation(fromType, type, f, toCardinal, message); relations.add(aggregation); }/*ww w . j a va 2 s . co m*/ return; } toName = CanonicalName.getClassName(delegateType.getName()); } Relation aggregation = new Aggregation(fromType, toName, f, toCardinal, message); relations.add(aggregation); }
From source file:com.vmware.photon.controller.common.dcp.validation.NotBlankValidator.java
public static void validate(ServiceDocument state) { try {//from w w w . jav a2 s . c o m Field[] declaredFields = state.getClass().getDeclaredFields(); for (Field field : declaredFields) { Annotation[] declaredAnnotations = field.getDeclaredAnnotations(); for (Annotation annotation : declaredAnnotations) { if (annotation.annotationType() == NotBlank.class) { checkState(null != field.get(state), String.format("%s cannot be null", field.getName())); if (String.class.equals(field.getType())) { checkState((StringUtils.isNotBlank((String) field.get(state))), String.format("%s cannot be blank", field.getName())); } } } } } catch (IllegalStateException e) { throw e; } catch (Throwable t) { throw new RuntimeException(t); } }
From source file:gr.abiss.calipso.tiers.specifications.GenericSpecifications.java
/** * Get a (cached) field for the given class' member name * @param clazz// w w w . j a v a 2s. c o m * @param fieldName the member name * @return */ public static Field getField(Class<?> clazz, String fieldName) { Field field = null; if (!IGNORED_FIELD_NAMES.contains(fieldName)) { String key = clazz.getName() + "#" + fieldName; field = FIELD_CACHE.get(key); // find it if not cached if (field == null && !FIELD_CACHE.containsKey(key)) { Class<?> tmpClass = clazz; do { for (Field tmpField : tmpClass.getDeclaredFields()) { String candidateName = tmpField.getName(); if (candidateName.equals(fieldName)) { // field.setAccessible(true); FIELD_CACHE.put(key, tmpField); field = tmpField; break; } } tmpClass = tmpClass.getSuperclass(); } while (tmpClass != null && field == null); } if (field == null) { LOGGER.warn("Field '" + fieldName + "' not found on class " + clazz); // HashMap handles null values so we can use containsKey to cach // invalid fields and hence skip the reflection scan FIELD_CACHE.put(key, null); } // throw new RuntimeException("Field '" + fieldName + // "' not found on class " + clazz); } return field; }
From source file:com.espertech.esper.event.vaevent.PropertyUtility.java
private static PropertyAccessException getAccessExceptionField(Field field, Exception e) { Class declaring = field.getDeclaringClass(); String message = "Failed to obtain field value for field " + field.getName() + " on class " + JavaClassHelper.getClassNameFullyQualPretty(declaring) + ": " + e.getMessage(); throw new PropertyAccessException(message, e); }
From source file:com.palantir.ptoss.util.Reflections.java
/** * Find a {@link Field} based on the field name. Will return private fields but will not * look in superclasses.// ww w . j a v a2s. c o m * * @return null if there is no field found */ // TODO (dcervelli) fix for superclasses public static Field getFieldByName(Class<?> klass, String fieldName) { for (Field f : klass.getDeclaredFields()) { if (f.getName().equals(fieldName)) { return f; } } return null; }
From source file:com.blackducksoftware.integration.hub.detect.workflow.report.util.ObjectPrinter.java
public static void populateField(final Field field, final String prefix, final Object guy, Map<String, String> fieldMap) { if (java.lang.reflect.Modifier.isStatic(field.getModifiers())) { return; // don't print static fields. }/*w w w . j a v a 2 s . c o m*/ final String name = field.getName(); String value = "unknown"; Object obj = null; try { if (!field.isAccessible()) { field.setAccessible(true); } obj = field.get(guy); } catch (final Exception e) { e.printStackTrace(); } boolean shouldPrintObjectsFields = false; if (obj == null) { value = "null"; } else { value = obj.toString(); shouldPrintObjectsFields = shouldRecursivelyPrintType(obj.getClass()); } if (!shouldPrintObjectsFields) { if (StringUtils.isBlank(prefix)) { fieldMap.put(name, value); } else { fieldMap.put(prefix + "." + name, value); } } else { String nestedPrefix = name; if (StringUtils.isNotBlank(prefix)) { nestedPrefix = prefix + "." + nestedPrefix; } populateObject(nestedPrefix, obj, fieldMap); } }
From source file:org.oncoblocks.centromere.web.controller.RequestUtils.java
/** * Checks a request parameter name against all possible {@link Model} attributes, converting it to * the appropriate repository field name for querying and sorting. * * @param param/*w ww.ja v a 2s. c om*/ * @return */ public static String remapParameterName(String param, Class<? extends Model<?>> model) { logger.debug(String.format("Attempting to remap query string parameter: %s", param)); for (Field field : model.getDeclaredFields()) { String fieldName = field.getName(); if (field.isAnnotationPresent(Aliases.class)) { Aliases aliases = field.getAnnotation(Aliases.class); for (Alias alias : aliases.value()) { if (alias.value().equals(param)) return fieldName; } } else if (field.isAnnotationPresent(Alias.class)) { Alias alias = field.getAnnotation(Alias.class); if (alias.value().equals(param)) return fieldName; } } logger.debug(String.format("Parameter remapped to: %s", param)); return param; }
From source file:com.feilong.commons.core.lang.reflect.FieldUtil.java
/** * (?),key fieldNamevalue ./* w w w . ja v a 2 s. c o m*/ * * @param obj * the obj * @param excludeFieldNames * ?field names,?nullOrEmpty ? * @return the field value map * @throws ReflectException * the reflect exception */ public static Map<String, Object> getFieldValueMap(Object obj, String[] excludeFieldNames) throws ReflectException { // (?,) Field[] fields = getDeclaredFields(obj); Map<String, Object> map = new TreeMap<String, Object>(); if (Validator.isNotNullOrEmpty(fields)) { for (Field field : fields) { String fieldName = field.getName(); if (Validator.isNotNullOrEmpty(excludeFieldNames) && ArrayUtil.isContain(excludeFieldNames, fieldName)) { continue; } int modifiers = field.getModifiers(); // ??? log boolean isPrivateAndStatic = Modifier.isPrivate(modifiers) && Modifier.isStatic(modifiers); log.debug("field name:[{}],modifiers:[{}],isPrivateAndStatic:[{}]", fieldName, modifiers, isPrivateAndStatic); if (!isPrivateAndStatic) { //TODO see org.apache.commons.lang3.reflect.MemberUtils.setAccessibleWorkaround(AccessibleObject) field.setAccessible(true); try { map.put(fieldName, field.get(obj)); } catch (Exception e) { log.error(e.getClass().getName(), e); throw new ReflectException(e); } } } } return map; }
From source file:com.sample.common.util.CommonsUtil.java
/** * Metodo di utilita' per controllare che un'istanza di una classe abbia almeno un campo valorizzato * //w w w . j a v a 2 s . co m * @param instance * @return true se l'istanza ha almeno un campo valorizzato, false altrimenti */ public static <T> boolean isFilled(T instance) { boolean isFilled = false; if (instance != null) { Field[] fields = instance.getClass().getDeclaredFields(); for (Field field : fields) { if (field.getType().getCanonicalName().startsWith("java.lang")) { String getterMethodName = "get" + capitalize(field.getName()); Method method = findMethod(instance.getClass(), getterMethodName, new Class[] {}); if (method != null) { try { Object value = method.invoke(instance, new Object[] {}); if (field.getType().getSimpleName().equals("String")) { String valueStr = (String) value; if (value != null && valueStr.trim().length() > 0) { isFilled = true; break; } } else { if (value != null) { isFilled = true; break; } } } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } } } } return isFilled; }
From source file:ClassTree.java
/** * Returns a description of the fields of a class. * @param the class to be described/*from w ww . ja v a2s.c om*/ * @return a string containing all field types and names */ public static String getFieldDescription(Class<?> c) { // use reflection to find types and names of fields StringBuilder r = new StringBuilder(); Field[] fields = c.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { Field f = fields[i]; if ((f.getModifiers() & Modifier.STATIC) != 0) r.append("static "); r.append(f.getType().getName()); r.append(" "); r.append(f.getName()); r.append("\n"); } return r.toString(); }