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.impetus.kundera.metadata.MetadataBuilder.java

/**
 * Add named/native query annotated fields to application meta data.
 * /*from   ww w  . j  ava  2s.  c  o  m*/
 * @param clazz
 *            entity class.
 */
private void addNamedNativeQueryMetadata(Class clazz) {
    ApplicationMetadata appMetadata = kunderaMetadata.getApplicationMetadata();
    String name, query = null;
    if (clazz.isAnnotationPresent(NamedQuery.class)) {
        NamedQuery ann = (NamedQuery) clazz.getAnnotation(NamedQuery.class);
        appMetadata.addQueryToCollection(ann.name(), ann.query(), false, clazz);
    }

    if (clazz.isAnnotationPresent(NamedQueries.class)) {
        NamedQueries ann = (NamedQueries) clazz.getAnnotation(NamedQueries.class);

        NamedQuery[] anns = ann.value();
        for (NamedQuery a : anns) {
            appMetadata.addQueryToCollection(a.name(), a.query(), false, clazz);
        }
    }

    if (clazz.isAnnotationPresent(NamedNativeQuery.class)) {
        NamedNativeQuery ann = (NamedNativeQuery) clazz.getAnnotation(NamedNativeQuery.class);
        appMetadata.addQueryToCollection(ann.name(), ann.query(), true, clazz);
    }

    if (clazz.isAnnotationPresent(NamedNativeQueries.class)) {
        NamedNativeQueries ann = (NamedNativeQueries) clazz.getAnnotation(NamedNativeQueries.class);

        NamedNativeQuery[] anns = ann.value();
        for (NamedNativeQuery a : anns) {
            appMetadata.addQueryToCollection(a.name(), a.query(), true, clazz);
        }
    }
}

From source file:de.liedtke.format.JSONFormatter.java

@Override
public <T extends BasicEntity> T reformat(final String object, final Class<T> entityClass)
        throws FormatException {
    T result = null;/*from  ww  w .  j  a v a  2s  .c o  m*/
    final Map<String, Field> fieldMap = new HashMap<String, Field>();
    if (entityClass.isAnnotationPresent(Entity.class)) {
        final Field[] fields = entityClass.getDeclaredFields();
        for (final Field field : fields) {
            if (field.isAnnotationPresent(FormatPart.class)) {
                fieldMap.put(field.getAnnotation(FormatPart.class).key(), field);
            }
        }
        try {
            final JSONObject json = new JSONObject(object);
            result = entityClass.newInstance();
            for (final String key : fieldMap.keySet()) {
                if (json.has(key)) {
                    final Field field = fieldMap.get(key);
                    final Method method = entityClass.getMethod(this.getSetter(field.getName()),
                            new Class<?>[] { field.getType() });
                    if (FindInterface.class.isAssignableFrom(field.getType())) {
                        final Method find = field.getType().getMethod("find", new Class<?>[] { String.class });
                        if (find != null) {
                            final Object enumObject = find.invoke(Enum.class, json.get(key));
                            method.invoke(result, enumObject);
                        }
                    } else {
                        final String type = field.getType().toString();
                        if (type.equals("class com.google.appengine.api.datastore.Key")) {
                            method.invoke(result, KeyFactory.stringToKey(json.getString(key)));
                        } else if (type.equals("class com.google.appengine.api.datastore.Text")) {
                            method.invoke(result, new Text(json.getString(key)));
                        } else if (type.equals("boolean")) {
                            method.invoke(result, json.getBoolean(key));
                        } else if (type.equals("long")) {
                            method.invoke(result, json.getLong(key));
                        } else if (type.equals("int")) {
                            method.invoke(result, json.getInt(key));
                        } else {
                            method.invoke(result, json.get(key));
                        }
                    }
                }
            }
        } catch (JSONException e) {
            logger.warning("JSONException occured: " + e.getMessage());
            throw new FormatException();
        } catch (NoSuchMethodException e) {
            logger.warning("NoSuchMethodException occured: " + e.getMessage());
            throw new FormatException();
        } catch (SecurityException e) {
            logger.warning("SecurityException occured: " + e.getMessage());
            throw new FormatException();
        } catch (IllegalAccessException e) {
            logger.warning("IllegalAccessException occured: " + e.getMessage());
            throw new FormatException();
        } catch (IllegalArgumentException e) {
            logger.warning("IllegalArgumentException occured: " + e.getMessage());
            throw new FormatException();
        } catch (InvocationTargetException e) {
            logger.warning("InvocationTargetException occured: " + e.getMessage());
            throw new FormatException();
        } catch (InstantiationException e) {
            logger.warning("InstantiationException occured: " + e.getMessage());
            throw new FormatException();
        }
    }
    return result;
}

From source file:com.github.gekoh.yagen.hst.CreateEntities.java

private Class getTableEntityClass(Class baseClass) {
    while (baseClass != null && !baseClass.isAnnotationPresent(Table.class)) {
        baseClass = baseClass.getSuperclass();
    }//from ww w.ja  va2s  .co m
    return baseClass;
}

From source file:org.keycloak.testsuite.arquillian.CrossDCTestEnricher.java

private Class getNearestSuperclassWithAnnotation(Class<?> testClass, Class annotationClass) {
    return (testClass.isAnnotationPresent(annotationClass)) ? testClass
            : (testClass.getSuperclass().equals(Object.class) ? null // stop recursion
                    : getNearestSuperclassWithAnnotation(testClass.getSuperclass(), annotationClass)); // continue recursion
}

From source file:org.cleverbus.test.AbstractTest.java

/**
 * Initializes selected routes for specific test.
 * Active route definitions are defined via {@link ActiveRoutes} annotation.
 *
 * @throws Exception when init fails//ww w  .j  av a  2s. c o  m
 */
@Before
public void initRoutes() throws Exception {
    getCamelContext().getShutdownStrategy().setTimeout(1);// no shutdown timeout:
    getCamelContext().getShutdownStrategy().setTimeUnit(TimeUnit.NANOSECONDS);
    getCamelContext().getShutdownStrategy().setShutdownNowOnTimeout(true);// no pending exchanges

    Map<String, Class<RoutesBuilder>> beans = new HashMap<String, Class<RoutesBuilder>>();

    String[] beanNames = getApplicationContext().getBeanDefinitionNames();

    for (String beanName : beanNames) {
        Class beanClass = getApplicationContext().getType(beanName);

        if (beanClass.isAnnotationPresent(CamelConfiguration.class)) {
            beans.put(beanName, beanClass);
        }
    }

    Set<Class> activeRoutesClasses = getActiveRoutes();

    for (Map.Entry<String, Class<RoutesBuilder>> entry : beans.entrySet()) {

        for (Class routesClass : activeRoutesClasses) {

            if (entry.getValue().isAssignableFrom(routesClass)) {
                getCamelContext().addRoutes((RoutesBuilder) getApplicationContext().getBean(entry.getKey()));
            }
        }
    }
}

From source file:org.castor.cpa.jpa.info.JPACallbackHandler.java

/**
 * Invokes listener callbacks accordingly.
 * //from   w  w w . ja v a 2  s  .  co m
 * @param annotationClass
 *            the annotation to look for
 * @param klass
 *            the class to handle listeners
 * @param <A>
 *            helper annotation generics
 * @throws InvocationTargetException
 *             on callback invocation error
 * @throws IllegalAccessException
 *             on illegal method access error
 * @throws InstantiationException
 *             on instantiation error
 */
private <A extends Annotation> void invokeListenerCallbacksFor(final Class<A> annotationClass,
        final Class<?> klass) throws InvocationTargetException, IllegalAccessException, InstantiationException {
    if (!klass.isAnnotationPresent(ExcludeDefaultListeners.class)) {
        final EntityListeners entityListeners = klass.getAnnotation(EntityListeners.class);
        if (entityListeners != null) {
            final Class<?>[] listeners = entityListeners.value();
            for (Class<?> listener : listeners) {
                invokeCallbacksFor(annotationClass, listener.newInstance());
            }
        }
    }
}

From source file:org.broadinstitute.gatk.tools.walkers.help.WalkerDocumentationHandler.java

/**
 * Utility function that determines the partition type of an instance of class c.
 *
 * @param myClass the class to query for the annotation
 * @return the partition type if applicable, otherwise an empty string
 *//*  w ww  .j ava  2s .  co  m*/
private String getPartitionType(Class myClass) {
    //
    // Retrieve annotation
    if (myClass.isAnnotationPresent(PartitionBy.class)) {
        final Annotation thisAnnotation = myClass.getAnnotation(PartitionBy.class);
        if (thisAnnotation instanceof PartitionBy) {
            final PartitionBy partAnnotation = (PartitionBy) thisAnnotation;
            return partAnnotation.value().toString();
        }
    }
    return "";
}

From source file:com.googlecode.wicketelements.security.AnnotationSecurityCheck.java

public final boolean isSignInPage(final Class<? extends Page> pageClassParam) {
    PARAM_REQ.Object.requireNotNull(pageClassParam, "Sign in page parameter must not be null.");
    return pageClassParam.equals(signInPage()) || pageClassParam.isAnnotationPresent(PageWithSignIn.class);
}

From source file:org.apache.usergrid.chop.runner.drivers.ResultsLog.java

/**
 * Sets up the JSON preamble to start streaming results into the entity. This must be
 * protected via isOpen. This is an unsafe call, make sure you know how it is used.
 *//*ww  w  .ja  v a 2s .  c  o m*/
private void setupJsonStream() throws IOException {
    Class<?> testClass = tracker.getTestClass();
    jgen.writeStartObject();
    jgen.writeStringField("testClass", testClass.getCanonicalName());
    jgen.writeNumberField("startTime", tracker.getStartTime());

    if (testClass.isAnnotationPresent(TimeChop.class)) {
        jgen.writeStringField("chopType", "TimeChop");
        jgen.writeObjectField("chopParameters", testClass.getAnnotation(TimeChop.class));
    } else if (tracker.getTestClass().isAnnotationPresent(IterationChop.class)) {
        jgen.writeStringField("chopType", "IterationChop");
        jgen.writeObjectField("chopParameters", testClass.getAnnotation(IterationChop.class));
    } else {
        throw new IllegalStateException(
                "Supplied testClass " + testClass.getCanonicalName() + "has no chop annotation.");
    }

    jgen.writeFieldName("runResults");
    jgen.writeStartArray();
    jgen.flush();
}

From source file:org.b3log.latke.servlet.handler.AdviceHandler.java

/**
 * get BeforeRequestProcessAdvice from annotation.
 *
 * @param invokeHolder the real invoked method
 * @param processorClass the class of the invoked methond
 * @return the list of BeforeRequestProcessAdvice
 *///from   w w  w. j  a v  a  2 s  . c o m
private List<Class<? extends BeforeRequestProcessAdvice>> getBeforeList(final Method invokeHolder,
        final Class<?> processorClass) {
    // before invoke(first class before advice and then method before advice).
    final List<Class<? extends BeforeRequestProcessAdvice>> beforeAdviceClassList = new ArrayList<Class<? extends BeforeRequestProcessAdvice>>();

    if (processorClass.isAnnotationPresent(Before.class)) {
        final Class<? extends BeforeRequestProcessAdvice>[] ac = processorClass.getAnnotation(Before.class)
                .adviceClass();

        beforeAdviceClassList.addAll(Arrays.asList(ac));
    }
    if (invokeHolder.isAnnotationPresent(Before.class)) {
        final Class<? extends BeforeRequestProcessAdvice>[] ac = invokeHolder.getAnnotation(Before.class)
                .adviceClass();

        beforeAdviceClassList.addAll(Arrays.asList(ac));
    }

    return beforeAdviceClassList;
}