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.xaniox.heavyspleef.core.flag.FlagManager.java

public static String generatePath(Flag flagAnnotation) {
    //Generate the full path
    StringBuilder pathBuilder = new StringBuilder();

    Flag lastParentFlagData = flagAnnotation;
    while (lastParentFlagData != null) {
        pathBuilder.insert(0, lastParentFlagData.name());

        Class<? extends AbstractFlag<?>> parentClass = lastParentFlagData.parent();
        lastParentFlagData = parentClass.getAnnotation(Flag.class);

        if (lastParentFlagData != null) {
            pathBuilder.insert(0, ":");
        }/*w w  w  .ja  v a 2 s  . c  om*/
    }

    String path = pathBuilder.toString();
    return path;
}

From source file:net.ostis.sc.memory.SCKeynodesBase.java

protected static void init(SCSession session, Class<? extends SCKeynodesBase> klass) {
    if (log.isDebugEnabled())
        log.debug("Start retrieving keynodes for " + klass);

    try {/*from   w  w w .  j a  v  a  2  s .co  m*/
        //
        // Search default segment for keynodes
        //
        SCSegment defaultSegment = null;
        {
            DefaultSegment annotation = klass.getAnnotation(DefaultSegment.class);
            if (annotation != null) {
                defaultSegment = session.openSegment(annotation.value());
                Validate.notNull(defaultSegment, "Default segment \"{0}\" not found", annotation.value());
                klass.getField(annotation.fieldName()).set(null, defaultSegment);
            }
        }

        Field[] fields = klass.getFields();
        for (Field field : fields) {
            int modifiers = field.getModifiers();

            if (Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers)) {
                Class<?> type = field.getType();
                if (type.equals(SCSegment.class)) {
                    //
                    // We have segment field. Load segment by uri.
                    //
                    SegmentURI annotation = field.getAnnotation(SegmentURI.class);

                    if (annotation != null) {
                        String uri = annotation.value();
                        SCSegment segment = session.openSegment(uri);
                        field.set(null, segment);
                    } else {
                        // May be it already has value?
                        if (log.isWarnEnabled()) {
                            if (field.get(null) == null)
                                log.warn(field + " doesn't have value");
                        }
                    }
                } else {
                    if (!(checkKeynode(session, defaultSegment, field) || checkKeynodeURI(session, field)
                            || checkKeynodesNumberPatternURI(session, klass, field))) {

                        if (log.isWarnEnabled()) {
                            if (field.get(null) == null)
                                log.warn(field + " doesn't have annotations and value");
                        }

                    }
                }
            }
        }
    } catch (Exception e) {
        // TODO: handle
        e.printStackTrace();
    }
}

From source file:me.ronghai.sa.dao.impl.AbstractModelDAOWithJDBCImpl.java

public static String table(Class<?> entityClass) {
    String table = entityClass.getSimpleName();
    if (entityClass.isAnnotationPresent(Entity.class)) {
        String t = ((Entity) entityClass.getAnnotation(Entity.class)).name();
        if (org.apache.commons.lang.StringUtils.isNotEmpty(t)) {
            table = t;//from w ww  .ja  va2  s.com
        }
    }
    return table;
}

From source file:com.github.gekoh.yagen.util.MappingUtils.java

private static Map<String, Class> getTableNameMap(Class... classes) {
    Map<String, Class> tableNameMap = new HashMap<String, Class>(classes.length);

    for (Class aClass : classes) {
        if (aClass.isAnnotationPresent(Table.class)) {
            tableNameMap.put(((Table) aClass.getAnnotation(Table.class)).name().toUpperCase(), aClass);
        }/*from   w  ww  . j a  va2 s . c om*/
    }

    return tableNameMap;
}

From source file:com.github.anba.es6draft.util.Resources.java

/**
 * Loads the configuration file./* ww w.j  a  v  a 2s . c o  m*/
 */
public static Configuration loadConfiguration(Class<?> clazz) {
    TestConfiguration config = clazz.getAnnotation(TestConfiguration.class);
    String file = config.file();
    String name = config.name();
    try {
        PropertiesConfiguration properties = new PropertiesConfiguration();
        // entries are mandatory unless an explicit default value was given
        properties.setThrowExceptionOnMissing(true);
        properties.getInterpolator().setParentInterpolator(MISSING_VAR);
        properties.load(resource(file), "UTF-8");

        Configuration configuration = new CompositeConfiguration(
                Arrays.asList(new SystemConfiguration(), properties));
        return configuration.subset(name);
    } catch (ConfigurationException | IOException e) {
        throw new RuntimeException(e);
    } catch (NoSuchElementException e) {
        throw e;
    }
}

From source file:com.medallia.spider.api.StRenderer.java

/** @return the interface declared within the given class which is also annotated with the given annotation */
private static <X extends Annotation> Class<X> findInterfaceWithAnnotation(Map<Class<?>, Class<?>> methodMap,
        Class<?> clazz, Class<? extends Annotation> annotation) {
    Class<?> annotatedInterface = methodMap.get(clazz);
    if (annotatedInterface == null) {
        for (Class<?> c : CollUtils.concat(Arrays.asList(clazz.getClasses()),
                Arrays.asList(clazz.getDeclaredClasses()))) {
            Annotation i = c.getAnnotation(annotation);
            if (i != null) {
                annotatedInterface = c;//from   w  w  w  . j a v  a  2 s  .c o m
                methodMap.put(clazz, annotatedInterface);
                break;
            }
        }
    }
    @SuppressWarnings("unchecked")
    Class<X> x = (Class<X>) annotatedInterface;
    return x;
}

From source file:com.unboundid.scim2.common.utils.SchemaUtils.java

/**
 * Gets the id of the schema from the annotation of the class
 * passed in.//from  w w  w  .j  av a2  s .  c om
 *
 * @param cls class to find the schema id property of the annotation from.
 * @return the id of the schema, or {@code null} if it was not provided.
 */
public static String getSchemaIdFromAnnotation(final Class<?> cls) {
    Schema schema = cls.getAnnotation(Schema.class);
    return SchemaUtils.getSchemaIdFromAnnotation(schema);
}

From source file:com.unboundid.scim2.common.utils.SchemaUtils.java

/**
 * Gets the name property from the annotation of the class
 * passed in.//from   ww  w .j  ava  2s . c o  m
 *
 * @param cls class to find the schema name property of the annotation from.
 * @return the name of the schema.
 */
public static String getNameFromSchemaAnnotation(final Class<?> cls) {
    Schema schema = (Schema) cls.getAnnotation(Schema.class);
    return SchemaUtils.getNameFromSchemaAnnotation(schema);
}

From source file:com.impetus.kundera.metadata.MetadataUtils.java

/**
 * Checks whether a given field is Element collection field of BASIC type
 * //from  w  w w.  j  a va2s .c o  m
 * @param collectionField
 * @return
 */
public static boolean isBasicElementCollectionField(Field collectionField) {
    if (!Collection.class.isAssignableFrom(collectionField.getType())
            && !Map.class.isAssignableFrom(collectionField.getType())) {
        return false;
    }

    List<Class<?>> genericClasses = PropertyAccessorHelper.getGenericClasses(collectionField);
    for (Class genericClass : genericClasses) {
        if (genericClass.getAnnotation(Embeddable.class) != null) {
            return false;
        }
    }
    return true;
}

From source file:com.zc.util.refelect.Reflector.java

/**
 * ?//w  ww .j av  a2s .com
 *
 * @param targetClass
 *            Class
 * @param annotationClass
 *            Class
 *
 * @return Object
 */
public static <T extends Annotation> T getAnnotation(Class targetClass, Class annotationClass) {
    if (targetClass.isAnnotationPresent(annotationClass)) {
        return (T) targetClass.getAnnotation(annotationClass);
    }

    return null;
}