Mercurial > hg > truffle
annotate graal/com.oracle.jvmci.service.processor/src/com/oracle/jvmci/service/processor/ServiceProviderProcessor.java @ 21713:454a99ca00a9
fixed canonicalizeprojects issues
author | Doug Simon <doug.simon@oracle.com> |
---|---|
date | Wed, 03 Jun 2015 22:13:10 +0200 |
parents | 93f282187d90 |
children | 22fe59641ba0 |
rev | line source |
---|---|
8529
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
1 /* |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
4 * |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
5 * This code is free software; you can redistribute it and/or modify it |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
6 * under the terms of the GNU General Public License version 2 only, as |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
7 * published by the Free Software Foundation. |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
8 * |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
13 * accompanied this code). |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
14 * |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
15 * You should have received a copy of the GNU General Public License version |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
18 * |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
20 * or visit www.oracle.com if you need additional information or have any |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
21 * questions. |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
22 */ |
21604
93f282187d90
moved JVMCI service API into separate com.oracle.jvmci.service module (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents:
21539
diff
changeset
|
23 package com.oracle.jvmci.service.processor; |
8529
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
24 |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
25 import java.io.*; |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
26 import java.util.*; |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
27 |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
28 import javax.annotation.processing.*; |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
29 import javax.lang.model.*; |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
30 import javax.lang.model.element.*; |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
31 import javax.lang.model.type.*; |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
32 import javax.tools.Diagnostic.Kind; |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
33 import javax.tools.*; |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
34 |
21604
93f282187d90
moved JVMCI service API into separate com.oracle.jvmci.service module (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents:
21539
diff
changeset
|
35 import com.oracle.jvmci.service.*; |
8529
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
36 |
21604
93f282187d90
moved JVMCI service API into separate com.oracle.jvmci.service module (JBS:GRAAL-53)
Doug Simon <doug.simon@oracle.com>
parents:
21539
diff
changeset
|
37 @SupportedAnnotationTypes("com.oracle.jvmci.service.ServiceProvider") |
8529
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
38 public class ServiceProviderProcessor extends AbstractProcessor { |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
39 |
9145
57f85e39c75f
Move logic from ServiceProviderProcessor to mx.
Roland Schatz <roland.schatz@oracle.com>
parents:
8998
diff
changeset
|
40 private final Set<TypeElement> processed = new HashSet<>(); |
8529
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
41 |
14763 | 42 @Override |
43 public SourceVersion getSupportedSourceVersion() { | |
44 return SourceVersion.latest(); | |
45 } | |
46 | |
8529
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
47 private boolean verifyAnnotation(TypeMirror serviceInterface, TypeElement serviceProvider) { |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
48 if (!processingEnv.getTypeUtils().isSubtype(serviceProvider.asType(), serviceInterface)) { |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
49 String msg = String.format("Service provider class %s doesn't implement service interface %s", serviceProvider.getSimpleName(), serviceInterface); |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
50 processingEnv.getMessager().printMessage(Kind.ERROR, msg, serviceProvider); |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
51 return false; |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
52 } |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
53 |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
54 return true; |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
55 } |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
56 |
8997
508ae1a5cada
Fix ServiceProviderProcessor to support multiple providers in the same project.
Roland Schatz <roland.schatz@oracle.com>
parents:
8529
diff
changeset
|
57 private void processElement(TypeElement serviceProvider) { |
9145
57f85e39c75f
Move logic from ServiceProviderProcessor to mx.
Roland Schatz <roland.schatz@oracle.com>
parents:
8998
diff
changeset
|
58 if (processed.contains(serviceProvider)) { |
57f85e39c75f
Move logic from ServiceProviderProcessor to mx.
Roland Schatz <roland.schatz@oracle.com>
parents:
8998
diff
changeset
|
59 return; |
57f85e39c75f
Move logic from ServiceProviderProcessor to mx.
Roland Schatz <roland.schatz@oracle.com>
parents:
8998
diff
changeset
|
60 } |
57f85e39c75f
Move logic from ServiceProviderProcessor to mx.
Roland Schatz <roland.schatz@oracle.com>
parents:
8998
diff
changeset
|
61 |
57f85e39c75f
Move logic from ServiceProviderProcessor to mx.
Roland Schatz <roland.schatz@oracle.com>
parents:
8998
diff
changeset
|
62 processed.add(serviceProvider); |
8997
508ae1a5cada
Fix ServiceProviderProcessor to support multiple providers in the same project.
Roland Schatz <roland.schatz@oracle.com>
parents:
8529
diff
changeset
|
63 ServiceProvider annotation = serviceProvider.getAnnotation(ServiceProvider.class); |
508ae1a5cada
Fix ServiceProviderProcessor to support multiple providers in the same project.
Roland Schatz <roland.schatz@oracle.com>
parents:
8529
diff
changeset
|
64 if (annotation != null) { |
508ae1a5cada
Fix ServiceProviderProcessor to support multiple providers in the same project.
Roland Schatz <roland.schatz@oracle.com>
parents:
8529
diff
changeset
|
65 try { |
508ae1a5cada
Fix ServiceProviderProcessor to support multiple providers in the same project.
Roland Schatz <roland.schatz@oracle.com>
parents:
8529
diff
changeset
|
66 annotation.value(); |
508ae1a5cada
Fix ServiceProviderProcessor to support multiple providers in the same project.
Roland Schatz <roland.schatz@oracle.com>
parents:
8529
diff
changeset
|
67 } catch (MirroredTypeException ex) { |
508ae1a5cada
Fix ServiceProviderProcessor to support multiple providers in the same project.
Roland Schatz <roland.schatz@oracle.com>
parents:
8529
diff
changeset
|
68 TypeMirror serviceInterface = ex.getTypeMirror(); |
508ae1a5cada
Fix ServiceProviderProcessor to support multiple providers in the same project.
Roland Schatz <roland.schatz@oracle.com>
parents:
8529
diff
changeset
|
69 if (verifyAnnotation(serviceInterface, serviceProvider)) { |
508ae1a5cada
Fix ServiceProviderProcessor to support multiple providers in the same project.
Roland Schatz <roland.schatz@oracle.com>
parents:
8529
diff
changeset
|
70 String interfaceName = ex.getTypeMirror().toString(); |
9145
57f85e39c75f
Move logic from ServiceProviderProcessor to mx.
Roland Schatz <roland.schatz@oracle.com>
parents:
8998
diff
changeset
|
71 createProviderFile(serviceProvider, interfaceName); |
8997
508ae1a5cada
Fix ServiceProviderProcessor to support multiple providers in the same project.
Roland Schatz <roland.schatz@oracle.com>
parents:
8529
diff
changeset
|
72 } |
508ae1a5cada
Fix ServiceProviderProcessor to support multiple providers in the same project.
Roland Schatz <roland.schatz@oracle.com>
parents:
8529
diff
changeset
|
73 } |
508ae1a5cada
Fix ServiceProviderProcessor to support multiple providers in the same project.
Roland Schatz <roland.schatz@oracle.com>
parents:
8529
diff
changeset
|
74 } |
508ae1a5cada
Fix ServiceProviderProcessor to support multiple providers in the same project.
Roland Schatz <roland.schatz@oracle.com>
parents:
8529
diff
changeset
|
75 } |
508ae1a5cada
Fix ServiceProviderProcessor to support multiple providers in the same project.
Roland Schatz <roland.schatz@oracle.com>
parents:
8529
diff
changeset
|
76 |
9145
57f85e39c75f
Move logic from ServiceProviderProcessor to mx.
Roland Schatz <roland.schatz@oracle.com>
parents:
8998
diff
changeset
|
77 private void createProviderFile(TypeElement serviceProvider, String interfaceName) { |
21532
ee764a50af61
require @ServiceProcessor annotated classes to be top level types
Doug Simon <doug.simon@oracle.com>
parents:
14763
diff
changeset
|
78 if (serviceProvider.getNestingKind().isNested()) { |
ee764a50af61
require @ServiceProcessor annotated classes to be top level types
Doug Simon <doug.simon@oracle.com>
parents:
14763
diff
changeset
|
79 // This is a simplifying constraint that means we don't have to |
ee764a50af61
require @ServiceProcessor annotated classes to be top level types
Doug Simon <doug.simon@oracle.com>
parents:
14763
diff
changeset
|
80 // processed the qualified name to insert '$' characters at |
ee764a50af61
require @ServiceProcessor annotated classes to be top level types
Doug Simon <doug.simon@oracle.com>
parents:
14763
diff
changeset
|
81 // the relevant positions. |
ee764a50af61
require @ServiceProcessor annotated classes to be top level types
Doug Simon <doug.simon@oracle.com>
parents:
14763
diff
changeset
|
82 String msg = String.format("Service provider class %s must be a top level class", serviceProvider.getSimpleName()); |
ee764a50af61
require @ServiceProcessor annotated classes to be top level types
Doug Simon <doug.simon@oracle.com>
parents:
14763
diff
changeset
|
83 processingEnv.getMessager().printMessage(Kind.ERROR, msg, serviceProvider); |
ee764a50af61
require @ServiceProcessor annotated classes to be top level types
Doug Simon <doug.simon@oracle.com>
parents:
14763
diff
changeset
|
84 return; |
ee764a50af61
require @ServiceProcessor annotated classes to be top level types
Doug Simon <doug.simon@oracle.com>
parents:
14763
diff
changeset
|
85 } |
ee764a50af61
require @ServiceProcessor annotated classes to be top level types
Doug Simon <doug.simon@oracle.com>
parents:
14763
diff
changeset
|
86 |
9145
57f85e39c75f
Move logic from ServiceProviderProcessor to mx.
Roland Schatz <roland.schatz@oracle.com>
parents:
8998
diff
changeset
|
87 String filename = "META-INF/providers/" + serviceProvider.getQualifiedName(); |
57f85e39c75f
Move logic from ServiceProviderProcessor to mx.
Roland Schatz <roland.schatz@oracle.com>
parents:
8998
diff
changeset
|
88 try { |
57f85e39c75f
Move logic from ServiceProviderProcessor to mx.
Roland Schatz <roland.schatz@oracle.com>
parents:
8998
diff
changeset
|
89 FileObject file = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", filename, serviceProvider); |
57f85e39c75f
Move logic from ServiceProviderProcessor to mx.
Roland Schatz <roland.schatz@oracle.com>
parents:
8998
diff
changeset
|
90 PrintWriter writer = new PrintWriter(new OutputStreamWriter(file.openOutputStream(), "UTF-8")); |
57f85e39c75f
Move logic from ServiceProviderProcessor to mx.
Roland Schatz <roland.schatz@oracle.com>
parents:
8998
diff
changeset
|
91 writer.println(interfaceName); |
57f85e39c75f
Move logic from ServiceProviderProcessor to mx.
Roland Schatz <roland.schatz@oracle.com>
parents:
8998
diff
changeset
|
92 writer.close(); |
57f85e39c75f
Move logic from ServiceProviderProcessor to mx.
Roland Schatz <roland.schatz@oracle.com>
parents:
8998
diff
changeset
|
93 } catch (IOException e) { |
57f85e39c75f
Move logic from ServiceProviderProcessor to mx.
Roland Schatz <roland.schatz@oracle.com>
parents:
8998
diff
changeset
|
94 processingEnv.getMessager().printMessage(Kind.ERROR, e.getMessage(), serviceProvider); |
8997
508ae1a5cada
Fix ServiceProviderProcessor to support multiple providers in the same project.
Roland Schatz <roland.schatz@oracle.com>
parents:
8529
diff
changeset
|
95 } |
508ae1a5cada
Fix ServiceProviderProcessor to support multiple providers in the same project.
Roland Schatz <roland.schatz@oracle.com>
parents:
8529
diff
changeset
|
96 } |
508ae1a5cada
Fix ServiceProviderProcessor to support multiple providers in the same project.
Roland Schatz <roland.schatz@oracle.com>
parents:
8529
diff
changeset
|
97 |
8529
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
98 @Override |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
99 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
100 if (roundEnv.processingOver()) { |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
101 return true; |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
102 } |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
103 |
10452
819251246143
Readd accidentally commented code.
Christian Humer <christian.humer@gmail.com>
parents:
10443
diff
changeset
|
104 for (Element element : roundEnv.getElementsAnnotatedWith(ServiceProvider.class)) { |
819251246143
Readd accidentally commented code.
Christian Humer <christian.humer@gmail.com>
parents:
10443
diff
changeset
|
105 assert element.getKind().isClass(); |
819251246143
Readd accidentally commented code.
Christian Humer <christian.humer@gmail.com>
parents:
10443
diff
changeset
|
106 processElement((TypeElement) element); |
819251246143
Readd accidentally commented code.
Christian Humer <christian.humer@gmail.com>
parents:
10443
diff
changeset
|
107 } |
8997
508ae1a5cada
Fix ServiceProviderProcessor to support multiple providers in the same project.
Roland Schatz <roland.schatz@oracle.com>
parents:
8529
diff
changeset
|
108 |
8529
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
109 return true; |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
110 } |
dc9cfb7ec5d0
Annotation processor for generating META-INF/services files.
Roland Schatz <roland.schatz@oracle.com>
parents:
diff
changeset
|
111 } |