List of usage examples for java.lang.reflect Field getName
public String getName()
From source file:org.LexGrid.LexBIG.caCore.utils.LexEVSCaCoreUtils.java
public static Object setFieldValue(Object input, Object value, String fieldName) throws Exception { Class searchClass = input.getClass(); while (searchClass != null) { Field[] fields = searchClass.getDeclaredFields(); for (Field field : fields) { if (field.getName().equals(fieldName)) { field.setAccessible(true); //Check to see if we're trying to set a int, long, etc with a String if (field.getType().getName().equals("java.lang.Long")) { if (value instanceof String) { field.set(input, Long.getLong((String) value)); } else { field.set(input, value); }//from w w w .j a v a 2s . c o m } else if (field.getType().getName().equals("java.lang.Integer")) { if (value instanceof String) { field.set(input, Integer.getInteger((String) value)); } else { field.set(input, value); } } else { field.set(input, value); } } } searchClass = searchClass.getSuperclass(); } return input; }
From source file:utils.ReflectionService.java
public static Map createClassModel(Class clazz, int maxDeep) { Map map = new HashMap(); for (Field field : clazz.getDeclaredFields()) { field.setAccessible(true);// w w w . j a v a 2s . co m String key = field.getName(); try { Class<?> type; if (Collection.class.isAssignableFrom(field.getType())) { ParameterizedType collectionType = (ParameterizedType) field.getGenericType(); type = (Class<?>) collectionType.getActualTypeArguments()[0]; } else { type = field.getType(); } // System.out.println(key + " ReflectionResource.createClassModel ==== putting type: " + field.getType().getName() + ", static: " // + Modifier.isStatic(field.getModifiers()) + ", enum: " + type.isEnum() + ", java.*: " + type.getName().startsWith("java")); if (Modifier.isStatic(field.getModifiers())) { continue; } else if (type.isEnum()) { map.put(key, Enum.class.getName()); } else if (!type.getName().startsWith("java") && !key.startsWith("_") && maxDeep > 0) { map.put(key, createClassModel(type, maxDeep - 1)); } else { map.put(key, type.getName()); } } catch (Exception e) { e.printStackTrace(); } } return map; }
From source file:com.sonatype.security.ldap.api.DeepEqualsBuilder.java
private static void reflectionAppend(Object lhs, Object rhs, Class clazz, EqualsBuilder builder, boolean useTransients, String[] excludeFields) { while (clazz.getSuperclass() != null) { Field[] fields = clazz.getDeclaredFields(); List excludedFieldList = excludeFields != null ? Arrays.asList(excludeFields) : Collections.EMPTY_LIST; AccessibleObject.setAccessible(fields, true); for (int i = 0; i < fields.length && builder.isEquals(); i++) { Field f = fields[i]; if (!excludedFieldList.contains(f.getName()) && (f.getName().indexOf('$') == -1) && (useTransients || !Modifier.isTransient(f.getModifiers())) && (!Modifier.isStatic(f.getModifiers()))) { try { Object lhsChild = f.get(lhs); Object rhsChild = f.get(rhs); Class testClass = getTestClass(lhsChild, rhsChild); boolean hasEqualsMethod = classHasEqualsMethod(testClass); if (testClass != null && !hasEqualsMethod) { reflectionAppend(lhsChild, rhsChild, testClass, builder, useTransients, excludeFields); } else { builder.append(lhsChild, rhsChild); }//from w w w .j a v a 2 s. c om } catch (IllegalAccessException e) { // this can't happen. Would get a Security exception instead // throw a runtime exception in case the impossible happens. throw new InternalError("Unexpected IllegalAccessException"); } } } // now for the parent clazz = clazz.getSuperclass(); reflectionAppend(lhs, rhs, clazz, builder, useTransients, excludeFields); } }
From source file:org.fastmongo.odm.util.ReflectionUtils.java
/** * Attempt to find a {@link Field field} on the supplied {@link Class} with the * supplied {@code name} and/or {@link Class type}. Searches all superclasses * up to {@link Object}.// ww w . java 2 s. c om * * @param clazz the class to introspect * @param name the name of the field (may be {@code null} if type is specified) * @param type the type of the field (may be {@code null} if name is specified) * @return the corresponding Field object, or {@code null} if not found */ public static Field findField(Class<?> clazz, String name, Class<?> type) { Class<?> searchType = clazz; while (!Object.class.equals(searchType) && searchType != null) { Field[] fields = searchType.getDeclaredFields(); for (Field field : fields) { if ((name == null || name.equals(field.getName())) && (type == null || type.equals(field.getType()))) { return field; } } searchType = searchType.getSuperclass(); } return null; }
From source file:org.mstc.zmq.json.Decoder.java
static Field getField(String name, Field[] fields) { for (Field f : fields) { if (f.getName().equals(name)) { return f; }// ww w . j a va2 s .c om } return null; }
From source file:utils.ReflectionService.java
public static JsonNode createJsonNode(Class clazz, int maxDeep) { if (maxDeep <= 0 || JsonNode.class.isAssignableFrom(clazz)) { return null; }/*from ww w .j a v a 2 s . c om*/ ObjectMapper objectMapper = new ObjectMapper(); ObjectNode objectNode = objectMapper.createObjectNode(); for (Field field : clazz.getDeclaredFields()) { field.setAccessible(true); String key = field.getName(); try { // is array or collection if (field.getType().isArray() || Collection.class.isAssignableFrom(field.getType())) { ParameterizedType collectionType = (ParameterizedType) field.getGenericType(); Class<?> type = (Class<?>) collectionType.getActualTypeArguments()[0]; ArrayNode arrayNode = objectMapper.createArrayNode(); arrayNode.add(createJsonNode(type, maxDeep - 1)); objectNode.put(key, arrayNode); } else { Class<?> type = field.getType(); if (Modifier.isStatic(field.getModifiers())) { continue; } else if (type.isEnum()) { objectNode.put(key, Enum.class.getName()); } else if (!type.getName().startsWith("java") && !key.startsWith("_")) { objectNode.put(key, createJsonNode(type, maxDeep - 1)); } else { objectNode.put(key, type.getName()); } } } catch (Exception e) { e.printStackTrace(); } } return objectNode; }
From source file:edu.cornell.med.icb.goby.util.barcode.BarcodeMatcherResult.java
public static synchronized void ensureFieldsMap() { if (FIELDS_MAP != null) { return;//from w w w . j a va 2 s . c om } final Field[] fields = BarcodeMatcherResult.class.getDeclaredFields(); FIELDS_MAP = new HashMap<String, Field>(fields.length); for (final Field field : fields) { FIELDS_MAP.put(field.getName(), field); } }
From source file:jp.co.opentone.bsol.framework.web.view.PagePropertyUtil.java
/** * page??{@link Transfer}????????./* www. j a v a 2s.c om*/ * <p> * ???????????????. * </p> * @param page * page * @return {@link Transfer}???? */ public static Map<String, Object> collectTransferValues(Page page) { Map<String, Object> values = new HashMap<String, Object>(); for (Class<?> c = page.getClass(); c != null; c = c.getSuperclass()) { for (Field field : c.getDeclaredFields()) { if (field.isAnnotationPresent(Transfer.class)) { String name = field.getName(); Object value; try { value = PropertyUtils.getProperty(page, name); if (value != null) { values.put(name, value); } } catch (IllegalAccessException e) { throw new ReflectionRuntimeException(e); } catch (InvocationTargetException e) { throw new ReflectionRuntimeException(e); } catch (NoSuchMethodException e) { throw new ReflectionRuntimeException(e); } } } } return values; }
From source file:kina.utils.AnnotationUtils.java
/** * Returns the value of the fields <i>kinaField</i> in the instance <i>entity</i> of type T. * * @param entity the entity to process. * @param kinaField the Field to process belonging to <i>entity</i> * @return the property value./* w ww . j a v a 2 s . c om*/ */ public static Serializable getBeanFieldValue(KinaType entity, java.lang.reflect.Field kinaField) { try { return (Serializable) PropertyUtils.getProperty(entity, kinaField.getName()); } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e1) { throw new IOException(e1); } }
From source file:com.bosscs.spark.commons.utils.AnnotationUtils.java
/** * Returns the value of the fields <i>deepField</i> in the instance <i>entity</i> of type T. * * @param entity the entity to process. * @param deepField the Field to process belonging to <i>entity</i> * @return the property value./* w w w . j av a2s . co m*/ */ public static Serializable getBeanFieldValue(IType entity, Field deepField) { try { return (Serializable) PropertyUtils.getProperty(entity, deepField.getName()); } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e1) { throw new HadoopIOException(e1); } }