Example usage for java.lang Class getAnnotations

List of usage examples for java.lang Class getAnnotations

Introduction

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

Prototype

public Annotation[] getAnnotations() 

Source Link

Usage

From source file:Main.java

public static void main(String[] args) {

    Main cls = new Main();
    Class c = cls.getClass();

    Annotation[] a = c.getAnnotations();
    for (Annotation val : a) {
        System.out.println(val.toString());
    }//from   w ww.  j  ava 2 s .  c  om
}

From source file:Main.java

public static void main(String... args) {
    try {/*  w w  w  . j  a va  2 s . c  o  m*/
        Class<?> c = Class.forName(args[0]);
        out.format("Class:%n  %s%n%n", c.getCanonicalName());
        out.format("Modifiers:%n  %s%n%n", Modifier.toString(c.getModifiers()));

        out.format("Type Parameters:%n");
        TypeVariable[] tv = c.getTypeParameters();
        if (tv.length != 0) {
            out.format("  ");
            for (TypeVariable t : tv)
                out.format("%s ", t.getName());
            out.format("%n%n");
        } else {
            out.format("  -- No Type Parameters --%n%n");
        }

        out.format("Implemented Interfaces:%n");
        Type[] intfs = c.getGenericInterfaces();
        if (intfs.length != 0) {
            for (Type intf : intfs)
                out.format("  %s%n", intf.toString());
            out.format("%n");
        } else {
            out.format("  -- No Implemented Interfaces --%n%n");
        }

        out.format("Inheritance Path:%n");
        List<Class> l = new ArrayList<Class>();
        printAncestor(c, l);
        if (l.size() != 0) {
            for (Class<?> cl : l)
                out.format("  %s%n", cl.getCanonicalName());
            out.format("%n");
        } else {
            out.format("  -- No Super Classes --%n%n");
        }

        out.format("Annotations:%n");
        Annotation[] ann = c.getAnnotations();
        if (ann.length != 0) {
            for (Annotation a : ann)
                out.format("  %s%n", a.toString());
            out.format("%n");
        } else {
            out.format("  -- No Annotations --%n%n");
        }

        // production code should handle this exception more gracefully
    } catch (ClassNotFoundException x) {
        x.printStackTrace();
    }
}

From source file:Main.java

public static <T> String getXmlTagNameFromJaxbClass(Class<T> jaxbClass) {
    String xmlTagName = "";
    for (Annotation annotation : jaxbClass.getAnnotations()) {
        if (annotation.annotationType() == XmlRootElement.class) {
            xmlTagName = ((XmlRootElement) annotation).name();
            break;
        }// ww w .j ava  2 s . c om
    }
    return xmlTagName;
}

From source file:com.test.edusys.common.utils.reflection.ReflectionUtils.java

/**
 * /*from w  w w .j a v a 2  s  .  c om*/
 */
public static String getShowLogTablename(final Class clazz) {
    Annotation[] classAnnotation = clazz.getAnnotations();
    for (Annotation cAnnotation : classAnnotation) {
        if (ShowLogTablename.class == cAnnotation.annotationType()) {
            ShowLogTablename xmlElement = (ShowLogTablename) cAnnotation;
            return xmlElement.value();
        }
    }
    return null;
}

From source file:org.apache.axis2.jaxrs.JAXRSUtils.java

/**
  * returns a jaxrs class model reading class level annotation given the service class
  * @param serviceClass/*from   ww  w.  ja  v  a 2s.  c  om*/
  * @return
  */
public static JAXRSModel getClassModel(Class serviceClass) {
    JAXRSModel model = new JAXRSModel();
    Annotation[] annotation = serviceClass.getAnnotations();
    for (Annotation a : annotation) {
        if (a != null) {
            if (a instanceof Produces) {
                addProducesToClassModel((Produces) a, model);
            } else if (a instanceof Consumes) {
                addConsumesToClassModel((Consumes) a, model);
            } else if (a instanceof Path) {
                addPathToClassModel((Path) a, model);
            } else {
                System.out.println("Could not identify the Annotation....");
            }

        }

    }

    return model;
}

From source file:com.oembedler.moon.graphql.engine.dfs.ResolvableTypeAccessor.java

public static ResolvableTypeAccessor forClass(Class<?> rawClass) {
    return new ResolvableTypeAccessor(rawClass.getSimpleName(), null,
            Lists.newArrayList(rawClass.getAnnotations()), rawClass);
}

From source file:net.zcarioca.zcommons.config.data.BeanPropertySetterFactory.java

private static Collection<Annotation> getBeanAnnotations(Class<?> beanClass) {
    return Arrays.asList(beanClass.getAnnotations());
}

From source file:com.sugaronrest.restapicalls.ModuleInfo.java

/**
 * Gets the module model info object.//from  w w  w  .j  ava 2s.c  o  m
 *
 *  @param type The module model type.
 *  @return The model info object.
 */
private static ModuleInfo readByType(Type type) throws Exception {
    ModuleInfo moduleInfo = new ModuleInfo();

    Class<?> moduleClass = (Class<?>) type;

    String moduleName = null;
    String jsonModuleName = null;

    Annotation[] moduleAnnotations = moduleClass.getAnnotations();
    for (Annotation annotation : moduleAnnotations) {
        if (annotation instanceof Module) {
            Module module = (Module) annotation;
            moduleName = module.name();
            jsonModuleName = module.tablename();
        }
    }

    if ((moduleName == null) || (jsonModuleName == null)) {
        return null;
    }

    moduleInfo.name = moduleName;
    moduleInfo.jsonName = jsonModuleName;
    moduleInfo.type = moduleClass;

    List<ModuleProperty> modelProperties = getFieldAnnotations(moduleClass);
    moduleInfo.modelProperties = modelProperties;

    return moduleInfo;
}

From source file:org.lunarray.model.descriptor.scanner.AnnotationScannerUtil.java

/**
 * Gets the marked annotations./* w w  w  . j ava2  s  . c o  m*/
 * 
 * @param marker
 *            The marker.
 * @param entityType
 *            The entity type. Entity type may not be null.
 * @param transitive
 *            Transitive traversal.
 * @return The marked annotations.
 */
public static List<Annotation> getMarked(final Class<? extends Annotation> marker, final Class<?> entityType,
        final boolean transitive) {
    Validate.notNull(entityType, "Entity type may not be null.");
    final List<Annotation> markedList = new LinkedList<Annotation>();
    for (final Annotation a : entityType.getAnnotations()) {
        INSTANCE.getMarkedProcessInner(marker, a, transitive, markedList, AnnotationScannerUtil.createSet());
    }
    return markedList;
}

From source file:org.constretto.spring.EnvironmentAnnotationConfigurer.java

@SuppressWarnings("unchecked")
public static Environment findEnvironmentAnnotation(Class beanClass) {
    if (beanClass.isAnnotationPresent(Environment.class)) {
        return (Environment) beanClass.getAnnotation(Environment.class);
    } else {/*from ww w .  ja v  a  2s  .  c om*/
        return findEnvironmentMetaAnnotation(new HashSet<Annotation>(), beanClass.getAnnotations());
    }
}