Search in sources :

Example 1 with ResultType

use of org.glassfish.apf.ResultType 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)

Aggregations

Annotation (java.lang.annotation.Annotation)1 AnnotatedElement (java.lang.reflect.AnnotatedElement)1 AnnotationHandler (org.glassfish.apf.AnnotationHandler)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