List of usage examples for java.lang.reflect Field isAnnotationPresent
@Override public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass)
From source file:gr.abiss.calipso.jpasearch.specifications.GenericSpecifications.java
public static Map<String, String[]> getSimpleSearchTerms(Class<?> clazz, String[] values) { Map<String, String[]> simpleSearchTerms = new HashMap<String, String[]>(); Class<?> tmpClass = clazz; // try the cache first List<Field> simpleSearchFieldList = SIMPLE_SEARCH_FIELDs_CACHE.get(clazz); if (simpleSearchFieldList == null) { simpleSearchFieldList = new LinkedList<Field>(); while (tmpClass != null) { for (Field tmpField : tmpClass.getDeclaredFields()) { String fieldName = tmpField.getName(); // if string and not excluded if (isAcceptableSimpleSearchFieldClass(tmpField) && !tmpField.isAnnotationPresent(Transient.class) && !tmpField.isAnnotationPresent(JsonIgnore.class)) { simpleSearchTerms.put(fieldName, values); simpleSearchFieldList.add(tmpField); }// w w w .j a va 2s.co m } tmpClass = tmpClass.getSuperclass(); } // update the cache SIMPLE_SEARCH_FIELDs_CACHE.put(clazz, simpleSearchFieldList); } else { // use the caches field list for (Field tmpField : simpleSearchFieldList) { simpleSearchTerms.put(tmpField.getName(), values); } } return simpleSearchTerms; }
From source file:cn.org.awcp.core.mybatis.mapper.EntityHelper.java
/** * ?/* w w w . ja va 2 s. com*/ * * @param entityClass */ public static synchronized void initEntityNameMap(Class<?> entityClass) { if (entityTableMap.get(entityClass) != null) { return; } // ?? EntityTable entityTable = null; if (entityClass.isAnnotationPresent(Table.class)) { Table table = entityClass.getAnnotation(Table.class); if (!table.name().equals("")) { entityTable = new EntityTable(); entityTable.setTable(table); } } if (entityTable == null) { entityTable = new EntityTable(); entityTable.name = camelhumpToUnderline(entityClass.getSimpleName()).toUpperCase(); } entityTableMap.put(entityClass, entityTable); // List<Field> fieldList = getAllField(entityClass, null); List<EntityColumn> columnList = new ArrayList<EntityColumn>(); List<EntityColumn> pkColumnList = new ArrayList<EntityColumn>(); List<EntityColumn> obColumnList = new ArrayList<EntityColumn>(); for (Field field : fieldList) { // if (field.isAnnotationPresent(Transient.class)) { continue; } EntityColumn entityColumn = new EntityColumn(); if (field.isAnnotationPresent(Id.class)) { entityColumn.setId(true); } if (field.isAnnotationPresent(OrderBy.class)) { OrderBy orderBy = field.getAnnotation(OrderBy.class); if (StringUtils.isNotBlank(orderBy.value()) && orderBy.value().equalsIgnoreCase("desc")) { entityColumn.setOrderBy(OrderByEnum.DESC); } else { entityColumn.setOrderBy(OrderByEnum.ASC); } } String columnName = null; if (field.isAnnotationPresent(Column.class)) { Column column = field.getAnnotation(Column.class); columnName = column.name(); } if (columnName == null || columnName.equals("")) { columnName = camelhumpToUnderline(field.getName()); } entityColumn.setProperty(field.getName()); entityColumn.setColumn(columnName.toUpperCase()); entityColumn.setJavaType(field.getType()); // - Oracle?MySqlUUID if (field.isAnnotationPresent(SequenceGenerator.class)) { SequenceGenerator sequenceGenerator = field.getAnnotation(SequenceGenerator.class); if (sequenceGenerator.sequenceName().equals("")) { throw new RuntimeException(entityClass + "" + field.getName() + "@SequenceGeneratorsequenceName!"); } entityColumn.setSequenceName(sequenceGenerator.sequenceName()); } else if (field.isAnnotationPresent(GeneratedValue.class)) { GeneratedValue generatedValue = field.getAnnotation(GeneratedValue.class); if (generatedValue.generator().equals("UUID")) { if (field.getType().equals(String.class)) { entityColumn.setUuid(true); } else { throw new RuntimeException(field.getName() + " - @GeneratedValue?UUID?String"); } } else if (generatedValue.generator().equals("JDBC")) { if (Number.class.isAssignableFrom(field.getType())) { entityColumn.setIdentity(true); entityColumn.setGenerator("JDBC"); } else { throw new RuntimeException(field.getName() + " - @GeneratedValue?UUID?String"); } } else { // ?generator??idsql,mysql=CALL // IDENTITY(),hsqldb=SELECT SCOPE_IDENTITY() // ??generator if (generatedValue.strategy() == GenerationType.IDENTITY) { // mysql entityColumn.setIdentity(true); if (!generatedValue.generator().equals("")) { String generator = null; MapperHelper.IdentityDialect identityDialect = MapperHelper.IdentityDialect .getDatabaseDialect(generatedValue.generator()); if (identityDialect != null) { generator = identityDialect.getIdentityRetrievalStatement(); } else { generator = generatedValue.generator(); } entityColumn.setGenerator(generator); } } else { throw new RuntimeException(field.getName() + " - @GeneratedValue?????:" + "\n1.?@GeneratedValue(generator=\"UUID\")" + "\n2.useGeneratedKeys@GeneratedValue(generator=\\\"JDBC\\\") " + "\n3.mysql?@GeneratedValue(strategy=GenerationType.IDENTITY[,generator=\"Mysql\"])"); } } } columnList.add(entityColumn); if (entityColumn.isId()) { pkColumnList.add(entityColumn); } if (entityColumn.getOrderBy() != null) { obColumnList.add(entityColumn); } } if (pkColumnList.size() == 0) { pkColumnList = columnList; } entityClassColumns.put(entityClass, columnList); entityClassPKColumns.put(entityClass, pkColumnList); entityOrderByColumns.put(entityClass, obColumnList); }
From source file:com.all.shared.sync.SyncGenericConverter.java
public static <T> HashMap<String, Object> toMap(T clazz, SyncOperation op) { HashMap<String, Object> map = new HashMap<String, Object>(); map.put(ENTITY, clazz.getClass().getSimpleName()); for (Field field : clazz.getClass().getDeclaredFields()) { String name = field.getName(); if (PropertyUtils.isReadable(clazz, name)) { try { Object attribute = PropertyUtils.getProperty(clazz, name); if (!field.isAnnotationPresent(Transient.class)) { switch (op) { case SAVE: if (!(attribute instanceof Collection<?>)) { map.put(name, attribute instanceof Date ? ((Date) attribute).getTime() : attribute instanceof SyncAble ? ((SyncAble) attribute).getSyncAbleId() : attribute); }/*from www. j av a 2 s . c o m*/ break; case UPDATE: if (field.isAnnotationPresent(SyncUpdateAble.class)) { map.put(name, attribute instanceof Date ? ((Date) attribute).getTime() : attribute instanceof SyncAble ? ((SyncAble) attribute).getSyncAbleId() : attribute); } break; } if (field.isAnnotationPresent(Id.class)) { map.put(SYNC_HASHCODE, attribute); } } } catch (Exception e) { LOGGER.error("Could not execute method for attribute : " + name); } } } return map; }
From source file:com.zc.util.refelect.Reflector.java
/** * ?fieldannotationClass/*from w w w .j a va2 s . c o m*/ * * @param field * field * @param annotationClass * annotationClass * * @return {@link java.lang.annotation.Annotation} */ public static <T extends Annotation> T getAnnotation(Field field, Class annotationClass) { field.setAccessible(true); if (field.isAnnotationPresent(annotationClass)) { return (T) field.getAnnotation(annotationClass); } return null; }
From source file:com.impetus.kundera.metadata.MetadataUtils.java
/** * Returns true if an entity contains embedded attribute * enabled//from w w w . j a v a 2 s.co m * * @param attribute * @return */ public static boolean onCheckEmbeddableAttribute(Field attribute) { // / Checks if attribute is embeddable return attribute.isAnnotationPresent(Embedded.class); }
From source file:com.impetus.kundera.metadata.MetadataUtils.java
/** * Returns true if an entity contains attributes with validation constraints * enabled/*from w w w . j a va2s. c o m*/ * * @param attribute * @return */ public static boolean onCheckValidationConstraints(Field attribute) { // / Checks if attribute contains any validation constraint enabled return attribute.isAnnotationPresent(AssertFalse.class) || attribute.isAnnotationPresent(AssertTrue.class) || attribute.isAnnotationPresent(DecimalMax.class) || attribute.isAnnotationPresent(DecimalMin.class) || attribute.isAnnotationPresent(Digits.class) || attribute.isAnnotationPresent(Future.class) || attribute.isAnnotationPresent(Max.class) || attribute.isAnnotationPresent(Min.class) || attribute.isAnnotationPresent(NotNull.class) || attribute.isAnnotationPresent(Null.class) || attribute.isAnnotationPresent(Past.class) || attribute.isAnnotationPresent(Pattern.class) || attribute.isAnnotationPresent(Size.class); }
From source file:com.github.gekoh.yagen.util.FieldInfo.java
private static List<FieldInfo> convertFields(List<FieldInfo> fields, Class baseEntity) { for (Field field : baseEntity.getDeclaredFields()) { FieldInfo fi;/*from www. j a v a2 s . c om*/ Class type = field.getType(); String name = field.getName(); Column column = field.getAnnotation(Column.class); if (field.isAnnotationPresent(Embedded.class)) { if (field.isAnnotationPresent(AttributeOverride.class)) { fi = new FieldInfo(type, name, field.getAnnotation(AttributeOverride.class)); } else { fi = new FieldInfo(type, name, field.getAnnotation(AttributeOverrides.class)); } } else if (field.isAnnotationPresent(Enumerated.class)) { fi = new FieldInfo(type, name, true, column); } else if (column != null && !field.isAnnotationPresent(CollectionTable.class)) { if (type.isPrimitive()) { if (type.equals(Boolean.TYPE)) { type = Boolean.class; } else if (type.equals(Long.TYPE)) { type = Long.class; } else if (type.equals(Integer.TYPE)) { type = Integer.class; } else if (type.equals(Short.TYPE)) { type = Short.class; } else if (type.equals(Byte.TYPE)) { type = Byte.class; } else if (type.equals(Double.TYPE)) { type = Double.class; } else if (type.equals(Float.TYPE)) { type = Float.class; } else if (type.equals(Character.TYPE)) { type = Character.class; } } fi = new FieldInfo(type, name, false, column); } else if ((field.isAnnotationPresent(ManyToOne.class) && !field.isAnnotationPresent(JoinTable.class)) || (field.isAnnotationPresent(OneToOne.class) && StringUtils.isEmpty(field.getAnnotation(OneToOne.class).mappedBy()))) { String columnName = field.isAnnotationPresent(JoinColumn.class) ? field.getAnnotation(JoinColumn.class).name() : field.getName(); fi = getIdFieldInfo(type, name, columnName); } else if (!field.isAnnotationPresent(Transient.class) && (Collection.class.isAssignableFrom(type) || Map.class.isAssignableFrom(type)) && (field.isAnnotationPresent(JoinColumn.class) || field.isAnnotationPresent(JoinTable.class) || field.isAnnotationPresent(CollectionTable.class))) { fi = new FieldInfo(type, name); } else { continue; } if (field.isAnnotationPresent(Type.class)) { fi.addAnnotation(field.getAnnotation(Type.class)); } fi.setField(field); fields.add(fi); } return fields; }
From source file:com.github.gekoh.yagen.ddl.TableConfig.java
private static <T extends Annotation> Set<AccessibleObject> getAnnotatedFieldOrMethod( Set<AccessibleObject> fieldsOrMethods, Class<T> annotationClass, Class entityClass, boolean withInheritance) { for (Field field : entityClass.getDeclaredFields()) { if (field.isAnnotationPresent(annotationClass)) { fieldsOrMethods.add(field);//from ww w .java2s .c o m } } for (Method method : entityClass.getDeclaredMethods()) { if (method.isAnnotationPresent(annotationClass)) { fieldsOrMethods.add(method); } } if (entityClass.getSuperclass() != null && withInheritance) { return getAnnotatedFieldOrMethod(fieldsOrMethods, annotationClass, entityClass.getSuperclass(), withInheritance); } return fieldsOrMethods; }
From source file:com.github.abel533.mapperhelper.EntityHelper.java
/** * ?//from www . ja va 2 s.co m * * @param entityClass */ public static synchronized void initEntityNameMap(Class<?> entityClass) { if (entityTableMap.get(entityClass) != null) { return; } //?? EntityTable entityTable = null; if (entityClass.isAnnotationPresent(Table.class)) { Table table = entityClass.getAnnotation(Table.class); if (!table.name().equals("")) { entityTable = new EntityTable(); entityTable.setTable(table); } } if (entityTable == null) { entityTable = new EntityTable(); //??????@Table entityTable.name = camelhumpToUnderline(entityClass.getSimpleName()); } // List<Field> fieldList = getAllField(entityClass, null); Set<EntityColumn> columnSet = new HashSet<EntityColumn>(); Set<EntityColumn> pkColumnSet = new HashSet<EntityColumn>(); for (Field field : fieldList) { // if (field.isAnnotationPresent(Transient.class)) { continue; } EntityColumn entityColumn = new EntityColumn(); if (field.isAnnotationPresent(Id.class)) { entityColumn.setId(true); } String columnName = null; if (field.isAnnotationPresent(Column.class)) { Column column = field.getAnnotation(Column.class); columnName = column.name(); } if (columnName == null || columnName.equals("")) { columnName = camelhumpToUnderline(field.getName()); } entityColumn.setProperty(field.getName()); entityColumn.setColumn(columnName.toUpperCase()); entityColumn.setJavaType(field.getType()); //order by if (field.isAnnotationPresent(OrderBy.class)) { OrderBy orderBy = field.getAnnotation(OrderBy.class); if (orderBy.value().equals("")) { entityColumn.setOrderBy("ASC"); } else { entityColumn.setOrderBy(orderBy.value()); } } // - Oracle?MySqlUUID if (field.isAnnotationPresent(SequenceGenerator.class)) { SequenceGenerator sequenceGenerator = field.getAnnotation(SequenceGenerator.class); if (sequenceGenerator.sequenceName().equals("")) { throw new RuntimeException(entityClass + "" + field.getName() + "@SequenceGeneratorsequenceName!"); } entityColumn.setSequenceName(sequenceGenerator.sequenceName()); } else if (field.isAnnotationPresent(GeneratedValue.class)) { GeneratedValue generatedValue = field.getAnnotation(GeneratedValue.class); if (generatedValue.generator().equals("UUID")) { if (field.getType().equals(String.class)) { entityColumn.setUuid(true); } else { throw new RuntimeException(field.getName() + " - @GeneratedValue?UUID?String"); } } else if (generatedValue.generator().equals("JDBC")) { if (Number.class.isAssignableFrom(field.getType())) { entityColumn.setIdentity(true); entityColumn.setGenerator("JDBC"); } else { throw new RuntimeException(field.getName() + " - @GeneratedValue?UUID?String"); } } else { //?generator??idsql,mysql=CALL IDENTITY(),hsqldb=SELECT SCOPE_IDENTITY() //??generator if (generatedValue.strategy() == GenerationType.IDENTITY) { //mysql entityColumn.setIdentity(true); if (!generatedValue.generator().equals("")) { String generator = null; MapperHelper.IdentityDialect identityDialect = MapperHelper.IdentityDialect .getDatabaseDialect(generatedValue.generator()); if (identityDialect != null) { generator = identityDialect.getIdentityRetrievalStatement(); } else { generator = generatedValue.generator(); } entityColumn.setGenerator(generator); } } else { throw new RuntimeException(field.getName() + " - @GeneratedValue?????:" + "\n1.?@GeneratedValue(generator=\"UUID\")" + "\n2.useGeneratedKeys@GeneratedValue(generator=\\\"JDBC\\\") " + "\n3.mysql?@GeneratedValue(strategy=GenerationType.IDENTITY[,generator=\"Mysql\"])"); } } } columnSet.add(entityColumn); if (entityColumn.isId()) { pkColumnSet.add(entityColumn); } } entityTable.entityClassColumns = columnSet; if (pkColumnSet.size() == 0) { entityTable.entityClassPKColumns = columnSet; } else { entityTable.entityClassPKColumns = pkColumnSet; } // entityTableMap.put(entityClass, entityTable); }
From source file:org.oncoblocks.centromere.web.controller.RequestUtils.java
/** * Inspects a {@link Model} class and returns all of the available and acceptable query parameter * definitions, as a map of parameter names and {@link QueryParameterDescriptor} objects. * /*from w ww . j a v a 2s . com*/ * @param model * @return */ public static Map<String, QueryParameterDescriptor> getAvailableQueryParameters(Class<? extends Model<?>> model, boolean recursive) { Map<String, QueryParameterDescriptor> paramMap = new HashMap<>(); for (Field field : model.getDeclaredFields()) { String fieldName = field.getName(); Class<?> type = field.getType(); if (Collection.class.isAssignableFrom(field.getType())) { ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType(); type = (Class<?>) parameterizedType.getActualTypeArguments()[0]; } if (field.isAnnotationPresent(Ignored.class)) { continue; } else { paramMap.put(fieldName, new QueryParameterDescriptor(fieldName, fieldName, type, Evaluation.EQUALS)); } if (field.isAnnotationPresent(ForeignKey.class)) { if (!recursive) continue; ForeignKey foreignKey = field.getAnnotation(ForeignKey.class); String relField = !"".equals(foreignKey.rel()) ? foreignKey.rel() : fieldName; Map<String, QueryParameterDescriptor> foreignModelMap = getAvailableQueryParameters( foreignKey.model(), false); for (QueryParameterDescriptor descriptor : foreignModelMap.values()) { String newParamName = relField + "." + descriptor.getParamName(); descriptor.setParamName(newParamName); paramMap.put(newParamName, descriptor); } } if (field.isAnnotationPresent(Aliases.class)) { Aliases aliases = field.getAnnotation(Aliases.class); for (Alias alias : aliases.value()) { paramMap.put(alias.value(), new QueryParameterDescriptor(alias.value(), alias.fieldName().equals("") ? fieldName : alias.fieldName(), type, alias.evaluation())); } } else if (field.isAnnotationPresent(Alias.class)) { Alias alias = field.getAnnotation(Alias.class); paramMap.put(alias.value(), new QueryParameterDescriptor(alias.value(), alias.fieldName().equals("") ? fieldName : alias.fieldName(), type, alias.evaluation())); } } return paramMap; }