List of usage examples for java.lang.reflect Field getAnnotation
public <T extends Annotation> T getAnnotation(Class<T> annotationClass)
From source file:com.l2jfree.config.model.ConfigClassInfo.java
private ConfigClassInfo(Class<?> clazz) throws InstantiationException, IllegalAccessException { _clazz = clazz;/*from w ww .j av a2 s. c o m*/ _configClass = _clazz.getAnnotation(ConfigClass.class); final Map<String, ConfigGroup> activeGroups = new FastMap<String, ConfigGroup>(); for (Field field : _clazz.getFields()) { final ConfigField configField = field.getAnnotation(ConfigField.class); if (configField == null) continue; if (!Modifier.isStatic(field.getModifiers()) || Modifier.isFinal(field.getModifiers())) { _log.warn("Invalid modifiers for " + field); continue; } final ConfigFieldInfo info = new ConfigFieldInfo(field); _infos.add(info); if (info.getConfigGroupBeginning() != null) { final ConfigGroup group = new ConfigGroup(); activeGroups.put(info.getConfigGroupBeginning().name(), group); info.setBeginningGroup(group); } for (ConfigGroup group : activeGroups.values()) group.add(info); if (info.getConfigGroupEnding() != null) { final ConfigGroup group = activeGroups.remove(info.getConfigGroupEnding().name()); info.setEndingGroup(group); } } if (!activeGroups.isEmpty()) _log.warn("Invalid config grouping!"); store(); try { // just in case it's missing if (!getConfigFile().exists()) store(getConfigFile(), null); } catch (IOException e) { e.printStackTrace(); } }
From source file:info.archinnov.achilles.entity.parsing.EmbeddedIdParser.java
private Map<Integer, Field> extractComponentsOrdering(Class<?> embeddedIdClass) { String embeddedIdClassName = embeddedIdClass.getCanonicalName(); Map<Integer, Field> components = new TreeMap<Integer, Field>(); @SuppressWarnings("unchecked") Set<Field> candidateFields = getFields(embeddedIdClass, ReflectionUtils.<Field>withAnnotation(Order.class)); Set<Integer> orders = new HashSet<Integer>(); int orderSum = 0; int componentCount = candidateFields.size(); for (Field candidateField : candidateFields) { Order orderAnnotation = candidateField.getAnnotation(Order.class); int order = orderAnnotation.value(); Class<?> componentType = candidateField.getType(); orderSum = validateNoDuplicateOrderAndType(embeddedIdClassName, orders, orderSum, order, componentType); components.put(order, candidateField); }/* w w w.j a va 2 s. co m*/ validateConsistentOrdering(embeddedIdClassName, orderSum, componentCount); Validator.validateBeanMappingTrue(componentCount > 1, "There should be at least 2 fields annotated with @Order for the @EmbeddedId class '%s'", embeddedIdClass.getCanonicalName()); return components; }
From source file:io.fabric8.spring.boot.AbstractServiceRegistar.java
@Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { for (Method method : REFLECTIONS.getMethodsAnnotatedWith(Factory.class)) { String methodName = method.getName(); Class sourceType = getSourceType(method); Class targetType = method.getReturnType(); Class beanType = method.getDeclaringClass(); BeanDefinitionHolder holder = createConverterBean(beanType, methodName, sourceType, targetType); BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry); }/*w w w . j a va2s. co m*/ for (Field field : REFLECTIONS.getFieldsAnnotatedWith(ServiceName.class)) { Class targetClass = field.getType(); Alias alias = field.getAnnotation(Alias.class); ServiceName name = field.getAnnotation(ServiceName.class); PortName port = field.getAnnotation(PortName.class); Protocol protocol = field.getAnnotation(Protocol.class); External external = field.getAnnotation(External.class); String serviceName = name != null ? name.value() : null; //We copy the service since we are going to add properties to it. Service serviceInstance = new ServiceBuilder(getService(serviceName)).build(); String servicePort = port != null ? port.value() : null; String serviceProtocol = protocol != null ? protocol.value() : DEFAULT_PROTOCOL; Boolean serviceExternal = external != null && external.value(); String serviceAlias = alias != null ? alias.value() : createAlias(serviceName, targetClass, serviceProtocol, servicePort, serviceExternal); //Add annotation info as additional properties serviceInstance.getAdditionalProperties().put(ALIAS, serviceAlias); serviceInstance.getAdditionalProperties().put(PROTOCOL, serviceProtocol); serviceInstance.getAdditionalProperties().put(EXTERNAL, serviceExternal); //We don't want to add a fallback value to the attributes. if (port != null) { serviceInstance.getAdditionalProperties().put(PORT, servicePort); } BeanDefinitionHolder holder = createServiceDefinition(serviceInstance, serviceAlias, serviceProtocol, servicePort, targetClass); BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry); } }
From source file:org.ff4j.spring.autowire.AutowiredFF4JBeanPostProcessor.java
private void autoWiredProperty(Object bean, Field field) { // Find the required and name parameters FF4JProperty annProperty = field.getAnnotation(FF4JProperty.class); String propertyName = StringUtils.hasLength(annProperty.value()) ? annProperty.value() : field.getName(); Property<?> property = readProperty(field, propertyName, annProperty.required()); // if not available in store if (property != null) { if (Property.class.isAssignableFrom(field.getType())) { injectValue(field, bean, propertyName, property); } else if (property.parameterizedType().isAssignableFrom(field.getType())) { injectValue(field, bean, propertyName, property.getValue()); } else if (property.parameterizedType().equals(Integer.class) && field.getType().equals(int.class) && (null != property.getValue())) { // Autoboxing Integer -> Int injectValue(field, bean, propertyName, property.getValue()); } else if (property.parameterizedType().equals(Long.class) && field.getType().equals(long.class) && (null != property.getValue())) { // Autoboxing Long -> long injectValue(field, bean, propertyName, property.getValue()); } else if (property.parameterizedType().equals(Double.class) && field.getType().equals(double.class) && (null != property.getValue())) { // Autoboxing Double -> double injectValue(field, bean, propertyName, property.getValue()); } else if (property.parameterizedType().equals(Byte.class) && field.getType().equals(byte.class) && (null != property.getValue())) { // Autoboxing Byte -> byte injectValue(field, bean, propertyName, property.getValue()); } else if (property.parameterizedType().equals(Boolean.class) && field.getType().equals(boolean.class) && (null != property.getValue())) { // Autoboxing Boolean -> boolean injectValue(field, bean, propertyName, property.getValue()); } else if (property.parameterizedType().equals(Short.class) && field.getType().equals(short.class) && (null != property.getValue())) { // Autoboxing Short -> short injectValue(field, bean, propertyName, property.getValue()); } else if (property.parameterizedType().equals(Character.class) && field.getType().equals(char.class) && (null != property.getValue())) { // Autoboxing Character -> char injectValue(field, bean, propertyName, property.getValue()); } else if (property.parameterizedType().equals(Float.class) && field.getType().equals(float.class) && (null != property.getValue())) { // Autoboxing Float -> float injectValue(field, bean, propertyName, property.getValue()); } else {/*from w w w . ja v a 2s . c o m*/ throw new IllegalArgumentException("Field annotated with @FF4JProperty" + " must inherit from org.ff4j.property.AbstractProperty or be of type " + property.parameterizedType() + "but is " + field.getType() + " [class=" + bean.getClass().getName() + ", field=" + field.getName() + "]"); } } }
From source file:com.heisenberg.impl.plugin.DescriptorField.java
public DescriptorField(Field field, DataType dataType, ConfigurationField configurationField) { this.name = field.getName(); this.field = field; this.field.setAccessible(true); this.dataType = dataType; this.isRequired = configurationField.required() ? true : null; Label label = field.getAnnotation(Label.class); if (label != null) { this.label = label.value(); } else {/*from w ww . ja v a 2 s. co m*/ this.label = name; } }
From source file:com.flowpowered.cerealization.config.annotated.AnnotatedObjectConfiguration.java
private void load(ConfigurationNodeSource source, Object object, String[] path, Set<Member> members) throws ConfigurationException { final Set<Method> methods = new HashSet<Method>(); for (Member member : members) { if (member instanceof Method) { final Method method = (Method) member; if (method.isAnnotationPresent(Load.class)) { methods.add(method);//from w w w . j a v a 2 s . co m } continue; } final Field field = (Field) member; field.setAccessible(true); String[] fieldPath = field.getAnnotation(Setting.class).value(); if (fieldPath.length == 0) { fieldPath = new String[] { field.getName() }; } final ConfigurationNode fieldNode = source.getNode(ArrayUtils.addAll(path, fieldPath)); final Object value = fieldNode.getTypedValue(field.getGenericType()); try { if (value != null) { field.set(object, value); } else { fieldNode.setValue(field.getGenericType(), field.get(object)); } } catch (IllegalAccessException ex) { throw new ConfigurationException(ex); } } invokeMethods(methods, object, source.getNode(path)); }
From source file:com.khubla.cbean.CBeanType.java
/** * find version field// w w w. j ava2 s .c om */ private Field findVersionField() { final Field[] fields = clazz.getDeclaredFields(); if (null != fields) { for (int i = 0; i < fields.length; i++) { final Field field = fields[i]; final Version version = field.getAnnotation(Version.class); if (null != version) { return field; } } } return null; }
From source file:com.conversantmedia.mapreduce.tool.AnnotatedToolContext.java
@Override protected void initExtraOptions(Options options) { // Find all the fields with @Option annotations... fieldsMap = new HashMap<>(); addedOptionsMap = new HashMap<>(); defaultValuesMap = new HashMap<>(); Class<?> clazz = this.bean.getClass(); while (clazz != Object.class) { for (Field field : clazz.getDeclaredFields()) { if (field.isAnnotationPresent(Option.class)) { Option option = field.getAnnotation(Option.class); // Ensure we don't add the same option more than 1x // For example, if our annotated bean includes an 'input', // we don't want to add a second version String optName = getValue(option.name(), field.getName()); org.apache.commons.cli.Option opt; if (options.hasOption(optName)) { opt = options.getOption(optName); updateOption(option, opt); } else { opt = initOption(options, option, optName); options.addOption(opt); addedOptionsMap.put(opt.getLongOpt(), field); }/*from ww w .j a v a2 s . c om*/ fieldsMap.put(opt.getLongOpt(), field); defaultValuesMap.put(opt.getLongOpt(), option.defaultValue()); } } clazz = clazz.getSuperclass(); } }
From source file:com.CodeSeance.JSeance.CodeGenXML.XMLElements.Node.java
public void loadAttributes(Context context) { ContextManager contextManager = context.getManager(); for (Field field : this.getClass().getDeclaredFields()) { if (field.isAnnotationPresent(XMLAttribute.class)) { Class type = field.getType(); String attributeName = field.getAnnotation(XMLAttribute.class).attributeName(); if ("".equals(attributeName) || attributeName == null) { attributeName = field.getName(); }//from w ww . ja va2 s .co m String stringValue = element.getAttribute(attributeName); try { field.set(this, replaceJSAndConvert(contextManager, stringValue, type)); } catch (IllegalAccessException ex) { // This is due to a programming error, member field should be public assert false : ex; } } else if (field.isAnnotationPresent(XMLTextContent.class)) { Class type = field.getType(); try { field.set(this, replaceJSAndConvert(contextManager, element.getTextContent(), type)); } catch (IllegalAccessException ex) { // This is due to a programming error, member field should be public assert false : ex; } } } }
From source file:info.archinnov.achilles.internal.metadata.parsing.EmbeddedIdParser.java
private Map<Integer, Field> extractComponentsOrdering(Class<?> embeddedIdClass) { log.trace("Extract components ordering from embedded id class {} ", embeddedIdClass.getCanonicalName()); String embeddedIdClassName = embeddedIdClass.getCanonicalName(); Map<Integer, Field> components = new TreeMap<Integer, Field>(); @SuppressWarnings("unchecked") Set<Field> candidateFields = getFields(embeddedIdClass, ReflectionUtils.<Field>withAnnotation(Order.class)); Set<Integer> orders = new HashSet<Integer>(); int orderSum = 0; int componentCount = candidateFields.size(); for (Field candidateField : candidateFields) { Order orderAnnotation = candidateField.getAnnotation(Order.class); int order = orderAnnotation.value(); Class<?> componentType = candidateField.getType(); orderSum = validateNoDuplicateOrderAndType(embeddedIdClassName, orders, orderSum, order, componentType); components.put(order, candidateField); }/* w ww . j ava2 s. c om*/ validateConsistentOrdering(embeddedIdClassName, orderSum, componentCount); Validator.validateBeanMappingTrue(componentCount > 1, "There should be at least 2 fields annotated with @Order for the @EmbeddedId class '%s'", embeddedIdClass.getCanonicalName()); return components; }