List of usage examples for java.lang.reflect Field isAnnotationPresent
@Override public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass)
From source file:org.constretto.spring.ConfigurationAnnotationConfigurer.java
private void injectEnvironment(final Object bean) { try {//ww w .j a v a2s .c o m Field[] fields = bean.getClass().getDeclaredFields(); for (Field field : fields) { if (field.isAnnotationPresent(Environment.class)) { field.setAccessible(true); field.set(bean, assemblyContextResolver.getAssemblyContext()); } } } catch (IllegalAccessException e) { throw new BeanInstantiationException(bean.getClass(), "Could not inject Environment on spring bean"); } }
From source file:com.impetus.kundera.metadata.processor.DocumentProcessor.java
@Override public void process(Class<?> clazz, EntityMetadata metadata) { if (!clazz.isAnnotationPresent(Document.class)) { return;//w ww .j a v a2 s . co m } LOG.debug("Processing @Entity(" + clazz.getName() + ") for Document."); metadata.setType(EntityMetadata.Type.DOCUMENT); Document coll = clazz.getAnnotation(Document.class); // set columnFamily //Name of collection for document based datastore metadata.setColumnFamilyName(coll.name()); // set keyspace //DB name for document based datastore String keyspace = coll.db().length() != 0 ? coll.db() : em.getKeyspace(); metadata.setKeyspaceName(keyspace); // scan for fields for (Field f : clazz.getDeclaredFields()) { // if @Id if (f.isAnnotationPresent(Id.class)) { LOG.debug(f.getName() + " => Id"); metadata.setIdProperty(f); populateIdAccessorMethods(metadata, clazz, f); populateIdColumn(metadata, clazz, f); } // if any valid JPA annotation? else { String name = getValidJPAColumnName(clazz, f); if (null != name) { metadata.addColumn(name, metadata.new Column(name, f)); } } } }
From source file:org.tylproject.vaadin.addon.beanfactory.DefaultBeanFactory.java
/** * @throws java.lang.UnsupportedOperationException * if no field is annotated using * {@link org.springframework.data.annotation.Id} *//*from ww w .j ava 2s .c o m*/ protected ObjectId getIdValue(T target) throws ReflectiveOperationException { for (Field f : beanClass.getDeclaredFields()) { if (f.isAnnotationPresent(org.springframework.data.annotation.Id.class)) { f.setAccessible(true); return (ObjectId) f.get(target); } } for (Method m : beanClass.getMethods()) { if (m.getName().equals("getId")) { return (ObjectId) m.invoke(target); } } throw new UnsupportedOperationException("no id field was found"); }
From source file:pl.maciejwalkowiak.plist.FieldSerializer.java
private String createKey(Field field) { String keyToWrap;/*from w w w .j a va 2 s.c om*/ if (field.isAnnotationPresent(PlistAlias.class)) { PlistAlias alias = field.getAnnotation(PlistAlias.class); if (alias.followStrategy()) { keyToWrap = plistSerializer.getNamingStrategy().fieldNameToKey(alias.value()); } else { keyToWrap = alias.value(); } } else { keyToWrap = plistSerializer.getNamingStrategy().fieldNameToKey(field.getName()); } return XMLHelper.wrap(keyToWrap).with("key"); }
From source file:org.tylproject.vaadin.addon.beanfactory.DefaultBeanFactory.java
/** * @throws java.lang.UnsupportedOperationException * if no field is annotated using * {@link org.springframework.data.annotation.Id} *//*w w w . j ava 2 s . c o m*/ protected void setIdValue(T target, ObjectId value) throws ReflectiveOperationException { for (Field f : beanClass.getDeclaredFields()) { if (f.isAnnotationPresent(org.springframework.data.annotation.Id.class)) { f.setAccessible(true); f.set(target, value); return; } } for (Method m : beanClass.getDeclaredMethods()) { if (m.getName().equals("setId")) { m.invoke(target, value); return; } } throw new UnsupportedOperationException("no id field was found"); }
From source file:com.impetus.kundera.metadata.processor.ColumnFamilyProcessor.java
@Override public final void process(Class<?> clazz, EntityMetadata metadata) { if (!clazz.isAnnotationPresent(ColumnFamily.class)) { return;/*from w w w . j a v a 2 s .c om*/ } LOG.debug("Processing @Entity(" + clazz.getName() + ") for ColumnFamily."); metadata.setType(EntityMetadata.Type.COLUMN_FAMILY); ColumnFamily cf = clazz.getAnnotation(ColumnFamily.class); // set columnFamily metadata.setColumnFamilyName(cf.family()); // set keyspace String keyspace = cf.keyspace().length() != 0 ? cf.keyspace() : em.getKeyspace(); metadata.setKeyspaceName(keyspace); // scan for fields for (Field f : clazz.getDeclaredFields()) { // if @Id if (f.isAnnotationPresent(Id.class)) { LOG.debug(f.getName() + " => Id"); metadata.setIdProperty(f); populateIdAccessorMethods(metadata, clazz, f); } // if any valid JPA annotation? else { String name = getValidJPAColumnName(clazz, f); if (null != name) { metadata.addColumn(name, metadata.new Column(name, f)); } } } }
From source file:com.github.srgg.springmockito.MockitoPropagatingFactoryPostProcessor.java
@Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { final Map<Field, Object> fields = new LinkedHashMap(); for (Object c : contexts) { for (Field f : FieldUtils.getAllFields(c.getClass())) { if (f.isAnnotationPresent(Mock.class) || f.isAnnotationPresent(Spy.class) || includeInjectMocks && f.isAnnotationPresent(InjectMocks.class)) { try { if (!f.isAccessible()) { f.setAccessible(true); }/*from w w w . j a v a 2 s . c om*/ Object o = f.get(c); fields.put(f, o); } catch (IllegalAccessException e) { throw new RuntimeException(e); } } } } for (final Map.Entry<Field, Object> e : fields.entrySet()) { final Field f = e.getKey(); /* * To be processed by BeanPostProcessors, value must be an instance of FactoryBean */ final FactoryBean fb = new SimpleHolderFactoryBean(e.getValue()); beanFactory.registerSingleton(f.getName(), fb); } }
From source file:com.thoughtworks.go.server.service.RailsAssetsServiceTest.java
@Test public void shouldHaveAssetsAsTheSerializedNameForAssetsMapInRailsAssetsManifest_ThisIsRequiredSinceManifestFileGeneratedBySprocketsHasAMapOfAssetsWhichThisServiceNeedsAccessTo() { List<Field> fields = new ArrayList<>( Arrays.asList(RailsAssetsService.RailsAssetsManifest.class.getDeclaredFields())); List<Field> fieldsAnnotatedWithSerializedNameAsAssets = fields.stream().filter(new Predicate<Field>() { @Override/*from ww w. j a v a 2 s . com*/ public boolean test(Field field) { if (field.isAnnotationPresent(SerializedName.class)) { SerializedName annotation = field.getAnnotation(SerializedName.class); if (annotation.value().equals("assets")) { return true; } return false; } return false; } }).collect(Collectors.toList()); assertThat("Expected a field annotated with SerializedName 'assets'", fieldsAnnotatedWithSerializedNameAsAssets.isEmpty(), is(false)); assertThat(fieldsAnnotatedWithSerializedNameAsAssets.size(), is(1)); assertThat(fieldsAnnotatedWithSerializedNameAsAssets.get(0).getType().getCanonicalName(), is(HashMap.class.getCanonicalName())); }
From source file:com.impetus.kundera.metadata.ValidatorImpl.java
/** * Checks the validity of a class for Cassandra entity. * /*from w w w. j a v a 2 s .c o m*/ * @param clazz * validates this class * * @return returns 'true' if valid */ @Override // TODO: reduce Cyclomatic complexity public final void validate(final Class<?> clazz) { if (classes.contains(clazz)) { return; } LOG.debug("Validating " + clazz.getName()); // Is Entity? if (!clazz.isAnnotationPresent(Entity.class)) { throw new PersistenceException(clazz.getName() + " is not annotated with @Entity"); } // must have a default no-argument constructor try { clazz.getConstructor(); } catch (NoSuchMethodException nsme) { throw new PersistenceException(clazz.getName() + " must have a default no-argument constructor."); } // what type is it? ColumnFamily or SuperColumnFamily, Document or simply relational entity? if (clazz.isAnnotationPresent(SuperColumnFamily.class) || clazz.isAnnotationPresent(ColumnFamily.class)) { LOG.debug("Entity is for NoSQL database: " + clazz.getName()); } else if (!clazz.isAnnotationPresent(Document.class)) { LOG.debug("Entity is for document based database: " + clazz.getName()); } else { LOG.debug("Entity is for relational database table: " + clazz.getName()); } // check for @Key and ensure that there is just 1 @Key field of String // type. List<Field> keys = new ArrayList<Field>(); for (Field field : clazz.getDeclaredFields()) { if (field.isAnnotationPresent(Id.class)) { keys.add(field); } } if (keys.size() == 0) { throw new PersistenceException(clazz.getName() + " must have an @Id field."); } else if (keys.size() > 1) { throw new PersistenceException(clazz.getName() + " can only have 1 @Id field."); } if (!keys.get(0).getType().equals(String.class)) { throw new PersistenceException(clazz.getName() + " @Id must be of String type."); } // save in cache classes.add(clazz); }
From source file:com.madrobot.di.wizard.json.JSONSerializer.java
private String getKeyName(final Field field) { if (field.isAnnotationPresent(SerializedName.class)) { SerializedName serializedName = field.getAnnotation(SerializedName.class); return serializedName.value(); } else {/*from w w w. jav a2 s. c o m*/ return field.getName(); } }