List of usage examples for java.lang.reflect Field getAnnotation
public <T extends Annotation> T getAnnotation(Class<T> annotationClass)
From source file:com.conversantmedia.mapreduce.tool.DistributedResourceManager.java
/** * Locates the resources in the configuration and distributed cache, etc., * and sets them on the provided mapper instance. * //from w w w . jav a2 s. c o m * @param bean the object to inspect for resource annotations * @param config the job configuration * @throws ToolException if there are errors with reflection or the cache */ @SuppressWarnings("unchecked") public static void initializeResources(Object bean, Configuration config) throws ToolException { try { List<Field> fields = MaraAnnotationUtil.INSTANCE.findAnnotatedFields(bean.getClass(), Resource.class); Path[] files = org.apache.hadoop.util.StringUtils .stringToPath(config.getStrings(MRJobConfig.CACHE_LOCALFILES)); for (Field field : fields) { Resource resAnnotation = field.getAnnotation(Resource.class); String key = StringUtils.isEmpty(resAnnotation.name()) ? field.getName() : resAnnotation.name(); String resourceId = config.get(CONFIGKEYBASE_RESOURCE + key); if (resourceId != null) { String[] parts = StringUtils.split(resourceId, VALUE_SEP); String className = parts[0]; String valueString = parts[1]; // Retrieve the value Object value = getResourceValue(field, valueString, className, files); setFieldValue(field, bean, value); } } } catch (IllegalArgumentException | IOException | ClassNotFoundException | IllegalAccessException e) { throw new ToolException(e); } }
From source file:com.epam.ta.reportportal.database.search.CriteriaMap.java
public static String getQueryCriteria(Field f) { String queryCriteria;//from w w w . ja va 2s . co m if (f.isAnnotationPresent(org.springframework.data.mongodb.core.mapping.Field.class)) { queryCriteria = f.getAnnotation(org.springframework.data.mongodb.core.mapping.Field.class).value(); } else { queryCriteria = f.getName(); } return queryCriteria; }
From source file:com.diversityarrays.dal.db.DalDatabaseUtil.java
static public Map<Field, Column> buildEntityFieldColumnMap(Class<? extends DalEntity> entityClass) { Map<Field, Column> columnByField = new LinkedHashMap<Field, Column>(); for (Field fld : entityClass.getDeclaredFields()) { if (!Modifier.isStatic(fld.getModifiers())) { Column column = fld.getAnnotation(Column.class); fld.setAccessible(true);//from www .j a v a 2s . c om columnByField.put(fld, column); } } return columnByField; }
From source file:com.adobe.acs.commons.mcp.util.AnnotatedFieldDeserializer.java
@SuppressWarnings("squid:S3776") private static void parseInput(Object target, ValueMap input, Field field) throws ReflectiveOperationException, ParseException { FormField inputAnnotation = field.getAnnotation(FormField.class); Object value;//from w w w . j a v a 2 s. c o m if (input.get(field.getName()) == null) { if (inputAnnotation != null && inputAnnotation.required()) { if (field.getType() == Boolean.class || field.getType() == Boolean.TYPE) { value = false; } else { throw new NullPointerException("Required field missing: " + field.getName()); } } else { return; } } else { value = input.get(field.getName()); } if (hasMultipleValues(field.getType())) { parseInputList(target, serializeToStringArray(value), field); } else { Object val = value; if (value.getClass().isArray()) { val = ((Object[]) value)[0]; } if (val instanceof RequestParameter) { /** Special case handling uploaded files; Method call ~ copied from parseInputValue(..) **/ if (field.getType() == RequestParameter.class) { FieldUtils.writeField(field, target, val, true); } else { try { FieldUtils.writeField(field, target, ((RequestParameter) val).getInputStream(), true); } catch (IOException ex) { LOG.error("Unable to get InputStream for uploaded file [ {} ]", ((RequestParameter) val).getName(), ex); } } } else { parseInputValue(target, String.valueOf(val), field); } } }
From source file:com.darkstar.beanCartography.utils.NameUtils.java
/** * @param field field to check/*from w ww . j a va2s .c o m*/ * @return the name associated to the field */ public static String getBusinessName(Field field) { Preconditions.checkNotNull(field, "Field cannot be null"); if (hasBusinessName(field)) return field.getAnnotation(NamedField.class).name(); return null; }
From source file:edu.usu.sdl.openstorefront.core.util.EntityUtil.java
public static <T extends BaseEntity> boolean isPKFieldGenerated(T entity) { boolean generated = false; Field field = getPKField(entity); if (field != null) { PK idAnnotation = field.getAnnotation(PK.class); if (idAnnotation != null) { generated = idAnnotation.generated(); }// www .j a v a 2 s. c o m } else { throw new OpenStorefrontRuntimeException("Unable to find PK for enity: " + entity.getClass().getName(), "Check entity passed in."); } return generated; }
From source file:com.u2apple.tool.util.AndroidDeviceUtils.java
public static String getPropertyByKey(AndroidDeviceRanking androidDevice, String key) { if (androidDevice == null || key == null) { return null; }/*from w ww .j a va 2s.c o m*/ for (Field field : androidDevice.getClass().getSuperclass().getDeclaredFields()) { field.setAccessible(true); if (field.isAnnotationPresent(Key.class)) { Key keyAnno = field.getAnnotation(Key.class); if (keyAnno.value().equals(key)) { try { return (String) field.get(androidDevice); } catch (IllegalArgumentException | IllegalAccessException ex) { Logger.getLogger(AndroidDeviceUtils.class.getName()).log(Level.SEVERE, null, ex); } } } } return null; }
From source file:org.querybyexample.jpa.JpaUtil.java
public static <T extends Identifiable<?>> String compositePkPropertyName(T entity) { for (Method m : entity.getClass().getMethods()) { if (m.getAnnotation(EmbeddedId.class) != null) { return BeanUtils.findPropertyForMethod(m).getName(); }//from ww w . ja v a 2 s . c om } for (Field f : entity.getClass().getFields()) { if (f.getAnnotation(EmbeddedId.class) != null) { return f.getName(); } } return null; }
From source file:org.querybyexample.jpa.JpaUtil.java
public static <T extends Identifiable<?>> boolean hasSimplePk(T entity) { for (Method m : entity.getClass().getMethods()) { if (m.getAnnotation(Id.class) != null) { return true; }//www . ja v a 2s. co m } for (Field f : entity.getClass().getFields()) { if (f.getAnnotation(Id.class) != null) { return true; } } return false; }
From source file:edu.usu.sdl.openstorefront.core.util.EntityUtil.java
/** * This will set default on the fields that are marked with a default and * are null//ww w.j a v a 2s .c om * * @param entity */ public static void setDefaultsOnFields(Object entity) { Objects.requireNonNull(entity, "Entity must not be NULL"); List<Field> fields = getAllFields(entity.getClass()); for (Field field : fields) { DefaultFieldValue defaultFieldValue = field.getAnnotation(DefaultFieldValue.class); if (defaultFieldValue != null) { field.setAccessible(true); try { if (field.get(entity) == null) { String value = defaultFieldValue.value(); Class fieldClass = field.getType(); if (fieldClass.getSimpleName().equalsIgnoreCase(String.class.getSimpleName())) { field.set(entity, value); } else if (fieldClass.getSimpleName().equalsIgnoreCase(Long.class.getSimpleName())) { field.set(entity, value); } else if (fieldClass.getSimpleName().equalsIgnoreCase(Integer.class.getSimpleName())) { field.set(entity, Integer.parseInt(value)); } else if (fieldClass.getSimpleName().equalsIgnoreCase(Boolean.class.getSimpleName())) { field.set(entity, Convert.toBoolean(value)); } else if (fieldClass.getSimpleName().equalsIgnoreCase(Double.class.getSimpleName())) { field.set(entity, Double.parseDouble(value)); } else if (fieldClass.getSimpleName().equalsIgnoreCase(Float.class.getSimpleName())) { field.set(entity, Float.parseFloat(value)); } else if (fieldClass.getSimpleName().equalsIgnoreCase(BigDecimal.class.getSimpleName())) { field.set(entity, Convert.toBigDecimal(value)); } else if (fieldClass.getSimpleName().equalsIgnoreCase(Date.class.getSimpleName())) { field.set(entity, TimeUtil.fromString(value)); } else if (fieldClass.getSimpleName().equalsIgnoreCase(BigInteger.class.getSimpleName())) { field.set(entity, new BigInteger(value)); } } } catch (IllegalArgumentException | IllegalAccessException ex) { throw new OpenStorefrontRuntimeException( "Unable to get value on " + entity.getClass().getName(), "Check entity passed in."); } } } }