8022765: Compiler crashes with exception on wrong usage of an annotation.

Fri, 27 Sep 2013 17:28:31 +0200

author
jlahoda
date
Fri, 27 Sep 2013 17:28:31 +0200
changeset 2070
b7d8b71e1658
parent 2069
16194509e483
child 2071
2c24a04ebfb4

8022765: Compiler crashes with exception on wrong usage of an annotation.
Summary: Error recovery for incorrect annotation attribute values - ensure the values are always attributed appropriately
Reviewed-by: jfranck, jjg

src/share/classes/com/sun/tools/javac/comp/Annotate.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javac/comp/Attr.java file | annotate | diff | comparison | revisions
test/tools/javac/annotations/neg/8022765/T8022765.java file | annotate | diff | comparison | revisions
test/tools/javac/annotations/neg/8022765/T8022765.out file | annotate | diff | comparison | revisions
test/tools/javac/annotations/neg/8022765/VerifyAnnotationsAttributed.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/comp/Annotate.java	Fri Sep 27 10:24:56 2013 +0100
     1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Annotate.java	Fri Sep 27 17:28:31 2013 +0200
     1.3 @@ -310,6 +310,7 @@
     1.4      Attribute enterAttributeValue(Type expected,
     1.5                                    JCExpression tree,
     1.6                                    Env<AttrContext> env) {
     1.7 +        Type original = expected;
     1.8          //first, try completing the attribution value sym - if a completion
     1.9          //error is thrown, we should recover gracefully, and display an
    1.10          //ordinary resolution diagnostic.
    1.11 @@ -317,7 +318,54 @@
    1.12              expected.tsym.complete();
    1.13          } catch(CompletionFailure e) {
    1.14              log.error(tree.pos(), "cant.resolve", Kinds.kindName(e.sym), e.sym);
    1.15 -            return new Attribute.Error(expected);
    1.16 +            expected = syms.errType;
    1.17 +        }
    1.18 +        if (expected.hasTag(ARRAY)) {
    1.19 +            if (!tree.hasTag(NEWARRAY)) {
    1.20 +                tree = make.at(tree.pos).
    1.21 +                    NewArray(null, List.<JCExpression>nil(), List.of(tree));
    1.22 +            }
    1.23 +            JCNewArray na = (JCNewArray)tree;
    1.24 +            if (na.elemtype != null) {
    1.25 +                log.error(na.elemtype.pos(), "new.not.allowed.in.annotation");
    1.26 +            }
    1.27 +            ListBuffer<Attribute> buf = new ListBuffer<Attribute>();
    1.28 +            for (List<JCExpression> l = na.elems; l.nonEmpty(); l=l.tail) {
    1.29 +                buf.append(enterAttributeValue(types.elemtype(expected),
    1.30 +                                               l.head,
    1.31 +                                               env));
    1.32 +            }
    1.33 +            na.type = expected;
    1.34 +            return new Attribute.
    1.35 +                Array(expected, buf.toArray(new Attribute[buf.length()]));
    1.36 +        }
    1.37 +        if (tree.hasTag(NEWARRAY)) { //error recovery
    1.38 +            if (!expected.isErroneous())
    1.39 +                log.error(tree.pos(), "annotation.value.not.allowable.type");
    1.40 +            JCNewArray na = (JCNewArray)tree;
    1.41 +            if (na.elemtype != null) {
    1.42 +                log.error(na.elemtype.pos(), "new.not.allowed.in.annotation");
    1.43 +            }
    1.44 +            for (List<JCExpression> l = na.elems; l.nonEmpty(); l=l.tail) {
    1.45 +                enterAttributeValue(syms.errType,
    1.46 +                                    l.head,
    1.47 +                                    env);
    1.48 +            }
    1.49 +            return new Attribute.Error(original);
    1.50 +        }
    1.51 +        if ((expected.tsym.flags() & Flags.ANNOTATION) != 0) {
    1.52 +            if (tree.hasTag(ANNOTATION)) {
    1.53 +                return enterAnnotation((JCAnnotation)tree, expected, env);
    1.54 +            } else {
    1.55 +                log.error(tree.pos(), "annotation.value.must.be.annotation");
    1.56 +                expected = syms.errType;
    1.57 +            }
    1.58 +        }
    1.59 +        if (tree.hasTag(ANNOTATION)) { //error recovery
    1.60 +            if (!expected.isErroneous())
    1.61 +                log.error(tree.pos(), "annotation.not.valid.for.type", expected);
    1.62 +            enterAnnotation((JCAnnotation)tree, syms.errType, env);
    1.63 +            return new Attribute.Error(original);
    1.64          }
    1.65          if (expected.isPrimitive() || types.isSameType(expected, syms.stringType)) {
    1.66              Type result = attr.attribExpr(tree, env, expected);
    1.67 @@ -353,33 +401,6 @@
    1.68              return new Attribute.Class(types,
    1.69                                         (((JCFieldAccess) tree).selected).type);
    1.70          }
    1.71 -        if ((expected.tsym.flags() & Flags.ANNOTATION) != 0) {
    1.72 -            if (!tree.hasTag(ANNOTATION)) {
    1.73 -                log.error(tree.pos(), "annotation.value.must.be.annotation");
    1.74 -                expected = syms.errorType;
    1.75 -            }
    1.76 -            return enterAnnotation((JCAnnotation)tree, expected, env);
    1.77 -        }
    1.78 -        if (expected.hasTag(ARRAY)) { // should really be isArray()
    1.79 -            if (!tree.hasTag(NEWARRAY)) {
    1.80 -                tree = make.at(tree.pos).
    1.81 -                    NewArray(null, List.<JCExpression>nil(), List.of(tree));
    1.82 -            }
    1.83 -            JCNewArray na = (JCNewArray)tree;
    1.84 -            if (na.elemtype != null) {
    1.85 -                log.error(na.elemtype.pos(), "new.not.allowed.in.annotation");
    1.86 -                return new Attribute.Error(expected);
    1.87 -            }
    1.88 -            ListBuffer<Attribute> buf = new ListBuffer<Attribute>();
    1.89 -            for (List<JCExpression> l = na.elems; l.nonEmpty(); l=l.tail) {
    1.90 -                buf.append(enterAttributeValue(types.elemtype(expected),
    1.91 -                                               l.head,
    1.92 -                                               env));
    1.93 -            }
    1.94 -            na.type = expected;
    1.95 -            return new Attribute.
    1.96 -                Array(expected, buf.toArray(new Attribute[buf.length()]));
    1.97 -        }
    1.98          if (expected.hasTag(CLASS) &&
    1.99              (expected.tsym.flags() & Flags.ENUM) != 0) {
   1.100              attr.attribExpr(tree, env, expected);
   1.101 @@ -394,6 +415,7 @@
   1.102              VarSymbol enumerator = (VarSymbol) sym;
   1.103              return new Attribute.Enum(expected, enumerator);
   1.104          }
   1.105 +        //error recovery:
   1.106          if (!expected.isErroneous())
   1.107              log.error(tree.pos(), "annotation.value.not.allowable.type");
   1.108          return new Attribute.Error(attr.attribExpr(tree, env, expected));
     2.1 --- a/src/share/classes/com/sun/tools/javac/comp/Attr.java	Fri Sep 27 10:24:56 2013 +0100
     2.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Attr.java	Fri Sep 27 17:28:31 2013 +0200
     2.3 @@ -4058,8 +4058,7 @@
     2.4      }
     2.5  
     2.6      public void visitAnnotation(JCAnnotation tree) {
     2.7 -        log.error(tree.pos(), "annotation.not.valid.for.type", pt());
     2.8 -        result = tree.type = syms.errType;
     2.9 +        Assert.error("should be handled in Annotate");
    2.10      }
    2.11  
    2.12      public void visitAnnotatedType(JCAnnotatedType tree) {
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/tools/javac/annotations/neg/8022765/T8022765.java	Fri Sep 27 17:28:31 2013 +0200
     3.3 @@ -0,0 +1,141 @@
     3.4 +/**
     3.5 + * @test /nodynamiccopyright/
     3.6 + * @bug 8022765
     3.7 + * @summary javac should not crash for incorrect attribute values
     3.8 + * @build VerifyAnnotationsAttributed
     3.9 + * @run main VerifyAnnotationsAttributed T8022765.java
    3.10 + * @compile/fail/ref=T8022765.out -XDrawDiagnostics T8022765.java
    3.11 + */
    3.12 +@Ann(@Override)
    3.13 +@Primitive(@Override)
    3.14 +@Str(@Override)
    3.15 +@En(@Override)
    3.16 +@ArrAnn(@Override)
    3.17 +@ArrPrimitive(@Override)
    3.18 +@ArrStr(@Override)
    3.19 +@ArrEn(@Override)
    3.20 +class AnnC { }
    3.21 +
    3.22 +class PrimitiveC {
    3.23 +    private static final int C = 1;
    3.24 +    @Ann(C)
    3.25 +    @Primitive(C)
    3.26 +    @Str(C)
    3.27 +    @En(C)
    3.28 +    @ArrAnn(C)
    3.29 +    @ArrPrimitive(C)
    3.30 +    @ArrStr(C)
    3.31 +    @ArrEn(C)
    3.32 +    class I {
    3.33 +    }
    3.34 +}
    3.35 +
    3.36 +class StringC {
    3.37 +
    3.38 +    private static final String C = "";
    3.39 +
    3.40 +    @Ann(C)
    3.41 +    @Primitive(C)
    3.42 +    @Str(C)
    3.43 +    @En(C)
    3.44 +    @ArrAnn(C)
    3.45 +    @ArrPrimitive(C)
    3.46 +    @ArrStr(C)
    3.47 +    @ArrEn(C)
    3.48 +    class I {
    3.49 +    }
    3.50 +}
    3.51 +
    3.52 +@Ann(E.A)
    3.53 +@Primitive(E.A)
    3.54 +@Str(E.A)
    3.55 +@En(E.A)
    3.56 +@ArrAnn(E.A)
    3.57 +@ArrPrimitive(E.A)
    3.58 +@ArrStr(E.A)
    3.59 +@ArrEn(E.A)
    3.60 +class EnC { }
    3.61 +
    3.62 +@Ann({@Override})
    3.63 +@Primitive({@Override})
    3.64 +@Str({@Override})
    3.65 +@En({@Override})
    3.66 +@ArrAnn({@Override})
    3.67 +@ArrPrimitive({@Override})
    3.68 +@ArrStr({@Override})
    3.69 +@ArrEn({@Override})
    3.70 +class ArrAnnC { }
    3.71 +
    3.72 +class ArrPrimitiveC {
    3.73 +    private static final int C = 1;
    3.74 +    @Ann({C})
    3.75 +    @Primitive({C})
    3.76 +    @Str({C})
    3.77 +    @En({C})
    3.78 +    @ArrAnn({C})
    3.79 +    @ArrPrimitive({C})
    3.80 +    @ArrStr({C})
    3.81 +    @ArrEn({C})
    3.82 +    class I {
    3.83 +    }
    3.84 +}
    3.85 +
    3.86 +class ArrStringC {
    3.87 +    private static final String C = "";
    3.88 +    @Ann({C})
    3.89 +    @Primitive({C})
    3.90 +    @Str({C})
    3.91 +    @En({C})
    3.92 +    @ArrAnn({C})
    3.93 +    @ArrPrimitive({C})
    3.94 +    @ArrStr({C})
    3.95 +    @ArrEn({C})
    3.96 +    class I {
    3.97 +    }
    3.98 +}
    3.99 +
   3.100 +@Ann({E.A})
   3.101 +@Primitive({E.A})
   3.102 +@Str({E.A})
   3.103 +@En({E.A})
   3.104 +@ArrAnn({E.A})
   3.105 +@ArrPrimitive({E.A})
   3.106 +@ArrStr({E.A})
   3.107 +@ArrEn({E.A})
   3.108 +class ArrEnC { }
   3.109 +
   3.110 +@interface Ann {
   3.111 +    Override value();
   3.112 +}
   3.113 +
   3.114 +@interface Primitive {
   3.115 +    int value();
   3.116 +}
   3.117 +
   3.118 +@interface Str {
   3.119 +    String value();
   3.120 +}
   3.121 +
   3.122 +@interface En {
   3.123 +    E value();
   3.124 +}
   3.125 +
   3.126 +@interface ArrAnn {
   3.127 +    Override[] value();
   3.128 +}
   3.129 +
   3.130 +@interface ArrPrimitive {
   3.131 +    int[] value();
   3.132 +}
   3.133 +
   3.134 +@interface ArrStr {
   3.135 +    String[] value();
   3.136 +}
   3.137 +
   3.138 +@interface ArrEn {
   3.139 +    E[] value();
   3.140 +}
   3.141 +
   3.142 +enum E {
   3.143 +    A;
   3.144 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/test/tools/javac/annotations/neg/8022765/T8022765.out	Fri Sep 27 17:28:31 2013 +0200
     4.3 @@ -0,0 +1,57 @@
     4.4 +T8022765.java:10:12: compiler.err.annotation.not.valid.for.type: int
     4.5 +T8022765.java:11:6: compiler.err.annotation.not.valid.for.type: java.lang.String
     4.6 +T8022765.java:12:5: compiler.err.annotation.not.valid.for.type: E
     4.7 +T8022765.java:14:15: compiler.err.annotation.not.valid.for.type: int
     4.8 +T8022765.java:15:9: compiler.err.annotation.not.valid.for.type: java.lang.String
     4.9 +T8022765.java:16:8: compiler.err.annotation.not.valid.for.type: E
    4.10 +T8022765.java:21:10: compiler.err.annotation.value.must.be.annotation
    4.11 +T8022765.java:23:10: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: int, java.lang.String)
    4.12 +T8022765.java:24:9: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: int, E)
    4.13 +T8022765.java:25:13: compiler.err.annotation.value.must.be.annotation
    4.14 +T8022765.java:27:13: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: int, java.lang.String)
    4.15 +T8022765.java:28:12: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: int, E)
    4.16 +T8022765.java:37:10: compiler.err.annotation.value.must.be.annotation
    4.17 +T8022765.java:38:16: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: java.lang.String, int)
    4.18 +T8022765.java:40:9: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: java.lang.String, E)
    4.19 +T8022765.java:41:13: compiler.err.annotation.value.must.be.annotation
    4.20 +T8022765.java:42:19: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: java.lang.String, int)
    4.21 +T8022765.java:44:12: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: java.lang.String, E)
    4.22 +T8022765.java:49:7: compiler.err.annotation.value.must.be.annotation
    4.23 +T8022765.java:50:13: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: E, int)
    4.24 +T8022765.java:51:7: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: E, java.lang.String)
    4.25 +T8022765.java:53:10: compiler.err.annotation.value.must.be.annotation
    4.26 +T8022765.java:54:16: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: E, int)
    4.27 +T8022765.java:55:10: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: E, java.lang.String)
    4.28 +T8022765.java:59:6: compiler.err.annotation.value.not.allowable.type
    4.29 +T8022765.java:60:12: compiler.err.annotation.value.not.allowable.type
    4.30 +T8022765.java:61:6: compiler.err.annotation.value.not.allowable.type
    4.31 +T8022765.java:62:5: compiler.err.annotation.value.not.allowable.type
    4.32 +T8022765.java:64:16: compiler.err.annotation.not.valid.for.type: int
    4.33 +T8022765.java:65:10: compiler.err.annotation.not.valid.for.type: java.lang.String
    4.34 +T8022765.java:66:9: compiler.err.annotation.not.valid.for.type: E
    4.35 +T8022765.java:71:10: compiler.err.annotation.value.not.allowable.type
    4.36 +T8022765.java:72:16: compiler.err.annotation.value.not.allowable.type
    4.37 +T8022765.java:73:10: compiler.err.annotation.value.not.allowable.type
    4.38 +T8022765.java:74:9: compiler.err.annotation.value.not.allowable.type
    4.39 +T8022765.java:75:14: compiler.err.annotation.value.must.be.annotation
    4.40 +T8022765.java:77:14: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: int, java.lang.String)
    4.41 +T8022765.java:78:13: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: int, E)
    4.42 +T8022765.java:85:10: compiler.err.annotation.value.not.allowable.type
    4.43 +T8022765.java:86:16: compiler.err.annotation.value.not.allowable.type
    4.44 +T8022765.java:87:10: compiler.err.annotation.value.not.allowable.type
    4.45 +T8022765.java:88:9: compiler.err.annotation.value.not.allowable.type
    4.46 +T8022765.java:89:14: compiler.err.annotation.value.must.be.annotation
    4.47 +T8022765.java:90:20: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: java.lang.String, int)
    4.48 +T8022765.java:92:13: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: java.lang.String, E)
    4.49 +T8022765.java:97:6: compiler.err.annotation.value.not.allowable.type
    4.50 +T8022765.java:97:8: compiler.err.attribute.value.must.be.constant
    4.51 +T8022765.java:98:12: compiler.err.annotation.value.not.allowable.type
    4.52 +T8022765.java:98:14: compiler.err.attribute.value.must.be.constant
    4.53 +T8022765.java:99:6: compiler.err.annotation.value.not.allowable.type
    4.54 +T8022765.java:99:8: compiler.err.attribute.value.must.be.constant
    4.55 +T8022765.java:100:5: compiler.err.annotation.value.not.allowable.type
    4.56 +T8022765.java:100:7: compiler.err.attribute.value.must.be.constant
    4.57 +T8022765.java:101:11: compiler.err.annotation.value.must.be.annotation
    4.58 +T8022765.java:102:17: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: E, int)
    4.59 +T8022765.java:103:11: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: E, java.lang.String)
    4.60 +56 errors
    4.61 \ No newline at end of file
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/test/tools/javac/annotations/neg/8022765/VerifyAnnotationsAttributed.java	Fri Sep 27 17:28:31 2013 +0200
     5.3 @@ -0,0 +1,78 @@
     5.4 +/*
     5.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     5.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     5.7 + *
     5.8 + * This code is free software; you can redistribute it and/or modify it
     5.9 + * under the terms of the GNU General Public License version 2 only, as
    5.10 + * published by the Free Software Foundation.
    5.11 + *
    5.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    5.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    5.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    5.15 + * version 2 for more details (a copy is included in the LICENSE file that
    5.16 + * accompanied this code).
    5.17 + *
    5.18 + * You should have received a copy of the GNU General Public License version
    5.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    5.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    5.21 + *
    5.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    5.23 + * or visit www.oracle.com if you need additional information or have any
    5.24 + * questions.
    5.25 + */
    5.26 +import com.sun.source.tree.CompilationUnitTree;
    5.27 +import com.sun.source.tree.IdentifierTree;
    5.28 +import com.sun.source.tree.MemberSelectTree;
    5.29 +import com.sun.source.util.JavacTask;
    5.30 +import com.sun.source.util.TreePathScanner;
    5.31 +import com.sun.source.util.Trees;
    5.32 +import com.sun.tools.javac.api.JavacTool;
    5.33 +import com.sun.tools.javac.file.JavacFileManager;
    5.34 +import java.io.File;
    5.35 +import java.io.IOException;
    5.36 +import java.net.URISyntaxException;
    5.37 +import java.util.Collections;
    5.38 +import javax.lang.model.element.Element;
    5.39 +import javax.lang.model.element.ElementKind;
    5.40 +
    5.41 +public class VerifyAnnotationsAttributed {
    5.42 +    public static void main(String... args) throws IOException, URISyntaxException {
    5.43 +        if (args.length != 1) throw new IllegalStateException("Must provide class name!");
    5.44 +        File testSrc = new File(System.getProperty("test.src"));
    5.45 +        File testFile = new File(testSrc, args[0]);
    5.46 +        if (!testFile.canRead()) throw new IllegalStateException("Cannot read the test source");
    5.47 +        JavacFileManager fm = JavacTool.create().getStandardFileManager(null, null, null);
    5.48 +        JavacTask task = JavacTool.create().getTask(null,
    5.49 +                                                    fm,
    5.50 +                                                    null,
    5.51 +                                                    Collections.<String>emptyList(),
    5.52 +                                                    null,
    5.53 +                                                    fm.getJavaFileObjects(testFile));
    5.54 +        final Trees trees = Trees.instance(task);
    5.55 +        final CompilationUnitTree cut = task.parse().iterator().next();
    5.56 +        task.analyze();
    5.57 +
    5.58 +        //ensure all the annotation attributes are annotated meaningfully
    5.59 +        //all the attributes in the test file should contain either an identifier
    5.60 +        //or a select, so only checking those for a reasonable Element/Symbol.
    5.61 +        new TreePathScanner<Void, Void>() {
    5.62 +            @Override
    5.63 +            public Void visitIdentifier(IdentifierTree node, Void p) {
    5.64 +                verifyAttributedMeaningfully();
    5.65 +                return super.visitIdentifier(node, p);
    5.66 +            }
    5.67 +            @Override
    5.68 +            public Void visitMemberSelect(MemberSelectTree node, Void p) {
    5.69 +                verifyAttributedMeaningfully();
    5.70 +                return super.visitMemberSelect(node, p);
    5.71 +            }
    5.72 +            private void verifyAttributedMeaningfully() {
    5.73 +                Element el = trees.getElement(getCurrentPath());
    5.74 +
    5.75 +                if (el == null || el.getKind() == ElementKind.OTHER) {
    5.76 +                    throw new IllegalStateException("Not attributed properly: " + getCurrentPath().getParentPath().getLeaf());
    5.77 +                }
    5.78 +            }
    5.79 +        }.scan(cut, null);
    5.80 +    }
    5.81 +}

mercurial