List of usage examples for java.lang Class isAnnotationPresent
@Override public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass)
From source file:android.reflect.ClazzLoader.java
/** * //from ww w.java 2 s.c om */ public static <O, Z> Z newInstance(O o, Class<Z> clazz, Object... objs) { Z z = null; if (clazz != null) { try { boolean isInner = clazz.isAnnotationPresent(Inner.class); if (isInner && o != null) { int size = (Assert.notEmpty(objs) ? objs.length : 0) + 1; // 1 ? Class<?>[] clazzes = new Class<?>[size]; clazzes[0] = o.getClass(); Object[] values = new Object[size]; values[0] = o; if (Assert.notEmpty(objs)) { for (int i = 0; i < objs.length; i++) { Object obj = objs[i]; if (obj != null) { clazzes[i + 1] = obj.getClass(); values[i + 1] = obj; } } } Constructor<Z> constructor = clazz.getConstructor(clazzes); z = constructor != null ? constructor.newInstance(values) : clazz.newInstance(); } else { Constructor<Z> constructor = clazz.getConstructor(getParameterTypes(objs)); z = constructor != null ? constructor.newInstance(objs) : clazz.newInstance(); } } catch (Throwable t) { Log.i(TAG, "class: " + clazz.getName()); Log.e(TAG, t); } } return z; }
From source file:com.github.abel533.mapperhelper.EntityHelper.java
/** * ?Field/*from w w w . ja v a2s .co m*/ * * @param entityClass * @param fieldList * @return */ private static List<Field> getAllField(Class<?> entityClass, List<Field> fieldList) { if (fieldList == null) { fieldList = new ArrayList<Field>(); } if (entityClass.equals(Object.class)) { return fieldList; } Field[] fields = entityClass.getDeclaredFields(); for (Field field : fields) { //??bug#2 if (!Modifier.isStatic(field.getModifiers())) { fieldList.add(field); } } Class<?> superClass = entityClass.getSuperclass(); if (superClass != null && !superClass.equals(Object.class) && (superClass.isAnnotationPresent(Entity.class) || (!Map.class.isAssignableFrom(superClass) && !Collection.class.isAssignableFrom(superClass)))) { return getAllField(entityClass.getSuperclass(), fieldList); } return fieldList; }
From source file:com.github.gekoh.yagen.ddl.TableConfig.java
public static Class getClassOfTableAnnotation(Class type) { do {//from w ww . j ava2 s. co m if (type.isAnnotationPresent(javax.persistence.Table.class)) { return type; } } while ((type = type.getSuperclass()) != null); return null; }
From source file:org.jspare.core.container.Environment.java
/** * Registry component.// w w w.j a va 2 s . c o m * * @param clazzImpl * the clazz impl */ @Synchronized public static void registryComponent(Class<?> clazzImpl) { Optional<Class<?>> optionalClazzInterface = ContainerUtils.findComponentInterface(clazzImpl); if (!optionalClazzInterface.isPresent() || !ContainerUtils.isAvailableForRegister(clazzImpl)) { throw new EnvironmentException(String .format("None interface with annotation @Component founded for [%s]", clazzImpl.getName())); } Class<?> clazzInterface = optionalClazzInterface.get(); String qualifierName = clazzImpl.isAnnotationPresent(Qualifier.class) ? clazzImpl.getAnnotation(Qualifier.class).value() : Qualifier.EMPTY; Key key = new Key(clazzInterface, qualifierName); componentKeys.put(key, clazzImpl); }
From source file:uk.ac.cam.cl.dtg.segue.api.managers.QuestionManager.java
/** * Reflection to try and determine the associated validator for the question being answered. * //w ww . j av a 2 s.c o m * @param questionType * - the type of question being answered. * @return a Validator */ @SuppressWarnings("unchecked") private static IValidator locateValidator(final Class<? extends Question> questionType) { // check we haven't gone too high up the superclass tree if (!Question.class.isAssignableFrom(questionType)) { return null; } // Does this class have the correct annotation? if (questionType.isAnnotationPresent(ValidatesWith.class)) { log.debug("Validator for question validation found. Using : " + questionType.getAnnotation(ValidatesWith.class).value()); Injector injector = IsaacApplicationRegister.injector; return injector.getInstance(questionType.getAnnotation(ValidatesWith.class).value()); } else if (questionType.equals(Question.class)) { // so if we get here then we haven't found a ValidatesWith class, so // we should just give up and return null. return null; } // we will continue our search of the superclasses for the annotation return locateValidator((Class<? extends Question>) questionType.getSuperclass()); }
From source file:cn.org.awcp.core.mybatis.mapper.EntityHelper.java
/** * ?// w w w .j a v a 2 s . c om * * @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:org.evosuite.setup.TestUsageChecker.java
public static boolean canUse(Class<?> c) { //if (Throwable.class.isAssignableFrom(c)) // return false; if (Modifier.isPrivate(c.getModifiers())) return false; if (!Properties.USE_DEPRECATED && c.isAnnotationPresent(Deprecated.class)) { final Class<?> targetClass = Properties.getTargetClassAndDontInitialise(); if (Properties.hasTargetClassBeenLoaded() && !c.equals(targetClass)) { logger.debug("Skipping deprecated class " + c.getName()); return false; }//from w w w. j av a2 s. c om } if (c.isAnonymousClass()) { return false; } if (c.getName().startsWith("junit")) return false; if (TestClusterUtils.isEvoSuiteClass(c) && !MockList.isAMockClass(c.getCanonicalName())) { return false; } if (c.getEnclosingClass() != null) { if (!canUse(c.getEnclosingClass())) return false; } if (c.getDeclaringClass() != null) { if (!canUse(c.getDeclaringClass())) return false; } // If the SUT is not in the default package, then // we cannot import classes that are in the default // package if (!c.isArray() && !c.isPrimitive() && !Properties.CLASS_PREFIX.isEmpty() && !c.getName().contains(".")) { return false; } if (c.getName().contains("EnhancerByMockito")) { return false; } // TODO: This should be unnecessary if Java reflection works... // This is inefficient if (TestClusterUtils.isAnonymousClass(c.getName())) { String message = c + " looks like an anonymous class, ignoring it (although reflection says " + c.isAnonymousClass() + ") " + c.getSimpleName(); LoggingUtils.logWarnAtMostOnce(logger, message); return false; } if (Modifier.isPublic(c.getModifiers())) { return true; } // If default access rights, then check if this class is in the same package as the target class if (!Modifier.isPrivate(c.getModifiers())) { // && !Modifier.isProtected(c.getModifiers())) { String packageName = ClassUtils.getPackageName(c); if (packageName.equals(Properties.CLASS_PREFIX)) { return true; } } logger.debug("Not public"); return false; }
From source file:me.anon.lib.Views.java
/** * Resets all @InjectView annotated members to null. * Use this in the `onDestroyView()` of your fragment * @param target The target class/*from w w w . j a v a2 s . c o m*/ */ public static void reset(Object target) { ArrayList<Field> fields = new ArrayList<Field>(); Class objOrSuper = target.getClass(); if (!objOrSuper.isAnnotationPresent(Injectable.class)) { Log.e("InjectView", "No Injectable annotation for class " + objOrSuper); return; } while (objOrSuper.isAnnotationPresent(Injectable.class)) { for (Field field : objOrSuper.getDeclaredFields()) { if (field.isAnnotationPresent(InjectView.class) || field.isAnnotationPresent(InjectFragment.class)) { fields.add(field); } } objOrSuper = objOrSuper.getSuperclass(); } for (Field field : fields) { Annotation annotation = field.getAnnotation(InjectView.class); if (annotation == null) { annotation = field.getAnnotation(InjectFragment.class); } if (annotation != null) { try { field.setAccessible(true); field.set(target, null); } catch (IllegalAccessException e) { e.printStackTrace(); } } } }
From source file:org.kuali.rice.core.framework.persistence.jpa.metadata.MetadataManager.java
@SuppressWarnings("unchecked") private static EntityDescriptor construct(Class clazz) { if (!clazz.isAnnotationPresent(Entity.class)) { return null; }/*from w w w . j av a 2 s. c o m*/ // Determine the base entity metadata EntityDescriptor entityDescriptor = new EntityDescriptor(); entityDescriptor.setClazz(clazz); String defaultName = clazz.getName().substring(clazz.getName().lastIndexOf(".") + 1); Entity entity = (Entity) clazz.getAnnotation(Entity.class); if (StringUtils.isBlank(entity.name())) { entityDescriptor.setName(defaultName); } else { entityDescriptor.setName(entity.name()); } if (clazz.isAnnotationPresent(Table.class)) { Table table = (Table) clazz.getAnnotation(Table.class); entityDescriptor.setTable(table.name()); } else { entityDescriptor.setTable(defaultName); } if (clazz.isAnnotationPresent(IdClass.class)) { entityDescriptor.setIdClass(((IdClass) clazz.getAnnotation(IdClass.class)).value()); } if (clazz.isAnnotationPresent(Sequence.class)) { entityDescriptor.setSequence((Sequence) clazz.getAnnotation(Sequence.class)); } // Check for an "extension" try { Class extensionClass = Class.forName(clazz.getName() + "Extension"); OneToOneDescriptor descriptor = new OneToOneDescriptor(); descriptor.setCascade(new CascadeType[] { CascadeType.PERSIST }); descriptor.setAttributeName("extension"); descriptor.setTargetEntity(extensionClass); descriptor.setMappedBy("extension"); EntityDescriptor extensionDescriptor = MetadataManager.getEntityDescriptor(extensionClass); for (FieldDescriptor fd : extensionDescriptor.getPrimaryKeys()) { descriptor.addFkField(fd.getName()); } entityDescriptor.add(descriptor); FieldDescriptor extension = new FieldDescriptor(); extension.setName("extension"); extension.setClazz(extensionClass); entityDescriptor.add(extension); } catch (Exception e) { } List<Class> classes = new ArrayList<Class>(); classes.add(clazz); Class c = clazz; while (!c.getSuperclass().equals(Object.class)) { c = c.getSuperclass(); classes.add(c); } Collections.reverse(classes); // Determine the field/relationship metadata for all classes in the clazz hierarchy for (Class temp : classes) { extractFieldMetadata(temp, entityDescriptor); if (temp.isAnnotationPresent(AttributeOverrides.class)) { for (AttributeOverride override : ((AttributeOverrides) temp .getAnnotation(AttributeOverrides.class)).value()) { entityDescriptor.getFieldByName(override.name()).setColumn(override.column().name()); } } if (temp.isAnnotationPresent(AttributeOverride.class)) { AttributeOverride override = (AttributeOverride) temp.getAnnotation(AttributeOverride.class); entityDescriptor.getFieldByName(override.name()).setColumn(override.column().name()); } //if (temp.isAnnotationPresent(AssociationOverrides.class)) { // for (AssociationOverride override : ((AssociationOverrides)temp.getAnnotation(AssociationOverride.class)).value()) { // entityDescriptor.getFieldByName(override.name()).; // } //} //if (temp.isAnnotationPresent(AttributeOverride.class)) { // AttributeOverride override = (AttributeOverride) temp.getAnnotation(AttributeOverride.class); // entityDescriptor.getFieldByName(override.name()).setColumn(override.column().name()); //} } return entityDescriptor; }
From source file:com.smart.utils.ReflectionUtils.java
/** * beanClass?Annotation/* w w w . j a v a 2 s .c o m*/ * * @return */ public static boolean isAnnotationPresent(String beanClass, Class annotation) { ClassLoader classLoader = annotation.getClassLoader(); Class clz = null; try { clz = classLoader.loadClass(beanClass); } catch (ClassNotFoundException e) { logger.warn("" + beanClass); return false; } return clz.isAnnotationPresent(annotation); }