17
17
18
18
import static com .google .auto .common .AnnotationMirrors .getAnnotationValue ;
19
19
import static com .google .auto .common .MoreElements .getAnnotationMirror ;
20
+ import static com .google .common .base .Throwables .getStackTraceAsString ;
20
21
import static com .google .common .collect .ImmutableSet .toImmutableSet ;
21
22
23
+ import com .google .auto .common .MoreElements ;
22
24
import com .google .auto .common .MoreTypes ;
23
25
import com .google .auto .service .AutoService ;
24
26
import com .google .common .annotations .VisibleForTesting ;
28
30
import com .google .common .collect .Sets ;
29
31
import java .io .IOException ;
30
32
import java .io .OutputStream ;
31
- import java .io .PrintWriter ;
32
- import java .io .StringWriter ;
33
33
import java .util .HashSet ;
34
34
import java .util .List ;
35
35
import java .util .Set ;
@@ -74,7 +74,7 @@ public class AutoServiceProcessor extends AbstractProcessor {
74
74
* {@code "com.google.apphosting.LocalRpcService" ->
75
75
* "com.google.apphosting.datastore.LocalDatastoreService"}
76
76
*/
77
- private Multimap <String , String > providers = HashMultimap .create ();
77
+ private final Multimap <String , String > providers = HashMultimap .create ();
78
78
79
79
@ Override
80
80
public ImmutableSet <String > getSupportedAnnotationTypes () {
@@ -104,24 +104,20 @@ public SourceVersion getSupportedSourceVersion() {
104
104
@ Override
105
105
public boolean process (Set <? extends TypeElement > annotations , RoundEnvironment roundEnv ) {
106
106
try {
107
- return processImpl (annotations , roundEnv );
108
- } catch (Exception e ) {
107
+ processImpl (annotations , roundEnv );
108
+ } catch (RuntimeException e ) {
109
109
// We don't allow exceptions of any kind to propagate to the compiler
110
- StringWriter writer = new StringWriter ();
111
- e .printStackTrace (new PrintWriter (writer ));
112
- fatalError (writer .toString ());
113
- return true ;
110
+ fatalError (getStackTraceAsString (e ));
114
111
}
112
+ return false ;
115
113
}
116
114
117
- private boolean processImpl (Set <? extends TypeElement > annotations , RoundEnvironment roundEnv ) {
115
+ private void processImpl (Set <? extends TypeElement > annotations , RoundEnvironment roundEnv ) {
118
116
if (roundEnv .processingOver ()) {
119
117
generateConfigFiles ();
120
118
} else {
121
119
processAnnotations (annotations , roundEnv );
122
120
}
123
-
124
- return true ;
125
121
}
126
122
127
123
private void processAnnotations (Set <? extends TypeElement > annotations ,
@@ -134,7 +130,7 @@ private void processAnnotations(Set<? extends TypeElement> annotations,
134
130
135
131
for (Element e : elements ) {
136
132
// TODO(gak): check for error trees?
137
- TypeElement providerImplementer = ( TypeElement ) e ;
133
+ TypeElement providerImplementer = MoreElements . asType ( e ) ;
138
134
AnnotationMirror annotationMirror = getAnnotationMirror (e , AutoService .class ).get ();
139
135
Set <DeclaredType > providerInterfaces = getValueFieldOfClasses (annotationMirror );
140
136
if (providerInterfaces .isEmpty ()) {
@@ -187,7 +183,7 @@ private void generateConfigFiles() {
187
183
log ("Resource file did not already exist." );
188
184
}
189
185
190
- Set <String > newServices = new HashSet <String >(providers .get (providerInterface ));
186
+ Set <String > newServices = new HashSet <>(providers .get (providerInterface ));
191
187
if (allServices .containsAll (newServices )) {
192
188
log ("No new service entries being added." );
193
189
return ;
@@ -197,9 +193,9 @@ private void generateConfigFiles() {
197
193
log ("New service file contents: " + allServices );
198
194
FileObject fileObject = filer .createResource (StandardLocation .CLASS_OUTPUT , "" ,
199
195
resourceFile );
200
- OutputStream out = fileObject .openOutputStream ();
201
- ServicesFiles .writeServiceFile (allServices , out );
202
- out . close ();
196
+ try ( OutputStream out = fileObject .openOutputStream ()) {
197
+ ServicesFiles .writeServiceFile (allServices , out );
198
+ }
203
199
log ("Wrote to: " + fileObject .toUri ());
204
200
} catch (IOException e ) {
205
201
fatalError ("Unable to create " + resourceFile + ", " + e );
@@ -216,7 +212,7 @@ private void generateConfigFiles() {
216
212
private boolean checkImplementer (TypeElement providerImplementer , TypeElement providerType ) {
217
213
218
214
String verify = processingEnv .getOptions ().get ("verify" );
219
- if (verify == null || !Boolean .valueOf (verify )) {
215
+ if (verify == null || !Boolean .parseBoolean (verify )) {
220
216
return true ;
221
217
}
222
218
@@ -241,28 +237,29 @@ private String getBinaryNameImpl(TypeElement element, String className) {
241
237
Element enclosingElement = element .getEnclosingElement ();
242
238
243
239
if (enclosingElement instanceof PackageElement ) {
244
- PackageElement pkg = ( PackageElement ) enclosingElement ;
240
+ PackageElement pkg = MoreElements . asPackage ( enclosingElement ) ;
245
241
if (pkg .isUnnamed ()) {
246
242
return className ;
247
243
}
248
244
return pkg .getQualifiedName () + "." + className ;
249
245
}
250
246
251
- TypeElement typeElement = ( TypeElement ) enclosingElement ;
247
+ TypeElement typeElement = MoreElements . asType ( enclosingElement ) ;
252
248
return getBinaryNameImpl (typeElement , typeElement .getSimpleName () + "$" + className );
253
249
}
254
250
255
251
/**
256
- * Returns the contents of a {@code Class[]}-typed "value" field in a given {@code annotationMirror}.
252
+ * Returns the contents of a {@code Class[]}-typed "value" field in a given {@code
253
+ * annotationMirror}.
257
254
*/
258
255
private ImmutableSet <DeclaredType > getValueFieldOfClasses (AnnotationMirror annotationMirror ) {
259
256
return getAnnotationValue (annotationMirror , "value" )
260
257
.accept (
261
258
new SimpleAnnotationValueVisitor8 <ImmutableSet <DeclaredType >, Void >() {
262
259
@ Override
263
260
public ImmutableSet <DeclaredType > visitType (TypeMirror typeMirror , Void v ) {
264
- // TODO(ronshapiro): class literals may not always be declared types, i.e. int.class,
265
- // int[].class
261
+ // TODO(ronshapiro): class literals may not always be declared types, i.e.
262
+ // int.class, int [].class
266
263
return ImmutableSet .of (MoreTypes .asDeclared (typeMirror ));
267
264
}
268
265
0 commit comments