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.reactive.hzdfs.cluster.HazelcastInstanceProxy.java

/**
 * Add an IMap configuration programmatically from an annotated class.
 * @param c/*from   w  ww  . j  av  a2 s. c o  m*/
 * @see IMapConfig
 */
void addMapConfig(Class<?> c) {
    if (!c.isAnnotationPresent(IMapConfig.class))
        throw new IllegalArgumentException(c + " not annotated with @" + IMapConfig.class.getSimpleName());

    IMapConfig hc = c.getAnnotation(IMapConfig.class);

    addMapConfig(hc, hc.name());
}

From source file:net.paoding.rose.web.impl.module.ModulesBuilderImpl.java

private List<InterceptorDelegate> findInterceptors(XmlWebApplicationContext context) {
    String[] interceptorNames = SpringUtils.getBeanNames(context.getBeanFactory(), ControllerInterceptor.class);
    ArrayList<InterceptorDelegate> interceptors = new ArrayList<InterceptorDelegate>(interceptorNames.length);
    for (String beanName : interceptorNames) {
        ControllerInterceptor interceptor = (ControllerInterceptor) context.getBean(beanName);
        Class<?> userClass = ClassUtils.getUserClass(interceptor);
        if (userClass.isAnnotationPresent(Ignored.class)) {
            if (logger.isDebugEnabled()) {
                logger.debug("Ignored interceptor (Ignored):" + interceptor);
            }/*from  w w  w .  j  a va2 s. c  o  m*/
            continue;
        }
        if (userClass.isAnnotationPresent(NotForSubModules.class)
                && !context.getBeanFactory().containsBeanDefinition(beanName)) {
            if (logger.isDebugEnabled()) {
                logger.debug("Ignored interceptor (NotForSubModules):" + interceptor);
            }
            continue;
        }
        if (!userClass.getSimpleName().endsWith(RoseConstants.INTERCEPTOR_SUFFIX)) {
            logger.error("", new IllegalArgumentException("Interceptor must be end with '"
                    + RoseConstants.INTERCEPTOR_SUFFIX + "': " + userClass.getName()));
            continue;
        }
        InterceptorBuilder builder = new InterceptorBuilder(interceptor);
        Interceptor annotation = userClass.getAnnotation(Interceptor.class);
        if (annotation != null) {
            builder.oncePerRequest(annotation.oncePerRequest());
        }
        String interceporName;
        if (beanName.startsWith(AUTO_BEAN_NAME_PREFIX)) {
            interceporName = StringUtils.removeEnd(StringUtils.uncapitalize(userClass.getSimpleName()),
                    RoseConstants.INTERCEPTOR_SUFFIX);
        } else {
            interceporName = StringUtils.removeEnd(beanName, RoseConstants.INTERCEPTOR_SUFFIX);
        }
        final String rose = "rose";
        if (interceporName.startsWith(rose)
                && (interceporName.length() == rose.length()
                        || Character.isUpperCase(interceporName.charAt(rose.length())))
                && !userClass.getName().startsWith("net.paoding.rose.")) {
            throw new IllegalArgumentException("illegal interceptor name '" + interceporName + "' for "
                    + userClass.getName() + ": don't starts with 'rose', it's reserved");
        }

        builder.name(interceporName);

        InterceptorDelegate wrapper = builder.build();
        interceptors.add(wrapper);
        if (logger.isDebugEnabled()) {
            int priority = 0;
            if (interceptor instanceof Ordered) {
                priority = ((Ordered) interceptor).getPriority();
            }
            logger.debug("recognized interceptor[priority=" + priority + "]: " // \r\n
                    + wrapper.getName() + "=" + userClass.getName());
        }
    }
    Collections.sort(interceptors);
    throwExceptionIfDuplicatedNames(interceptors);
    return interceptors;
}

From source file:com.wit.android.support.fragment.BaseFragment.java

/**
 * Iterates all declared fields of the given <var>classOfFragment</var> using
 * {@link FragmentAnnotations#iterateFields(Class, FragmentAnnotations.FieldProcessor)} utility
 * method passing it the given <var>fieldProcessor</var> and classOfFragment.
 *
 * @param classOfFragment Class of which fields to iterate.
 * @param fieldProcessor  Field processor to handle fields iteration.
 *///from ww w .  j av a2s.  co m
private void iterateInjectableViewFields(Class<?> classOfFragment,
        FragmentAnnotations.FieldProcessor fieldProcessor) {
    if (classOfFragment.isAnnotationPresent(InjectViews.class)) {
        FragmentAnnotations.iterateFields(classOfFragment, fieldProcessor);
    }
    // Iterate also fields of super class, but only to this BaseFragment super.
    final Class<?> superOfFragment = classOfFragment.getSuperclass();
    if (superOfFragment != null && !superOfFragment.equals(BaseFragment.class)) {
        iterateInjectableViewFields(superOfFragment, fieldProcessor);
    }
}

From source file:com.laxser.blitz.web.impl.module.ModulesBuilderImpl.java

private List<InterceptorDelegate> findInterceptors(XmlWebApplicationContext context) {
    String[] interceptorNames = SpringUtils.getBeanNames(context.getBeanFactory(), ControllerInterceptor.class);
    ArrayList<InterceptorDelegate> interceptors = new ArrayList<InterceptorDelegate>(interceptorNames.length);
    for (String beanName : interceptorNames) {
        ControllerInterceptor interceptor = (ControllerInterceptor) context.getBean(beanName);
        Class<?> userClass = ClassUtils.getUserClass(interceptor);
        if (userClass.isAnnotationPresent(Ignored.class)) {
            if (logger.isDebugEnabled()) {
                logger.debug("Ignored interceptor (Ignored):" + interceptor);
            }//from   w ww .j av  a2s. c om
            continue;
        }
        if (userClass.isAnnotationPresent(NotForSubModules.class)
                && !context.getBeanFactory().containsBeanDefinition(beanName)) {
            if (logger.isDebugEnabled()) {
                logger.debug("Ignored interceptor (NotForSubModules):" + interceptor);
            }
            continue;
        }
        if (!userClass.getSimpleName().endsWith(BlitzConstants.INTERCEPTOR_SUFFIX)) {
            logger.error("", new IllegalArgumentException("Interceptor must be end with '"
                    + BlitzConstants.INTERCEPTOR_SUFFIX + "': " + userClass.getName()));
            continue;
        }
        InterceptorBuilder builder = new InterceptorBuilder(interceptor);
        Interceptor annotation = userClass.getAnnotation(Interceptor.class);
        if (annotation != null) {
            builder.oncePerRequest(annotation.oncePerRequest());
        }
        String interceporName;
        if (beanName.startsWith(AUTO_BEAN_NAME_PREFIX)) {
            interceporName = StringUtils.removeEnd(StringUtils.uncapitalize(userClass.getSimpleName()),
                    BlitzConstants.INTERCEPTOR_SUFFIX);
        } else {
            interceporName = StringUtils.removeEnd(beanName, BlitzConstants.INTERCEPTOR_SUFFIX);
        }
        final String rose = "rose";
        if (interceporName.startsWith(rose)
                && (interceporName.length() == rose.length()
                        || Character.isUpperCase(interceporName.charAt(rose.length())))
                && !userClass.getName().startsWith("net.paoding.rose.")) {
            throw new IllegalArgumentException("illegal interceptor name '" + interceporName + "' for "
                    + userClass.getName() + ": don't starts with 'rose', it's reserved");
        }

        builder.name(interceporName);

        InterceptorDelegate wrapper = builder.build();
        interceptors.add(wrapper);
        if (logger.isDebugEnabled()) {
            int priority = 0;
            if (interceptor instanceof Ordered) {
                priority = ((Ordered) interceptor).getPriority();
            }
            logger.debug("recognized interceptor[priority=" + priority + "]: " // \r\n
                    + wrapper.getName() + "=" + userClass.getName());
        }
    }
    Collections.sort(interceptors);
    throwExceptionIfDuplicatedNames(interceptors);
    return interceptors;
}

From source file:org.projectforge.framework.xstream.XmlObjectReader.java

private void initialize(final Set<Class<?>> processed, final Class<?> clazz) {
    if (processed.contains(clazz) == true) {
        // Already processed, avoid endless loops:
        return;/*from w w  w.  j a  va 2  s. c o  m*/
    }
    processed.add(clazz);
    if (clazz.isAnnotationPresent(XmlObject.class) == true) {
        final XmlObject xmlObject = clazz.getAnnotation(XmlObject.class);
        if (StringUtils.isNotEmpty(xmlObject.alias()) == true) {
            getAliasMap().put(clazz, xmlObject.alias());
        }
    }
    for (final Field field : BeanHelper.getAllDeclaredFields(clazz)) {
        if (field.getType().isPrimitive() == false && XmlObjectWriter.ignoreField(field) == false) {
            initialize(processed, field.getType());
        }
    }
}

From source file:therian.operator.copy.PropertyCopier.java

protected PropertyCopier() {
    try {//from  www  . j a  va2  s  .co m
        @SuppressWarnings("rawtypes")
        final Class<? extends PropertyCopier> c = getClass();
        matching = c.getAnnotation(Matching.class);

        final Mapping mapping = c.getAnnotation(Mapping.class);
        if (mapping == null) {
            Validate.validState(c.isAnnotationPresent(Matching.class),
                    "%s specifies neither @Mapping nor @Matching", c);
            mappings = Collections.emptyList();
        } else {
            mappings = parse(mapping);
        }
    } catch (Exception e) {
        throw new OperatorDefinitionException(this, e);
    }
}

From source file:com.github.gekoh.yagen.util.MappingUtils.java

private static void fillTreeEntityMap(Map<Class, TreeEntity> treeEntities, Set<Attribute> attributes,
        Class entityClass) {
    for (Attribute attribute : attributes) {
        if (attribute instanceof SingularAttribute
                && ((SingularAttribute) attribute).getType() instanceof EmbeddableType) {
            fillTreeEntityMap(treeEntities,
                    ((EmbeddableType) ((SingularAttribute) attribute).getType()).getAttributes(), entityClass);
        } else if (!attribute.isCollection()
                && attribute.getPersistentAttributeType() != Attribute.PersistentAttributeType.BASIC
                && attribute.getDeclaringType() instanceof EntityType) {
            Class targetEntity = attribute.getJavaType();
            addMasterEntity(treeEntities, entityClass, targetEntity);
        } else if (attribute.isCollection()
                && attribute.getPersistentAttributeType() != Attribute.PersistentAttributeType.MANY_TO_MANY
                && attribute instanceof PluralAttribute) {
            addMasterEntity(treeEntities, ((PluralAttribute) attribute).getElementType().getJavaType(),
                    entityClass);//w  ww .ja v  a2  s  .co  m
        }
    }
    if (!entityClass.isAnnotationPresent(Table.class)) {
        Class lastEntity = entityClass;
        Class parent = lastEntity;
        do {
            parent = parent.getSuperclass();
            if (parent.isAnnotationPresent(Entity.class)) {
                addMasterEntity(treeEntities, parent, lastEntity);
                lastEntity = parent;
            }
            if (parent.isAnnotationPresent(Table.class)) {
                break;
            }
        } while (parent.getSuperclass() != null);
    }
}

From source file:com.opensymphony.xwork2.util.finder.DefaultClassFinder.java

public List<Class> findAnnotatedClasses(Class<? extends Annotation> annotation) {
    classesNotLoaded.clear();//from  w ww .jav  a2 s.com
    List<Class> classes = new ArrayList<Class>();
    List<Info> infos = getAnnotationInfos(annotation.getName());
    for (Info info : infos) {
        if (info instanceof ClassInfo) {
            ClassInfo classInfo = (ClassInfo) info;
            try {
                Class clazz = classInfo.get();
                // double check via proper reflection
                if (clazz.isAnnotationPresent(annotation)) {
                    classes.add(clazz);
                }
            } catch (Throwable e) {
                LOG.error("Error loading class [{}]", classInfo.getName(), e);
                classesNotLoaded.add(classInfo.getName());
            }
        }
    }
    return classes;
}

From source file:nl.knaw.dans.common.ldap.repo.LdapMapper.java

/**
 * Get the set of ldap objectClasses as annotated with {@link LdapObject} on the implementing mapped class and it's
 * super classes./*from   w w w  .  j  av a2s  .c  o  m*/
 *
 * @return the set of ldap objectClasses
 */
protected Set<String> getObjectClasses() {
    if (objectClasses == null) {
        objectClasses = new LinkedHashSet<String>();
        Class<?> superC = clazz;
        while (superC != null) {
            if (superC.isAnnotationPresent(LdapObject.class)) {
                String[] oc = superC.getAnnotation(LdapObject.class).objectClasses();
                objectClasses.addAll(Arrays.asList(oc));
            }
            superC = superC.getSuperclass();
        }
        if (!objectClasses.contains("top")) {
            objectClasses.add("top");
        }
    }
    return objectClasses;
}

From source file:org.red5.io.amf3.Output.java

/** {@inheritDoc} */
public void writeObject(Object object, Serializer serializer) {
    writeAMF3();/*w  w  w .  j ava  2 s. c  o  m*/
    buf.put(AMF3.TYPE_OBJECT);
    if (hasReference(object)) {
        putInteger(getReferenceId(object) << 1);
        return;
    }

    storeReference(object);
    String className = object.getClass().getName();
    if (className.startsWith("org.red5.compatibility.")) {
        // Strip compatibility prefix from classname
        className = className.substring(23);
    }

    if (object instanceof IExternalizable) {
        // The object knows how to serialize itself.
        int type = 1 << 1 | 1;
        if (object instanceof ObjectProxy) {
            type |= AMF3.TYPE_OBJECT_PROXY << 2;
        } else {
            type |= AMF3.TYPE_OBJECT_EXTERNALIZABLE << 2;
        }
        putInteger(type);
        putString(className);
        amf3_mode += 1;
        ((IExternalizable) object).writeExternal(new DataOutput(this, serializer));
        amf3_mode -= 1;
        return;
    }

    // We have an inline class that is not a reference.
    // We store the properties using key/value pairs
    int type = AMF3.TYPE_OBJECT_VALUE << 2 | 1 << 1 | 1;
    putInteger(type);

    // Create new map out of bean properties
    BeanMap beanMap = new BeanMap(object);
    // Set of bean attributes
    Set<BeanMap.Entry<?, ?>> set = beanMap.entrySet();
    if ((set.size() == 0) || (set.size() == 1 && beanMap.containsKey("class"))) {
        // BeanMap is empty or can only access "class" attribute, skip it
        writeArbitraryObject(object, serializer);
        return;
    }

    // Write out either start of object marker for class name or "empty" start of object marker
    Class<?> objectClass = object.getClass();
    if (!objectClass.isAnnotationPresent(Anonymous.class)) {
        // classname
        putString(className);
    } else {
        putString("");
    }

    // Store key/value pairs
    amf3_mode += 1;
    for (BeanMap.Entry<?, ?> entry : set) {
        String keyName = entry.getKey().toString();
        if ("class".equals(keyName)) {
            continue;
        }

        // Check if the Field corresponding to the getter/setter pair is transient
        if (dontSerializeField(objectClass, keyName))
            continue;

        putString(keyName);
        serializer.serialize(this, entry.getValue());
    }
    amf3_mode -= 1;

    // End of object marker
    putString("");
}