8023682: Incorrect attributes emitted for anonymous class declaration

Thu, 24 Oct 2013 01:27:10 -0400

author
emc
date
Thu, 24 Oct 2013 01:27:10 -0400
changeset 2168
119747cd9f25
parent 2167
d2fa3f7e964e
child 2169
667843bd2193

8023682: Incorrect attributes emitted for anonymous class declaration
Summary: Cause javac to emit type annotations on new instruction as well as anonymous class supertype for annotated anonymous classes.
Reviewed-by: jjg, jfranck

src/share/classes/com/sun/tools/javac/code/TypeAnnotations.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javac/comp/Check.java file | annotate | diff | comparison | revisions
test/tools/javac/annotations/typeAnnotations/failures/TypeOnAnonClass.java file | annotate | diff | comparison | revisions
test/tools/javac/annotations/typeAnnotations/failures/TypeOnAnonClass.out file | annotate | diff | comparison | revisions
test/tools/javac/annotations/typeAnnotations/failures/common/arrays/DeclarationAnnotation.out file | annotate | diff | comparison | revisions
test/tools/javac/annotations/typeAnnotations/newlocations/AnonymousClass.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/code/TypeAnnotations.java	Wed Oct 23 23:20:32 2013 -0400
     1.2 +++ b/src/share/classes/com/sun/tools/javac/code/TypeAnnotations.java	Thu Oct 24 01:27:10 2013 -0400
     1.3 @@ -370,9 +370,9 @@
     1.4              sym.appendUniqueTypeAttributes(typeAnnotations);
     1.5  
     1.6              if (sym.getKind() == ElementKind.PARAMETER ||
     1.7 -                    sym.getKind() == ElementKind.LOCAL_VARIABLE ||
     1.8 -                    sym.getKind() == ElementKind.RESOURCE_VARIABLE ||
     1.9 -                    sym.getKind() == ElementKind.EXCEPTION_PARAMETER) {
    1.10 +                sym.getKind() == ElementKind.LOCAL_VARIABLE ||
    1.11 +                sym.getKind() == ElementKind.RESOURCE_VARIABLE ||
    1.12 +                sym.getKind() == ElementKind.EXCEPTION_PARAMETER) {
    1.13                  // Make sure all type annotations from the symbol are also
    1.14                  // on the owner.
    1.15                  sym.owner.appendUniqueTypeAttributes(sym.getRawTypeAttributes());
    1.16 @@ -1221,6 +1221,22 @@
    1.17              super.visitTypeParameter(tree);
    1.18          }
    1.19  
    1.20 +        private void copyNewClassAnnotationsToOwner(JCNewClass tree) {
    1.21 +            Symbol sym = tree.def.sym;
    1.22 +            TypeAnnotationPosition pos = new TypeAnnotationPosition();
    1.23 +            ListBuffer<Attribute.TypeCompound> newattrs =
    1.24 +                new ListBuffer<Attribute.TypeCompound>();
    1.25 +
    1.26 +            for (Attribute.TypeCompound old : sym.getRawTypeAttributes()) {
    1.27 +                newattrs.append(new Attribute.TypeCompound(old.type, old.values,
    1.28 +                                                           pos));
    1.29 +            }
    1.30 +
    1.31 +            pos.type = TargetType.NEW;
    1.32 +            pos.pos = tree.pos;
    1.33 +            sym.owner.appendUniqueTypeAttributes(newattrs.toList());
    1.34 +        }
    1.35 +
    1.36          @Override
    1.37          public void visitNewClass(JCNewClass tree) {
    1.38              if (tree.def != null &&
    1.39 @@ -1239,7 +1255,7 @@
    1.40                  }
    1.41                  Type before = classdecl.sym.type;
    1.42                  separateAnnotationsKinds(classdecl, tree.clazz.type, classdecl.sym, pos);
    1.43 -
    1.44 +                copyNewClassAnnotationsToOwner(tree);
    1.45                  // classdecl.sym.type now contains an annotated type, which
    1.46                  // is not what we want there.
    1.47                  // TODO: should we put this type somewhere in the superclass/interface?
     2.1 --- a/src/share/classes/com/sun/tools/javac/comp/Check.java	Wed Oct 23 23:20:32 2013 -0400
     2.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Check.java	Thu Oct 24 01:27:10 2013 -0400
     2.3 @@ -3011,7 +3011,6 @@
     2.4      boolean annotationApplicable(JCAnnotation a, Symbol s) {
     2.5          Attribute.Array arr = getAttributeTargetAttribute(a.annotationType.type.tsym);
     2.6          Name[] targets;
     2.7 -
     2.8          if (arr == null) {
     2.9              targets = defaultTargetMetaInfo(a, s);
    2.10          } else {
    2.11 @@ -3028,7 +3027,7 @@
    2.12          }
    2.13          for (Name target : targets) {
    2.14              if (target == names.TYPE)
    2.15 -                { if (s.kind == TYP) return true; }
    2.16 +                { if (s.kind == TYP && !s.isAnonymous()) return true; }
    2.17              else if (target == names.FIELD)
    2.18                  { if (s.kind == VAR && s.owner.kind != MTH) return true; }
    2.19              else if (target == names.METHOD)
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/tools/javac/annotations/typeAnnotations/failures/TypeOnAnonClass.java	Thu Oct 24 01:27:10 2013 -0400
     3.3 @@ -0,0 +1,13 @@
     3.4 +/*
     3.5 + * @test /nodynamiccopyright/
     3.6 + * @bug 8023682
     3.7 + * @summary Cannot annotate an anonymous class with a target type of TYPE
     3.8 + * @compile/fail/ref=TypeOnAnonClass.out -XDrawDiagnostics TypeOnAnonClass.java
     3.9 + */
    3.10 +import java.lang.annotation.*;
    3.11 +
    3.12 +@Retention(RetentionPolicy.RUNTIME)
    3.13 +@Target(ElementType.TYPE)
    3.14 +@interface X {}
    3.15 +interface Foo {}
    3.16 +class TypeOnAnonClass { void m() { new @X Foo() {}; } }
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/test/tools/javac/annotations/typeAnnotations/failures/TypeOnAnonClass.out	Thu Oct 24 01:27:10 2013 -0400
     4.3 @@ -0,0 +1,2 @@
     4.4 +TypeOnAnonClass.java:13:40: compiler.err.annotation.type.not.applicable
     4.5 +1 error
     5.1 --- a/test/tools/javac/annotations/typeAnnotations/failures/common/arrays/DeclarationAnnotation.out	Wed Oct 23 23:20:32 2013 -0400
     5.2 +++ b/test/tools/javac/annotations/typeAnnotations/failures/common/arrays/DeclarationAnnotation.out	Thu Oct 24 01:27:10 2013 -0400
     5.3 @@ -1,4 +1,5 @@
     5.4  DeclarationAnnotation.java:10:21: compiler.err.annotation.type.not.applicable
     5.5  DeclarationAnnotation.java:11:21: compiler.err.annotation.type.not.applicable
     5.6  DeclarationAnnotation.java:12:21: compiler.err.annotation.type.not.applicable
     5.7 -3 errors
     5.8 \ No newline at end of file
     5.9 +DeclarationAnnotation.java:16:21: compiler.err.annotation.type.not.applicable
    5.10 +4 errors
     6.1 --- a/test/tools/javac/annotations/typeAnnotations/newlocations/AnonymousClass.java	Wed Oct 23 23:20:32 2013 -0400
     6.2 +++ b/test/tools/javac/annotations/typeAnnotations/newlocations/AnonymousClass.java	Thu Oct 24 01:27:10 2013 -0400
     6.3 @@ -32,12 +32,9 @@
     6.4   */
     6.5  class AnonymousClass {
     6.6      Object o1 = new @TA Object() { };
     6.7 -    // Declaration annotations are also allowed.
     6.8 -    Object o2 = new @TA @DA Object() { };
     6.9 +    Object o2 = new @TA Object() { };
    6.10  }
    6.11  
    6.12 -@interface DA { }
    6.13 -
    6.14  @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
    6.15  @interface TA { }
    6.16  

mercurial