8181464: Invalid lambda in annotation causes NPE in Lint.augment

Reviewed-by: mcimadamore
This commit is contained in:
Vicente Romero 2017-06-13 11:21:09 -07:00
parent e94dd9f48f
commit bfe58ed881
24 changed files with 273 additions and 24 deletions

View File

@ -42,6 +42,7 @@ import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
import com.sun.tools.javac.util.List;
import javax.tools.JavaFileObject;
import java.util.*;
import static com.sun.tools.javac.code.Flags.SYNTHETIC;
@ -56,8 +57,10 @@ import static com.sun.tools.javac.tree.JCTree.Tag.ANNOTATION;
import static com.sun.tools.javac.tree.JCTree.Tag.ASSIGN;
import static com.sun.tools.javac.tree.JCTree.Tag.IDENT;
import static com.sun.tools.javac.tree.JCTree.Tag.NEWARRAY;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
/** Enter annotations onto symbols and types (and trees).
*
* This is also a pseudo stage in the compiler taking care of scheduling when annotations are
@ -565,6 +568,20 @@ public class Annotate {
return new Attribute.Error(((JCAnnotation)tree).annotationType.type);
}
MemberEnter.InitTreeVisitor initTreeVisitor = new MemberEnter.InitTreeVisitor() {
// the methods below are added to allow class literals on top of constant expressions
@Override
public void visitTypeIdent(JCPrimitiveTypeTree that) {}
@Override
public void visitTypeArray(JCArrayTypeTree that) {}
};
tree.accept(initTreeVisitor);
if (!initTreeVisitor.result) {
log.error(tree.pos(), Errors.ExpressionNotAllowableAsAnnotationValue);
return new Attribute.Error(syms.errType);
}
if (expectedElementType.isPrimitive() ||
(types.isSameType(expectedElementType, syms.stringType) && !expectedElementType.hasTag(TypeTag.ERROR))) {
return getAnnotationPrimitiveValue(expectedElementType, tree, env);
@ -614,12 +631,6 @@ public class Annotate {
}
}
// Class literals look like field accesses of a field named class
// at the tree level
if (TreeInfo.name(tree) != names._class) {
log.error(tree.pos(), "annotation.value.must.be.class.literal");
return new Attribute.Error(syms.errType);
}
return new Attribute.Class(types,
(((JCFieldAccess) tree).selected).type);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -345,7 +345,7 @@ public class MemberEnter extends JCTree.Visitor {
Tag.LT, Tag.LE, Tag.GT, Tag.GE, Tag.EQ, Tag.NE,
Tag.BITAND, Tag.BITXOR, Tag.BITOR, Tag.AND, Tag.OR);
private boolean result = true;
boolean result = true;
@Override
public void visitTree(JCTree tree) {

View File

@ -129,15 +129,15 @@ compiler.err.annotation.type.not.applicable.to.type=\
compiler.err.annotation.value.must.be.annotation=\
annotation value must be an annotation
compiler.err.annotation.value.must.be.class.literal=\
annotation value must be a class literal
compiler.err.annotation.value.must.be.name.value=\
annotation values must be of the form ''name=value''
compiler.err.annotation.value.not.allowable.type=\
annotation value not of an allowable type
compiler.err.expression.not.allowable.as.annotation.value=\
expression not allowed as annotation value
compiler.err.anon.class.impl.intf.no.args=\
anonymous class implements interface; cannot have arguments

View File

@ -1,3 +1,2 @@
T8002286.java:8:22: compiler.err.cant.resolve.location.args: kindname.method, value, , , (compiler.misc.location: kindname.annotation, T8002286.Anno, null)
T8002286.java:8:11: compiler.err.cant.resolve.location.args: kindname.method, nonExistent, , , (compiler.misc.location: kindname.class, T8002286, null)
2 errors
1 error

View File

@ -0,0 +1,26 @@
/*
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
@interface Anno {
Class<?> value();
}

View File

@ -0,0 +1,26 @@
/*
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
@interface Anno2 {
String value();
}

View File

@ -0,0 +1,36 @@
/*
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.element.TypeElement;
@SupportedAnnotationTypes("*")
public class AnnoProcessor extends AbstractProcessor {
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
return false;
}
}

View File

@ -0,0 +1,11 @@
/* @test /nodynamiccopyright/
* @bug 8181464
* @summary Invalid lambda in annotation causes NPE in Lint.augment
* @modules java.compiler
* jdk.compiler
* @compile Anno.java AnnoProcessor.java
* @compile/fail/ref=LambdaInAnnotationsCausesNPETest1.out -XDrawDiagnostics -processor AnnoProcessor -proc:only LambdaInAnnotationsCausesNPETest1.java
*/
@Anno(value = x -> x)
class LambdaInAnnotationsCausesNPETest1 {}

View File

@ -0,0 +1,2 @@
LambdaInAnnotationsCausesNPETest1.java:10:15: compiler.err.expression.not.allowable.as.annotation.value
1 error

View File

@ -0,0 +1,11 @@
/* @test /nodynamiccopyright/
* @bug 8181464
* @summary Invalid lambda in annotation causes NPE in Lint.augment
* @modules java.compiler
* jdk.compiler
* @compile Anno.java AnnoProcessor.java
* @compile/fail/ref=LambdaInAnnotationsCausesNPETest2.out -XDrawDiagnostics -processor AnnoProcessor -proc:only LambdaInAnnotationsCausesNPETest2.java
*/
@Anno(value = (String x) -> x)
class LambdaInAnnotationsCausesNPETest2 {}

View File

@ -0,0 +1,2 @@
LambdaInAnnotationsCausesNPETest2.java:10:15: compiler.err.expression.not.allowable.as.annotation.value
1 error

View File

@ -0,0 +1,15 @@
/* @test /nodynamiccopyright/
* @bug 8181464
* @summary Invalid lambda in annotation causes NPE in Lint.augment
* @modules java.compiler
* jdk.compiler
* @compile Anno2.java AnnoProcessor.java
* @compile/fail/ref=LambdaInAnnotationsCausesNPETest3.out -XDrawDiagnostics -processor AnnoProcessor -proc:only LambdaInAnnotationsCausesNPETest3.java
*/
@Anno2(value = LambdaInAnnotationsCausesNPETest3.m(x -> x))
class LambdaInAnnotationsCausesNPETest3 {
static String m(Class<?> target) {
return null;
}
}

View File

@ -0,0 +1,2 @@
LambdaInAnnotationsCausesNPETest3.java:10:51: compiler.err.expression.not.allowable.as.annotation.value
1 error

View File

@ -1,2 +1,2 @@
AnonSubclass.java:10:15: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: compiler.misc.anonymous.class: java.lang.Object, java.lang.String)
AnonSubclass.java:10:15: compiler.err.expression.not.allowable.as.annotation.value
1 error

View File

@ -1,2 +1,2 @@
Z15.java:11:36: compiler.err.attribute.value.must.be.constant
Z15.java:11:36: compiler.err.expression.not.allowable.as.annotation.value
1 error

View File

@ -1,2 +1,2 @@
package-info.java:11:20: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: compiler.misc.anonymous.class: java.lang.Object, java.lang.String)
package-info.java:11:20: compiler.err.expression.not.allowable.as.annotation.value
1 error

View File

@ -1,7 +1,7 @@
compiler.err.already.annotated # internal compiler error?
compiler.err.already.defined.this.unit # seems to be masked by compiler.err.duplicate.class
compiler.err.annotation.value.not.allowable.type # cannot happen: precluded by complete type-specific tests
compiler.err.bad.functional.intf.anno # seems to be masked by compiler.err.annotation.type.not.applicable
compiler.err.annotation.value.not.allowable.type # should be detected in advance by the annotation value visitor
compiler.err.cant.read.file # (apt.JavaCompiler?)
compiler.err.cant.select.static.class.from.param.type
compiler.err.dc.unterminated.string # cannot happen

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -21,7 +21,7 @@
* questions.
*/
// key: compiler.err.annotation.value.must.be.class.literal
// key: compiler.err.expression.not.allowable.as.annotation.value
@interface Anno {
Class value();

View File

@ -0,0 +1,37 @@
/*
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
// key: compiler.err.expression.not.allowable.as.annotation.value
import java.util.function.*;
@interface Anno {
String value();
}
@Anno(value = AnnotationMustBeConstant.m(x -> x))
class AnnotationMustBeConstant {
static String m(Function<String, String> f) {
return null;
}
}

View File

@ -0,0 +1,33 @@
/*
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
// key: compiler.err.attribute.value.must.be.constant
@T(a = AnnotationMustBeConstant2.x)
@interface T {
int a();
}
class AnnotationMustBeConstant2 {
static int x;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -21,7 +21,7 @@
* questions.
*/
// key: compiler.err.attribute.value.must.be.constant
// key: compiler.err.expression.not.allowable.as.annotation.value
@interface Anno {
String value();

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -21,7 +21,7 @@
* questions.
*/
// key: compiler.err.enum.annotation.must.be.enum.constant
// key: compiler.err.expression.not.allowable.as.annotation.value
enum E { A, B, C }

View File

@ -0,0 +1,38 @@
/*
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
// key: compiler.err.enum.annotation.must.be.enum.constant
enum E {
A,
B;
public static final E e = A;
}
@interface Anno {
E value();
}
@Anno(E.e)
class EnumAnnoValueMustBeEnumConstant { }

View File

@ -3,5 +3,5 @@ Generated.java:3:26: compiler.err.prob.found.req: (compiler.misc.inconvertible.t
Generated.java:4:22: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: java.lang.String, E)
Generated.java:5:37: compiler.err.annotation.not.valid.for.type: int
Generated.java:6:32: compiler.err.annotation.value.not.allowable.type
Generated.java:7:35: compiler.err.annotation.value.must.be.class.literal
Generated.java:7:35: compiler.err.expression.not.allowable.as.annotation.value
6 errors