Example usage for java.lang Class isAnnotationPresent

List of usage examples for java.lang Class isAnnotationPresent

Introduction

In this page you can find the example usage for java.lang Class isAnnotationPresent.

Prototype

@Override
public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) 

Source Link

Usage

From source file:com.conversantmedia.mapreduce.tool.annotation.handler.MaraAnnotationUtil.java

@SuppressWarnings("unchecked")
public static boolean hasAnyAnnotation(Class<?> clazz, Class<? extends Annotation>... annotations) {
    for (Class<? extends Annotation> annotationClass : annotations) {
        if (clazz.isAnnotationPresent(annotationClass)) {
            return true;
        }//  ww w .  j  av  a  2 s  .co  m
    }
    return false;
}

From source file:ReflectionUtils.java

/**
 * beanClassAnnotation//from  ww w. j  av  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);
}

From source file:com.asakusafw.compiler.bootstrap.AllBatchCompilerDriver.java

private static Class<? extends BatchDescription> loadIfBatchClass(String className) {
    try {/*from w ww. j  a  v a2s . c o m*/
        Class<?> aClass = Class.forName(className, false, AllBatchCompilerDriver.class.getClassLoader());
        if (BatchDescription.class.isAssignableFrom(aClass) == false) {
            return null;
        }
        if (aClass.isAnnotationPresent(Batch.class) == false) {
            LOG.warn(MessageFormat.format(Messages.getString("AllBatchCompilerDriver.warnMissingAnnotation"), //$NON-NLS-1$
                    aClass.getName()));
            return null;
        }
        return aClass.asSubclass(BatchDescription.class);
    } catch (ClassNotFoundException e) {
        LOG.debug("failed to load batch class", e); //$NON-NLS-1$
        return null;
    }
}

From source file:com.google.code.simplestuff.bean.BusinessObjectContext.java

/**
 * Retrieve a {@link BusinessObjectDescriptor} object for a
 * {@link BusinessObject} class bean.//w  w  w  . ja  va  2s . c o  m
 * 
 * @param objectClass The class of the bean to check.
 * @param annotationObjectClass The class to use for retrieving the
 *        {@link BusinessField} annotated field.
 * @return A {@link BusinessObjectDescriptor} for the bean class passed.
 */
private static BusinessObjectDescriptor getBusinessObjectDescriptor(Class<? extends Object> objectClass,
        Class<? extends Object> annotationObjectClass) {

    BusinessObjectDescriptor businessObjectDescriptor = new BusinessObjectDescriptor();

    if (objectClass == null) {
        businessObjectDescriptor.setNearestBusinessObjectClass(null);
        return businessObjectDescriptor;
    } else {
        if (objectClass.isAnnotationPresent(BusinessObject.class)) {

            businessObjectDescriptor.setAnnotatedFields(getAnnotatedFields(annotationObjectClass));
            businessObjectDescriptor.setClassToBeConsideredInComparison(
                    objectClass.getAnnotation(BusinessObject.class).includeClassAsBusinessField());
            businessObjectDescriptor.setNearestBusinessObjectClass(objectClass);
            return businessObjectDescriptor;
        } else {
            return getBusinessObjectDescriptor(objectClass.getSuperclass(), annotationObjectClass);
        }

    }
}

From source file:com.wit.android.support.fragment.util.FragmentAnnotations.java

/**
 * Obtains the specified type of an annotation from the given <var>fromClass</var> if it is presented.
 *
 * @param fromClass         A class from which should be the requested annotation obtained.
 * @param classOfAnnotation A class of the requested annotation.
 * @param maxSuperClass     If {@code not null}, this method will be called (recursively)
 *                          for all super classes of the given annotated class (max to the specified
 *                          <var>maxSuperClass</var>) until the requested annotation is presented
 *                          and obtained, otherwise annotation will be obtained only from the given
 *                          annotated class.
 * @param <A>               The type of the requested annotation.
 * @return Obtained annotation or {@code null} if the requested annotation is not presented
 * for the given class or its supers if requested.
 * @see #obtainAnnotationFrom(Class, Class)
 *///www.  j a  va2s  . c  om
@Nullable
public static <A extends Annotation> A obtainAnnotationFrom(@NonNull Class<?> fromClass,
        @NonNull Class<A> classOfAnnotation, @Nullable Class<?> maxSuperClass) {
    if (fromClass.isAnnotationPresent(classOfAnnotation)) {
        return fromClass.getAnnotation(classOfAnnotation);
    } else if (maxSuperClass != null) {
        final Class<?> parent = fromClass.getSuperclass();
        if (parent != null && !parent.equals(maxSuperClass)) {
            return obtainAnnotationFrom(parent, classOfAnnotation, maxSuperClass);
        }
    }
    return null;
}

From source file:org.b3log.latke.servlet.converter.Converters.java

/**
 * getRendererId from mark {@link org.b3log.latke.servlet.annotation.Render},using"-" as split:class_method_PARAMETER.
 *
 * @param processorClass  class/*from   ww w . j a  v  a  2  s  .  co m*/
 * @param processorMethod method
 * @param i               the index of the
 * @return string
 */
private static String getRendererId(final Class<?> processorClass, final Method processorMethod, final int i) {
    final StringBuilder sb = new StringBuilder();

    if (processorClass.isAnnotationPresent(Render.class)) {
        final String v = processorClass.getAnnotation(Render.class).value();

        if (StringUtils.isNotBlank(v)) {
            sb.append(v).append(v);
        }
    }

    if (processorMethod.isAnnotationPresent(Render.class)) {

        final String v = processorClass.getAnnotation(Render.class).value();

        if (StringUtils.isNotBlank(v)) {
            if (sb.length() > 0) {
                sb.append("-");
            }
            sb.append(v).append(v);
        }
    }

    for (java.lang.annotation.Annotation annotation : processorMethod.getParameterAnnotations()[i]) {
        if (annotation instanceof Render) {
            final String v = ((PathVariable) annotation).value();

            if (sb.length() > 0) {
                sb.append("-");
            }
            sb.append(v).append(v);
        }
    }

    return sb.toString();
}

From source file:br.gov.frameworkdemoiselle.ldap.internal.ClazzUtils.java

/**
 * Verify if a class or yours super classes have annotation, throw
 * EntryException if annotation is required and wasn't found.
 * //w w w  .  jav a  2 s  .  co  m
 * @param clazz
 *            a class to be verified
 * @param aclazz
 *            a Annotation class to be searched
 * @param required
 *            define if should throw a EntryExpcetion when annotation was't
 *            found.
 * 
 */
public static boolean isAnnotationPresent(Class<?> clazz, Class<? extends Annotation> aclazz,
        boolean required) {
    for (Class<?> claz : getSuperClasses(clazz))
        if (claz.isAnnotationPresent(aclazz))
            return true;
    if (required)
        throw new EntryException("Entry " + clazz.getSimpleName() + " doesn't have @" + aclazz.getName());
    return false;
}

From source file:com.reactivetechnologies.platform.rest.WebbitRestServerBean.java

/**
 * //  w  w w . j a  va 2  s .c  om
 * @param restletClass
 * @return
 * @throws InstantiationException
 * @throws IllegalAccessException
 */
static JAXRSInstanceMetadata scanJaxRsClass(Class<?> restletClass)
        throws InstantiationException, IllegalAccessException {
    final JAXRSInstanceMetadata proxy = new JAXRSInstanceMetadata(restletClass.newInstance());
    if (restletClass.isAnnotationPresent(Path.class)) {
        String rootUri = restletClass.getAnnotation(Path.class).value();
        if (rootUri == null)
            rootUri = "";
        proxy.setRootUri(rootUri);
    }
    ReflectionUtils.doWithMethods(restletClass, new MethodCallback() {

        @Override
        public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
            String subUri = "";
            if (method.isAnnotationPresent(Path.class)) {
                subUri = method.getAnnotation(Path.class).value();
            }
            if (method.isAnnotationPresent(GET.class)) {
                MethodDetail m = createMethodDetail(method);
                proxy.addGetMethod(proxy.getRootUri() + subUri, m);
            }
            if (method.isAnnotationPresent(POST.class)) {
                MethodDetail m = createMethodDetail(method);
                proxy.addPostMethod(proxy.getRootUri() + subUri, m);
            }
            if (method.isAnnotationPresent(DELETE.class)) {
                MethodDetail m = createMethodDetail(method);
                proxy.addDelMethod(proxy.getRootUri() + subUri, m);
            }

        }
    }, new MethodFilter() {

        @Override
        public boolean matches(Method method) {
            return (method.isAnnotationPresent(GET.class) || method.isAnnotationPresent(POST.class)
                    || method.isAnnotationPresent(DELETE.class));
        }
    });

    return proxy;
}

From source file:me.ronghai.sa.dao.impl.AbstractModelDAOWithJDBCImpl.java

public static String table(Class<?> entityClass) {
    String table = entityClass.getSimpleName();
    if (entityClass.isAnnotationPresent(Entity.class)) {
        String t = ((Entity) entityClass.getAnnotation(Entity.class)).name();
        if (org.apache.commons.lang.StringUtils.isNotEmpty(t)) {
            table = t;//w w  w  . j av a 2s  . c o m
        }
    }
    return table;
}

From source file:net.ymate.platform.plugin.Plugins.java

/**
 * @param clazz  ?/*from w w w .ja  va2 s .c  o m*/
 * @param config ???
 * @return ??clazz?
 * @throws Exception ??
 */
public static IPluginFactory createFactory(Class<? extends IPluginFactory> clazz, IPluginConfig config)
        throws Exception {
    IPluginFactory _factory = ClassUtils.impl(clazz, IPluginFactory.class);
    if (_factory != null) {
        if (config != null) {
            _factory.init(config);
        } else if (clazz.isAnnotationPresent(PluginFactory.class)) {
            _factory.init(loadConfig(clazz));
        }
    }
    return _factory;
}