diff --git a/openide.filesystems/src/org/openide/filesystems/annotations/LayerGenerationException.java b/openide.filesystems/src/org/openide/filesystems/annotations/LayerGenerationException.java --- a/openide.filesystems/src/org/openide/filesystems/annotations/LayerGenerationException.java +++ b/openide.filesystems/src/org/openide/filesystems/annotations/LayerGenerationException.java @@ -39,13 +39,19 @@ package org.openide.filesystems.annotations; +import java.lang.annotation.Annotation; +import java.util.Map; import javax.annotation.processing.Messager; +import javax.annotation.processing.ProcessingEnvironment; import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.AnnotationValue; import javax.lang.model.element.Element; +import javax.lang.model.element.ExecutableElement; +import javax.lang.model.element.TypeElement; /** * Exception thrown when a layer entry cannot be generated due to erroneous sources. + * @see LayerGeneratingProcessor * @since org.openide.filesystems 7.15 */ public class LayerGenerationException extends Exception { @@ -60,7 +66,7 @@ * @see Messager#printMessage(javax.tools.Diagnostic.Kind, CharSequence) */ public LayerGenerationException(String message) { - this(message, null, null, null); + this(message, (Element) null, (AnnotationMirror) null, (AnnotationValue) null); } /** @@ -70,7 +76,7 @@ * @see Messager#printMessage(javax.tools.Diagnostic.Kind, CharSequence, Element) */ public LayerGenerationException(String message, Element erroneousElement) { - this(message, erroneousElement, null, null); + this(message, erroneousElement, (AnnotationMirror) null, (AnnotationValue) null); } /** @@ -81,7 +87,7 @@ * @see Messager#printMessage(javax.tools.Diagnostic.Kind, CharSequence, Element, AnnotationMirror) */ public LayerGenerationException(String message, Element erroneousElement, AnnotationMirror erroneousAnnotation) { - this(message, erroneousElement, erroneousAnnotation, null); + this(message, erroneousElement, erroneousAnnotation, (AnnotationValue) null); } /** @@ -99,4 +105,63 @@ this.erroneousAnnotationValue = erroneousAnnotationValue; } + /** + * An exception with an associated annotation. + * Convenience constructor which locates an annotation on the erroneous element for you. + * @param message a detail message which could be reported to the user + * @param erroneousElement the associated element + * @param processingEnv the processing environment passed to the processor + * @param erroneousAnnotation the type of the annotation on the element + * @see Messager#printMessage(javax.tools.Diagnostic.Kind, CharSequence, Element, AnnotationMirror) + */ + public LayerGenerationException(String message, Element erroneousElement, ProcessingEnvironment processingEnv, + Class erroneousAnnotation) { + this(message, erroneousElement, processingEnv, erroneousAnnotation, (String) null); + } + + /** + * An exception with an associated annotation value. + * Convenience constructor which locates an annotation and its value on the erroneous element for you. + * @param message a detail message which could be reported to the user + * @param erroneousElement the associated element + * @param processingEnv the processing environment passed to the processor + * @param erroneousAnnotation the type of the annotation on the element + * @param erroneousAnnotationValue the name of a method in that annotation + * @see Messager#printMessage(javax.tools.Diagnostic.Kind, CharSequence, Element, AnnotationMirror, AnnotationValue) + */ + public LayerGenerationException(String message, Element erroneousElement, ProcessingEnvironment processingEnv, + Class erroneousAnnotation, String erroneousAnnotationValue) { + super(message); + this.erroneousElement = erroneousElement; + if (erroneousAnnotationValue != null) { + try { + erroneousAnnotation.getMethod(erroneousAnnotationValue); + } catch (NoSuchMethodException x) { + throw new IllegalArgumentException("No such method " + erroneousAnnotationValue + " in " + erroneousAnnotation); + } catch (SecurityException x) {/* ignore? */} + } + this.erroneousAnnotation = findAnnotationMirror(erroneousElement, processingEnv, erroneousAnnotation); + this.erroneousAnnotationValue = this.erroneousAnnotation != null && erroneousAnnotationValue != null ? + findAnnotationValue(this.erroneousAnnotation, erroneousAnnotationValue) : null; + } + + private static AnnotationMirror findAnnotationMirror(Element element, ProcessingEnvironment processingEnv, Class annotation) { + for (AnnotationMirror ann : element.getAnnotationMirrors()) { + if (processingEnv.getElementUtils().getBinaryName((TypeElement) ann.getAnnotationType().asElement()). + contentEquals(annotation.getName())) { + return ann; + } + } + return null; + } + + private AnnotationValue findAnnotationValue(AnnotationMirror annotation, String name) { + for (Map.Entry entry : annotation.getElementValues().entrySet()) { + if (entry.getKey().getSimpleName().contentEquals(name)) { + return entry.getValue(); + } + } + return null; + } + }