Search in sources :

Example 1 with AnnotationHandler

use of org.glassfish.apf.AnnotationHandler in project Payara by payara.

the class AnnotationProcessorImpl method process.

private void process(ProcessingContext ctx, AnnotationInfo element, HandlerProcessingResultImpl result) throws AnnotationProcessorException {
    Annotation annotation = element.getAnnotation();
    if (AnnotationUtils.shouldLog("annotation")) {
        logger.finer("Annotation : " + annotation.annotationType().getName() + " delegate = " + delegate);
    }
    result.addResult(annotation.annotationType(), ResultType.UNPROCESSED);
    // we ignore all java.* annotations
    Package annPackage = annotation.annotationType().getPackage();
    if (annPackage != null && annPackage.getName().startsWith("java.lang"))
        return;
    List<AnnotationHandler> annotationHandlers = handlers.get(annotation.annotationType().getName());
    if (annotationHandlers != null) {
        for (AnnotationHandler handler : annotationHandlers) {
            // here we need to be careful, we are ready to invoke a handler
            // to process a particular annotation type. However, this handler
            // may have defined a list of annotations that should be processed
            // (if present on the annotated element) before itself.
            // do this check and process those annotations first.
            Class<? extends Annotation>[] dependencies = handler.getTypeDependencies();
            if (dependencies != null) {
                AnnotatedElement ae = element.getAnnotatedElement();
                for (Class<? extends Annotation> annotationType : dependencies) {
                    Annotation depAnnotation = ae.getAnnotation(annotationType);
                    if (depAnnotation != null) {
                        ResultType resultType = result.processedAnnotations().get(annotationType);
                        if (resultType == null || resultType == ResultType.UNPROCESSED) {
                            // annotation is present, process it.
                            AnnotationInfo info = new AnnotationInfo(ctx, ae, depAnnotation, getTopElementType());
                            process(ctx, info, result);
                        }
                    }
                }
            }
            // at this point, all annotation that I declared depending on
            // are processed
            HandlerProcessingResult processingResult = null;
            try {
                processingResult = handler.processAnnotation(element);
            } catch (AnnotationProcessorException ape) {
                // I am logging this exception
                log(Level.SEVERE, ape.getLocator(), ape.getMessage());
                // errors.
                if (ape.isFatal()) {
                    throw ape;
                }
                if (++errorCount > 100) {
                    throw new AnnotationProcessorException(AnnotationUtils.getLocalString("enterprise.deployment.annotation.toomanyerror", "Too many errors, annotation processing abandoned."));
                }
                processingResult = HandlerProcessingResultImpl.getDefaultResult(annotation.annotationType(), ResultType.FAILED);
            } catch (Throwable e) {
                AnnotationProcessorException ape = new AnnotationProcessorException(e.getMessage(), element);
                ape.initCause(e);
                throw ape;
            }
            result.addAll(processingResult);
        }
    } else {
        if (delegate != null) {
            delegate.process(ctx, element, result);
        } else {
            ctx.getErrorHandler().fine(new AnnotationProcessorException("No handler defined for " + annotation.annotationType()));
        }
    }
}
Also used : HandlerProcessingResult(org.glassfish.apf.HandlerProcessingResult) AnnotatedElement(java.lang.reflect.AnnotatedElement) ResultType(org.glassfish.apf.ResultType) Annotation(java.lang.annotation.Annotation) AnnotationHandler(org.glassfish.apf.AnnotationHandler) AnnotationProcessorException(org.glassfish.apf.AnnotationProcessorException) AnnotationInfo(org.glassfish.apf.AnnotationInfo)

Example 2 with AnnotationHandler

use of org.glassfish.apf.AnnotationHandler in project Payara by payara.

the class SJSASFactory method postConstruct.

@SuppressWarnings({ "unused", "unchecked" })
@PostConstruct
private void postConstruct() {
    if (systemProcessor != null && systemProcessorMetaDataComplete != null)
        return;
    // initialize our system annotation processor...
    systemProcessor = new AnnotationProcessorImpl();
    systemProcessorMetaDataComplete = new AnnotationProcessorImpl();
    for (ActiveDescriptor<?> i : locator.getDescriptors(BuilderHelper.createContractFilter(AnnotationHandler.class.getName()))) {
        ActiveDescriptor<AnnotationHandler> descriptor = (ActiveDescriptor<AnnotationHandler>) i;
        String annotationTypeName = getAnnotationHandlerForStringValue(descriptor);
        if (annotationTypeName == null)
            continue;
        systemProcessor.pushAnnotationHandler(annotationTypeName, new LazyAnnotationHandler(descriptor));
        annotationClassNames.add("L" + annotationTypeName.replace('.', '/') + ";");
        // falling in this category in the future, add them to this list
        if (annotationTypeName.equals("javax.annotation.ManagedBean")) {
            systemProcessorMetaDataComplete.pushAnnotationHandler(annotationTypeName, new LazyAnnotationHandler(descriptor));
            annotationClassNamesMetaDataComplete.add("L" + annotationTypeName.replace('.', '/') + ";");
        }
    }
}
Also used : ActiveDescriptor(org.glassfish.hk2.api.ActiveDescriptor) AnnotationHandler(org.glassfish.apf.AnnotationHandler) AnnotationProcessorImpl(org.glassfish.apf.impl.AnnotationProcessorImpl) PostConstruct(javax.annotation.PostConstruct)

Aggregations

AnnotationHandler (org.glassfish.apf.AnnotationHandler)2 Annotation (java.lang.annotation.Annotation)1 AnnotatedElement (java.lang.reflect.AnnotatedElement)1 PostConstruct (javax.annotation.PostConstruct)1 AnnotationInfo (org.glassfish.apf.AnnotationInfo)1 AnnotationProcessorException (org.glassfish.apf.AnnotationProcessorException)1 HandlerProcessingResult (org.glassfish.apf.HandlerProcessingResult)1 ResultType (org.glassfish.apf.ResultType)1 AnnotationProcessorImpl (org.glassfish.apf.impl.AnnotationProcessorImpl)1 ActiveDescriptor (org.glassfish.hk2.api.ActiveDescriptor)1