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:com.manydesigns.portofino.pageactions.PageActionLogic.java

public static String getScriptTemplate(Class<?> actionClass) {
    if (!PageAction.class.isAssignableFrom(actionClass)) {
        return null;
    }/*ww  w .j  av  a2 s.  co m*/
    ScriptTemplate scriptTemplate = actionClass.getAnnotation(ScriptTemplate.class);
    if (scriptTemplate != null) {
        String templateLocation = scriptTemplate.value();
        try {
            return IOUtils.toString(actionClass.getResourceAsStream(templateLocation));
        } catch (Exception e) {
            throw new Error(
                    "Can't load script template: " + templateLocation + " for class: " + actionClass.getName(),
                    e);
        }
    } else {
        String template = getScriptTemplate(actionClass.getSuperclass());
        if (template != null) {
            return template;
        } else {
            try {
                InputStream stream = PageActionLogic.class.getResourceAsStream(
                        "/com/manydesigns/portofino/pageactions/default_script_template.txt");
                return IOUtils.toString(stream);
            } catch (Exception e) {
                throw new Error("Can't load script template", e);
            }
        }
    }
}

From source file:com.dianping.avatar.cache.util.CacheAnnotationUtils.java

/**
 * Retrieve the cache key values from entity instance
 *//*from w w w . j av  a2 s.co m*/
public static Object[] getCacheKeyValues(Object entity) {
    if (entity == null) {
        throw new IllegalArgumentException("Entity is null.");
    }

    Class<?> cz = entity.getClass();

    Cache cache = cz.getAnnotation(Cache.class);

    if (cache == null) {
        throw new SystemException("The entity must be annotated by Cache.");
    }

    Field[] fields = ClassUtils.getDeclaredFields(cz);

    final List<OrderedField> cacheFields = new ArrayList<OrderedField>();

    // Extract annotated fields
    for (int i = 0; i < fields.length; i++) {
        Field f = fields[i];
        CacheParam fCache = f.getAnnotation(CacheParam.class);
        if (fCache != null) {
            cacheFields.add(new OrderedField(f, i, fCache.order()));
        }
    }

    // Extract declared fields
    for (int i = 0; i < cache.fields().length; i++) {
        String fieldName = cache.fields()[i];
        if (fieldName.isEmpty()) {
            continue;
        }
        Field f = ReflectionUtils.findField(cz, fieldName);
        if (f == null) {
            throw new IllegalArgumentException(
                    "Invalid cahce parameter " + fieldName + ", the filed is not exists.");
        }

        cacheFields.add(new OrderedField(f, i, -Integer.MAX_VALUE + i));
    }

    Collections.sort(cacheFields);

    Object[] values = new Object[cacheFields.size()];

    for (int i = 0; i < cacheFields.size(); i++) {
        OrderedField oField = cacheFields.get(i);

        ReflectionUtils.makeAccessible((Field) oField.field);

        values[i] = ReflectionUtils.getField((Field) oField.field, entity);
    }

    return values;
}

From source file:bear.plugins.Plugin.java

public static String getName(Class<? extends Plugin> pluginClass) {
    bear.annotations.Plugin plugin = pluginClass.getAnnotation(bear.annotations.Plugin.class);

    if (plugin != null)
        return plugin.value();

    Shell shell = pluginClass.getAnnotation(Shell.class);

    if (shell != null)
        return shell.value();

    return shortenName(pluginClass.getName());
}

From source file:com.github.cherimojava.data.mongo.entity.EntityUtils.java

/**
 * retrieves the name for the Entity, which might be different from the clazz name if the @Named annotation is
 * present/*from  ww w.  j a v  a 2  s  .c  o  m*/
 *
 * @param clazz Entity from which to retrieve the collection name
 * @return the name of the Entity as declared with the Named annotation or the clazz name if annotation isn't
 *         present as plural
 */
public static String getCollectionName(Class<? extends Entity> clazz) {
    Named name = clazz.getAnnotation(Named.class);
    if (name != null && isNotEmpty(name.value())) {
        return name.value();
    }
    return uncapitalize(clazz.getSimpleName() + "s");
}

From source file:com.nabla.wapp.server.database.SqlInsert.java

@SuppressWarnings("unchecked")
protected static IRecordTable getTable(final Class clazz) {
    Assert.notNull(clazz, "have you forgotten to set @IRecordTable for record " + clazz.getSimpleName());

    final IRecordTable t = (IRecordTable) clazz.getAnnotation(IRecordTable.class);
    return (t != null) ? t : getTable(clazz.getSuperclass());
}

From source file:io.swagger.jaxrs.MethodProcessor.java

private static boolean isResourceClass(Class<?> cls) {
    return cls.getAnnotation(Api.class) != null;
}

From source file:com.epam.dlab.Help.java

/** Find and return help for module.
 * @param type the type of module.//from  w  w  w  .  ja  va2 s.  c om
 * @param name the name of module.
 * @throws InitializationException
 */
private static String findModuleHelp(ModuleType type, String name) throws InitializationException {
    List<Class<?>> modules = BillingUtils.getModuleClassList();
    String typeNames = null;
    for (Class<?> module : modules) {
        ModuleType t = BillingUtils.getModuleType(module);
        if (t == type) {
            JsonTypeName typeName = module.getAnnotation(JsonTypeName.class);
            if (typeName != null) {
                if (name.equals(typeName.value())) {
                    JsonClassDescription description = module.getAnnotation(JsonClassDescription.class);
                    if (description != null) {
                        return description.value();
                    }
                    throw new InitializationException("Help for " + type + " " + name + " not found");
                } else {
                    typeNames = (typeNames == null ? typeName.value() : typeNames + ", " + typeName.value());
                }
            }
        }
    }
    throw new InitializationException("Module for " + type + " " + name + " not found."
            + (typeNames == null ? "" : " Module type must be one of next: " + typeNames));
}

From source file:jp.rikinet.util.dto.DtoFactory.java

/**
 * JSON ?????????//  ww  w  .j a v  a  2 s .  c om
 * ?????
 * @param clazz Root ??
 */
public static void register(Class<? extends Root> clazz) {
    if (!clazz.isAnnotationPresent(DtoType.class))
        return;
    DtoType dt = clazz.getAnnotation(DtoType.class);
    classTable.put(dt.value(), clazz);
}

From source file:com.bstek.dorado.common.event.ClientEventRegistry.java

protected static void collectClientEventRegisterInfosFromSingleType(Class<?> type) {
    if (!processedTypes.contains(type)) {
        processedTypes.add(type);//from www.j  av  a  2  s  .  co m

        ClientEvents clientEvents = type.getAnnotation(ClientEvents.class);
        if (clientEvents != null && clientEvents.value() != null) {
            for (com.bstek.dorado.annotation.ClientEvent clientEvent : clientEvents.value()) {
                String[] signature = clientEvent.signature();
                if (signature.length == 1 && StringUtils.isEmpty(signature[0])) {
                    signature = null;
                }
                ClientEventRegisterInfo clientEventRegisterInfo = new ClientEventRegisterInfo(type,
                        clientEvent.name(), signature);
                clientEventRegisterInfo.setDeprecated(clientEvent.deprecated());

                int clientTypes = ClientType.parseClientTypes(clientEvent.clientTypes());
                if (clientTypes > 0) {
                    clientEventRegisterInfo.setClientTypes(clientTypes);
                }

                ClientEventRegistry.registerClientEvent(clientEventRegisterInfo);
            }
        }
    }
}

From source file:com.weibo.api.motan.protocol.yar.YarProtocolUtil.java

public static String getYarPath(Class<?> interfaceClazz, URL url) {
    if (interfaceClazz != null) {
        YarConfig config = interfaceClazz.getAnnotation(YarConfig.class);
        if (config != null && StringUtils.isNotBlank(config.path())) {
            return config.path();
        }//from  ww  w.  j a v a  2s.  c  o  m
    }
    // '/group/urlpath' as default
    return "/" + url.getGroup() + "/" + url.getPath();
}