Example usage for java.lang Class getAnnotation

List of usage examples for java.lang Class getAnnotation

Introduction

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

Prototype

@SuppressWarnings("unchecked")
public <A extends Annotation> A getAnnotation(Class<A> annotationClass) 

Source Link

Usage

From source file:de.hasait.clap.impl.CLAPClassNode.java

private static <A extends Annotation> A findAnnotation(final Class<?> pClass, final Class<A> pAnnotationClass) {
    final LinkedList<Class<?>> queue = new LinkedList<Class<?>>();
    queue.add(pClass);/*w  ww  . j a v  a 2  s.  c o  m*/
    while (!queue.isEmpty()) {
        final Class<?> clazz = queue.removeFirst();
        if (clazz != null) {
            final A result = clazz.getAnnotation(pAnnotationClass);
            if (result != null) {
                return result;
            }
            queue.add(clazz.getSuperclass());
            for (final Class<?> interfaze : clazz.getInterfaces()) {
                queue.add(interfaze);
            }
        }
    }
    return null;
}

From source file:unquietcode.tools.beanmachine.AnnotationUtils.java

/**
 * Find a single {@link java.lang.annotation.Annotation} of <code>annotationType</code> from the supplied {@link Class},
 * traversing its interfaces and superclasses if no annotation can be found on the given class itself.
 * <p>This method explicitly handles class-level annotations which are not declared as
 * {@link java.lang.annotation.Inherited inherited} <i>as well as annotations on interfaces</i>.
 * <p>The algorithm operates as follows: Searches for an annotation on the given class and returns
 * it if found. Else searches all interfaces that the given class declares, returning the annotation
 * from the first matching candidate, if any. Else proceeds with introspection of the superclass
 * of the given class, checking the superclass itself; if no annotation found there, proceeds
 * with the interfaces that the superclass declares. Recursing up through the entire superclass
 * hierarchy if no match is found.//w  ww.jav a  2  s. co m
 * @param clazz the class to look for annotations on
 * @param annotationType the annotation class to look for
 * @return the annotation found, or <code>null</code> if none found
 */
public static <A extends Annotation> A findAnnotation(Class<?> clazz, Class<A> annotationType) {
    Assert.notNull(clazz, "Class must not be null");
    A annotation = clazz.getAnnotation(annotationType);
    if (annotation != null) {
        return annotation;
    }
    for (Class<?> ifc : clazz.getInterfaces()) {
        annotation = findAnnotation(ifc, annotationType);
        if (annotation != null) {
            return annotation;
        }
    }
    if (!Annotation.class.isAssignableFrom(clazz)) {
        for (Annotation ann : clazz.getAnnotations()) {
            annotation = findAnnotation(ann.annotationType(), annotationType);
            if (annotation != null) {
                return annotation;
            }
        }
    }
    Class<?> superClass = clazz.getSuperclass();
    if (superClass == null || superClass == Object.class) {
        return null;
    }
    return findAnnotation(superClass, annotationType);
}

From source file:ch.rasc.extclassgenerator.association.AbstractAssociation.java

protected static String getModelName(Class<?> model) {
    Model modelAnnotation = model.getAnnotation(Model.class);

    if (modelAnnotation != null && StringUtils.hasText(modelAnnotation.value())) {
        return modelAnnotation.value();
    }/* w w w .j  a  v a  2 s. c o m*/
    return model.getName();
}

From source file:com.btmatthews.selenium.junit4.runner.SeleniumJUnit4ClassRunner.java

/**
 * Build the test runners for each browser. The test class must have been
 * annotated with {@link ServerConfiguration} or
 * {@link WebDriverConfiguration} to provide the configuration.
 *
 * @param klass The test class.//from   w  w w  .ja  va2  s.  c  o m
 * @return A list of {@link Runner} objects.
 * @throws InitializationError If there was an error initialising the test runners.
 */
private static List<Runner> buildRunners(final Class<?> klass) throws InitializationError {
    final List<Runner> runners;
    final ServerConfiguration seleniumServerConfiguration = klass.getAnnotation(ServerConfiguration.class);
    final WebDriverConfiguration webDriverConfiguration = klass.getAnnotation(WebDriverConfiguration.class);
    final WrappedDriverConfiguration wrappedDriverConfiguration = klass
            .getAnnotation(WrappedDriverConfiguration.class);
    if (seleniumServerConfiguration != null) {
        runners = buildSeleniumServerRunners(seleniumServerConfiguration, klass);
    } else if (webDriverConfiguration != null) {
        runners = buildWebDriverRunners(webDriverConfiguration, klass);
    } else if (wrappedDriverConfiguration != null) {
        runners = buildWrappedDriverRunners(wrappedDriverConfiguration, klass);
    } else {
        throw new InitializationError(
                "Annotate test class with either ServerConfiguration, WebDriverConfiguration or WrappedDriverConfiguration");
    }
    return runners;
}

From source file:it.jnrpe.utils.PluginRepositoryUtil.java

@SuppressWarnings({ "rawtypes", "unchecked" })
private static PluginDefinition loadFromPluginAnnotation(final Class clazz,
        final IPluginInterface pluginInstance) {
    Plugin plugin = (Plugin) clazz.getAnnotation(Plugin.class);
    PluginOptions options = (PluginOptions) clazz.getAnnotation(PluginOptions.class);
    String name = plugin.name();//from w  ww.ja  v a  2 s  . c  o  m
    String description = plugin.description();
    PluginDefinition def;

    if (pluginInstance == null) {
        def = new PluginDefinition(name, description, clazz);
    } else {
        def = new PluginDefinition(name, description, pluginInstance);
    }
    for (Option option : options.value()) {
        def.addOption(parsePluginOption(option));
    }
    return def;
}

From source file:com.eviware.x.form.support.ADialogBuilder.java

public static XFormDialog buildTabbedDialog(Class<? extends Object> tabbedFormClass, ActionList actions) {
    AForm formAnnotation = tabbedFormClass.getAnnotation(AForm.class);
    if (formAnnotation == null) {
        throw new RuntimeException("formClass is not annotated correctly..");
    }/*from  ww  w.j av a  2s. co  m*/

    MessageSupport messages = MessageSupport.getMessages(tabbedFormClass);
    XFormDialogBuilder builder = XFormFactory.createDialogBuilder(formAnnotation.name());

    for (Field field : tabbedFormClass.getFields()) {
        APage pageAnnotation = field.getAnnotation(APage.class);
        if (pageAnnotation != null) {
            buildForm(builder, pageAnnotation.name(), field.getType(), messages);
        }

        AField fieldAnnotation = field.getAnnotation(AField.class);
        if (fieldAnnotation != null) {
            try {
                Class<?> formClass = Class.forName(fieldAnnotation.description());
                buildForm(builder, fieldAnnotation.name(), formClass, messages);
            } catch (Exception e) {
                SoapUI.logError(e);
            }
        }
    }

    ActionList defaultActions = StringUtils.isBlank(formAnnotation.helpUrl()) ? builder.buildOkCancelActions()
            : builder.buildOkCancelHelpActions(formAnnotation.helpUrl());

    if (actions == null) {
        actions = defaultActions;
    } else {
        actions.addActions(defaultActions);
    }

    XFormDialog dialog = builder.buildDialog(actions, formAnnotation.description(),
            UISupport.createImageIcon(formAnnotation.icon()));

    return dialog;
}

From source file:de.micromata.genome.tpsb.htmlunit.HtmlPageBase.java

public static String getPageName(Class<? extends HtmlPageBase> clazz) {
    HtmlPageUrl an = clazz.getAnnotation(HtmlPageUrl.class);
    if (an == null)
        throw new RuntimeException("Class missing HtmlPageUrl annotation: " + clazz.getSimpleName());
    return an.value();
}

From source file:com.predic8.membrane.annot.bean.MCUtil.java

@SuppressWarnings("unchecked")
public static <T> T clone(T object, boolean deep) {
    try {/*from   w w w  .  j a  v  a2s.c  om*/
        if (object == null)
            throw new InvalidParameterException("'object' must not be null.");

        Class<? extends Object> clazz = object.getClass();

        MCElement e = clazz.getAnnotation(MCElement.class);
        if (e == null)
            throw new IllegalArgumentException("'object' must be @MCElement-annotated.");

        BeanWrapperImpl dst = new BeanWrapperImpl(clazz);
        BeanWrapperImpl src = new BeanWrapperImpl(object);

        for (Method m : clazz.getMethods()) {
            if (!m.getName().startsWith("set"))
                continue;
            String propertyName = AnnotUtils.dejavaify(m.getName().substring(3));
            MCAttribute a = m.getAnnotation(MCAttribute.class);
            if (a != null) {
                dst.setPropertyValue(propertyName, src.getPropertyValue(propertyName));
            }
            MCChildElement c = m.getAnnotation(MCChildElement.class);
            if (c != null) {
                if (deep) {
                    dst.setPropertyValue(propertyName, cloneInternal(src.getPropertyValue(propertyName), deep));
                } else {
                    dst.setPropertyValue(propertyName, src.getPropertyValue(propertyName));
                }
            }
            MCOtherAttributes o = m.getAnnotation(MCOtherAttributes.class);
            if (o != null) {
                dst.setPropertyValue(propertyName, src.getPropertyValue(propertyName));
            }
            MCTextContent t = m.getAnnotation(MCTextContent.class);
            if (t != null) {
                dst.setPropertyValue(propertyName, src.getPropertyValue(propertyName));
            }
        }

        return (T) dst.getRootInstance();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.eviware.x.form.support.ADialogBuilder.java

public static XFormDialog buildTabbedDialogWithCustomActions(Class<? extends Object> tabbedFormClass,
        ActionList actions) {/*w  w  w  .j  a  v a 2 s .  co m*/
    AForm formAnnotation = tabbedFormClass.getAnnotation(AForm.class);
    if (formAnnotation == null) {
        throw new RuntimeException("formClass is not annotated correctly..");
    }

    MessageSupport messages = MessageSupport.getMessages(tabbedFormClass);
    XFormDialogBuilder builder = XFormFactory.createDialogBuilder(formAnnotation.name());

    for (Field field : tabbedFormClass.getFields()) {
        APage pageAnnotation = field.getAnnotation(APage.class);
        if (pageAnnotation != null) {
            buildForm(builder, pageAnnotation.name(), field.getType(), messages);
        }

        AField fieldAnnotation = field.getAnnotation(AField.class);
        if (fieldAnnotation != null) {
            try {
                Class<?> formClass = Class.forName(fieldAnnotation.description());
                buildForm(builder, fieldAnnotation.name(), formClass, messages);
            } catch (Exception e) {
                SoapUI.logError(e);
            }
        }
    }

    ActionList defaultActions = StringUtils.isBlank(formAnnotation.helpUrl()) ? null
            : builder.buildHelpActions(formAnnotation.helpUrl());

    if (actions == null) {
        actions = defaultActions;
    } else {
        defaultActions.addActions(actions);
        actions = defaultActions;
    }

    XFormDialog dialog = builder.buildDialog(actions, formAnnotation.description(),
            UISupport.createImageIcon(formAnnotation.icon()));

    return dialog;
}

From source file:com.siberhus.ngai.core.CrudHelper.java

/**
 * //from   w ww.j  a  va 2s.  c  om
 * @param entityClass
 * @return
 */
private synchronized final static EntityInfo getEntityInfo(Class<?> entityClass) {

    EntityInfo entityInfo = ENTITY_INFO_CACHE.get(entityClass);
    if (entityInfo != null) {
        return entityInfo;
    }
    entityInfo = new EntityInfo();
    Entity entityAnnot = (Entity) entityClass.getAnnotation(Entity.class);
    if (!"".equals(entityAnnot.name())) {
        entityInfo.setEntityName(entityAnnot.name());
    } else {
        entityInfo.setEntityName(entityClass.getSimpleName());
    }
    Collection<Field> entityFields = ReflectUtil.getFields(entityClass);
    for (Field field : entityFields) {
        if (Modifier.isStatic(field.getModifiers())) {
            continue;
        }
        if (field.getName().equals("id")) {
            continue;
        }
        if (!field.isAccessible()) {
            field.setAccessible(true);
        }
        entityInfo.getFieldSet().add(field);
    }
    ENTITY_INFO_CACHE.put(entityClass, entityInfo);
    return entityInfo;
}