Example usage for java.lang Class isAnnotationPresent

List of usage examples for java.lang Class isAnnotationPresent

Introduction

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

Prototype

@Override
public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) 

Source Link

Usage

From source file:com.cognifide.qa.bb.junit.concurrent.ConcurrentSuiteRunnerScheduler.java

private int determineNumberOfThreads(Class<?> clazz) {
    String property = properties.getProperty(THREAD_COUNT_PROPERTY);
    int numberOfThreads;
    if (StringUtils.isNotEmpty(property)) {
        numberOfThreads = Integer.parseInt(property);
    } else if (clazz.isAnnotationPresent(Concurrent.class)) {
        numberOfThreads = clazz.getAnnotation(Concurrent.class).threads();
    } else {/*ww  w .  j av a 2s .c  om*/
        numberOfThreads = (int) (Runtime.getRuntime().availableProcessors() * Concurrent.THREADS_NUMBER_FACTOR);
    }
    return numberOfThreads;
}

From source file:org.LexGrid.LexBIG.test.ContentLoadingTestListener.java

protected boolean loadContent(Class<?> clazz) {
    List<LoadContent> contents = new ArrayList<LoadContent>();

    if (clazz.isAnnotationPresent(LoadContent.class)) {
        contents.add(clazz.getAnnotation(LoadContent.class));
    }/*from w w  w . ja va 2  s .  c o  m*/

    LoadContents loadContents = clazz.getAnnotation(LoadContents.class);
    if (loadContents != null) {
        contents.addAll(Arrays.asList(loadContents.value()));
    }

    return this.doLoadContent(contents);
}

From source file:org.springframework.integration.config.annotation.MessagingAnnotationPostProcessor.java

private boolean isStereotype(Class<?> beanClass) {
    List<Annotation> annotations = new ArrayList<Annotation>(Arrays.asList(beanClass.getAnnotations()));
    Class<?>[] interfaces = beanClass.getInterfaces();
    for (Class<?> iface : interfaces) {
        annotations.addAll(Arrays.asList(iface.getAnnotations()));
    }//from   w w w. j ava2s . c  o  m
    for (Annotation annotation : annotations) {
        Class<? extends Annotation> annotationType = annotation.annotationType();
        if (annotationType.equals(Component.class) || annotationType.isAnnotationPresent(Component.class)) {
            return true;
        }
    }
    return false;
}

From source file:code.elix_x.excore.utils.nbt.mbt.encoders.NBTClassEncoder.java

public void populate(MBT mbt, NBTTagCompound nbt, Object o) {
    Class clazz = o.getClass();
    try {//  w  ww  .  ja va2s . c o m
        while (clazz != null && clazz != Object.class) {
            if (!clazz.isAnnotationPresent(MBTIgnore.class)) {
                for (Field field : clazz.getDeclaredFields()) {
                    if (!field.isAnnotationPresent(MBTIgnore.class)) {
                        field.setAccessible(true);
                        if (nbt.hasKey(field.getName())) {
                            if (encodeStatic || !Modifier.isStatic(field.getModifiers())) {
                                if (Modifier.isFinal(field.getModifiers())) {
                                    if (encodeFinal) {
                                        Field modifiers = Field.class.getDeclaredField("modifiers");
                                        modifiers.setAccessible(true);
                                        modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL);

                                        if (field.getGenericType() instanceof ParameterizedType) {
                                            Type[] types = ((ParameterizedType) field.getGenericType())
                                                    .getActualTypeArguments();
                                            Class[] clas = new Class[] {};
                                            for (Type type : types) {
                                                if (type instanceof Class) {
                                                    clas = ArrayUtils.add(clas, (Class) type);
                                                }
                                            }
                                            field.set(o, mbt.fromNBT(nbt.getTag(field.getName()),
                                                    field.getType(), clas));
                                        } else {
                                            field.set(o,
                                                    mbt.fromNBT(nbt.getTag(field.getName()), field.getType()));
                                        }
                                    }
                                } else {
                                    if (field.getGenericType() instanceof ParameterizedType) {
                                        Type[] types = ((ParameterizedType) field.getGenericType())
                                                .getActualTypeArguments();
                                        Class[] clas = new Class[] {};
                                        for (Type type : types) {
                                            if (type instanceof Class) {
                                                clas = ArrayUtils.add(clas, (Class) type);
                                            }
                                        }
                                        field.set(o, mbt.fromNBT(nbt.getTag(field.getName()), field.getType(),
                                                clas));
                                    } else {
                                        field.set(o, mbt.fromNBT(nbt.getTag(field.getName()), field.getType()));
                                    }
                                }
                            }
                        }
                    }
                }
            }
            clazz = encodeSuper ? clazz.getSuperclass() : Object.class;
        }
    } catch (IllegalArgumentException e) {
        Throwables.propagate(e);
    } catch (IllegalAccessException e) {
        Throwables.propagate(e);
    } catch (NoSuchFieldException e) {
        Throwables.propagate(e);
    } catch (SecurityException e) {
        Throwables.propagate(e);
    }
}

From source file:net.neevek.android.lib.paginize.ViewPagerPage.java

public ViewPagerPage(PageActivity pageActivity) {
    super(pageActivity);

    Class clazz = getClass();

    ViewPagerResId resIdAnnotation = null;

    try {/*from  w ww  . j  a va 2  s  . com*/
        do {
            if (clazz.isAnnotationPresent(ViewPagerResId.class)) {
                resIdAnnotation = (ViewPagerResId) clazz.getAnnotation(ViewPagerResId.class);
                break;
            }
        } while ((clazz = clazz.getSuperclass()) != ViewPagerPage.class);

    } catch (Exception e) {
        e.printStackTrace();
        throw new InjectFailedException(e);
    }

    if (resIdAnnotation == null) {
        throw new IllegalStateException("Must specify a ViewPager resource Id for " + clazz.getSimpleName()
                + " with the @ViewPagerResId annotation.");
    }

    View view = getView().findViewById(resIdAnnotation.value());
    if (view == null) {
        throw new IllegalStateException(
                "Can not find the View with the specified resource ID: " + resIdAnnotation.value());
    }
    if (!(view instanceof ViewPager)) {
        throw new IllegalStateException("The specified View with @ViewPagerResId is not of type ViewPager.");
    }

    mViewPager = (ViewPager) view;
}

From source file:com.agwego.fuzz.FuzzTester.java

/**
 * Only called reflectively. Do not use programmatically.
  *//from w w w.ja v  a 2  s  .co m
 * @param klass the class of this test
 * @throws com.agwego.fuzz.exception.ParametersError if any errors or otherwise
 * @throws InitializationError -
 * @throws FuzzTestJsonError - for any semantic errors in the JSON formatted test case
 */
public FuzzTester(Class<?> klass) throws ParametersError, InitializationError, FuzzTestJsonError {
    super(klass, Collections.<Runner>emptyList());

    if (!klass.isAnnotationPresent(Parameters.class))
        throw new ParametersError(
                "@Parameters( TestDirectory, Prefix, Suffix = '.json', TestDirectoryRootPropertyName = ''");

    Parameters params = klass.getAnnotation(Parameters.class);

    log.debug("TestDirectory " + params.TestDirectory());
    String prefix = StringHelper.unempty(params.Prefix(), klass.getSimpleName());

    log.debug("Prefix " + prefix);
    log.debug("Suffix " + params.Suffix());
    log.debug("TestDirectoryRootPropertyName " + params.TestDirectoryRootPropertyName());

    Map<String, List<FuzzTestCase>> testMethods = getTestMethods(params.TestDirectory(), prefix,
            params.Suffix(), klass);

    // add the Fuzz tests
    for (Map.Entry<String, List<FuzzTestCase>> ltc : testMethods.entrySet())
        runners.add(new FuzzTestRunner(getTestClass().getJavaClass(), ltc.getKey(), ltc.getValue()));

    // add the regular tests, optional
    try {
        runners.add(new BlockJUnit4ClassRunner(getTestClass().getJavaClass()));
    } catch (Exception ex) {
        log.debug(ex);
    }
}

From source file:org.devproof.portal.core.config.factory.DevproofClassPathBeanDefinitionScanner.java

@Override
protected void registerBeanDefinition(BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry) {
    try {//  w  w w . j  a v  a  2 s. c  o  m
        @SuppressWarnings({ "unchecked" })
        Class<? extends Page> clazz = (Class<? extends Page>) Class
                .forName(definitionHolder.getBeanDefinition().getBeanClassName());
        if (clazz.isAnnotationPresent(ModulePage.class)) {
            moduleConfiguration.addPageConfiguration(new PageConfiguration(clazz));
        } else if (clazz.isAnnotationPresent(NavigationBox.class)) {
            moduleConfiguration.addBox(new BoxConfiguration(clazz));
        } else if (clazz.isAnnotationPresent(GenericRepository.class)) {
            BeanDefinitionHolder beanDefinitionHolder = buildGenericRepositoryDefinition(clazz);
            super.registerBeanDefinition(beanDefinitionHolder, registry);
        } else if (clazz.isAnnotationPresent(Entity.class)
                || clazz.isAnnotationPresent(org.hibernate.annotations.Entity.class)) {
            moduleConfiguration.addEntity(clazz);
            if (clazz.isAnnotationPresent(RegisterGenericDataProvider.class)) {
                BeanDefinitionHolder beanDefinitionHolder = buildGenericDataProviderDefinition(clazz);
                super.registerBeanDefinition(beanDefinitionHolder, registry);
            }
        } else {
            super.registerBeanDefinition(definitionHolder, registry);
        }

    } catch (ClassNotFoundException e) {
        logger.fatal(e);
    }
}

From source file:com.frank.search.solr.core.convert.CustomConversions.java

private void registerConversion(Object converter) {
    Class<?> type = converter.getClass();
    boolean isWriting = type.isAnnotationPresent(WritingConverter.class);
    boolean isReading = type.isAnnotationPresent(ReadingConverter.class);

    if (!isReading && !isWriting) {
        isReading = true;/*ww  w . ja v  a2 s  .c o  m*/
        isWriting = true;
    }

    if (converter instanceof GenericConverter) {
        GenericConverter genericConverter = (GenericConverter) converter;
        for (ConvertiblePair pair : genericConverter.getConvertibleTypes()) {
            register(new ConvertibleContext(pair, isReading, isWriting));
        }
    } else if (converter instanceof Converter) {
        Class<?>[] arguments = GenericTypeResolver.resolveTypeArguments(converter.getClass(), Converter.class);
        register(new ConvertibleContext(arguments[0], arguments[1], isReading, isWriting));
    } else {
        throw new IllegalArgumentException(
                "Unsupported Converter type! Expected either GenericConverter if Converter.");
    }
}

From source file:plugins.ApiHelpInventory.java

private boolean hasAnnotation(Class<?> type, Class<? extends Annotation> annotation) {

    // null cannot have annotations
    if (type == null) {
        return false;
    }//from   w w w . j a v  a  2s  .c om

    // class annotation
    if (type.isAnnotationPresent(annotation)) {
        return true;
    }

    // annotation on interface
    for (Class<?> interfaceType : type.getInterfaces()) {
        if (hasAnnotation(interfaceType, annotation))
            return true;
    }

    // annotation on superclass
    return hasAnnotation(type.getSuperclass(), annotation);

}

From source file:br.gov.frameworkdemoiselle.internal.configuration.ConfigurationLoader.java

private void loadField(Field field, Object object, Class<?> clazz) {
    if (!field.isAnnotationPresent(Ignore.class) && clazz.isAnnotationPresent(Configuration.class)) {
        String resource = clazz.getAnnotation(Configuration.class).resource();
        ConfigType type = clazz.getAnnotation(Configuration.class).type();
        org.apache.commons.configuration.Configuration config = getConfiguration(resource, type);

        String key = getKey(field, clazz, config);
        Object value = getValue(key, field.getType(), config);

        validate(field, key, value, resource);
        setValue(field, key, object, value);
    }//w ww .ja  v  a2 s . co  m
}