List of usage examples for org.apache.commons.lang3.reflect FieldUtils getFieldsWithAnnotation
public static Field[] getFieldsWithAnnotation(final Class<?> cls, final Class<? extends Annotation> annotationCls)
From source file:io.dropwizard.sharding.dao.RelationalDao.java
/** * Create a relational DAO.//from w w w. j av a 2 s. co m * @param sessionFactories List of session factories. One for each shard. * @param entityClass The class for which the dao will be used. * @param shardManager The {@link ShardManager} used to manage the bucket to shard mapping. */ public RelationalDao(List<SessionFactory> sessionFactories, Class<T> entityClass, ShardManager shardManager, BucketIdExtractor<String> bucketIdExtractor) { this.shardManager = shardManager; this.bucketIdExtractor = bucketIdExtractor; this.daos = sessionFactories.stream().map(RelationalDaoPriv::new).collect(Collectors.toList()); this.entityClass = entityClass; Field fields[] = FieldUtils.getFieldsWithAnnotation(entityClass, Id.class); Preconditions.checkArgument(fields.length != 0, "A field needs to be designated as @Id"); Preconditions.checkArgument(fields.length == 1, "Only one field can be designated as @Id"); keyField = fields[0]; if (!keyField.isAccessible()) { try { keyField.setAccessible(true); } catch (SecurityException e) { log.error("Error making key field accessible please use a public method and mark that as @Id", e); throw new IllegalArgumentException("Invalid class, DAO cannot be created.", e); } } }
From source file:io.dropwizard.sharding.dao.LookupDao.java
/** * Creates a new sharded DAO. The number of managed shards and bucketing is controlled by the {@link ShardManager}. * * @param sessionFactories a session provider for each shard *//*from www .j a v a 2 s.c om*/ public LookupDao(List<SessionFactory> sessionFactories, Class<T> entityClass, ShardManager shardManager, BucketIdExtractor<String> bucketIdExtractor) { this.shardManager = shardManager; this.bucketIdExtractor = bucketIdExtractor; this.daos = sessionFactories.stream().map(LookupDaoPriv::new).collect(Collectors.toList()); this.entityClass = entityClass; Field fields[] = FieldUtils.getFieldsWithAnnotation(entityClass, LookupKey.class); Preconditions.checkArgument(fields.length != 0, "At least one field needs to be sharding key"); Preconditions.checkArgument(fields.length == 1, "Only one field can be sharding key"); keyField = fields[0]; if (!keyField.isAccessible()) { try { keyField.setAccessible(true); } catch (SecurityException e) { log.error("Error making key field accessible please use a public method and mark that as LookupKey", e); throw new IllegalArgumentException("Invalid class, DAO cannot be created.", e); } } Preconditions.checkArgument(ClassUtils.isAssignable(keyField.getType(), String.class), "Key field must be a string"); }
From source file:lite.flow.util.ActivityInspector.java
protected static FieldInfo[] discoverParameters(Class<?> clazz) { Field[] fields = FieldUtils.getFieldsWithAnnotation(clazz, Parameter.class); FieldInfo[] fieldInfos = new FieldInfo[fields.length]; for (int i = 0; i < fieldInfos.length; i++) { Field field = fields[i];//from www .j a v a2s. com fieldInfos[i] = new FieldInfo(field.getName(), field.getType()); } return fieldInfos; }
From source file:com.github.jinahya.sql.database.metadata.bind.MetadataContext.java
private <T> T bindSingle(final ResultSet resultSet, final Class<T> beanClass, final T beanInstance) throws SQLException, ReflectiveOperationException { if (resultSet != null) { final Set<String> resultLabels = ResultSets.getColumnLabels(resultSet); // @SuppressWarnings("unchecked") // final List<Field> fields // = Reflections.fields(beanClass, Label.class); final Field[] fields = FieldUtils.getFieldsWithAnnotation(beanClass, Label.class); for (final Field field : fields) { final String label = field.getAnnotation(Label.class).value(); final String suppression = suppression(beanClass, field); final String info = String.format("field=%s, label=%s, suppression=%s", field, label, suppression); if (suppressed(suppression)) { logger.log(Level.FINE, "suppressed; {0}", info); continue; }//from w ww . ja va 2 s . c o m if (!resultLabels.remove(label)) { final String message = "unknown column; " + info; if (!suppressUnknownColumns()) { throw new RuntimeException(message); } logger.warning(message); continue; } final Object value; try { value = resultSet.getObject(label); } catch (final Exception e) { final String message = "failed to get value; " + info; logger.severe(message); if (e instanceof SQLException) { throw (SQLException) e; } throw new RuntimeException(e); } Values.set(field.getName(), beanInstance, value); //Reflections.fieldValue(field, beanInstance, value); //FieldUtils.writeField(field, beanInstance, value); //FieldUtils.writeField(field, beanInstance, value, true); } if (!resultLabels.isEmpty()) { for (final String resultLabel : resultLabels) { final Object resultValue = resultSet.getObject(resultLabel); logger.log(Level.WARNING, "unknown result; {0}({1})", new Object[] { resultLabel, resultValue }); } } } // @SuppressWarnings("unchecked") // final List<Field> fields // = Reflections.fields(beanClass, Invocation.class); final List<Field> fields = FieldUtils.getFieldsListWithAnnotation(beanClass, Invocation.class); for (final Field field : fields) { final Invocation invocation = field.getAnnotation(Invocation.class); final String suppression = suppression(beanClass, field); final String info = String.format("field=%s, invocation=%s, suppression=%s", field, invocation, suppression); if (suppressed(suppression)) { logger.log(Level.FINE, "suppressed; {0}", new Object[] { info }); continue; } final String name = invocation.name(); getMethodNames().remove(name); final Class<?>[] types = invocation.types(); final Method method; try { method = DatabaseMetaData.class.getMethod(name, types); } catch (final NoSuchMethodException nsme) { final String message = "unknown methods; " + info; if (!suppressUnknownMethods()) { throw new RuntimeException(message); } logger.warning(message); continue; } for (final InvocationArgs invocationArgs : invocation.argsarr()) { final String[] names = invocationArgs.value(); final Object[] args = Invocations.args(beanClass, beanInstance, types, names); final Object value; try { value = method.invoke(database, args); } catch (final Exception e) { logger.log(Level.SEVERE, "failed to invoke" + info, e); throw new RuntimeException(e); } catch (final AbstractMethodError ame) { logger.log(Level.SEVERE, "failed by abstract" + info, ame); throw ame; } setValue(field, beanInstance, value, args); } } if (TableDomain.class.isAssignableFrom(beanClass)) { getMethodNames().remove("getCrossReference"); final List<Table> tables = ((TableDomain) beanInstance).getTables(); final List<CrossReference> crossReferences = getCrossReferences(tables); ((TableDomain) beanInstance).setCrossReferences(crossReferences); } return beanInstance; }
From source file:lite.flow.util.ActivityInspector.java
protected static FieldInfo[] discoverResources(Class<?> clazz) { Field[] fields = FieldUtils.getFieldsWithAnnotation(clazz, Resource.class); FieldInfo[] fieldInfos = new FieldInfo[fields.length]; for (int i = 0; i < fieldInfos.length; i++) { Field field = fields[i];//from ww w .j av a2 s . c o m fieldInfos[i] = new FieldInfo(field.getName(), field.getType()); } return fieldInfos; }
From source file:gr.abiss.calipso.controller.AbstractServiceBasedRestController.java
protected void applyCurrentPrincipal(T resource) { Field[] fields = FieldUtils.getFieldsWithAnnotation(this.service.getDomainClass(), ApplyCurrentPrincipal.class); //ApplyPrincipalUse predicate = this.service.getDomainClass().getAnnotation(CurrentPrincipalIdPredicate.class); if (fields.length > 0) { ICalipsoUserDetails principal = this.service.getPrincipal(); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; ApplyCurrentPrincipal applyRule = field.getAnnotation(ApplyCurrentPrincipal.class); // if property is not already set try { if (PropertyUtils.getProperty(resource, field.getName()) == null) { boolean skipApply = this.hasAnyRoles(applyRule.ignoreforRoles()); // if role is not ignored if (!skipApply) { String id = principal != null ? principal.getId() : null; if (id != null) { User user = new User(); user.setId(id); LOGGER.info("Applying principal to field: " + field.getName() + ", value: " + id); PropertyUtils.setProperty(resource, field.getName(), user); } else { LOGGER.warn( "User is anonymous, cannot apply principal to field: " + field.getName()); }//from www.j av a 2 s .c o m } else { LOGGER.info("Skipping setting principal to field: " + field.getName()); } } } catch (Exception e) { throw new RuntimeException("Failed to apply ApplyCurrentPrincipal annotation", e); } } } }