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.baidu.terminator.manager.service.StubServiceImpl.java

@Override
public PluginInfo getExtractor(int linkId) {
    Link link = linkService.getLinkById(linkId);
    Class<?> extractClass = linkControlService.getPluginClass(link, WorkMode.STUB);

    PluginInfo pi = new PluginInfo();
    pi.setName(link.getExtractClass());/*from  w w  w. j  a  v a  2s.co m*/

    DefaultExtractor defaultExtractor = extractClass.getAnnotation(DefaultExtractor.class);
    if (defaultExtractor != null) {
        pi.setProtocol(defaultExtractor.protocol());
        pi.setMessage(defaultExtractor.message());
    } else {
        CustomizedExtractor customizedExtractor = extractClass.getAnnotation(CustomizedExtractor.class);
        pi.setProtocol(customizedExtractor.protocol());
        pi.setMessage(customizedExtractor.message());
    }
    return pi;
}

From source file:com.consol.citrus.simulator.model.UseCaseParameter.java

/**
 * Adds use case filter which resides in conditional display of parameter field in Html form.
 * @param triggerType/*from ww  w .  j a v  a  2  s .co  m*/
 * @return
 */
public UseCaseParameter addUseCaseFilter(Class<? extends UseCaseTrigger> triggerType) {
    if (StringUtils.hasText(useCaseFilter)) {
        this.useCaseFilter += " " + triggerType.getAnnotation(Component.class).value();
    } else {
        this.useCaseFilter = triggerType.getAnnotation(Component.class).value();
    }

    return this;
}

From source file:ch.sdi.plugins.oxwall.profile.OxQuestionFactory.java

/**
 * Instantiates a custom oxwall ProfileQuestion type.
 * <p>/*from   w ww . j  av a2 s.  c  om*/
 * see properties ox.target.qn.xxxx). For example:
 * <pre>
 *     ox.target.qn.person.gender=custom:gendermap:sex
 * </pre>
 * <p>
 * @param aBeanName
 *        the name of the implementing bean (in fact its the name parameter of the OxCustomQuestion
 *        annotation). This corresponds to the second parameter of the property value.
 * @param aQuestionName
 *        the name of the question. This corresponds to the third parameter of the property value.
 * @param aPersonKey
 *        The key in the person PropertySource under which the corresponding value will be stored.
 * @return
 * @throws SdiException
 */
public OxProfileQuestion getCustomQuestion(String aBeanName, String aQuestionName, String aPersonKey)
        throws SdiException {
    Collection<Class<OxProfileQuestion>> candidates = ClassUtil.findCandidatesByAnnotation(
            OxProfileQuestion.class, OxCustomQuestion.class, "ch.sdi.plugins.oxwall");

    for (Class<OxProfileQuestion> clazz : candidates) {
        myLog.trace("found candidate for custom question: " + clazz.getName());
        OxCustomQuestion ann = clazz.getAnnotation(OxCustomQuestion.class);

        if (!ann.value().endsWith(aBeanName)) {
            myLog.trace("candidate has not the desired bean name (" + aBeanName + ") but: " + ann.value());
            continue;
        } // if !ann.value().endsWith( "beanName" )

        myLog.trace("found custom question class with beanName(" + aBeanName + "): " + clazz.getName());

        try {
            Constructor<OxProfileQuestion> constructor = clazz
                    .getConstructor(new Class<?>[] { String.class, String.class });
            return constructor.newInstance(new Object[] { aQuestionName, aPersonKey });

        } catch (Throwable t) {
            throw new SdiException("missing desired constructor found custom question class with" + " beanName("
                    + aBeanName + "): " + clazz.getName(), SdiException.EXIT_CODE_CONFIG_ERROR);
        }

    }

    throw new SdiException("No suitable custom question found. BeanName: " + aBeanName,
            SdiException.EXIT_CODE_CONFIG_ERROR);
}

From source file:com.zuoxiaolong.niubi.job.persistent.BaseDaoImpl.java

public String getEntityAnnotationName(Class<?> clazz) {
    try {//  ww  w  .j  a  v a 2s  .com
        Entity entityAnnotation = clazz.getAnnotation(Entity.class);
        return StringHelper.isEmpty(entityAnnotation.name()) ? clazz.getSimpleName() : entityAnnotation.name();
    } catch (Exception e) {
        return clazz.getSimpleName();
    }
}

From source file:fr.eoit.db.AbstractDTOPopulator.java

public List<FieldDefinition> getFieldDefinition(Class<?> beanClass) {
    if (beanClassCache.containsKey(beanClass)) {
        return beanClassCache.get(beanClass);
    } else if (beanClass.getAnnotation(DTO.class) != null) {
        List<FieldDefinition> fieldDefinitions = new ArrayList<FieldDefinition>();
        for (Field field : beanClass.getDeclaredFields()) {
            DTOField fieldAnnotation = field.getAnnotation(DTOField.class);
            if (fieldAnnotation != null) {
                FieldDefinition fieldDefinition = new FieldDefinition(fieldAnnotation);
                if (fieldAnnotation.name().equals("")) {
                    fieldDefinition.columnName = field.getName();
                }/*from  ww w. j  a v a 2 s. c  om*/
                fieldDefinition.fieldName = field.getName();
                fieldDefinitions.add(fieldDefinition);
            }
        }

        fieldDefinitions.addAll(getFieldDefinition(beanClass.getSuperclass()));
        beanClassCache.put(beanClass, fieldDefinitions);
        return fieldDefinitions;
    } else {
        return new ArrayList<FieldDefinition>();
    }
}

From source file:net.cpollet.jixture.hibernate3.helper.Hibernate3MappingDefinitionHolder.java

private String getLowercaseTableNameFromMapping(Class mapping) {
    Table annotation = (Table) mapping.getAnnotation(Table.class);

    return annotation.name().toLowerCase();
}

From source file:org.bytesoft.bytetcc.supports.dubbo.CompensableDubboConfigValidator.java

public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    ClassLoader cl = Thread.currentThread().getContextClassLoader();

    final Map<String, Class<?>> beanClassMap = new HashMap<String, Class<?>>();
    final Map<String, BeanDefinition> serviceMap = new HashMap<String, BeanDefinition>();
    final Map<String, BeanDefinition> references = new HashMap<String, BeanDefinition>();

    String[] beanNameArray = beanFactory.getBeanDefinitionNames();
    for (int i = 0; i < beanNameArray.length; i++) {
        String beanName = beanNameArray[i];
        BeanDefinition beanDef = beanFactory.getBeanDefinition(beanName);
        String beanClassName = beanDef.getBeanClassName();

        Class<?> beanClass = null;
        try {/*  www. j a  v  a 2 s  .com*/
            beanClass = cl.loadClass(beanClassName);
            beanClassMap.put(beanName, beanClass);
        } catch (Exception ex) {
            continue;
        }

        if (com.alibaba.dubbo.config.spring.ServiceBean.class.equals(beanClass)) {
            serviceMap.put(beanName, beanDef);
        } else if (com.alibaba.dubbo.config.spring.ReferenceBean.class.equals(beanClass)) {
            references.put(beanName, beanDef);
        }

    }

    for (Iterator<Map.Entry<String, BeanDefinition>> itr = serviceMap.entrySet().iterator(); itr.hasNext();) {
        Map.Entry<String, BeanDefinition> entry = itr.next();
        String beanKey = entry.getKey();
        BeanDefinition beanDef = entry.getValue();
        MutablePropertyValues mpv = beanDef.getPropertyValues();
        PropertyValue ref = mpv.getPropertyValue("ref");
        PropertyValue filter = mpv.getPropertyValue("filter");
        PropertyValue group = mpv.getPropertyValue("group");
        if (ref == null || ref.getValue() == null
                || RuntimeBeanReference.class.equals(ref.getValue().getClass()) == false) {
            continue;
        }
        RuntimeBeanReference beanRef = (RuntimeBeanReference) ref.getValue();
        Class<?> refClass = beanClassMap.get(beanRef.getBeanName());
        if (refClass.getAnnotation(Compensable.class) == null) {
            continue;
        }

        if (group == null || group.getValue() == null
                || KEY_GROUP_COMPENSABLE.equals(group.getValue()) == false) {
            logger.warn("The value of attr 'group'(beanId= {}) should be 'org.bytesoft.bytetcc'.", beanKey);
            continue;
        } else if (filter == null || filter.getValue() == null
                || KEY_FILTER_COMPENSABLE.equals(filter.getValue()) == false) {
            logger.warn("The value of attr 'filter'(beanId= {}) should be 'compensable'.", beanKey);
            continue;
        }

        PropertyValue timeoutPv = mpv.getPropertyValue(KEY_TIMEOUT);
        Object value = timeoutPv == null ? null : timeoutPv.getValue();
        if (String.valueOf(Integer.MAX_VALUE).equals(value) == false) {
            throw new FatalBeanException(String.format("Timeout value(beanId= %s) must be %s." //
                    , beanKey, Integer.MAX_VALUE));
        }
    }

    for (Iterator<Map.Entry<String, BeanDefinition>> itr = references.entrySet().iterator(); itr.hasNext();) {
        Map.Entry<String, BeanDefinition> entry = itr.next();
        String beanKey = entry.getKey();
        BeanDefinition beanDef = entry.getValue();
        MutablePropertyValues mpv = beanDef.getPropertyValues();
        PropertyValue filter = mpv.getPropertyValue("filter");
        PropertyValue group = mpv.getPropertyValue("group");

        if (group == null || group.getValue() == null
                || KEY_GROUP_COMPENSABLE.equals(group.getValue()) == false) {
            logger.warn("The value of attr 'group'(beanId= {}) should be 'org.bytesoft.bytetcc'.", beanKey);
            continue;
        } else if (filter == null || filter.getValue() == null
                || KEY_FILTER_COMPENSABLE.equals(filter.getValue()) == false) {
            logger.warn("The value of attr 'filter'(beanId= {}) should be 'compensable'.", beanKey);
            continue;
        }

        PropertyValue timeoutPv = mpv.getPropertyValue(KEY_TIMEOUT);
        Object value = timeoutPv == null ? null : timeoutPv.getValue();
        if (String.valueOf(Integer.MAX_VALUE).equals(value) == false) {
            throw new FatalBeanException(
                    String.format("The value of attribute 'timeout' (beanId= %s) must be %s." //
                            , beanKey, Integer.MAX_VALUE));
        }
    }
}

From source file:com.jroossien.boxx.nms.NMS.java

public <T> Object loadFromNMS(Class<T> dep, Object... objects) {
    if (!dep.isAnnotationPresent(NMSDependant.class))
        return null;
    NMSDependant nmsDependant = dep.getAnnotation(NMSDependant.class);
    Class<?> impl = null;/* www.j ava2 s. c  o  m*/
    try {
        impl = Class.forName(nmsDependant.implementationPath() + "." + dep.getSimpleName() + "_" + version);
        return ConstructorUtils.invokeConstructor(impl, objects);
    } catch (ClassNotFoundException e) {
        Boxx.get().error("The current version is not supported: " + version + ".\n" + e.getMessage());
    } catch (InstantiationException | IllegalAccessException | NoSuchMethodException
            | InvocationTargetException e) {
        e.printStackTrace();
    }
    return impl;
}

From source file:com.baidu.terminator.register.ProtocolRegisterImpl.java

private void scanDefaultSigner() {
    AnnotationScanner<Signer> requestSignerScanner = new AnnotationScanner<Signer>(DEFAULT_PLUGIN_PACKAGE,
            DefaultSigner.class);
    List<Class<? extends Signer>> rsClasses = requestSignerScanner.scanAnnotatedClass();
    for (Class<? extends Signer> clazz : rsClasses) {
        DefaultSigner drs = clazz.getAnnotation(DefaultSigner.class);

        String protocol = drs.protocol();
        protocolRequestSignerMap.put(protocol, clazz);

        String message = drs.message();

        PluginInfo pi = new PluginInfo();
        pi.setName(clazz.getName());/*  w  w  w.  ja va  2  s . c  om*/
        pi.setProtocol(protocol);
        pi.setMessage(message);
        defaultSigners.add(pi);
    }
}

From source file:com.baidu.terminator.register.ProtocolRegisterImpl.java

private void scanCustomizedSigner() {
    AnnotationScanner<Signer> requestSignerScanner = new AnnotationScanner<Signer>(DEFAULT_PLUGIN_PACKAGE,
            CustomizedSigner.class);
    List<Class<? extends Signer>> rsClasses = requestSignerScanner.scanAnnotatedClass();
    for (Class<? extends Signer> clazz : rsClasses) {
        CustomizedSigner crs = clazz.getAnnotation(CustomizedSigner.class);
        String protocol = crs.protocol();
        String message = crs.message();

        PluginInfo cs = new PluginInfo();
        cs.setName(clazz.getName());//from   w  w  w .j  a va2  s.  c o m
        cs.setProtocol(protocol);
        cs.setMessage(message);
        customizedSigners.add(cs);
    }
}