Skip to content

Commit

Permalink
support for TYPE_USE @nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
brychcy committed Nov 30, 2015
1 parent f10d5c9 commit 3b65e8e
Show file tree
Hide file tree
Showing 3 changed files with 668 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ private ImmutableList<String> buildAnnotations(TypeSimplifier typeSimplifier) {
builder.add(annotationOutput.sourceFormForAnnotation(annotationMirror));
}

for (AnnotationMirror annotationMirror : Java8Support.getAnnotationMirrors(method.getReturnType())) {
AnnotationOutput annotationOutput = new AnnotationOutput(typeSimplifier);
builder.add(annotationOutput.sourceFormForAnnotation(annotationMirror));
}
return builder.build();
}

Expand Down Expand Up @@ -541,6 +545,9 @@ private Set<TypeMirror> allMethodAnnotationTypes(Iterable<ExecutableElement> met
for (AnnotationMirror annotationMirror : method.getAnnotationMirrors()) {
annotationTypes.add(annotationMirror.getAnnotationType());
}
for (AnnotationMirror annotationMirror : Java8Support.getAnnotationMirrors(method.getReturnType())) {
annotationTypes.add(annotationMirror.getAnnotationType());
}
}
return annotationTypes;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.google.auto.value.processor;

import java.lang.reflect.Method;
import java.util.Collections;
import java.util.List;

import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.type.TypeMirror;

/**
* this class provides access to java 8 type annotations via reflection (to
* still allow running on older java versions).
*
* @author Till Brychcy
*/
public class Java8Support {
private static Method determineAnnotationsMirrorsMethod() {
try {
return Class.forName("javax.lang.model.AnnotatedConstruct")
.getMethod("getAnnotationMirrors");
} catch (Exception e) {
// method and type only exist on java 8 and later
return null;
}
}

static Method getAnnotationsMirrorsMethod = determineAnnotationsMirrorsMethod();

/**
* provides access to
* {@link javax.lang.model.AnnotatedConstruct#getAnnotationMirrors()} via
* reflection
*
* @param typeMirror
* @return if possible, the result of typeMirror.getAnnotationMirrors(),
* otherwise an empty list.
*/
@SuppressWarnings("unchecked")
static List<? extends AnnotationMirror> getAnnotationMirrors(TypeMirror typeMirror) {
try {
if (getAnnotationsMirrorsMethod != null) {
return (List<? extends AnnotationMirror>) getAnnotationsMirrorsMethod
.invoke(typeMirror);
} else {
return Collections.emptyList();
}
} catch (Exception e) {
throw new RuntimeException("exception during invocation of getAnnotationMirrors", e);
}
}
}
Loading

0 comments on commit 3b65e8e

Please sign in to comment.