Search in sources :

Example 16 with AnnotatedElement

use of java.lang.reflect.AnnotatedElement in project uPortal by Jasig.

the class ModelAttributeService method init.

@PostConstruct
public void init() {
    /*
         * Gather classes & methods that reference @SoffitMoldelAttribute
         */
    final Map<AnnotatedElement, Object> map = new HashMap<>();
    final String[] beanNames = applicationContext.getBeanDefinitionNames();
    for (String name : beanNames) {
        final Object bean = applicationContext.getBean(name);
        final Class clazz = AopUtils.isAopProxy(bean) ? AopUtils.getTargetClass(bean) : bean.getClass();
        if (clazz.isAnnotationPresent(SoffitModelAttribute.class)) {
            // The bean itself is the model attribute
            map.put(clazz, bean);
        } else {
            // Check the bean for annotated methods...
            for (Method m : clazz.getMethods()) {
                if (m.isAnnotationPresent(SoffitModelAttribute.class)) {
                    map.put(m, bean);
                }
            }
        }
    }
    logger.debug("Found {} beans and/or methods referencing @SoffitModelAttribute", map.size());
    modelAttributes = Collections.unmodifiableMap(map);
}
Also used : HashMap(java.util.HashMap) AnnotatedElement(java.lang.reflect.AnnotatedElement) Method(java.lang.reflect.Method) PostConstruct(javax.annotation.PostConstruct)

Example 17 with AnnotatedElement

use of java.lang.reflect.AnnotatedElement in project uPortal by Jasig.

the class ModelAttributeService method gatherModelAttributes.

/* package-private */
Map<String, Object> gatherModelAttributes(String viewName, HttpServletRequest req, HttpServletResponse res, PortalRequest portalRequest, Bearer bearer, Preferences preferences, Definition definition) {
    final Map<String, Object> rslt = new HashMap<>();
    logger.debug("Processing model attributes for viewName='{}'", viewName);
    for (Map.Entry<AnnotatedElement, Object> y : modelAttributes.entrySet()) {
        final AnnotatedElement annotatedElement = y.getKey();
        final Object bean = y.getValue();
        final SoffitModelAttribute sma = annotatedElement.getAnnotation(SoffitModelAttribute.class);
        if (attributeAppliesToView(sma, viewName)) {
            logger.debug("The following  SoffitModelAttribute applies to viewName='{}':  {}", viewName, sma);
            final String modelAttributeName = sma.value();
            // Are we looking at a class or a method?
            if (annotatedElement instanceof Class) {
                // The bean itself is the model attribute
                rslt.put(modelAttributeName, bean);
            } else if (annotatedElement instanceof Method) {
                final Method m = (Method) annotatedElement;
                final Object modelAttribute = getModelAttributeFromMethod(bean, m, req, res, portalRequest, bearer, preferences, definition);
                rslt.put(modelAttributeName, modelAttribute);
            } else {
                final String msg = "Unsupported AnnotatedElement type:  " + AnnotatedElement.class.getName();
                throw new UnsupportedOperationException(msg);
            }
        }
    }
    logger.debug("Calculated the following model attributes for viewName='{}':  {}", viewName, rslt);
    return rslt;
}
Also used : HashMap(java.util.HashMap) AnnotatedElement(java.lang.reflect.AnnotatedElement) Method(java.lang.reflect.Method) HashMap(java.util.HashMap) Map(java.util.Map)

Example 18 with AnnotatedElement

use of java.lang.reflect.AnnotatedElement in project hibernate-orm by hibernate.

the class Ejb3XmlTestCase method getReader.

protected JPAOverriddenAnnotationReader getReader(Class<?> entityClass, String fieldName, String ormResourceName) throws Exception {
    AnnotatedElement el = getAnnotatedElement(entityClass, fieldName);
    XMLContext xmlContext = getContext(ormResourceName);
    return new JPAOverriddenAnnotationReader(el, xmlContext, ClassLoaderAccessTestingImpl.INSTANCE);
}
Also used : XMLContext(org.hibernate.cfg.annotations.reflection.XMLContext) AnnotatedElement(java.lang.reflect.AnnotatedElement) JPAOverriddenAnnotationReader(org.hibernate.cfg.annotations.reflection.JPAOverriddenAnnotationReader)

Example 19 with AnnotatedElement

use of java.lang.reflect.AnnotatedElement in project jersey by jersey.

the class ParamInjectionResolver method hasEncodedAnnotation.

private boolean hasEncodedAnnotation(Injectee injectee) {
    AnnotatedElement element = injectee.getParent();
    final boolean isConstructor = element instanceof Constructor;
    final boolean isMethod = element instanceof Method;
    // if injectee is method or constructor, check its parameters
    if (isConstructor || isMethod) {
        Annotation[] annotations;
        if (isMethod) {
            annotations = ((Method) element).getParameterAnnotations()[injectee.getPosition()];
        } else {
            annotations = ((Constructor) element).getParameterAnnotations()[injectee.getPosition()];
        }
        for (Annotation annotation : annotations) {
            if (annotation.annotationType().equals(Encoded.class)) {
                return true;
            }
        }
    }
    // check injectee itself (method, constructor or field)
    if (element.isAnnotationPresent(Encoded.class)) {
        return true;
    }
    // check class which contains injectee
    Class<?> clazz = injectee.getInjecteeClass();
    return clazz.isAnnotationPresent(Encoded.class);
}
Also used : Constructor(java.lang.reflect.Constructor) AnnotatedElement(java.lang.reflect.AnnotatedElement) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation)

Example 20 with AnnotatedElement

use of java.lang.reflect.AnnotatedElement in project newts by OpenNMS.

the class MergeSort method createCmdLineParser.

private CmdLineParser createCmdLineParser() {
    return new CmdLineParser(this) {

        @SuppressWarnings("rawtypes")
        @Override
        public void addArgument(final Setter setter, Argument a) {
            Setter newSetter = setter;
            if (setter instanceof MethodSetter) {
                newSetter = new Setter() {

                    @SuppressWarnings("unchecked")
                    @Override
                    public void addValue(Object value) throws CmdLineException {
                        setter.addValue(value);
                    }

                    @Override
                    public Class getType() {
                        return setter.getType();
                    }

                    @Override
                    public boolean isMultiValued() {
                        return false;
                    }

                    @Override
                    public FieldSetter asFieldSetter() {
                        return setter.asFieldSetter();
                    }

                    @Override
                    public AnnotatedElement asAnnotatedElement() {
                        return setter.asAnnotatedElement();
                    }
                };
            }
            super.addArgument(newSetter, a);
        }
    };
}
Also used : CmdLineParser(org.kohsuke.args4j.CmdLineParser) Argument(org.kohsuke.args4j.Argument) MethodSetter(org.kohsuke.args4j.spi.MethodSetter) FieldSetter(org.kohsuke.args4j.spi.FieldSetter) MethodSetter(org.kohsuke.args4j.spi.MethodSetter) Setter(org.kohsuke.args4j.spi.Setter) FieldSetter(org.kohsuke.args4j.spi.FieldSetter) AnnotatedElement(java.lang.reflect.AnnotatedElement) CmdLineException(org.kohsuke.args4j.CmdLineException)

Aggregations

AnnotatedElement (java.lang.reflect.AnnotatedElement)29 Test (org.junit.Test)7 Method (java.lang.reflect.Method)5 HashMap (java.util.HashMap)4 SimpleType (com.mysema.codegen.model.SimpleType)3 Type (com.mysema.codegen.model.Type)3 EntityType (com.querydsl.codegen.EntityType)3 Property (com.querydsl.codegen.Property)3 Annotation (java.lang.annotation.Annotation)3 Type (java.lang.reflect.Type)3 ArrayList (java.util.ArrayList)3 UncaughtException (com.carrotsearch.randomizedtesting.RandomizedRunner.UncaughtException)2 QueryEntity (com.querydsl.core.annotations.QueryEntity)2 Constructor (java.lang.reflect.Constructor)2 Nullable (javax.annotation.Nullable)2 MappingException (org.hibernate.MappingException)2 org.hibernate.mapping (org.hibernate.mapping)2 Nullable (org.jetbrains.annotations.Nullable)2 AssumptionViolatedException (org.junit.internal.AssumptionViolatedException)2 StoppedByUserException (org.junit.runner.notification.StoppedByUserException)2