Question about type annotations
Jonathan Gibbons
jonathan.gibbons at oracle.com
Tue Oct 18 01:00:51 UTC 2016
A small program that parses the test case you already provided, to get
an AST, then walks the AST, looking for selected nodes, printing out any
relevant info. Essentially, encapsulate this part of your message in
demonstrable code:
> 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".
If (but only if) you're familiar with the javac code base, and more
importantly, the test base, if you know of DPrinter, you might be able
to use that to dump the AST. In fact, something like
java -classpath ... DPrinter.Main -after PARSE -showTreeTypes
source-files...
might get you pretty much all the way to a script you could post.
-- Jon
On 10/17/2016 05:46 PM, Julian Bangert wrote:
> Thanks for the quick response -- how would you prefer the stripped
> down example? Should I send an error-prone testcase?
>
> On Mon, Oct 17, 2016 at 5:38 PM Jonathan Gibbons
> <jonathan.gibbons at oracle.com <mailto:jonathan.gibbons at oracle.com>> wrote:
>
> 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20161017/a0388663/attachment.html>
More information about the compiler-dev
mailing list