This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 149136
Collapse All | Expand All

(-)a/openide.filesystems/src/org/openide/filesystems/annotations/LayerGenerationException.java (-3 / +68 lines)
Lines 39-51 Link Here
39
39
40
package org.openide.filesystems.annotations;
40
package org.openide.filesystems.annotations;
41
41
42
import java.lang.annotation.Annotation;
43
import java.util.Map;
42
import javax.annotation.processing.Messager;
44
import javax.annotation.processing.Messager;
45
import javax.annotation.processing.ProcessingEnvironment;
43
import javax.lang.model.element.AnnotationMirror;
46
import javax.lang.model.element.AnnotationMirror;
44
import javax.lang.model.element.AnnotationValue;
47
import javax.lang.model.element.AnnotationValue;
45
import javax.lang.model.element.Element;
48
import javax.lang.model.element.Element;
49
import javax.lang.model.element.ExecutableElement;
50
import javax.lang.model.element.TypeElement;
46
51
47
/**
52
/**
48
 * Exception thrown when a layer entry cannot be generated due to erroneous sources.
53
 * Exception thrown when a layer entry cannot be generated due to erroneous sources.
54
 * @see LayerGeneratingProcessor
49
 * @since org.openide.filesystems 7.15
55
 * @since org.openide.filesystems 7.15
50
 */
56
 */
51
public class LayerGenerationException extends Exception {
57
public class LayerGenerationException extends Exception {
Lines 60-66 Link Here
60
     * @see Messager#printMessage(javax.tools.Diagnostic.Kind, CharSequence)
66
     * @see Messager#printMessage(javax.tools.Diagnostic.Kind, CharSequence)
61
     */
67
     */
62
    public LayerGenerationException(String message) {
68
    public LayerGenerationException(String message) {
63
        this(message, null, null, null);
69
        this(message, (Element) null, (AnnotationMirror) null, (AnnotationValue) null);
64
    }
70
    }
65
71
66
    /**
72
    /**
Lines 70-76 Link Here
70
     * @see Messager#printMessage(javax.tools.Diagnostic.Kind, CharSequence, Element)
76
     * @see Messager#printMessage(javax.tools.Diagnostic.Kind, CharSequence, Element)
71
     */
77
     */
72
    public LayerGenerationException(String message, Element erroneousElement) {
78
    public LayerGenerationException(String message, Element erroneousElement) {
73
        this(message, erroneousElement, null, null);
79
        this(message, erroneousElement, (AnnotationMirror) null, (AnnotationValue) null);
74
    }
80
    }
75
81
76
    /**
82
    /**
Lines 81-87 Link Here
81
     * @see Messager#printMessage(javax.tools.Diagnostic.Kind, CharSequence, Element, AnnotationMirror)
87
     * @see Messager#printMessage(javax.tools.Diagnostic.Kind, CharSequence, Element, AnnotationMirror)
82
     */
88
     */
83
    public LayerGenerationException(String message, Element erroneousElement, AnnotationMirror erroneousAnnotation) {
89
    public LayerGenerationException(String message, Element erroneousElement, AnnotationMirror erroneousAnnotation) {
84
        this(message, erroneousElement, erroneousAnnotation, null);
90
        this(message, erroneousElement, erroneousAnnotation, (AnnotationValue) null);
85
    }
91
    }
86
92
87
    /**
93
    /**
Lines 99-102 Link Here
99
        this.erroneousAnnotationValue = erroneousAnnotationValue;
105
        this.erroneousAnnotationValue = erroneousAnnotationValue;
100
    }
106
    }
101
107
108
    /**
109
     * An exception with an associated annotation.
110
     * Convenience constructor which locates an annotation on the erroneous element for you.
111
     * @param message a detail message which could be reported to the user
112
     * @param erroneousElement the associated element
113
     * @param processingEnv the processing environment passed to the processor
114
     * @param erroneousAnnotation the type of the annotation on the element
115
     * @see Messager#printMessage(javax.tools.Diagnostic.Kind, CharSequence, Element, AnnotationMirror)
116
     */
117
    public LayerGenerationException(String message, Element erroneousElement, ProcessingEnvironment processingEnv,
118
            Class<? extends Annotation> erroneousAnnotation) {
119
        this(message, erroneousElement, processingEnv, erroneousAnnotation, (String) null);
120
    }
121
122
    /**
123
     * An exception with an associated annotation value.
124
     * Convenience constructor which locates an annotation and its value on the erroneous element for you.
125
     * @param message a detail message which could be reported to the user
126
     * @param erroneousElement the associated element
127
     * @param processingEnv the processing environment passed to the processor
128
     * @param erroneousAnnotation the type of the annotation on the element
129
     * @param erroneousAnnotationValue the name of a method in that annotation
130
     * @see Messager#printMessage(javax.tools.Diagnostic.Kind, CharSequence, Element, AnnotationMirror, AnnotationValue)
131
     */
132
    public LayerGenerationException(String message, Element erroneousElement, ProcessingEnvironment processingEnv,
133
            Class<? extends Annotation> erroneousAnnotation, String erroneousAnnotationValue) {
134
        super(message);
135
        this.erroneousElement = erroneousElement;
136
        if (erroneousAnnotationValue != null) {
137
            try {
138
                erroneousAnnotation.getMethod(erroneousAnnotationValue);
139
            } catch (NoSuchMethodException x) {
140
                throw new IllegalArgumentException("No such method " + erroneousAnnotationValue + " in " + erroneousAnnotation);
141
            } catch (SecurityException x) {/* ignore? */}
142
        }
143
        this.erroneousAnnotation = findAnnotationMirror(erroneousElement, processingEnv, erroneousAnnotation);
144
        this.erroneousAnnotationValue = this.erroneousAnnotation != null && erroneousAnnotationValue != null ?
145
            findAnnotationValue(this.erroneousAnnotation, erroneousAnnotationValue) : null;
146
    }
147
148
    private static AnnotationMirror findAnnotationMirror(Element element, ProcessingEnvironment processingEnv, Class<? extends Annotation> annotation) {
149
        for (AnnotationMirror ann : element.getAnnotationMirrors()) {
150
            if (processingEnv.getElementUtils().getBinaryName((TypeElement) ann.getAnnotationType().asElement()).
151
                    contentEquals(annotation.getName())) {
152
                return ann;
153
            }
154
        }
155
        return null;
156
    }
157
158
    private AnnotationValue findAnnotationValue(AnnotationMirror annotation, String name) {
159
        for (Map.Entry<? extends ExecutableElement,? extends AnnotationValue> entry : annotation.getElementValues().entrySet()) {
160
            if (entry.getKey().getSimpleName().contentEquals(name)) {
161
                return entry.getValue();
162
            }
163
        }
164
        return null;
165
    }
166
102
}
167
}

Return to bug 149136