List of usage examples for java.lang.reflect Field isAnnotationPresent
@Override public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass)
From source file:pl.konczak.mystartupapp.sharedkernel.enversRepository.EnversRevisionRepositoryImpl.java
@SuppressWarnings("unchecked") private List<Revision<N, T>> toRevisions(Map<N, T> source, Map<Number, Object> revisionEntities) { List<Revision<N, T>> result = new ArrayList<Revision<N, T>>(); for (Entry<N, T> revision : source.entrySet()) { N revisionNumber = revision.getKey(); T entity = revision.getValue();//from w w w .ja va 2 s . c o m RevisionMetadata<N> metadata = (RevisionMetadata<N>) getRevisionMetadata( revisionEntities.get(revisionNumber)); result.add(new Revision<N, T>(metadata, entity)); } Collections.sort(result); Map<Field, Object> fieldPrevValues = new HashMap<>(); for (Revision<N, T> revision : result) { T entity = revision.getEntity(); try { List<Field> fields = getAllFieldsFrom(entity); for (Field field : fields) { if (field.isAnnotationPresent(AuditComparable.class)) { field.setAccessible(true); Object prev = fieldPrevValues.get(field); Object current = field.get(entity); fieldPrevValues.put(field, current); if (prev != null) { if (prev.equals(current)) { field.set(entity, null); } } } } } catch (IllegalAccessException e) { throw new RuntimeException(e); } } return Collections.unmodifiableList(result); }
From source file:com.alfresco.orm.ObjectFillHelper.java
public void getFilledObject(final NodeRef nodeRef, final AlfrescoORM alfrescoORM, boolean isLazy) throws ORMException { NodeService nodeService = serviceRegistry.getNodeService(); if (!nodeService.exists(nodeRef)) { throw new ORMException("NodeRef does not exist : " + nodeRef); }/*from w w w . j a va 2s .c om*/ Map<QName, Serializable> properties = nodeService.getProperties(nodeRef); List<Field> fields = new ArrayList<Field>(); ReflectionUtil.getFields(alfrescoORM.getClass(), fields); for (Field field : fields) { if (field.isAnnotationPresent(AlfrescoQName.class) && !field.isAnnotationPresent(AlfrescoAssociation.class)) { AlfrescoQName alfrescoQName = field.getAnnotation(AlfrescoQName.class); QName qName = QName.createQName(alfrescoQName.namespaceURI(), alfrescoQName.localName()); Method setterMethod = ReflectionUtil.setMethod(alfrescoORM.getClass(), field.getName()); if (setterMethod.getParameterTypes().length == 0 || setterMethod.getParameterTypes().length > 1) { throw new ORMException("invalid method found for this tool : " + setterMethod.getName()); } Class<?> classType = setterMethod.getParameterTypes()[0]; Serializable value = properties.get(qName); try { setterMethod.invoke(alfrescoORM, getManipulatedValue(classType, value)); } catch (IllegalAccessException e) { throw new ORMException(e.getMessage() + " method : " + setterMethod, e); } catch (IllegalArgumentException e) { throw new ORMException(e.getMessage() + " method : " + setterMethod, e); } catch (InvocationTargetException e) { throw new ORMException(e.getMessage() + " method : " + setterMethod, e); } } else if (field.isAnnotationPresent(AlfrescoAspect.class)) { Method setterMethod = ReflectionUtil.setMethod(alfrescoORM.getClass(), field.getName()); if (setterMethod.getParameterTypes().length == 0 || setterMethod.getParameterTypes().length > 1) { throw new ORMException("invalid method found for this tool : " + setterMethod.getName()); } Class<?> classType = setterMethod.getParameterTypes()[0]; try { AlfrescoORM alfrescoORMAspect = (AlfrescoORM) classType.newInstance(); // setting the aspect to pojo setterMethod.invoke(alfrescoORM, alfrescoORMAspect); getFilledObject(nodeRef, alfrescoORMAspect, isLazy); } catch (InstantiationException e) { throw new ORMException(e.getMessage() + "class type: " + classType, e); } catch (IllegalAccessException e) { throw new ORMException(e.getMessage() + "class type: " + classType, e); } catch (IllegalArgumentException e) { throw new ORMException(e.getMessage() + "class type: " + classType, e); } catch (InvocationTargetException e) { throw new ORMException(e.getMessage() + "class type: " + classType, e); } } else 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"); } QName qName = QName.createQName(alfrescoQName.namespaceURI(), alfrescoQName.localName()); AlfrescoAssociation alfrescoAssociation = field.getAnnotation(AlfrescoAssociation.class); List<AssociationRef> associationRefs = nodeService.getTargetAssocs(nodeRef, qName); List<AlfrescoContent> associationList = new ArrayList<AlfrescoContent>(); for (AssociationRef associationRef : associationRefs) { if (nodeService.exists(associationRef.getTargetRef())) { try { AlfrescoContent alfrescoORMForAssociation = null; if (!isLazy) { alfrescoORMForAssociation = alfrescoAssociation.type().newInstance(); getFilledObject(associationRef.getTargetRef(), alfrescoORMForAssociation, isLazy); } else { alfrescoORMForAssociation = LazyProxyFactoryBean.getLazyProxyFactoryBean() .getObject(associationRef.getTargetRef().getId(), alfrescoAssociation.type()); } associationList.add(alfrescoORMForAssociation); } catch (InstantiationException e) { throw new ORMException(e.getMessage() + "class type: " + alfrescoAssociation.type(), e); } catch (IllegalAccessException e) { throw new ORMException(e.getMessage() + "class type: " + alfrescoAssociation.type(), e); } } else { // invalid association noderef found } } if (!associationList.isEmpty()) { Method setterMethod = ReflectionUtil.setMethod(alfrescoORM.getClass(), field.getName()); if (alfrescoAssociation.many()) { try { setterMethod.invoke(alfrescoORM, associationList); } catch (IllegalArgumentException e) { throw new ORMException(e.getMessage() + " method : " + setterMethod, e); } catch (IllegalAccessException e) { throw new ORMException(e.getMessage() + " method : " + setterMethod, e); } catch (InvocationTargetException e) { throw new ORMException(e.getMessage() + " method : " + setterMethod, e); } } else { try { setterMethod.invoke(alfrescoORM, associationList.get(0)); } catch (IllegalArgumentException e) { throw new ORMException(e.getMessage() + " method : " + setterMethod, e); } catch (IllegalAccessException e) { throw new ORMException(e.getMessage() + " method : " + setterMethod, e); } catch (InvocationTargetException e) { throw new ORMException(e.getMessage() + " method : " + setterMethod, e); } } } } } }
From source file:br.gov.frameworkdemoiselle.internal.implementation.ConfigurationLoader.java
private boolean hasIgnore(Field field) { return field.isAnnotationPresent(Ignore.class); }
From source file:br.gov.frameworkdemoiselle.internal.configuration.ConfigurationLoader.java
private void loadField(Field field, Object object, Class<?> clazz) { if (!field.isAnnotationPresent(Ignore.class) && clazz.isAnnotationPresent(Configuration.class)) { String resource = clazz.getAnnotation(Configuration.class).resource(); ConfigType type = clazz.getAnnotation(Configuration.class).type(); org.apache.commons.configuration.Configuration config = getConfiguration(resource, type); String key = getKey(field, clazz, config); Object value = getValue(key, field.getType(), config); validate(field, key, value, resource); setValue(field, key, object, value); }/* ww w .j av a 2s. c o m*/ }
From source file:net.camelpe.extension.cdi.spi.CamelInjectionTargetWrapper.java
private void injectEndpointInto(final T instance, final Field camelInjectAnnotatedField) throws ResolutionException { final Object valueToInject; if (camelInjectAnnotatedField.isAnnotationPresent(org.apache.camel.EndpointInject.class)) { final org.apache.camel.EndpointInject endpointInjectAnnotation = camelInjectAnnotatedField .getAnnotation(org.apache.camel.EndpointInject.class); valueToInject = getCamelPostProcessorHelper().getInjectionValue(camelInjectAnnotatedField.getType(), endpointInjectAnnotation.uri(), endpointInjectAnnotation.ref(), camelInjectAnnotatedField.getName(), instance, instance.getClass().getName()); } else if (camelInjectAnnotatedField.isAnnotationPresent(org.apache.camel.Produce.class)) { final org.apache.camel.Produce produceAnnotation = camelInjectAnnotatedField .getAnnotation(org.apache.camel.Produce.class); valueToInject = getCamelPostProcessorHelper().getInjectionValue(camelInjectAnnotatedField.getType(), produceAnnotation.uri(), produceAnnotation.ref(), camelInjectAnnotatedField.getName(), instance, instance.getClass().getName()); } else {// w ww. ja v a 2 s .co m throw new IllegalStateException("Neither [" + org.apache.camel.EndpointInject.class.getName() + "] nor [" + org.apache.camel.Produce.class + "] are present on field [" + camelInjectAnnotatedField.toString() + "]"); } setField(instance, camelInjectAnnotatedField, valueToInject); }
From source file:com.google.api.ads.adwords.awreporting.model.csv.CsvReportEntitiesMapping.java
/** * Adds the report property to select if the CSV annotation is present * * @param propertiesToSelect the list of properties that will be selected for the report. * @param field the field//from ww w. j a va2s . c om * @param propertyExclusions the properties that must not be added to the report. */ private void addPropertyNameIfAnnotationPresent(List<String> propertiesToSelect, Field field, Set<String> propertyExclusions) { if (field.isAnnotationPresent(CsvField.class)) { CsvField reportFieldAnnotation = field.getAnnotation(CsvField.class); String reportPropertyName = reportFieldAnnotation.reportField(); if (!propertyExclusions.contains(reportPropertyName)) { propertiesToSelect.add(reportPropertyName); } } }
From source file:br.gov.frameworkdemoiselle.internal.configuration.ConfigurationLoader.java
private void validate(Field field, String key, Object value, String resource) { if (field.isAnnotationPresent(NotNull.class) && value == null) { throw new ConfigurationException( bundle.getString("configuration-attribute-is-mandatory", key, resource)); }// www .ja v a 2 s. c o m }
From source file:de.taimos.dvalin.cloud.aws.AWSClientBeanPostProcessor.java
private InjectionMetadata buildResourceMetadata(Class<?> clazz) { LinkedList<InjectionMetadata.InjectedElement> elements = new LinkedList<>(); Class<?> targetClass = clazz; do {//from w w w .j a v a 2 s . c om LinkedList<InjectionMetadata.InjectedElement> currElements = new LinkedList<>(); for (Field field : targetClass.getDeclaredFields()) { if (field.isAnnotationPresent(AWSClient.class)) { if (Modifier.isStatic(field.getModifiers())) { throw new IllegalStateException("@AWSClient annotation is not supported on static fields"); } currElements.add(new AWSClientElement(field, null, field.getAnnotation(AWSClient.class))); } } for (Method method : targetClass.getDeclaredMethods()) { Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(method); if (!BridgeMethodResolver.isVisibilityBridgeMethodPair(method, bridgedMethod)) { continue; } if (method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) { if (bridgedMethod.isAnnotationPresent(AWSClient.class)) { if (Modifier.isStatic(method.getModifiers())) { throw new IllegalStateException( "@AWSClient annotation is not supported on static methods"); } if (method.getParameterTypes().length != 1) { throw new IllegalStateException( "@AWSClient annotation requires a single-arg method: " + method); } PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, clazz); currElements.add(new AWSClientElement(method, pd, method.getAnnotation(AWSClient.class))); } } } elements.addAll(0, currElements); targetClass = targetClass.getSuperclass(); } while ((targetClass != null) && (targetClass != Object.class)); return new InjectionMetadata(clazz, elements); }
From source file:br.gov.frameworkdemoiselle.internal.implementation.ConfigurationLoader.java
private String getKey(Field field) { String key;/*from www.java 2 s . com*/ if (field.isAnnotationPresent(Name.class)) { key = field.getAnnotation(Name.class).value(); } else { key = field.getName(); } return key; }
From source file:com.tojc.ormlite.android.framework.TableInfo.java
public TableInfo(Class<?> tableClassType) { // can't happen // keep a while, May 18th 2013 // TODO remove after a while // if (!(tableClassType instanceof Class<?>)) { // throw new IllegalArgumentException("Parameter is not a Class<?>."); // }//from w ww . java2 s .c o m this.classType = tableClassType; this.name = OrmLiteAnnotationAccessor.getAnnotationTableName(tableClassType); this.defaultContentUriInfo = new ContentUriInfo(tableClassType); this.defaultContentMimeTypeVndInfo = new ContentMimeTypeVndInfo(tableClassType); this.columns = new HashMap<String, ColumnInfo>(); this.projectionMap = new HashMap<String, String>(); SortedMap<Integer, String> defaultSortOrderMap = new TreeMap<Integer, String>(); this.idColumnInfo = null; for (Field classfield : tableClassType.getDeclaredFields()) { if (classfield.isAnnotationPresent(DatabaseField.class)) { classfield.setAccessible(true); // private field accessible ColumnInfo columnInfo = new ColumnInfo(classfield); this.columns.put(columnInfo.getColumnName(), columnInfo); // check id if (columnInfo.getColumnName().equals(BaseColumns._ID)) { boolean generatedId = classfield.getAnnotation(DatabaseField.class).generatedId(); if (generatedId) { this.idColumnInfo = columnInfo; } } // DefaultSortOrder SortOrderInfo defaultSortOrderInfo = columnInfo.getDefaultSortOrderInfo(); if (defaultSortOrderInfo.isValid()) { defaultSortOrderMap.put(defaultSortOrderInfo.getWeight(), defaultSortOrderInfo.makeSqlOrderString(columnInfo.getColumnName())); } // ProjectionMap this.projectionMap.put(columnInfo.getProjectionColumnName(), columnInfo.getColumnName()); } } if (this.idColumnInfo == null) { // @DatabaseField(columnName = _ID, generatedId = true) // private int _id; throw new IllegalArgumentException("Proper ID is not defined for field."); } // DefaultSortOrder if (defaultSortOrderMap.size() >= 1) { // make SQL OrderBy StringBuilder result = new StringBuilder(); String comma = ""; for (Map.Entry<Integer, String> entry : defaultSortOrderMap.entrySet()) { result.append(comma); result.append(entry.getValue()); comma = ", "; } this.defaultSortOrder = result.toString(); } else { this.defaultSortOrder = ""; } }