List of usage examples for java.lang.reflect Field isAnnotationPresent
@Override public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass)
From source file:net.ceos.project.poi.annotated.bean.ObjectMaskBuilder.java
/** * Transform BigDecimal values to validate. * /*from www . j av a2 s.c o m*/ * @param fieldName * @param value * @return */ private static BigDecimal transformValuesToValidate(String fieldName, BigDecimal value) { ObjectMask a = new ObjectMask(); try { Field f = a.getClass().getDeclaredField(fieldName); if (f.isAnnotationPresent(XlsElement.class)) { XlsElement xlsAnnotation = (XlsElement) f.getAnnotation(XlsElement.class); if (StringUtils.isNotBlank(xlsAnnotation.transformMask())) { DecimalFormat df = new DecimalFormat(xlsAnnotation.transformMask()); String formattedValue = df.format((BigDecimal) value); value = BigDecimal.valueOf(Double.valueOf(formattedValue.replace(",", "."))); } } } catch (NoSuchFieldException | SecurityException e) { e.printStackTrace(); } return value; }
From source file:net.ceos.project.poi.annotated.bean.ObjectMaskBuilder.java
/** * Transform Double values to validate.//from ww w.ja v a 2s. c o m * * @param fieldName * @param value * @return */ private static Double transformValuesToValidate(String fieldName, Double value) { ObjectMask a = new ObjectMask(); try { Field f = a.getClass().getDeclaredField(fieldName); if (f.isAnnotationPresent(XlsElement.class)) { XlsElement xlsAnnotation = (XlsElement) f.getAnnotation(XlsElement.class); if (StringUtils.isNotBlank(xlsAnnotation.transformMask())) { DecimalFormat df = new DecimalFormat(xlsAnnotation.transformMask()); String formattedValue = df.format((Double) value); value = Double.valueOf(formattedValue.replace(",", ".")); } } } catch (NoSuchFieldException | SecurityException e) { e.printStackTrace(); } return value; }
From source file:org.bigmouth.nvwa.utils.xml.Dom4jDecoder.java
/** * <p>XML???</p>/*from ww w . j av a 2s . c om*/ * ? * appId? * ?XMLappId?appid?APPID?app_id?APP_ID * @param <T> * @param xml * @param xpath * @param cls * @return */ public static <T> T decode(String xml, String xpath, Class<T> cls) throws Exception { if (StringUtils.isBlank(xml)) return null; T t = cls.newInstance(); Document doc = DocumentHelper.parseText(xml); Node itemNode = doc.selectSingleNode(xpath); Field[] fields = cls.getDeclaredFields(); for (Field field : fields) { // if the field name is 'appId' String name = field.getName(); String nodename = name; if (field.isAnnotationPresent(Argument.class)) { nodename = field.getAnnotation(Argument.class).name(); } // select appId node Node current = itemNode.selectSingleNode(nodename); if (null == current) { // select appid node current = itemNode.selectSingleNode(nodename.toLowerCase()); } if (null == current) { // select APPID node current = itemNode.selectSingleNode(nodename.toUpperCase()); } if (null == current) { // select app_id node nodename = StringUtils.join(StringUtils.splitByCharacterTypeCamelCase(nodename), "_").toLowerCase(); current = itemNode.selectSingleNode(nodename); } if (null == current) { // select APP_ID node nodename = StringUtils.join(StringUtils.splitByCharacterTypeCamelCase(nodename), "_").toUpperCase(); current = itemNode.selectSingleNode(nodename); } if (null != current) { String invokeName = StringUtils.join(new String[] { "set", StringUtils.capitalize(name) }); try { MethodUtils.invokeMethod(t, invokeName, current.getText()); } catch (NoSuchMethodException e) { LOGGER.warn("NoSuchMethod-" + invokeName); } catch (IllegalAccessException e) { LOGGER.warn("IllegalAccess-" + invokeName); } catch (InvocationTargetException e) { LOGGER.warn("InvocationTarget-" + invokeName); } } } return t; }
From source file:net.elsched.utils.SettingsBinder.java
/** * Bind configuration parameters into Guice Module. * /* w w w.jav a 2 s .c om*/ * @return a Guice module configured with setting support. * @throws ConfigurationException * on configuration error */ public static Module bindSettings(String propertiesFileKey, Class<?>... settingsArg) throws ConfigurationException { final CompositeConfiguration config = new CompositeConfiguration(); config.addConfiguration(new SystemConfiguration()); String propertyFile = config.getString(propertiesFileKey); if (propertyFile != null) { config.addConfiguration(new PropertiesConfiguration(propertyFile)); } List<Field> fields = new ArrayList<Field>(); for (Class<?> settings : settingsArg) { fields.addAll(Arrays.asList(settings.getDeclaredFields())); } // Reflect on settings class and absorb settings final Map<Setting, Field> settings = new LinkedHashMap<Setting, Field>(); for (Field field : fields) { if (!field.isAnnotationPresent(Setting.class)) { continue; } // Validate target type SettingTypeValidator typeHelper = supportedSettingTypes.get(field.getType()); if (typeHelper == null || !typeHelper.check(field.getGenericType())) { throw new IllegalArgumentException(field.getType() + " is not one of the supported setting types"); } Setting setting = field.getAnnotation(Setting.class); settings.put(setting, field); } // Now validate them List<String> missingProperties = new ArrayList<String>(); for (Setting setting : settings.keySet()) { if (setting.defaultValue().isEmpty()) { if (!config.containsKey(setting.name())) { missingProperties.add(setting.name()); } } } if (missingProperties.size() > 0) { StringBuilder error = new StringBuilder(); error.append("The following required properties are missing from the server configuration: "); error.append(Joiner.on(", ").join(missingProperties)); } // bundle everything up in an injectable guice module return new AbstractModule() { @Override protected void configure() { // We must iterate the settings a third time when binding. // Note: do not collapse these loops as that will damage // early error detection. The runtime is still O(n) in setting // count. for (Map.Entry<Setting, Field> entry : settings.entrySet()) { Class<?> type = entry.getValue().getType(); Setting setting = entry.getKey(); if (int.class.equals(type)) { Integer defaultValue = null; if (!setting.defaultValue().isEmpty()) { defaultValue = Integer.parseInt(setting.defaultValue()); } bindConstant().annotatedWith(Names.named(setting.name())) .to(config.getInteger(setting.name(), defaultValue)); } else if (boolean.class.equals(type)) { Boolean defaultValue = null; if (!setting.defaultValue().isEmpty()) { defaultValue = Boolean.parseBoolean(setting.defaultValue()); } bindConstant().annotatedWith(Names.named(setting.name())) .to(config.getBoolean(setting.name(), defaultValue)); } else if (String.class.equals(type)) { bindConstant().annotatedWith(Names.named(setting.name())) .to(config.getString(setting.name(), setting.defaultValue())); } else { String[] value = config.getStringArray(setting.name()); if (value.length == 0 && !setting.defaultValue().isEmpty()) { value = setting.defaultValue().split(","); } bind(new TypeLiteral<List<String>>() { }).annotatedWith(Names.named(setting.name())).toInstance(ImmutableList.copyOf(value)); } } } }; }
From source file:com.darkstar.beanCartography.utils.NameUtils.java
/** * @param field Field to check/*from ww w . j a v a 2s . c o m*/ * @return <code>true</code> if the Field object has a name annotation on it */ public static boolean hasBusinessName(Field field) { Preconditions.checkNotNull(field, "Field cannot be null"); return field.isAnnotationPresent(NamedField.class); }
From source file:com.alfresco.orm.ORMUtil.java
public static void executeAssociation(final AlfrescoContent alfrescoContent, final BeanFactory beanFactory, final ServiceRegistry serviceRegistry) throws ORMException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { NodeRef nodeRef = getNodeRef(alfrescoContent); List<Field> fields = new ArrayList<Field>(); ReflectionUtil.getFields(alfrescoContent.getClass(), fields); for (Field field : fields) { if (field.isAnnotationPresent(AlfrescoAssociation.class)) { AlfrescoQName alfrescoQName = field.getAnnotation(AlfrescoQName.class); if (null == alfrescoQName) { throw new ORMException("please add alfresco quname aspect to the association field: " + field); }//w ww. j a va2 s . c om List<AlfrescoContent> associationAlfrescoORMs = getAsscoiationObject(alfrescoContent, field); QName qName = QName.createQName(alfrescoQName.namespaceURI(), alfrescoQName.localName()); removeAllAssociation(serviceRegistry, nodeRef, qName); for (AlfrescoContent associationAlfrescoORM : associationAlfrescoORMs) { if (StringUtils.isNotBlank(associationAlfrescoORM.getNodeUUID())) { // TODO: understand requirement and check that do we need to update pojo or just need to update association //UpdateHelper.getUpdateHelper().update(associationAlfrescoORM); NodeRef associationNodeRef = getNodeRef(associationAlfrescoORM); List<AssociationRef> associationRefs = serviceRegistry.getNodeService() .getTargetAssocs(nodeRef, qName); if (!associationRefs.isEmpty()) { boolean doAdd = true; for (AssociationRef associationRef : associationRefs) { if (associationRef.getTargetRef().equals(associationNodeRef)) { doAdd = false; } } if (doAdd) { serviceRegistry.getNodeService().createAssociation(nodeRef, associationNodeRef, qName); } } else { serviceRegistry.getNodeService().createAssociation(nodeRef, associationNodeRef, qName); } } else { CreateHelper.getCreateHelper().save(associationAlfrescoORM); NodeRef associationNodeRef = getNodeRef(associationAlfrescoORM); serviceRegistry.getNodeService().createAssociation(nodeRef, associationNodeRef, qName); } } } } }
From source file:cc.kune.wave.server.CustomSettingsBinder.java
/** * Bind configuration parameters into Guice Module. * * @param propertyFile the property file * @param settingsArg the settings arg/*from w ww . ja va 2 s . c om*/ * @return a Guice module configured with setting support. * @throws ConfigurationException on configuration error */ public static Module bindSettings(String propertyFile, Class<?>... settingsArg) throws ConfigurationException { final CompositeConfiguration config = new CompositeConfiguration(); config.addConfiguration(new SystemConfiguration()); config.addConfiguration(new PropertiesConfiguration(propertyFile)); List<Field> fields = new ArrayList<Field>(); for (Class<?> settings : settingsArg) { fields.addAll(Arrays.asList(settings.getDeclaredFields())); } // Reflect on settings class and absorb settings final Map<Setting, Field> settings = new LinkedHashMap<Setting, Field>(); for (Field field : fields) { if (!field.isAnnotationPresent(Setting.class)) { continue; } // Validate target type SettingTypeValidator typeHelper = supportedSettingTypes.get(field.getType()); if (typeHelper == null || !typeHelper.check(field.getGenericType())) { throw new IllegalArgumentException(field.getType() + " is not one of the supported setting types"); } Setting setting = field.getAnnotation(Setting.class); settings.put(setting, field); } // Now validate them List<String> missingProperties = new ArrayList<String>(); for (Setting setting : settings.keySet()) { if (setting.defaultValue().isEmpty()) { if (!config.containsKey(setting.name())) { missingProperties.add(setting.name()); } } } if (missingProperties.size() > 0) { StringBuilder error = new StringBuilder(); error.append("The following required properties are missing from the server configuration: "); error.append(Joiner.on(", ").join(missingProperties)); throw new ConfigurationException(error.toString()); } // bundle everything up in an injectable guice module return new AbstractModule() { @Override protected void configure() { // We must iterate the settings a third time when binding. // Note: do not collapse these loops as that will damage // early error detection. The runtime is still O(n) in setting count. for (Map.Entry<Setting, Field> entry : settings.entrySet()) { Class<?> type = entry.getValue().getType(); Setting setting = entry.getKey(); if (int.class.equals(type)) { Integer defaultValue = null; if (!setting.defaultValue().isEmpty()) { defaultValue = Integer.parseInt(setting.defaultValue()); } bindConstant().annotatedWith(Names.named(setting.name())) .to(config.getInteger(setting.name(), defaultValue)); } else if (boolean.class.equals(type)) { Boolean defaultValue = null; if (!setting.defaultValue().isEmpty()) { defaultValue = Boolean.parseBoolean(setting.defaultValue()); } bindConstant().annotatedWith(Names.named(setting.name())) .to(config.getBoolean(setting.name(), defaultValue)); } else if (String.class.equals(type)) { bindConstant().annotatedWith(Names.named(setting.name())) .to(config.getString(setting.name(), setting.defaultValue())); } else { String[] value = config.getStringArray(setting.name()); if (value.length == 0 && !setting.defaultValue().isEmpty()) { value = setting.defaultValue().split(","); } bind(new TypeLiteral<List<String>>() { }).annotatedWith(Names.named(setting.name())).toInstance(ImmutableList.copyOf(value)); } } } }; }
From source file:edu.uchicago.lowasser.flaginjection.Flags.java
public static void addFlagBindings(Binder binder, TypeLiteral<?> literal) { for (Field field : literal.getRawType().getDeclaredFields()) { if (field.isAnnotationPresent(Flag.class)) { Flag annot = field.getAnnotation(Flag.class); addFlagBinding(binder, annot, literal.getFieldType(field)); }//from ww w .j a va2 s. c o m } for (Constructor<?> constructor : literal.getRawType().getDeclaredConstructors()) { List<TypeLiteral<?>> parameterTypes = literal.getParameterTypes(constructor); Annotation[][] parameterAnnotations = constructor.getParameterAnnotations(); for (int i = 0; i < parameterTypes.size(); i++) { Annotation[] annotations = parameterAnnotations[i]; TypeLiteral<?> typ = parameterTypes.get(i); for (Annotation annot : annotations) { if (annot instanceof Flag) { addFlagBinding(binder, (Flag) annot, typ); } } } } }
From source file:org.dbg4j.core.DebugUtils.java
/** * Get object fields filtered by annotation of given class. Third parameter determines if the fields marked with * particular annotation should be included (<code>true</code>) or excluded (<code>false</code>) from result set. * F.e. <code>getObjectFields(instance, MyAnnotation.class, true)</code> will return only wields annotated with * annotation <code>MyAnnotation</code>. At the same time * <code>getObjectFields(instance, MyAnnotation.class, false)</code> will return all fields except those are * annotated with <code>MyAnnotation</code> * * @param instance//w w w . ja v a 2 s . co m * @param clz * @param include * @return */ @Nonnull public static Set<Field> getObjectFields(Object instance, @Nonnull Class clz, boolean include) { Set<Field> result = new HashSet<Field>(); if (instance != null && instance.getClass().getDeclaredFields() != null && instance.getClass().getDeclaredFields().length > 0) { for (Field field : instance.getClass().getDeclaredFields()) { if (field.isAnnotationPresent(clz) == include) { result.add(field); } } } return result; }
From source file:gumga.framework.presentation.api.CSVGeneratorAPI.java
public static Field getIdField(Class clazz) { for (Field f : getAllAtributes(clazz)) { if (f.isAnnotationPresent(Id.class)) { return f; }/*from w ww.j a v a2 s . c om*/ } return null; }