Search in sources :

Example 1 with MethodInfo

use of org.jboss.jandex.MethodInfo in project wildfly by wildfly.

the class PassivationAnnotationParsingProcessor method processPassivation.

private void processPassivation(final EEModuleDescription eeModuleDescription, final AnnotationTarget target, final DotName annotationType, final EEApplicationClasses applicationClasses) throws DeploymentUnitProcessingException {
    if (!(target instanceof MethodInfo)) {
        throw EeLogger.ROOT_LOGGER.methodOnlyAnnotation(annotationType);
    }
    final MethodInfo methodInfo = MethodInfo.class.cast(target);
    final ClassInfo classInfo = methodInfo.declaringClass();
    final EEModuleClassDescription classDescription = eeModuleDescription.addOrGetLocalClassDescription(classInfo.name().toString());
    final Type[] args = methodInfo.args();
    if (args.length > 1) {
        ROOT_LOGGER.warn(EeLogger.ROOT_LOGGER.invalidNumberOfArguments(methodInfo.name(), annotationType, classInfo.name()));
        return;
    } else if (args.length == 1 && !args[0].name().toString().equals(InvocationContext.class.getName())) {
        ROOT_LOGGER.warn(EeLogger.ROOT_LOGGER.invalidSignature(methodInfo.name(), annotationType, classInfo.name(), "void methodName(InvocationContext ctx)"));
        return;
    }
    final MethodIdentifier methodIdentifier;
    if (args.length == 0) {
        methodIdentifier = MethodIdentifier.getIdentifier(Void.TYPE, methodInfo.name());
    } else {
        methodIdentifier = MethodIdentifier.getIdentifier(Void.TYPE, methodInfo.name(), InvocationContext.class);
    }
    final InterceptorClassDescription.Builder builder = InterceptorClassDescription.builder(classDescription.getInterceptorClassDescription());
    if (annotationType == POST_ACTIVATE) {
        builder.setPostActivate(methodIdentifier);
    } else {
        builder.setPrePassivate(methodIdentifier);
    }
    classDescription.setInterceptorClassDescription(builder.build());
}
Also used : Type(org.jboss.jandex.Type) InterceptorClassDescription(org.jboss.as.ee.component.interceptors.InterceptorClassDescription) MethodInfo(org.jboss.jandex.MethodInfo) EEModuleClassDescription(org.jboss.as.ee.component.EEModuleClassDescription) MethodIdentifier(org.jboss.invocation.proxy.MethodIdentifier) InvocationContext(javax.interceptor.InvocationContext) ClassInfo(org.jboss.jandex.ClassInfo)

Example 2 with MethodInfo

use of org.jboss.jandex.MethodInfo in project wildfly-swarm by wildfly-swarm.

the class HealthAnnotationProcessor method process.

@Override
public void process() throws NamingException {
    // first pass: jboss-web context root
    Optional<String> jbossWebContext = Optional.empty();
    // if (archive instanceof JBossWebContainer) {
    if (archive.getName().endsWith(".war")) {
        JBossWebContainer war = archive.as(WARArchive.class);
        if (war.getContextRoot() != null) {
            jbossWebContext = Optional.of(war.getContextRoot());
        }
    }
    // second pass: JAX-RS applications
    Optional<String> appPath = Optional.empty();
    Collection<AnnotationInstance> appPathAnnotations = index.getAnnotations(APP_PATH);
    for (AnnotationInstance annotation : appPathAnnotations) {
        if (annotation.target().kind() == AnnotationTarget.Kind.CLASS) {
            appPath = Optional.of(annotation.value().asString());
        }
    }
    // third pass: JAX-RS resources
    Collection<AnnotationInstance> pathAnnotations = index.getAnnotations(PATH);
    for (AnnotationInstance annotation : pathAnnotations) {
        if (annotation.target().kind() == AnnotationTarget.Kind.CLASS) {
            ClassInfo classInfo = annotation.target().asClass();
            for (MethodInfo methodInfo : classInfo.methods()) {
                if (methodInfo.hasAnnotation(HEALTH)) {
                    StringBuilder sb = new StringBuilder();
                    boolean isSecure = false;
                    // prepend the jboss-web cntext if given
                    if (jbossWebContext.isPresent() && !jbossWebContext.get().equals("/")) {
                        safeAppend(sb, jbossWebContext.get());
                    }
                    // prepend the appPath if given
                    if (appPath.isPresent() && !appPath.get().equals("/")) {
                        safeAppend(sb, appPath.get());
                    }
                    // the class level @Path
                    for (AnnotationInstance classAnnotation : classInfo.classAnnotations()) {
                        if (classAnnotation.name().equals(PATH)) {
                            String methodPathValue = classAnnotation.value().asString();
                            if (!methodPathValue.equals("/")) {
                                safeAppend(sb, methodPathValue);
                            }
                        }
                    }
                    if (methodInfo.hasAnnotation(PATH)) {
                        // the method level @Path
                        safeAppend(sb, methodInfo.annotation(PATH).value().asString());
                        // the method level @Health
                        AnnotationInstance healthAnnotation = methodInfo.annotation(HEALTH);
                        isSecure = healthAnnotation.value("inheritSecurity") != null ? healthAnnotation.value("inheritSecurity").asBoolean() : true;
                    } else {
                        throw new RuntimeException("@Health requires an explicit @Path annotation");
                    }
                    HealthMetaData metaData = new HealthMetaData(sb.toString(), isSecure);
                    Monitor.lookup().registerHealth(metaData);
                }
            }
        }
    }
}
Also used : HealthMetaData(org.wildfly.swarm.monitor.HealthMetaData) JBossWebContainer(org.wildfly.swarm.undertow.descriptors.JBossWebContainer) MethodInfo(org.jboss.jandex.MethodInfo) AnnotationInstance(org.jboss.jandex.AnnotationInstance) ClassInfo(org.jboss.jandex.ClassInfo)

Example 3 with MethodInfo

use of org.jboss.jandex.MethodInfo in project wildfly by wildfly.

the class JPAAnnotationProcessor method processPersistenceAnnotations.

private void processPersistenceAnnotations(final DeploymentUnit deploymentUnit, final EEModuleDescription eeModuleDescription, List<AnnotationInstance> persistenceContexts, final EEApplicationClasses applicationClasses) throws DeploymentUnitProcessingException {
    for (AnnotationInstance annotation : persistenceContexts) {
        ClassInfo declaringClass;
        final AnnotationTarget annotationTarget = annotation.target();
        if (annotationTarget instanceof FieldInfo) {
            FieldInfo fieldInfo = (FieldInfo) annotationTarget;
            declaringClass = fieldInfo.declaringClass();
            EEModuleClassDescription eeModuleClassDescription = eeModuleDescription.addOrGetLocalClassDescription(declaringClass.name().toString());
            this.processField(deploymentUnit, annotation, fieldInfo, eeModuleClassDescription);
        } else if (annotationTarget instanceof MethodInfo) {
            MethodInfo methodInfo = (MethodInfo) annotationTarget;
            declaringClass = methodInfo.declaringClass();
            EEModuleClassDescription eeModuleClassDescription = eeModuleDescription.addOrGetLocalClassDescription(declaringClass.name().toString());
            this.processMethod(deploymentUnit, annotation, methodInfo, eeModuleClassDescription);
        } else if (annotationTarget instanceof ClassInfo) {
            declaringClass = (ClassInfo) annotationTarget;
            EEModuleClassDescription eeModuleClassDescription = eeModuleDescription.addOrGetLocalClassDescription(declaringClass.name().toString());
            this.processClass(deploymentUnit, annotation, eeModuleClassDescription);
        }
    }
}
Also used : AnnotationTarget(org.jboss.jandex.AnnotationTarget) MethodInfo(org.jboss.jandex.MethodInfo) EEModuleClassDescription(org.jboss.as.ee.component.EEModuleClassDescription) AnnotationInstance(org.jboss.jandex.AnnotationInstance) FieldInfo(org.jboss.jandex.FieldInfo) ClassInfo(org.jboss.jandex.ClassInfo)

Example 4 with MethodInfo

use of org.jboss.jandex.MethodInfo in project wildfly by wildfly.

the class ServerInterceptorCache method findAnnotatedMethods.

private List<InterceptorFactory> findAnnotatedMethods(final Class<?> interceptorClass, final Index index, final Class<?> annotationClass) {
    final List<InterceptorFactory> interceptorFactories = new ArrayList<>();
    final DotName annotationName = DotName.createSimple(annotationClass.getName());
    final List<AnnotationInstance> annotations = index.getAnnotations(annotationName);
    for (final AnnotationInstance annotation : annotations) {
        if (annotation.target().kind() == AnnotationTarget.Kind.METHOD) {
            final MethodInfo methodInfo = annotation.target().asMethod();
            final Constructor<?> constructor;
            try {
                constructor = interceptorClass.getConstructor();
            } catch (NoSuchMethodException e) {
                throw EjbLogger.ROOT_LOGGER.serverInterceptorNoEmptyConstructor(interceptorClass.toString(), e);
            }
            try {
                final Method annotatedMethod = interceptorClass.getMethod(methodInfo.name(), new Class[] { InvocationContext.class });
                final InterceptorFactory interceptorFactory = createInterceptorFactoryForServerInterceptor(annotatedMethod, constructor);
                interceptorFactories.add(interceptorFactory);
            } catch (NoSuchMethodException e) {
                throw EjbLogger.ROOT_LOGGER.serverInterceptorInvalidMethod(methodInfo.name(), interceptorClass.toString(), annotationClass.toString(), e);
            }
        }
    }
    return interceptorFactories;
}
Also used : InterceptorFactory(org.jboss.invocation.InterceptorFactory) ContainerInterceptorMethodInterceptorFactory(org.jboss.as.ejb3.component.ContainerInterceptorMethodInterceptorFactory) ArrayList(java.util.ArrayList) MethodInfo(org.jboss.jandex.MethodInfo) Method(java.lang.reflect.Method) DotName(org.jboss.jandex.DotName) AnnotationInstance(org.jboss.jandex.AnnotationInstance)

Example 5 with MethodInfo

use of org.jboss.jandex.MethodInfo in project wildfly by wildfly.

the class JandexAnnotationRepositoryImpl method getAnnotation.

@Override
public Collection<Annotation> getAnnotation(Class<?> annotationClass) {
    List<AnnotationInstance> instances = backingRepository.getAnnotations(DotName.createSimple(annotationClass.getName()));
    ArrayList<Annotation> annotations = new ArrayList<Annotation>(instances.size());
    for (AnnotationInstance instance : instances) {
        AnnotationTarget target = instance.target();
        Annotation annotation = null;
        if (target instanceof MethodInfo) {
            MethodInfo m = (MethodInfo) target;
            List<String> parameterTypes = new ArrayList<String>(m.args().length);
            for (Type type : m.args()) {
                parameterTypes.add(type.toString());
            }
            String declaringClass = m.declaringClass().name().toString();
            annotation = new AnnotationImpl(declaringClass, cl, parameterTypes, m.name(), true, false, annotationClass);
        }
        if (target instanceof FieldInfo) {
            FieldInfo f = (FieldInfo) target;
            String declaringClass = f.declaringClass().name().toString();
            annotation = new AnnotationImpl(declaringClass, cl, null, f.name(), false, true, annotationClass);
        }
        if (target instanceof ClassInfo) {
            ClassInfo c = (ClassInfo) target;
            annotation = new AnnotationImpl(c.name().toString(), cl, null, null, false, false, annotationClass);
        }
        if (annotation != null) {
            annotations.add(annotation);
        }
    }
    annotations.trimToSize();
    if (annotations.isEmpty()) {
        return null;
    } else {
        return Collections.unmodifiableList(annotations);
    }
}
Also used : AnnotationTarget(org.jboss.jandex.AnnotationTarget) ArrayList(java.util.ArrayList) Annotation(org.jboss.jca.common.spi.annotations.repository.Annotation) Type(org.jboss.jandex.Type) MethodInfo(org.jboss.jandex.MethodInfo) AnnotationInstance(org.jboss.jandex.AnnotationInstance) FieldInfo(org.jboss.jandex.FieldInfo) ClassInfo(org.jboss.jandex.ClassInfo)

Aggregations

MethodInfo (org.jboss.jandex.MethodInfo)24 AnnotationInstance (org.jboss.jandex.AnnotationInstance)18 ClassInfo (org.jboss.jandex.ClassInfo)18 EEModuleClassDescription (org.jboss.as.ee.component.EEModuleClassDescription)7 FieldInfo (org.jboss.jandex.FieldInfo)7 AnnotationTarget (org.jboss.jandex.AnnotationTarget)6 ArrayList (java.util.ArrayList)5 HashSet (java.util.HashSet)5 InterceptorClassDescription (org.jboss.as.ee.component.interceptors.InterceptorClassDescription)5 InvocationContext (javax.interceptor.InvocationContext)4 AnnotationValue (org.jboss.jandex.AnnotationValue)4 Type (org.jboss.jandex.Type)4 DeploymentUnit (org.jboss.as.server.deployment.DeploymentUnit)3 CompositeIndex (org.jboss.as.server.deployment.annotation.CompositeIndex)3 MethodIdentifier (org.jboss.invocation.proxy.MethodIdentifier)3 EEModuleDescription (org.jboss.as.ee.component.EEModuleDescription)2 DotName (org.jboss.jandex.DotName)2 MethodParameterInfo (org.jboss.jandex.MethodParameterInfo)2 PropertyReplacer (org.jboss.metadata.property.PropertyReplacer)2 JBossWebContainer (org.wildfly.swarm.undertow.descriptors.JBossWebContainer)2