List of usage examples for java.lang.reflect Field getName
public String getName()
From source file:com.github.fcannizzaro.resourcer.Resourcer.java
/** * Read a parsed value//from ww w .j av a 2 s . c o m * * @throws IllegalAccessException */ private static void annotate(Field field, String type, String name, Object annotated) throws IllegalAccessException { if (name.equals("null")) name = field.getName(); field.set(annotated, values.get(type).get(name)); }
From source file:com.feilong.core.lang.reflect.FieldUtil.java
/** * <code>klass</code> ? <code>excludeFieldNames</code> ?list. * * @param klass//from w ww . j a va 2 s . c o m * the klass * @param excludeFieldNames * ?field names,?nullOrEmpty ? * @return <code>obj</code> null, {@link NullPointerException}<br> * {@link FieldUtils#getAllFieldsList(Class)} nullempty, {@link Collections#emptyList()}<br> * @see FieldUtils#getAllFieldsList(Class) * @since 1.7.1 */ //no static public static List<Field> getAllFieldList(final Class<?> klass, String... excludeFieldNames) { // {@link Field},parents, public/protect/private/inherited... List<Field> fieldList = FieldUtils.getAllFieldsList(klass); if (isNullOrEmpty(fieldList)) { return Collections.emptyList(); } //********************************************************************************************** Predicate<Field> excludeFieldPredicate = BeanPredicateUtil.containsPredicate("name", excludeFieldNames); Predicate<Field> staticPredicate = new Predicate<Field>() { @Override public boolean evaluate(Field field) { int modifiers = field.getModifiers(); // ??? log serialVersionUID boolean isStatic = Modifier.isStatic(modifiers); String pattern = "[{}.{}],modifiers:[{}]{}"; LOGGER.trace(pattern, klass.getSimpleName(), field.getName(), modifiers, isStatic ? " [isStatic]" : EMPTY); return isStatic; } }; return CollectionsUtil.selectRejected(fieldList, PredicateUtils.orPredicate(excludeFieldPredicate, staticPredicate)); }
From source file:com.stratio.deep.commons.utils.CellsUtils.java
/** * Gets object from json.//from w w w .j av a 2 s.c o m * * @param classEntity the class entity * @param bsonObject the bson object * @return the object from json * @throws IllegalAccessException the illegal access exception * @throws InstantiationException the instantiation exception * @throws InvocationTargetException the invocation target exception */ public static <T> T getObjectFromJson(Class<T> classEntity, JSONObject bsonObject) throws IllegalAccessException, InstantiationException, InvocationTargetException { T t = classEntity.newInstance(); Field[] fields = AnnotationUtils.filterDeepFields(classEntity); Object insert = null; for (Field field : fields) { Object currentBson = null; Method method = null; try { method = Utils.findSetter(field.getName(), classEntity, field.getType()); Class<?> classField = field.getType(); currentBson = bsonObject.get(AnnotationUtils.deepFieldName(field)); if (currentBson != null) { if (Iterable.class.isAssignableFrom(classField)) { Type type = field.getGenericType(); insert = subDocumentListCase(type, (List) bsonObject.get(AnnotationUtils.deepFieldName(field))); } else if (IDeepType.class.isAssignableFrom(classField)) { insert = getObjectFromJson(classField, (JSONObject) bsonObject.get(AnnotationUtils.deepFieldName(field))); } else { insert = currentBson; } method.invoke(t, insert); } } catch (IllegalAccessException | InstantiationException | InvocationTargetException | IllegalArgumentException e) { LOG.error("impossible to create a java object from Bson field:" + field.getName() + " and type:" + field.getType() + " and value:" + t + "; bsonReceived:" + currentBson + ", bsonClassReceived:" + currentBson.getClass()); method.invoke(t, Utils.castNumberType(insert, t.getClass())); } } return t; }
From source file:com.stratio.deep.commons.utils.CellsUtils.java
public static <T> T getObjectWithMapFromJson(Class<T> classEntity, JSONObject bsonObject) throws IllegalAccessException, InstantiationException, InvocationTargetException { T t = classEntity.newInstance();/* ww w .j a v a2 s . c o m*/ Field[] fields = AnnotationUtils.filterDeepFields(classEntity); Object insert = null; for (Field field : fields) { Object currentBson = null; Method method = null; try { method = Utils.findSetter(field.getName(), classEntity, field.getType()); Class<?> classField = field.getType(); currentBson = bsonObject.get(AnnotationUtils.deepFieldName(field)); if (currentBson != null) { if (Collection.class.isAssignableFrom(classField)) { Type type = field.getGenericType(); List list = new ArrayList(); for (Object o : (List) bsonObject.get(AnnotationUtils.deepFieldName(field))) { list.add((String) o); } insert = list; } else if (IDeepType.class.isAssignableFrom(classField)) { insert = getObjectFromJson(classField, (JSONObject) bsonObject.get(AnnotationUtils.deepFieldName(field))); } else { insert = currentBson; } if (insert != null) { method.invoke(t, insert); } } } catch (IllegalAccessException | InstantiationException | InvocationTargetException | IllegalArgumentException e) { LOG.error("impossible to create a java object from Bson field:" + field.getName() + " and type:" + field.getType() + " and value:" + t + "; bsonReceived:" + currentBson + ", bsonClassReceived:" + currentBson.getClass()); method.invoke(t, Utils.castNumberType(insert, t.getClass())); } } return t; }
From source file:com.kcs.core.utilities.Utility.java
public static void analyze(final Object obj) { ReflectionUtils.doWithFields(obj.getClass(), new ReflectionUtils.FieldCallback() { @Override/* w w w .j a va2s. com*/ public void doWith(final Field field) throws IllegalArgumentException, IllegalAccessException { field.setAccessible(true); logger.debug(field.getName() + " : " + field.get(obj)); } }); }
From source file:net.ceos.project.poi.annotated.core.CellFormulaHandler.java
/** * Apply a explicit formula value at the Cell. * /*from ww w . j a v a2 s . c o m*/ * @param object * the object * @param field * the field * @throws NoSuchMethodException * @throws IllegalAccessException * @throws InvocationTargetException */ private static Object toExplicitFormula(final Object object, final Field field) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { @SuppressWarnings("rawtypes") Class[] argTypes = {}; String method = Constants.FORMULA + field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1); Method m = object.getClass().getDeclaredMethod(method, argTypes); return m.invoke(object, (Object[]) null); }
From source file:net.sourceforge.stripes.integration.spring.SpringHelper.java
/** * Looks for all methods and fields annotated with {@code @SpringBean} and attempts * to lookup and inject a managed bean into the field/property. If any annotated * element cannot be injected an exception is thrown. * * @param bean the bean into which to inject spring beans * @param ctx the Spring application context *//*from w w w. ja va2 s . c om*/ public static void injectBeans(Object bean, ApplicationContext ctx) { // First inject any values using annotated methods for (Method m : getMethods(bean.getClass())) { try { SpringBean springBean = m.getAnnotation(SpringBean.class); boolean nameSupplied = !"".equals(springBean.value()); String name = nameSupplied ? springBean.value() : methodToPropertyName(m); Class<?> beanType = m.getParameterTypes()[0]; Object managedBean = findSpringBean(ctx, name, beanType, !nameSupplied); m.invoke(bean, managedBean); } catch (Exception e) { throw new StripesRuntimeException( "Exception while trying to lookup and inject " + "a Spring bean into a bean of type " + bean.getClass().getSimpleName() + " using method " + m.toString(), e); } } // And then inject any properties that are annotated for (Field f : getFields(bean.getClass())) { try { SpringBean springBean = f.getAnnotation(SpringBean.class); boolean nameSupplied = !"".equals(springBean.value()); String name = nameSupplied ? springBean.value() : f.getName(); Object managedBean = findSpringBean(ctx, name, f.getType(), !nameSupplied); f.set(bean, managedBean); } catch (Exception e) { throw new StripesRuntimeException( "Exception while trying to lookup and inject " + "a Spring bean into a bean of type " + bean.getClass().getSimpleName() + " using field access on field " + f.toString(), e); } } }
From source file:com.yukthi.utils.beans.PropertyMapper.java
/** * Validates the field information specified in the "mapping" and adds the mapping information * to specified bean info/*from w w w . j a v a 2 s . c o m*/ * @param beanInfo Bean info to which mapping needs to be added * @param field Field on which mapping is found * @param mapping Mapping to be added */ private static void addMapping(BeanInfo beanInfo, Field field, PropertyMapping mapping) { NestedProperty externalProperty = null, localProperty = null; try { externalProperty = NestedProperty.getNestedProperty(mapping.type(), mapping.from()); String localPropertyName = field.getName(); //if local sub property is specified if (mapping.subproperty().length() > 0) { localPropertyName = localPropertyName + "." + mapping.subproperty(); } localProperty = NestedProperty.getNestedProperty(field.getDeclaringClass(), localPropertyName); } catch (Exception ex) { throw new InvalidConfigurationException(ex, "Invalid property mapping specified on field - {}.{}. " + "An error occurred while processing mapping properties.", field.getDeclaringClass().getName(), field.getName()); } //ensure target and source are of same types if (!CommonUtils.isAssignable(externalProperty.getType(), localProperty.getType())) { throw new InvalidConfigurationException( "Invalid property mapping specified on field - {}.{}. " + "Source property type and target property type are not matching", field.getDeclaringClass().getName(), field.getName()); } beanInfo.addCustomMapping(mapping.type(), new MappingInfo(externalProperty, localProperty)); }
From source file:com.changhong.util.db.util.sql.InsertSqlBuilder.java
/** * ,?/* w ww . ja va 2s. c o m*/ * * @return * @throws CHDBException * @throws IllegalArgumentException * @throws IllegalAccessException */ public static CHArrayList getFieldsAndValue(Object entity) throws CHDBException, IllegalArgumentException, IllegalAccessException { // TODO Auto-generated method stub CHArrayList arrayList = new CHArrayList(); if (entity == null) { throw new CHDBException("?"); } Class<?> clazz = entity.getClass(); Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { if (!DBUtils.isTransient(field)) { if (DBUtils.isBaseDateType(field)) { CHPrimaryKey annotation = field.getAnnotation(CHPrimaryKey.class); if (annotation == null || !annotation.autoIncrement()) { String columnName = DBUtils.getColumnByField(field); columnName = (columnName != null && !columnName.equals("")) ? columnName : field.getName(); field.setAccessible(true); String val = DBUtils.toString(field.get(entity)); arrayList.add(columnName, val); } } } } return arrayList; }
From source file:com.test.edusys.common.utils.reflection.ReflectionUtils.java
/** * Comment?/*from w ww . j ava 2s.c o m*/ */ public static Map<String, String> getCommentName(final Class clazz) { Field[] fields = clazz.getDeclaredFields(); Map<String, String> map = new HashMap<String, String>(); for (Field f : fields) { Annotation[] aAnnotation = f.getAnnotations(); for (Annotation annotation : aAnnotation) { if (annotation.annotationType() == Comment.class) { Comment element = (Comment) annotation; map.put(f.getName(), element.value()); } } } return map; }