File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\jenkins\PluginSubtypeMarker.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2011, CloudBees, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package jenkins;
25
26 import hudson.Plugin;
27 import org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement;
28 import org.kohsuke.MetaInfServices;
29
30 import javax.annotation.processing.AbstractProcessor;
31 import javax.annotation.processing.Processor;
32 import javax.annotation.processing.RoundEnvironment;
33 import javax.annotation.processing.SupportedAnnotationTypes;
34 import javax.annotation.processing.SupportedSourceVersion;
35 import javax.lang.model.SourceVersion;
36 import javax.lang.model.element.Element;
37 import javax.lang.model.element.ElementKind;
38 import javax.lang.model.element.Modifier;
39 import javax.lang.model.element.TypeElement;
40 import javax.lang.model.type.TypeMirror;
41 import javax.lang.model.util.ElementScanner6;
42 import javax.tools.Diagnostic.Kind;
43 import javax.tools.FileObject;
44 import javax.tools.StandardLocation;
45 import java.io.IOException;
46 import java.io.OutputStreamWriter;
47 import java.io.PrintWriter;
48 import java.io.StringWriter;
49 import java.io.Writer;
50 import java.util.Set;
51
52 /**
53 * Discovers the subtype of {@link Plugin} and generates service loader index file.
54 *
55 * @author Kohsuke Kawaguchi
56 * @since 1.420
57 */
58 @SupportedSourceVersion(SourceVersion.RELEASE_6)
59 @SupportedAnnotationTypes("*")
60 @MetaInfServices(Processor.class)
61 @IgnoreJRERequirement
62 @SuppressWarnings({"Since15"})
63 public class PluginSubtypeMarker extends AbstractProcessor {
64 @Override
65 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
66 try {
67 ElementScanner6<Void,Void> scanner = new ElementScanner6<Void, Void>() {
68 @Override
69 public Void visitType(TypeElement e, Void aVoid) {
70 if(!e.getModifiers().contains(Modifier.ABSTRACT)) {
71 Element sc = asElement(e.getSuperclass());
72 if (sc!=null && ((TypeElement)sc).getQualifiedName().contentEquals("hudson.Plugin")) {
73 try {
74 write(e);
75 } catch (IOException x) {
76 StringWriter sw = new StringWriter();
77 x.printStackTrace(new PrintWriter(sw));
78 processingEnv.getMessager().printMessage(Kind.ERROR,sw.toString(),e);
79 }
80 }
81 }
82
83 return super.visitType(e, aVoid);
84 }
85 };
86
87 for (Element e : roundEnv.getRootElements()) {
88 if (e.getKind() == ElementKind.PACKAGE) { // JENKINS-11739
89 continue;
90 }
91 scanner.scan(e, null);
92 }
93
94 return false;
95 } catch (RuntimeException e) {
96 // javac sucks at reporting errors in annotation processors
97 e.printStackTrace();
98 throw e;
99 } catch (Error e) {
100 e.printStackTrace();
101 throw e;
102 }
103 }
104
105 private void write(TypeElement c) throws IOException {
106 FileObject f = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT,
107 "", "META-INF/services/hudson.Plugin");
108 Writer w = new OutputStreamWriter(f.openOutputStream(),"UTF-8");
109 try {
110 w.write(c.getQualifiedName().toString());
111 } finally {
112 w.close();
113 }
114 }
115
116 private Element asElement(TypeMirror m) {
117 return processingEnv.getTypeUtils().asElement(m);
118 }
119 }
120