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.crudetech.junit.categories.Categories.java

private static String getPatternFrom(Class<?> clazz) throws InitializationError {
    TestNamePattern pattern = clazz.getAnnotation(TestNamePattern.class);
    if (isNull(pattern) || isNullOrEmpty(pattern.value())) {
        throw new InitializationError("No proper test name pattern specified!");
    }/*ww  w . j a v  a2 s  .co  m*/
    return pattern.value();
}

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

/** Create and return substitutions for names of modules.
 * @return// ww w  . j  av a  2 s  . co  m
 * @throws InitializationException
 */
private static Map<String, String> findModules() throws InitializationException {
    List<Class<?>> modules = BillingUtils.getModuleClassList();
    Map<String, String> substitute = new HashMap<>();

    for (Class<?> module : modules) {
        ModuleType type = BillingUtils.getModuleType(module);
        JsonTypeName typeName = module.getAnnotation(JsonTypeName.class);
        if (typeName != null) {
            String typeNames = substitute.get(type.toString() + "s");
            typeNames = (typeNames == null ? typeName.value() : typeNames + ", " + typeName.value());
            substitute.put(type.toString() + "s", typeNames);
        }
    }

    return substitute;
}

From source file:com.opensymphony.xwork2.util.AnnotationUtils.java

/**
 * Returns the annotation on the given class or the package of the class. This searchs up the
 * class hierarchy and the package hierarchy for the closest match.
 *
 * @param   <T> class type/*from www. j  a  v  a2s  . c o m*/
 * @param   clazz The class to search for the annotation.
 * @param   annotationClass The Class of the annotation.
 * @return  The annotation or null.
 */
public static <T extends Annotation> T findAnnotation(Class<?> clazz, Class<T> annotationClass) {
    T ann = clazz.getAnnotation(annotationClass);
    while (ann == null && clazz != null) {
        ann = clazz.getAnnotation(annotationClass);
        if (ann == null) {
            ann = clazz.getPackage().getAnnotation(annotationClass);
        }
        if (ann == null) {
            clazz = clazz.getSuperclass();
            if (clazz != null) {
                ann = clazz.getAnnotation(annotationClass);
            }
        }
    }

    return ann;
}

From source file:com.cassius.spring.assembly.test.common.toolbox.ContextUtil.java

/**
 * Get spring context locations./*from  ww w.  j a  v  a2 s.c o  m*/
 *
 * @param testClass the test class
 * @return the string [ ]
 */
public static Set<String> getSpringContextLocations(Class<?> testClass) {
    Set<String> files = new HashSet<String>();
    while (testClass != null) {
        SpringContextConfigure configure = testClass.getAnnotation(SpringContextConfigure.class);
        if (configure != null) {
            files.addAll(Arrays.asList(configure.value()));
        }
        testClass = testClass.getSuperclass();
    }
    return files;
}

From source file:com.manydesigns.portofino.pageactions.PageActionLogic.java

public static boolean supportsDetail(Class<?> actionClass) {
    if (!PageAction.class.isAssignableFrom(actionClass)) {
        return false;
    }//from w  w  w  .  j  a v a2 s.c  o m
    SupportsDetail supportsDetail = actionClass.getAnnotation(SupportsDetail.class);
    if (supportsDetail != null) {
        return supportsDetail.value();
    } else {
        return supportsDetail(actionClass.getSuperclass());
    }
}

From source file:net.sourceforge.fenixedu.webServices.jersey.api.FenixJerseyAPIConfig.java

private static void searchForAPIFenixScope(Class<?> apiClass) {
    if (apiClass != null) {
        Path pathAnnotation = apiClass.getAnnotation(Path.class);
        if (pathAnnotation != null) {
            for (Method method : apiClass.getMethods()) {
                Path methodPathAnnotation = method.getAnnotation(Path.class);
                FenixAPIScope apiScopeAnnotation = method.getAnnotation(FenixAPIScope.class);
                if (apiScopeAnnotation != null) {
                    if (methodPathAnnotation != null) {
                        String path = ends(pathAnnotation.value());
                        String methodPath = ends(methodPathAnnotation.value());
                        String absolutePath = Joiner.on("/").join(path, methodPath);
                        String scopeName = apiScopeAnnotation.value();
                        scopePathsMap.put(scopeName, absolutePath);
                        LOGGER.debug("add {} to scope {}", absolutePath, scopeName);
                    } else {
                        LOGGER.debug("No path for method {}", method.getName());
                    }/*from w  ww  . ja  v a2  s  .co m*/
                } else {
                    FenixAPIPublic publicAPIAnnotation = method.getAnnotation(FenixAPIPublic.class);
                    if (publicAPIAnnotation != null) {
                        if (methodPathAnnotation != null) {
                            String path = ends(pathAnnotation.value());
                            String methodPath = ends(methodPathAnnotation.value());
                            String absolutePath = Joiner.on("/").join(path, methodPath);
                            publicScopes.add(absolutePath);
                            LOGGER.debug("add public endpoint {}", absolutePath);
                        }
                    }
                }
            }
        } else {
            LOGGER.debug("No api class");
        }
    }
}

From source file:com.ariatemplates.attester.junit.Attester.java

private static String getConfigFile(Class<?> testClass) {
    ConfigFile configFileAnnotation = testClass.getAnnotation(ConfigFile.class);
    if (configFileAnnotation == null) {
        return null;
    }//from  w  w w.java 2 s. c o m
    File configFile = new File(configFileAnnotation.value());
    if (!configFile.exists()) {
        throw new RuntimeException(new FileNotFoundException(configFile.getAbsolutePath()));
    }
    return configFileAnnotation.value();
}

From source file:com.mycomm.dao.mydao.base.MyDaoSupport.java

/**
 * ???/*from   w w  w  . j ava2  s  . c  o  m*/
 *
 * @param <E>
 * @param clazz 
 * @return
 */
protected static <E> String getEntityName(Class<E> clazz) {
    String entityname = clazz.getSimpleName();
    Entity entity = clazz.getAnnotation(Entity.class);
    if (entity.name() != null && !"".equals(entity.name())) {
        entityname = entity.name();
    }
    return entityname;
}

From source file:com.manydesigns.portofino.pageactions.PageActionLogic.java

public static Class<?> getConfigurationClass(Class<?> actionClass) {
    if (!PageAction.class.isAssignableFrom(actionClass)) {
        return null;
    }//from  ww w . ja  v a2s . c o m
    ConfigurationClass configurationClass = actionClass.getAnnotation(ConfigurationClass.class);
    if (configurationClass != null) {
        return configurationClass.value();
    } else {
        return getConfigurationClass(actionClass.getSuperclass());
    }
}

From source file:com.ineunet.knife.mgt.ActivatorHelper.java

static int getBundleSize() {
    int size = 0;
    Package[] pkgs = Package.getPackages();
    for (Package pkg : pkgs) {
        String name = pkg.getName();
        if (name.endsWith(".internal")) {
            if (name.contains(".knife.")) {
                size++;/*from   w  ww . j av a2  s . c  o m*/
            } else if ("com.sun.naming.internal".equals(name)) {
                continue;
            } else {
                try {
                    Class<?> cls = ClassLocator.loadClass(name + ".Activator");
                    BundleActivator c = cls.getAnnotation(BundleActivator.class);
                    if (c != null) {
                        size++;
                    }
                } catch (ClassNotFoundException e) {
                    // other package has the same name '*.internal', ignore
                    continue;
                }
            }
        }
    }
    return size;
}