8019243: AnnotationTypeMismatchException instead of MirroredTypeException

Tue, 20 Aug 2013 17:21:47 +0200

author
jfranck
date
Tue, 20 Aug 2013 17:21:47 +0200
changeset 1960
e811fb09a1dc
parent 1959
55da6b3a6940
child 1961
58da1296c6b3

8019243: AnnotationTypeMismatchException instead of MirroredTypeException
Reviewed-by: jjg

src/share/classes/com/sun/tools/javac/code/Attribute.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javac/comp/Annotate.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javac/model/AnnotationProxyMaker.java file | annotate | diff | comparison | revisions
test/tools/javac/processing/errors/EnsureMirroredTypeException/Processor.java file | annotate | diff | comparison | revisions
test/tools/javac/processing/errors/EnsureMirroredTypeException/Source.java file | annotate | diff | comparison | revisions
test/tools/javac/processing/errors/EnsureMirroredTypeException/Source.out file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/code/Attribute.java	Tue Aug 20 17:34:06 2013 +0400
     1.2 +++ b/src/share/classes/com/sun/tools/javac/code/Attribute.java	Tue Aug 20 17:21:47 2013 +0200
     1.3 @@ -340,6 +340,14 @@
     1.4          }
     1.5      }
     1.6  
     1.7 +    public static class UnresolvedClass extends Error {
     1.8 +        public Type classType;
     1.9 +        public UnresolvedClass(Type type, Type classType) {
    1.10 +            super(type);
    1.11 +            this.classType = classType;
    1.12 +        }
    1.13 +    }
    1.14 +
    1.15      /** A visitor type for dynamic dispatch on the kind of attribute value. */
    1.16      public static interface Visitor {
    1.17          void visitConstant(Attribute.Constant value);
     2.1 --- a/src/share/classes/com/sun/tools/javac/comp/Annotate.java	Tue Aug 20 17:34:06 2013 +0400
     2.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Annotate.java	Tue Aug 20 17:21:47 2013 +0200
     2.3 @@ -332,8 +332,20 @@
     2.4          }
     2.5          if (expected.tsym == syms.classType.tsym) {
     2.6              Type result = attr.attribExpr(tree, env, expected);
     2.7 -            if (result.isErroneous())
     2.8 -                return new Attribute.Error(expected);
     2.9 +            if (result.isErroneous()) {
    2.10 +                // Does it look like a class literal?
    2.11 +                if (TreeInfo.name(tree) == names._class) {
    2.12 +                    Name n = (((JCFieldAccess) tree).selected).type.tsym.flatName();
    2.13 +                    return new Attribute.UnresolvedClass(expected,
    2.14 +                            types.createErrorType(n,
    2.15 +                                    syms.unknownSymbol, syms.classType));
    2.16 +                } else {
    2.17 +                    return new Attribute.Error(expected);
    2.18 +                }
    2.19 +            }
    2.20 +
    2.21 +            // Class literals look like field accesses of a field named class
    2.22 +            // at the tree level
    2.23              if (TreeInfo.name(tree) != names._class) {
    2.24                  log.error(tree.pos(), "annotation.value.must.be.class.literal");
    2.25                  return new Attribute.Error(expected);
     3.1 --- a/src/share/classes/com/sun/tools/javac/model/AnnotationProxyMaker.java	Tue Aug 20 17:34:06 2013 +0400
     3.2 +++ b/src/share/classes/com/sun/tools/javac/model/AnnotationProxyMaker.java	Tue Aug 20 17:21:47 2013 +0200
     3.3 @@ -244,7 +244,10 @@
     3.4          }
     3.5  
     3.6          public void visitError(Attribute.Error e) {
     3.7 -            value = null;       // indicates a type mismatch
     3.8 +            if (e instanceof Attribute.UnresolvedClass)
     3.9 +                value = new MirroredTypeExceptionProxy(((Attribute.UnresolvedClass)e).classType);
    3.10 +            else
    3.11 +                value = null;       // indicates a type mismatch
    3.12          }
    3.13  
    3.14  
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/test/tools/javac/processing/errors/EnsureMirroredTypeException/Processor.java	Tue Aug 20 17:21:47 2013 +0200
     4.3 @@ -0,0 +1,100 @@
     4.4 +/*
     4.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     4.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4.7 + *
     4.8 + * This code is free software; you can redistribute it and/or modify it
     4.9 + * under the terms of the GNU General Public License version 2 only, as
    4.10 + * published by the Free Software Foundation.
    4.11 + *
    4.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    4.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    4.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    4.15 + * version 2 for more details (a copy is included in the LICENSE file that
    4.16 + * accompanied this code).
    4.17 + *
    4.18 + * You should have received a copy of the GNU General Public License version
    4.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    4.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    4.21 + *
    4.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    4.23 + * or visit www.oracle.com if you need additional information or have any
    4.24 + * questions.
    4.25 + */
    4.26 +
    4.27 +import java.lang.annotation.*;
    4.28 +import java.util.*;
    4.29 +import javax.annotation.processing.*;
    4.30 +import javax.lang.model.element.*;
    4.31 +import javax.lang.model.type.DeclaredType;
    4.32 +import javax.lang.model.type.MirroredTypeException;
    4.33 +import javax.lang.model.type.TypeMirror;
    4.34 +import javax.lang.model.type.TypeKind;
    4.35 +import javax.tools.*;
    4.36 +
    4.37 +import com.sun.tools.javac.util.Assert;
    4.38 +import com.sun.tools.javac.code.Symbol;
    4.39 +import static com.sun.tools.javac.code.Symbol.TypeSymbol;
    4.40 +
    4.41 +public class Processor extends JavacTestingAbstractProcessor {
    4.42 +
    4.43 +    @Override
    4.44 +    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    4.45 +        for (Element e : roundEnv.getElementsAnnotatedWith(A.class)) {
    4.46 +            A rtg = e.getAnnotation(A.class);
    4.47 +
    4.48 +            try {
    4.49 +                rtg.a();
    4.50 +                Assert.check(false); //Should not reach here
    4.51 +            } catch (MirroredTypeException ex) {
    4.52 +                TypeMirror tm = ex.getTypeMirror();
    4.53 +                Assert.check(tm.getKind() == TypeKind.ERROR);
    4.54 +
    4.55 +                TypeElement elm = (TypeElement)((DeclaredType)tm).asElement();
    4.56 +                Assert.check(elm.getQualifiedName().toString().
    4.57 +                        endsWith("some.path.to.SomeUnknownClass$Inner"));
    4.58 +
    4.59 +                TypeSymbol sym = (TypeSymbol)elm;
    4.60 +                Assert.check(sym.name.contentEquals("some.path.to.SomeUnknownClass$Inner"));
    4.61 +            }
    4.62 +        }
    4.63 +        for (Element e : roundEnv.getElementsAnnotatedWith(B.class)) {
    4.64 +            B rtg = e.getAnnotation(B.class);
    4.65 +
    4.66 +            try {
    4.67 +                rtg.a();
    4.68 +                Assert.check(false); //Should not reach here
    4.69 +            } catch (MirroredTypeException ex) {
    4.70 +                TypeMirror tm = ex.getTypeMirror();
    4.71 +                Assert.check(tm.getKind() == TypeKind.ERROR);
    4.72 +
    4.73 +                TypeElement elm = (TypeElement)((DeclaredType)tm).asElement();
    4.74 +                Assert.check(elm.getQualifiedName().toString().
    4.75 +                        endsWith("SomeUnknownClass"));
    4.76 +
    4.77 +                TypeSymbol sym = (TypeSymbol)elm;
    4.78 +                Assert.check(sym.name.contentEquals("SomeUnknownClass"));
    4.79 +            }
    4.80 +        }
    4.81 +        for (Element e : roundEnv.getElementsAnnotatedWith(C.class)) {
    4.82 +            C rtg = e.getAnnotation(C.class);
    4.83 +
    4.84 +            try {
    4.85 +                rtg.a();
    4.86 +                Assert.check(false); //Should not reach here
    4.87 +            } catch (AnnotationTypeMismatchException ex) {
    4.88 +                ;
    4.89 +            }
    4.90 +        }
    4.91 +        return true;
    4.92 +    }
    4.93 +
    4.94 +    @interface A {
    4.95 +        Class<?> a();
    4.96 +    }
    4.97 +    @interface B {
    4.98 +        Class<?> a();
    4.99 +    }
   4.100 +    @interface C {
   4.101 +        Class<?> a();
   4.102 +    }
   4.103 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/test/tools/javac/processing/errors/EnsureMirroredTypeException/Source.java	Tue Aug 20 17:21:47 2013 +0200
     5.3 @@ -0,0 +1,17 @@
     5.4 +/*
     5.5 + * @test /nodynamiccopyright/
     5.6 + * @bug 8019243
     5.7 + * @summary AnnotationTypeMismatchException instead of MirroredTypeException
     5.8 + * @library /tools/javac/lib
     5.9 + * @build JavacTestingAbstractProcessor Processor
    5.10 + * @compile/fail/ref=Source.out -XDrawDiagnostics -processor Processor Source.java
    5.11 + */
    5.12 +
    5.13 +@Processor.A(a=some.path.to.SomeUnknownClass$Inner.class)
    5.14 +class Source1{}
    5.15 +
    5.16 +@Processor.B(a=SomeUnknownClass.class)
    5.17 +class Source2{}
    5.18 +
    5.19 +@Processor.C(a=SomeUnknownClass.clas) // this is not a class literal
    5.20 +class Source3{}
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/test/tools/javac/processing/errors/EnsureMirroredTypeException/Source.out	Tue Aug 20 17:21:47 2013 +0200
     6.3 @@ -0,0 +1,4 @@
     6.4 +Source.java:10:28: compiler.err.doesnt.exist: some.path.to
     6.5 +Source.java:13:16: compiler.err.cant.resolve: kindname.class, SomeUnknownClass, , 
     6.6 +Source.java:16:16: compiler.err.cant.resolve: kindname.variable, SomeUnknownClass, , 
     6.7 +3 errors

mercurial