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.taimos.dvalin.interconnect.core.spring.test.InterconnectRequestMock.java

private static String getQueueName(final Class<?>[] interfaces) {
    for (final Class<?> iface : interfaces) {
        if (iface.isAnnotationPresent(Daemon.class)) {
            return iface.getAnnotation(Daemon.class).name() + ".request";
        }/*from  www .  j  a va 2  s  .  co  m*/
    }
    throw new RuntimeException("No @Daemon annotation found");
}

From source file:com.hpcloud.util.Serialization.java

private static String rootNameFor(Class<?> type) {
    JsonRootName rootName = type.getAnnotation(JsonRootName.class);
    return rootName == null ? type.getSimpleName() : rootName.value();
}

From source file:com.chiorichan.configuration.serialization.ConfigurationSerialization.java

/**
 * Gets the correct alias for the given {@link ConfigurationSerializable} class
 * //from  w  w  w.ja  v  a 2s. c o m
 * @param clazz
 *            Class to get alias for
 * @return Alias to use for the class
 */
public static String getAlias(Class<? extends ConfigurationSerializable> clazz) {
    DelegateDeserialization delegate = clazz.getAnnotation(DelegateDeserialization.class);

    if (delegate != null) {
        if ((delegate.value() == null) || (delegate.value() == clazz)) {
            delegate = null;
        } else {
            return getAlias(delegate.value());
        }
    }

    if (delegate == null) {
        SerializableAs alias = clazz.getAnnotation(SerializableAs.class);

        if ((alias != null) && (alias.value() != null)) {
            return alias.value();
        }
    }

    return clazz.getName();
}

From source file:com.codelanx.codelanxlib.util.Reflections.java

/**
 * Returns the relevant {@link JavaPlugin} that is specified by a
 * class-level {@link PluginClass} annotation if it is loaded, otherwise
 * {@code null}//w ww  .j  a v a2s .c o  m
 * 
 * @since 0.1.0
 * @version 0.1.0
 * 
 * @param clazz The {@link Class} that holds the {@link Annotation}
 * @return The relevant {@link JavaPlugin}, or {@code null} if not found
 */
public static JavaPlugin getPlugin(Class<?> clazz) {
    PluginClass pc = clazz.getAnnotation(PluginClass.class);
    if (pc == null) {
        return null;
    }
    return JavaPlugin.getPlugin(pc.value());
}

From source file:com.flipkart.foxtrot.core.TestUtils.java

public static void registerActions(AnalyticsLoader analyticsLoader, ObjectMapper mapper) throws Exception {
    Reflections reflections = new Reflections("com.flipkart.foxtrot", new SubTypesScanner());
    Set<Class<? extends Action>> actions = reflections.getSubTypesOf(Action.class);
    if (actions.isEmpty()) {
        throw new Exception("No analytics actions found!!");
    }/*from  ww  w.  ja  va  2s.c o  m*/
    List<NamedType> types = new Vector<NamedType>();
    for (Class<? extends Action> action : actions) {
        AnalyticsProvider analyticsProvider = action.getAnnotation(AnalyticsProvider.class);
        if (null == analyticsProvider.request() || null == analyticsProvider.opcode()
                || analyticsProvider.opcode().isEmpty() || null == analyticsProvider.response()) {
            throw new Exception("Invalid annotation on " + action.getCanonicalName());
        }
        if (analyticsProvider.opcode().equalsIgnoreCase("default")) {
            logger.warn("Action " + action.getCanonicalName() + " does not specify cache token. "
                    + "Using default cache.");
        }
        analyticsLoader.register(new ActionMetadata(analyticsProvider.request(), action,
                analyticsProvider.cacheable(), analyticsProvider.opcode()));
        types.add(new NamedType(analyticsProvider.request(), analyticsProvider.opcode()));
        types.add(new NamedType(analyticsProvider.response(), analyticsProvider.opcode()));
        logger.info("Registered action: " + action.getCanonicalName());
    }
    mapper.getSubtypeResolver().registerSubtypes(types.toArray(new NamedType[types.size()]));
}

From source file:gr.abiss.calipso.tiers.util.ModelContext.java

public static Map<String, Object> getApiAnnotationMembers(Class<?> domainClass) {
    ModelResource resource = domainClass.getAnnotation(ModelResource.class);
    Map<String, Object> apiAnnotationMembers = new HashMap<String, Object>();
    if (resource != null) {
        // get tags (grouping key, try API name)
        if (StringUtils.isNotBlank(resource.apiName())) {
            String[] tags = { resource.apiName() };
            apiAnnotationMembers.put("tags", tags);
        }/*from   w w  w  . jav a2 s  . co  m*/
        // or path
        else if (StringUtils.isNotBlank(resource.path())) {

            String[] tags = { resource.path() };
            apiAnnotationMembers.put("tags", tags);
        }
        // or simple name
        else {
            String[] tags = { StringUtils
                    .join(StringUtils.splitByCharacterTypeCamelCase(domainClass.getSimpleName()), ' ') };
            apiAnnotationMembers.put("tags", tags);
        }
        // add description
        if (StringUtils.isNotBlank(resource.apiDescription())) {
            apiAnnotationMembers.put("description", resource.apiDescription());
        }
    } else {
        throw new IllegalStateException("Not an entity");
    }

    return apiAnnotationMembers.size() > 0 ? apiAnnotationMembers : null;
}

From source file:org.synyx.hades.util.ClassUtils.java

/**
 * Returns the name ot the entity represented by this class. Used to build
 * queries for that class./* w  w  w  . j av a2  s  .  c  o m*/
 * 
 * @param domainClass
 * @return
 */
public static String getEntityName(Class<?> domainClass) {

    Entity entity = domainClass.getAnnotation(Entity.class);
    boolean hasName = null != entity && StringUtils.hasText(entity.name());

    return hasName ? entity.name() : domainClass.getSimpleName();
}

From source file:com.streamsets.datacollector.definition.StageDefinitionExtractor.java

private static void addGroupsToList(Class<?> klass, Set<String> set) {
    ConfigGroups groups = klass.getAnnotation(ConfigGroups.class);
    if (groups != null) {
        Class<? extends Enum> groupKlass = (Class<? extends Enum>) groups.value();
        for (Enum e : groupKlass.getEnumConstants()) {
            set.add(e.name());/*w  w  w. j  a  v a2s  . c o  m*/
        }
    }
}

From source file:com.meiah.core.util.ReflectionUtils.java

/**
 * ?/*from  w  ww . j a v a2 s  .c om*/
 * @param className className ??  com.meiah.app.entity.portal.Module
 * @return
 * @author "zhangshaofeng"  
 * @time Jun 10, 2012 2:44:51 PM
 */
@SuppressWarnings("unchecked")
public static String getClassDesc(String className) {
    try {
        Class model = Class.forName(className);
        if (model.isAnnotationPresent(Description.class)) {
            String description = model.getAnnotation(Description.class).toString();
            description = description.split("name=")[1];
            description = description.substring(0, description.lastIndexOf(")"));
            return description;
        } else {
            return className;
        }
    } catch (Exception e) {
        logger.error("?", e);
    }
    return null;
}

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

/**
 * Returns <code>true</code> if the class contains plugin annotations.
 * //from  w w  w  .j a va  2 s  .co m
 * @param clazz
 *            The plugin class
        
 * @return <code>true</code> if the class contains plugin */
@SuppressWarnings({ "rawtypes", "unchecked" })
private static boolean isAnnotated(final Class clazz) {
    Plugin plugin = (Plugin) clazz.getAnnotation(Plugin.class);
    return plugin != null;
}