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.skcraft.launcher.persistence.Persistence.java

/**
 * Read an object from file.//from w w  w .  j  av  a 2s.c om
 *
 * @param file the file
 * @param cls the class
 * @param returnNull true to return null if the object could not be loaded
 * @param <V> the type of class
 * @return an object
 */
public static <V> V load(File file, Class<V> cls, boolean returnNull) {
    ByteSource source = Files.asByteSource(file);
    ByteSink sink = new MkdirByteSink(Files.asByteSink(file), file.getParentFile());

    Scrambled scrambled = cls.getAnnotation(Scrambled.class);
    if (cls.getAnnotation(Scrambled.class) != null) {
        source = new ScramblingSourceFilter(source, scrambled.value());
        sink = new ScramblingSinkFilter(sink, scrambled.value());
    }

    V object = read(source, cls, returnNull);
    Persistence.bind(object, sink);
    return object;
}

From source file:ch.sdi.core.impl.cfg.ConfigUtils.java

/**
 * Converts the given classname into a properties file name according following rules:
 *
 * <ul>/*from   w w  w .  jav a  2  s .c  om*/
 *     <li> if class is annotated with SdiProps the value of the annotation is used, suffixed by
 *          ".properties" (unless already present).</li>
 *     <li>if annotation is missing or no value configured:</li>
 *     <ul>
 *         <li> if class name ends with "Properties" this suffix will be truncated and replaced by
 *         ".properties"</li>
 *         <li> any other class name is used as is and suffixed with ".properties"</li>
 *     </ul>
 * </ul>
 *
 */
public static String makePropertyResourceName(Class<?> aClass) {
    String found = null;

    SdiProps ann = aClass.getAnnotation(SdiProps.class);

    if (ann != null) {
        found = ann.value();
    } // if ann != null

    if (StringUtils.hasText(found)) {
        if (found.endsWith(".properties")) {
            return found;
        } // if condition

        return found + ".properties";
    }

    String classname = aClass.getSimpleName();

    if (classname.endsWith("Properties") || classname.endsWith("properties")) {
        return classname.substring(0, classname.length() - "Properties".length()) + ".properties";
    } // if condition

    return classname + ".properties";

}

From source file:me.philnate.textmanager.updates.Updater.java

static TreeMap<Version, Class<? extends Update>> createUpdateList(String packageName) {
    final TreeMap<Version, Class<? extends Update>> updates = Maps.newTreeMap();

    final TypeReporter reporter = new TypeReporter() {

        @SuppressWarnings("unchecked")
        @Override/*from  www.j  a  va2 s .co m*/
        public Class<? extends Annotation>[] annotations() {
            return new Class[] { UpdateScript.class };
        }

        @SuppressWarnings("unchecked")
        @Override
        public void reportTypeAnnotation(Class<? extends Annotation> annotation, String className) {
            Class<? extends Update> clazz;
            try {
                clazz = (Class<? extends Update>) Updater.class.getClassLoader().loadClass(className);
                updates.put(new Version(clazz.getAnnotation(UpdateScript.class).UpdatesVersion()), clazz);
            } catch (ClassNotFoundException e) {
                LOG.error("Found annotated class, but could not load it " + className, e);
            }
        }

    };
    final AnnotationDetector cf = new AnnotationDetector(reporter);
    try {
        // load updates
        cf.detect(packageName);
    } catch (IOException e) {
        LOG.error("An error occured while collecting Updates", e);
    }
    return updates;
}

From source file:com.haulmont.cuba.core.config.ConfigUtil.java

/**
 * Search for an annotation on a configuration interface method. In
 * addition to searching the method itself, the {@link #getGetMethod
 * plain get method} is also searched, as can the {@link
 * #getMethodType method type} be./*from  w w  w  .jav a 2s.  c  o m*/
 *
 * @param configInterface  The configuration interface.
 * @param method           The method.
 * @param annotationType   The annotation type of interest.
 * @param searchMethodType Whether to search the method type.
 * @return The annotation, or null.
 */
public static <T extends Annotation> T getAnnotation(Class<?> configInterface, Method method,
        Class<T> annotationType, boolean searchMethodType) {
    T annotation = method.getAnnotation(annotationType);
    if (annotation == null) {
        Method getMethod = getGetMethod(configInterface, method);
        if (getMethod != null) {
            annotation = getMethod.getAnnotation(annotationType);
        }
        if ((annotation == null) && searchMethodType) {
            String methodName = method.getName();
            if (ACCESS_RE.matcher(methodName).matches()) {
                // Is the annotation present on the method type?
                Class<?> methodType = getMethodType(method);
                annotation = methodType.getAnnotation(annotationType);
            }
        }
    }
    return annotation;
}

From source file:de.unisb.cs.st.javalanche.mutation.runtime.testDriver.junit.Junit4MutationTestDriver.java

private static Class<? extends Runner> getRunWithRunner(Class<?> clazz, boolean useSuite) {
    RunWith runWithAnnotation = clazz.getAnnotation(RunWith.class);
    if (runWithAnnotation == null) {
        AllDefaultPossibilitiesBuilder builder = new AllDefaultPossibilitiesBuilder(useSuite);
        try {//from  w ww  .  jav a2  s . co  m
            return builder.runnerForClass(clazz).getClass();
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
        // return BlockJUnit4ClassRunner.class;
    }
    Class<? extends Runner> runner = runWithAnnotation.value();
    if (!runnerImplementsFilterable(runner))
        return BlockJUnit4ClassRunner.class;
    return runner;
}

From source file:com.cuubez.visualizer.resource.ResourceMetaDataScanner.java

public static boolean isProvider(Class<?> clazz) {

    if (clazz.getAnnotation(Provider.class) != null) {
        return true;
    }//from www . j  a v a2s  .  c  om

    return false;
}

From source file:edu.usu.sdl.openstorefront.doc.EntityProcessor.java

private static EntityDocModel createEntityModel(Class entity) {
    EntityDocModel docModel = new EntityDocModel();
    docModel.setName(entity.getSimpleName());

    APIDescription description = (APIDescription) entity.getAnnotation(APIDescription.class);
    if (description != null) {
        docModel.setDescription(description.value());
    }//from  w w w .  ja  v a2 s. co  m

    if (!entity.isInterface()) {
        addFields(entity, docModel);
    }

    return docModel;
}

From source file:com.eviware.soapui.plugins.PluginLoader.java

static PluginInfo readPluginInfoFrom(Class<?> pluginClass) {
    PluginInfo pluginInfo = readPluginInfoFromAnnotation(pluginClass.getAnnotation(PluginConfiguration.class));
    addDependency(pluginInfo, pluginClass.getAnnotation(PluginDependency.class));
    PluginDependencies pluginDependenciesAnnotation = pluginClass.getAnnotation(PluginDependencies.class);
    if (pluginDependenciesAnnotation != null) {
        for (PluginDependency pluginDependency : pluginDependenciesAnnotation.value()) {
            addDependency(pluginInfo, pluginDependency);
        }/* w ww  . j av a2s . c o m*/
    }
    return pluginInfo;
}

From source file:com.darkstar.beanCartography.utils.NameUtils.java

/**
 * @param clazz class to check/*from w  ww.j a v  a 2s.  co m*/
 * @return array of string names associated to the composite
 */
public static String[] getBusinessComposites(Class<?> clazz) {
    Preconditions.checkNotNull(clazz, "Class cannot be null");
    if (!hasBusinessComposites(clazz))
        throw new IllegalArgumentException("not a business composite");
    return clazz.getAnnotation(NamedClassComposite.class).names();
}

From source file:com.eviware.x.form.support.ADialogBuilder.java

public static XFormDialog buildWizard(Class<? extends Object> tabbedFormClass) {
    AForm formAnnotation = tabbedFormClass.getAnnotation(AForm.class);
    if (formAnnotation == null) {
        throw new RuntimeException("formClass is not annotated correctly..");
    }/*from w w  w.j  a  v  a 2  s. c  o m*/

    MessageSupport messages = MessageSupport.getMessages(tabbedFormClass);
    XFormDialogBuilder builder = XFormFactory.createDialogBuilder(formAnnotation.name());

    for (Field field : tabbedFormClass.getFields()) {
        APage pageAnnotation = field.getAnnotation(APage.class);
        if (pageAnnotation != null) {
            buildForm(builder, pageAnnotation.name(), field.getType(), messages);
        }
    }

    XFormDialog dialog = builder.buildWizard(formAnnotation.description(),
            UISupport.createImageIcon(formAnnotation.icon()), formAnnotation.helpUrl());

    return dialog;
}