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.nabla.wapp.server.database.InsertStatement.java

@SuppressWarnings("unchecked")
private IRecordTable commonConstructor(final Class clazz) {
    if (clazz == null)
        return null;
    final IRecordTable t = (IRecordTable) clazz.getAnnotation(IRecordTable.class);
    for (Field field : clazz.getDeclaredFields()) {
        final IRecordField definition = field.getAnnotation(IRecordField.class);
        if (definition == null)
            continue;
        if (definition.unique())
            uniqueFieldName = field.getName();
        parameters.add(createParameter(field));
    }//ww  w .j  a v  a 2 s  .c o  m
    final IRecordTable tt = commonConstructor(clazz.getSuperclass());
    return (t == null) ? tt : t;
}

From source file:org.springmodules.validation.bean.conf.loader.annotation.handler.hibernate.HibernatePropertyValidationAnnotationHandler.java

/**
 * Handles the given property level annotation and manipulates the given bean validation configuration accordingly.
 *
 * @param annotation The annotation to handle.
 * @param clazz The annotated class.//from w w  w .  j  a  v a  2  s  .  c o m
 * @param descriptor The property descriptor of the annotated property.
 * @param configuration The bean validation configuration to manipulate.
 */
public void handleAnnotation(Annotation annotation, Class clazz, PropertyDescriptor descriptor,
        MutableBeanValidationConfiguration configuration) {

    if (annotation instanceof Valid) {
        configuration.addCascadeValidation(new CascadeValidation(descriptor.getName()));
        return;
    }

    Class annotationClass = annotation.annotationType();
    ValidatorClass validatorClassAnnotation = (ValidatorClass) annotationClass
            .getAnnotation(ValidatorClass.class);
    Class<? extends Validator> validatorClass = validatorClassAnnotation.value();
    Validator validator = (Validator) BeanUtils.instantiateClass(validatorClass);
    validator.initialize(annotation);
    Condition condition = Conditions.property(descriptor.getName(), new HibernateValidatorCondition(validator));
    String message;
    try {
        message = (String) annotationClass.getMethod("message").invoke(annotation);
    } catch (NoSuchMethodException nsme) {
        message = annotationClass.getSimpleName() + ".error";
    } catch (IllegalAccessException e) {
        message = annotationClass.getSimpleName() + ".error";
    } catch (InvocationTargetException e) {
        message = annotationClass.getSimpleName() + ".error";
    }

    configuration.addPropertyRule(descriptor.getName(),
            new DefaultValidationRule(condition, message, message, new Object[0]));

}

From source file:moe.encode.airblock.commands.core.components.ComponentBag.java

/**
 * Register the implemented interfaces of the superclass.
 * @param cls The type of the object.//  www.  j  a va 2s  .c  o  m
 * @param obj The object to register.
 */
private void registerComponent(Class<?> cls, Object obj) {
    Components components = cls.getAnnotation(Components.class);
    if (components == null)
        return;

    for (Class<?> interfaceCls : components.value()) {
        if (!this.methods.containsKey(interfaceCls))
            this.methods.put(interfaceCls, obj);
    }

    this.registerComponent(cls.getSuperclass(), obj);
}

From source file:com.thinkbiganalytics.feedmgr.sla.ServiceLevelAgreementMetricTransformer.java

public List<ServiceLevelAgreementRule> discoverSlaMetrics() {

    List<ServiceLevelAgreementRule> rules = new ArrayList<>();
    Set<Class<?>> metrics = ReflectionPolicyAnnotationDiscoverer
            .getTypesAnnotatedWith(ServiceLevelAgreementMetric.class);
    for (Class c : metrics) {
        List<FieldRuleProperty> properties = getUiProperties(c);
        ServiceLevelAgreementMetric policy = (ServiceLevelAgreementMetric) c
                .getAnnotation(ServiceLevelAgreementMetric.class);
        rules.add(buildUiModel(policy, c, properties));
    }/*from  w  w  w .j  av a  2 s.  c o  m*/
    return rules;
}

From source file:de.anhquan.config4j.internal.ConfigHandler.java

public ConfigHandler(Class<?> configType) {
    this.configType = configType;
    ConfigContainer annotation = configType.getAnnotation(ConfigContainer.class);
    configLocation = annotation.Location();
    prefix = annotation.Prefix();/*w w  w.  j  av  a 2 s . c  om*/

    try {
        configuration = new PropertiesConfiguration(configLocation);
    } catch (ConfigurationException e) {
        e.printStackTrace();
    }
}

From source file:de.openknowledge.jaxrs.versioning.conversion.InterversionConverter.java

public <T> T convertToHigherVersion(Class<T> targetType, Object source, String sourceVersion) {
    SupportedVersion supportedVersion = targetType.getAnnotation(SupportedVersion.class);
    if (supportedVersion == null) {
        throw new IllegalVersionException(sourceVersion);
    }/*from w ww . j  a v  a  2  s  . co m*/
    if (supportedVersion.version().equals(sourceVersion)) {
        mapper.map(source);
        return targetType.cast(source);
    }
    if (supportedVersion.previous() == Object.class) {
        throw new IllegalVersionException(sourceVersion);
    }
    Object previousVersion = convertToHigherVersion(supportedVersion.previous(), source, sourceVersion);
    return map(previousVersion, targetType, new DefaultVersionContext());
}

From source file:com.mycollab.aspect.MonitorItemAspect.java

@AfterReturning("execution(public * com.mycollab..service..*.saveWithSession(..)) && args(bean, username)")
public void traceSaveActivity(JoinPoint joinPoint, Object bean, String username) {
    Advised advised = (Advised) joinPoint.getThis();
    Class<?> cls = advised.getTargetSource().getTargetClass();
    try {/*from   w w w .  java 2  s .c o  m*/
        Watchable watchableAnnotation = cls.getAnnotation(Watchable.class);
        if (watchableAnnotation != null) {
            int sAccountId = (Integer) PropertyUtils.getProperty(bean, "saccountid");
            int typeId = (Integer) PropertyUtils.getProperty(bean, "id");
            Integer extraTypeId = null;
            if (!"".equals(watchableAnnotation.extraTypeId())) {
                extraTypeId = (Integer) PropertyUtils.getProperty(bean, watchableAnnotation.extraTypeId());
            }

            MonitorItem monitorItem = new MonitorItem();
            monitorItem.setMonitorDate(new GregorianCalendar().getTime());
            monitorItem.setType(ClassInfoMap.getType(cls));
            monitorItem.setTypeid(typeId);
            monitorItem.setExtratypeid(extraTypeId);
            monitorItem.setUser(username);
            monitorItem.setSaccountid(sAccountId);

            monitorItemService.saveWithSession(monitorItem, username);

            if (!watchableAnnotation.userFieldName().equals("")) {
                String moreUser = (String) PropertyUtils.getProperty(bean, watchableAnnotation.userFieldName());
                if (moreUser != null && !moreUser.equals(username)) {
                    monitorItem.setId(null);
                    monitorItem.setUser(moreUser);
                    monitorItemService.saveWithSession(monitorItem, moreUser);
                }
            }
        }

        Traceable traceAnnotation = cls.getAnnotation(Traceable.class);
        if (traceAnnotation != null) {
            int sAccountId = (Integer) PropertyUtils.getProperty(bean, "saccountid");
            int typeId = (Integer) PropertyUtils.getProperty(bean, "id");
            RelayEmailNotificationWithBLOBs relayNotification = new RelayEmailNotificationWithBLOBs();
            relayNotification.setChangeby(username);
            relayNotification.setChangecomment("");
            relayNotification.setSaccountid(sAccountId);
            relayNotification.setType(ClassInfoMap.getType(cls));
            relayNotification.setAction(MonitorTypeConstants.CREATE_ACTION);
            relayNotification.setTypeid("" + typeId);
            relayEmailNotificationService.saveWithSession(relayNotification, username);
            // Save notification item
        }
    } catch (Exception e) {
        LOG.error("Error when save relay email notification for save action of service " + cls.getName(), e);
    }
}

From source file:net.kaczmarzyk.spring.data.jpa.web.AnnotatedSpecInterfaceArgumentResolver.java

private final Object getAnnotation(Class<?> target) {
    for (Class<? extends Annotation> annotationType : annotationTypes) {
        Annotation potentialAnnotation = target.getAnnotation(annotationType);
        if (potentialAnnotation != null) {
            return potentialAnnotation;
        }//from   w  w w. j av  a 2  s  . com
    }
    return null;
}

From source file:com.msopentech.odatajclient.proxy.api.impl.EntityContainerInvocationHandler.java

private EntityContainerInvocationHandler(final ODataV3Client client, final Class<?> ref,
        final EntityContainerFactory factory) {

    super(client, null);

    final Annotation annotation = ref.getAnnotation(EntityContainer.class);
    if (!(annotation instanceof EntityContainer)) {
        throw new IllegalArgumentException(
                ref.getName() + " is not annotated as @" + EntityContainer.class.getSimpleName());
    }//from   w w w . j a v a2s  .  co  m

    this.factory = factory;
    this.entityContainerName = ((EntityContainer) annotation).name();
    this.defaultEC = ((EntityContainer) annotation).isDefaultEntityContainer();
    this.schemaName = ClassUtils.getNamespace(ref);
}

From source file:de.hybris.platform.webservices.util.objectgraphtransformer.impl.DefaultNodeConfig.java

public DefaultNodeConfig(final Class<?> type) {
    super();//from   ww  w . java  2 s.  co  m
    this.type = type;

    if (type.isAnnotationPresent(GraphNode.class)) {
        final GraphNode cfg = type.getAnnotation(GraphNode.class);
        if (cfg.uidProperties().trim().length() > 0) {
            setUidPropertyNames(cfg.uidProperties());
        }
    }
}