Question about type annotations
Jonathan Gibbons
jonathan.gibbons at oracle.com
Tue Oct 18 00:38:56 UTC 2016
As is typically the case in requests like this, it would help if you
could post a working, stripped down example.
You're also working with javac internal objects. It would help if you
could post an example in terms of the public API, such as is available using
com.sun.source.*
javax.lang.model.*
For example, if you have identified a node in an AST by a TreePath, use
com.sun.source.util.Trees.getTypeMirror(TreePath)
and then, since TypeMirror extends AnnotatedConstruct, you can get the
annotation mirrors.
See:
http://download.java.net/java/jdk9/docs/jdk/api/javac/tree/com/sun/source/util/Trees.html
http://download.java.net/java/jdk9/docs/api/javax/lang/model/type/TypeMirror.html
http://download.java.net/java/jdk9/docs/api/javax/lang/model/AnnotatedConstruct.html
-- Jon
On 10/17/2016 05:18 PM, Julian Bangert wrote:
> Hello (re-sending due to moderation),
>
> I am working on a checker based on the error-prone framework that
> works with (Java 8) type annotations. The javac AST seems to be
> missing annotation types for generic methods (and constructors on
> generic types):
>
> // Annotation .java
> package test;
> import java.lang.annotation.ElementType;
> import java.lang.annotation.Target;
>
> @Target({
> ElementType.TYPE_USE,
> ElementType.FIELD,
> ElementType.LOCAL_VARIABLE,
> ElementType.PARAMETER,
> ElementType.METHOD
> })
> public interface @Annotation {}
>
>
> // Generic.java
> package test;
> public class Generic {
> public static <V> void Method(V value) {
> }
> }
>
> // Testcase.java
>
> package test;
> import java.util.List;
> public class Testcase {
> void test(List<@Annotation String> list, String foo) {
> list.add(foo);
> Generic.<@Annotation String>Method(foo);
> }
> }
>
> Now call node.getMethodSelect() on the MethodInvocation AST nodes
> generated by this snippet. For the first call site (list.add(foo)),
> node.getMethodSelect() returns a JCFieldAccess with type
>
> "(@test.Annotation java.lang.String)boolean" (note the annotation).
>
> When I do the same for Generic.<@Annotation String>Method, I merely get
> "(java.lang.String)void".
>
> Is this a bug or intended behaviour? Is there a way to get annotation
> on the types (besides implementing my own lookup, which might be
> buggy/unelegant).
>
> Regards,
> Julian
More information about the compiler-dev
mailing list