Skip to content

Commit

Permalink
8252409: JShell: Intersection types cause NoSuchFieldError
Browse files Browse the repository at this point in the history
Reviewed-by: vromero
  • Loading branch information
lahodaj committed Jan 29, 2021
1 parent 64a150c commit 0675473
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 11 deletions.
Expand Up @@ -518,7 +518,7 @@ private String varTypeName(Type type, BinaryOperator<String> fullClassNameAndPac
try {
Function<TypeSymbol, String> anonymousClass2DeclareName =
cs -> anon2Name.computeIfAbsent(cs, state.eval::computeDeclareName);
TypePrinter tp = new TypePrinter(at.messages(),
TypePrinter tp = new TypePrinter(at.messages(), at.types(),
fullClassNameAndPackageToClass, anonymousClass2DeclareName,
printIntersectionTypes, anonymousTypesKind);
List<Type> captures = types.captures(type);
Expand Down
2 changes: 1 addition & 1 deletion src/jdk.jshell/share/classes/jdk/jshell/TreeDissector.java
Expand Up @@ -227,7 +227,7 @@ static String signature(Types types, MethodType mt) {
public static String printType(AnalyzeTask at, JShell state, TypeMirror type) {
Type typeImpl = (Type) type;
try {
TypePrinter tp = new TypePrinter(at.messages(),
TypePrinter tp = new TypePrinter(at.messages(), at.types(),
state.maps::fullClassNameAndPackageToClass, true, AnonymousTypeKind.DISPLAY);
return tp.toString(typeImpl);
} catch (Exception ex) {
Expand Down
11 changes: 7 additions & 4 deletions src/jdk.jshell/share/classes/jdk/jshell/TypePrinter.java
Expand Up @@ -34,6 +34,7 @@
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.code.Type.ClassType;
import com.sun.tools.javac.code.Type.IntersectionClassType;
import com.sun.tools.javac.code.Types;
import com.sun.tools.javac.util.JavacMessages;
import java.util.Locale;
import java.util.function.BinaryOperator;
Expand All @@ -49,6 +50,7 @@ class TypePrinter extends Printer {
private static final String OBJECT = "Object";

private final JavacMessages messages;
private final Types types;
private final BinaryOperator<String> fullClassNameAndPackageToClass;
private final Function<TypeSymbol, String> anonymousToName;
private final boolean printIntersectionTypes;
Expand All @@ -62,10 +64,10 @@ class TypePrinter extends Printer {
* @param printIntersectionTypes whether intersection types should be printed
* @param anonymousTypesKind how the anonymous types should be printed
*/
TypePrinter(JavacMessages messages,
TypePrinter(JavacMessages messages, Types types,
BinaryOperator<String> fullClassNameAndPackageToClass,
boolean printIntersectionTypes, AnonymousTypeKind anonymousTypesKind) {
this(messages, fullClassNameAndPackageToClass, cs -> cs.flatName().toString(),
this(messages, types, fullClassNameAndPackageToClass, cs -> cs.flatName().toString(),
printIntersectionTypes, anonymousTypesKind);
}

Expand All @@ -79,11 +81,12 @@ class TypePrinter extends Printer {
* @param printIntersectionTypes whether intersection types should be printed
* @param anonymousTypesKind how the anonymous types should be printed
*/
TypePrinter(JavacMessages messages,
TypePrinter(JavacMessages messages, Types types,
BinaryOperator<String> fullClassNameAndPackageToClass,
Function<TypeSymbol, String> anonymousToName,
boolean printIntersectionTypes, AnonymousTypeKind anonymousTypesKind) {
this.messages = messages;
this.types = types;
this.fullClassNameAndPackageToClass = fullClassNameAndPackageToClass;
this.anonymousToName = anonymousToName;
this.printIntersectionTypes = printIntersectionTypes;
Expand Down Expand Up @@ -137,7 +140,7 @@ protected String className(ClassType t, boolean longform, Locale locale) {
.map(i -> visit(i, locale))
.collect(Collectors.joining("&"));
} else {
return OBJECT;
return visit(types.erasure(t), locale);
}
} else if (sym.name.length() == 0) {
if (anonymousTypesKind == AnonymousTypeKind.DECLARE) {
Expand Down
4 changes: 2 additions & 2 deletions test/langtools/jdk/jshell/TypeNameTest.java
Expand Up @@ -194,7 +194,7 @@ public void testJEP286NonDenotable2() {
assertEval("<Z> Z choose(Z z1, Z z2) { return z1; }");
assertType("choose(1, 1L);",
"Number&Comparable<? extends Number&Comparable<?>&java.lang.constant.Constable&java.lang.constant.ConstantDesc>&java.lang.constant.Constable&java.lang.constant.ConstantDesc",
"Object");
"Number");
}

public void testVariableTypeName() {
Expand Down Expand Up @@ -249,7 +249,7 @@ public void testBounds() {
assertType("test1.get()", "CharSequence");
assertEval("class Test2<X extends Number & CharSequence> { public X get() { return null; } }");
assertEval("Test2<?> test2 = new Test2<>();");
assertType("test2.get()", "Number&CharSequence", "Object");
assertType("test2.get()", "Number&CharSequence", "Number");
assertEval("class Test3<T> { T[][] get() { return null; } }");
assertEval("Test3<? extends String> test3 = new Test3<>();");
assertType("test3.get()", "String[][]");
Expand Down
18 changes: 15 additions & 3 deletions test/langtools/jdk/jshell/VariablesTest.java
Expand Up @@ -23,7 +23,7 @@

/*
* @test
* @bug 8144903 8177466 8191842 8211694 8213725 8239536 8257236
* @bug 8144903 8177466 8191842 8211694 8213725 8239536 8257236 8252409
* @summary Tests for EvaluationState.variables
* @library /tools/lib
* @modules jdk.compiler/com.sun.tools.javac.api
Expand All @@ -38,6 +38,7 @@
import java.util.List;
import javax.tools.Diagnostic;

import jdk.jshell.MethodSnippet;
import jdk.jshell.Snippet;
import jdk.jshell.TypeDeclSnippet;
import jdk.jshell.VarSnippet;
Expand Down Expand Up @@ -417,10 +418,14 @@ public void test8191842() {

public void lvtiRecompileDependentsWithIntersectionTypes() {
assertEval("<Z extends Runnable & CharSequence> Z get1() { return null; }", added(VALID));
VarSnippet var = varKey(assertEval("var i1 = get1();", added(VALID)));
assertEval("var i1 = get1();", added(VALID));
MethodSnippet get2 = methodKey(assertEval("<Z extends Runnable & Stream> Z get2() { return null; }",
ste(MAIN_SNIPPET, NONEXISTENT, RECOVERABLE_NOT_DEFINED, false, null)));
assertEval("import java.util.stream.*;", added(VALID),
ste(var, VALID, VALID, true, MAIN_SNIPPET));
ste(get2, RECOVERABLE_NOT_DEFINED, VALID, true, MAIN_SNIPPET));
assertEval("void t1() { i1.run(); i1.length(); }", added(VALID));
assertEval("var i2 = get2();", added(VALID));
assertEval("void t2() { i2.run(); i2.count(); }", added(VALID));
}

public void arrayInit() {
Expand Down Expand Up @@ -605,4 +610,11 @@ public void setUp() {
.remoteVMOptions("--class-path", tpath)
.compilerOptions("--class-path", tpath));
}

public void varIntersection() {
assertEval("interface Marker {}");
assertEval("var v = (Marker & Runnable) () -> {};", added(VALID));
assertEval("v.run()");
}

}

1 comment on commit 0675473

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.