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.github.gekoh.yagen.util.MappingUtils.java

private static Map<String, Class> getTableNameMap(Class... classes) {
    Map<String, Class> tableNameMap = new HashMap<String, Class>(classes.length);

    for (Class aClass : classes) {
        if (aClass.isAnnotationPresent(Table.class)) {
            tableNameMap.put(((Table) aClass.getAnnotation(Table.class)).name().toUpperCase(), aClass);
        }//from w  ww  .ja va2  s .  c  o m
    }

    return tableNameMap;
}

From source file:ch.aonyx.broker.ib.api.util.AnnotationUtils.java

/**
 * Determine whether an annotation for the specified <code>annotationType</code> is present on the supplied
 * <code>clazz</code> and is {@link java.lang.annotation.Inherited inherited} i.e., not declared locally for the
 * class)./*from   ww  w .ja  v  a2  s.c o  m*/
 * <p>
 * If the supplied <code>clazz</code> is an interface, only the interface itself will be checked. In accordance with
 * standard meta-annotation semantics, the inheritance hierarchy for interfaces will not be traversed. See the
 * {@link java.lang.annotation.Inherited JavaDoc} for the &#064;Inherited meta-annotation for further details
 * regarding annotation inheritance.
 * 
 * @param annotationType
 *            the Class object corresponding to the annotation type
 * @param clazz
 *            the Class object corresponding to the class on which to check for the annotation
 * @return <code>true</code> if an annotation for the specified <code>annotationType</code> is present on the
 *         supplied <code>clazz</code> and is {@link java.lang.annotation.Inherited inherited}
 * @see Class#isAnnotationPresent(Class)
 * @see #isAnnotationDeclaredLocally(Class, Class)
 */
public static boolean isAnnotationInherited(final Class<? extends Annotation> annotationType,
        final Class<?> clazz) {
    Validate.notNull(annotationType, "Annotation type must not be null");
    Validate.notNull(clazz, "Class must not be null");
    return (clazz.isAnnotationPresent(annotationType) && !isAnnotationDeclaredLocally(annotationType, clazz));
}

From source file:org.agiso.core.i18n.util.I18nUtils.java

public static String getCode(Class<?> c) {
    if (c.isAnnotationPresent(I18n.class)) {
        if (c.getAnnotation(I18n.class).value().length() > 0) {
            return c.getAnnotation(I18n.class).value();
        } else {/*w  ww .  ja v a  2  s. c  o m*/
            return c.getName();
        }
    }
    // else if(c.isAnnotationPresent(I18nRef.class)) {
    //    Object ref = c.getAnnotation(I18nRef.class).value();
    //    if(((Class<?>)ref).isEnum()) {
    //       return getCode((Enum<?>)ref);
    //    } else {
    //       return getCode((Class<?>)ref);
    //    }
    // }
    return c.getName();
}

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

public static Set<Class> getSubEntities(EntityManagerFactory emf, Class superClass) throws Exception {
    Set<Class> entities = new HashSet<Class>();
    for (Class aClass : getEntityClasses(emf)) {
        if (aClass.isAnnotationPresent(Entity.class) && aClass != superClass
                && superClass.isAssignableFrom(aClass)) {
            entities.add(aClass);// w ww.j a  v  a2s  . c  o m
        }
    }
    return entities;
}

From source file:org.jspare.forvertx.web.collector.HandlerCollector.java

public static Collection<HandlerData> collect(Transporter transporter, Class<?> clazz) {

    List<HandlerData> collectedHandlers = new ArrayList<>();
    List<Annotation> httpMethodsAnnotations = new ArrayList<>(getHttpMethodsPresents(clazz));
    boolean hasAuthClass = clazz.isAnnotationPresent(Auth.class);
    Auth authClass = clazz.getAnnotation(Auth.class);

    for (Method method : clazz.getDeclaredMethods()) {

        if (!isHandler(method)) {

            continue;
        }//from  w ww. ja  v a2 s  .c  om

        final List<Annotation> handlerHttpMethodsAnnotations = new ArrayList<>();
        handlerHttpMethodsAnnotations.addAll(httpMethodsAnnotations);

        String consumes = method.isAnnotationPresent(Consumes.class)
                ? method.getAnnotation(Consumes.class).value()
                : StringUtils.EMPTY;
        String produces = method.isAnnotationPresent(Produces.class)
                ? method.getAnnotation(Produces.class).value()
                : StringUtils.EMPTY;
        Class<? extends Handler<RoutingContext>> routeHandlerClass = transporter.getRouteHandlerClass();
        List<org.jspare.forvertx.web.handler.BodyEndHandler> bodyEndHandler = collectBodyEndHandlers(
                transporter, method);
        boolean hasMethodAuth = false;
        boolean skipAuthorities = false;
        String autority = StringUtils.EMPTY;
        HandlerDocumentation hDocumentation = null;

        if (method.isAnnotationPresent(Documentation.class)) {
            Documentation documentation = method.getAnnotation(Documentation.class);
            hDocumentation = new HandlerDocumentation();
            hDocumentation.description(documentation.description());
            hDocumentation.status(Arrays.asList(documentation.responseStatus()).stream()
                    .map(s -> new HandlerDocumentation.ResponseStatus(s)).collect(Collectors.toList()));
            hDocumentation.queryParameters(Arrays.asList(documentation.queryParameters()).stream()
                    .map(q -> new HandlerDocumentation.QueryParameter(q)).collect(Collectors.toList()));
            hDocumentation.requestSchema(documentation.requestClass());
            hDocumentation.responseSchema(documentation.responseClass());
        }

        if (!method.isAnnotationPresent(IgnoreAuth.class)) {
            if (method.isAnnotationPresent(Auth.class)) {
                Auth auth = method.getAnnotation(Auth.class);
                hasMethodAuth = true;
                skipAuthorities = auth.skipAuthorities();
                autority = StringUtils.defaultIfEmpty(auth.value(), StringUtils.EMPTY);
            } else {
                if (hasAuthClass) {
                    hasMethodAuth = true;
                    skipAuthorities = authClass.skipAuthorities();
                    autority = StringUtils.defaultIfEmpty(authClass.value(), StringUtils.EMPTY);
                }
            }
        }

        HandlerData defaultHandlerData = new HandlerData().clazz(clazz).method(method).consumes(consumes)
                .produces(produces).bodyEndHandler(bodyEndHandler).auth(hasMethodAuth)
                .skipAuthorities(skipAuthorities).autority(autority).authProvider(transporter.getAuthProvider())
                .routeHandler(routeHandlerClass).documentation(hDocumentation);

        if (hasHttpMethodsPresents(method)) {

            handlerHttpMethodsAnnotations.clear();
            handlerHttpMethodsAnnotations.addAll(getHttpMethodsPresents(method));
        }

        getHandlersPresents(method).forEach(handlerType -> {

            try {

                // Extract order from Handler, all Hanlder having order()
                // method
                int order = annotationMethod(handlerType, "order");

                HandlerData handlerData = (HandlerData) defaultHandlerData.clone();
                handlerData.order(order);

                if (isHandlerAnnotation(handlerType, org.jspare.forvertx.web.mapping.handlers.Handler.class)) {

                    handlerData.handlerType(HandlerType.HANDLER);
                } else if (isHandlerAnnotation(handlerType, FailureHandler.class)) {

                    handlerData.handlerType(HandlerType.HANDLER);
                } else if (isHandlerAnnotation(handlerType, BlockingHandler.class)) {

                    handlerData.handlerType(HandlerType.BLOCKING_HANDLER);
                }

                if (handlerHttpMethodsAnnotations.isEmpty()) {

                    collectedHandlers.add(handlerData);
                } else {

                    collectedHandlers.addAll(collectByMethods(handlerData, handlerHttpMethodsAnnotations));
                }

            } catch (Exception e) {

                log.warn("Ignoring handler class {} method {} - {}", clazz.getName(), method.getName(),
                        e.getMessage());
            }
        });
    }
    return collectedHandlers;
}

From source file:com.zc.util.refelect.Reflector.java

/**
 * ?//from w ww.java  2s  . c om
 *
 * @param targetClass
 *            Class
 * @param annotationClass
 *            Class
 *
 * @return Object
 */
public static <T extends Annotation> T getAnnotation(Class targetClass, Class annotationClass) {
    if (targetClass.isAnnotationPresent(annotationClass)) {
        return (T) targetClass.getAnnotation(annotationClass);
    }

    return null;
}

From source file:com.jedi.oracle.OracleTypeUtils.java

private static void findCustomTypesRecursive(List<Field> fields, Map<String, Class<?>> map) {
    for (Field field : fields) {
        Class fieldType = field.getType();
        if (List.class.isAssignableFrom(fieldType)) {
            ParameterizedType listType = (ParameterizedType) field.getGenericType();
            fieldType = (Class<?>) listType.getActualTypeArguments()[0];
        }/*  w  w  w  .  ja v  a  2  s  .co  m*/

        if (fieldType.isAnnotationPresent(CustomTypeMapping.class)) {
            CustomTypeMapping mapping = (CustomTypeMapping) fieldType.getAnnotation(CustomTypeMapping.class);
            map.put(mapping.name(), fieldType);
            List<Field> oracleObjectFields = FieldUtils.getFieldsListWithAnnotation(fieldType,
                    OracleObjectMapping.class);
            if (oracleObjectFields != null && !oracleObjectFields.isEmpty()) {
                findCustomTypesRecursive(oracleObjectFields, map);
            }
        }
    }
}

From source file:xiaofei.library.hermes.util.TypeUtils.java

public static void validateClass(Class<?> clazz) {
    if (clazz == null) {
        throw new IllegalArgumentException("Class object is null.");
    }/*ww w .j av a2s.c  o  m*/
    if (clazz.isPrimitive() || clazz.isInterface()) {
        return;
    }
    if (clazz.isAnnotationPresent(WithinProcess.class)) {
        throw new IllegalArgumentException("Error occurs when registering class " + clazz.getName()
                + ". Class with a WithinProcess annotation presented on it cannot be accessed"
                + " from outside the process.");
    }

    if (clazz.isAnonymousClass()) {
        throw new IllegalArgumentException("Error occurs when registering class " + clazz.getName()
                + ". Anonymous class cannot be accessed from outside the process.");
    }
    if (clazz.isLocalClass()) {
        throw new IllegalArgumentException("Error occurs when registering class " + clazz.getName()
                + ". Local class cannot be accessed from outside the process.");
    }
    if (Context.class.isAssignableFrom(clazz)) {
        return;
    }
    if (Modifier.isAbstract(clazz.getModifiers())) {
        throw new IllegalArgumentException("Error occurs when registering class " + clazz.getName()
                + ". Abstract class cannot be accessed from outside the process.");
    }
}

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

/**
 * Injects all annotated field views. Note, that this can run recursively, so it will check all
 * fields for {@link com.wit.android.support.fragment.annotation.InjectView @InjectView} or
 * {@link com.wit.android.support.fragment.annotation.InjectView.Last @InjectView.Last}
 * annotation presented above each of fields of the given <var>classOfRootContext</var>.
 *
 * @param rootContext        A context in which is the passed <var>root</var> view presented and
 *                           into which should be views injected.
 * @param classOfRootContext A class of a context for the current recursive iteration.
 * @param root               A root view of the given context.
 * @param maxSuperClass      If {@code not null}, this method will be called (recursively)
 *                           for all super classes of the given class (max to the specified
 *                           <var>maxSuperClass</var>), otherwise only fields of the given class
 *                           will be iterated.
 * @param onClickListener    An instance of OnClickListener which should be set to injected views
 *                           if {@link com.wit.android.support.fragment.annotation.InjectView#clickable() @InjectView.clickable()}
 *                           flag is set to {@code true}.
 *///from  w ww.  ja  va  2 s.com
private static void injectViews(Object rootContext, Class<?> classOfRootContext, View root,
        Class<?> maxSuperClass, View.OnClickListener onClickListener) {
    // Class of fragment must have @InjectViews annotation present to really iterate and inject
    // annotated views.
    if (classOfRootContext.isAnnotationPresent(InjectViews.class)) {
        // Process annotated fields.
        final Field[] fields = classOfRootContext.getDeclaredFields();
        if (fields.length > 0) {
            for (Field field : fields) {
                injectViewInner(field, rootContext, root, onClickListener);
                if (field.isAnnotationPresent(InjectView.Last.class)) {
                    break;
                }
            }
        }
    }

    // Inject also views of supper class, but only to this BaseFragment super.
    final Class<?> superOfRootContext = classOfRootContext.getSuperclass();
    if (superOfRootContext != null && !superOfRootContext.equals(maxSuperClass)) {
        injectViews(rootContext, superOfRootContext, root, maxSuperClass, onClickListener);
    }
}

From source file:net.sf.firemox.xml.XmlConfiguration.java

/**
 * @param mdbClass/*from w  w  w .  ja v a  2  s.  co m*/
 * @throws IllegalAccessException
 * @throws InstantiationException
 * @throws ClassNotFoundException
 */
private static XmlToMDB getAnnotedBuilder(Class<?> mdbClass, String tagName, String packageName,
        String namespace) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
    if (!mdbClass.isAnnotationPresent(XmlTestElement.class)) {
        error("Unsupported annoted " + namespace + " '" + tagName + "'");
        return DummyBuilder.instance();
    }
    XmlTestElement xmlElement = mdbClass.getAnnotation(XmlTestElement.class);
    XmlAnnoted annotedInstance = (XmlAnnoted) Class.forName(packageName + "." + namespace + ".XmlAnnoted")
            .newInstance();
    annotedInstance.setXmlElement(xmlElement);
    annotedInstance.setAnnotedClass(mdbClass);
    return annotedInstance;
}