From 97e4acde587b094bf6705408b77f6159f8756ec1 Mon Sep 17 00:00:00 2001 From: Jan Lahoda Date: Fri, 17 Jan 2025 19:19:28 +0100 Subject: [PATCH] Improving test. --- .../tools/javac/api/TestGetScopeResult.java | 35 +++++++++++++++---- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/test/langtools/tools/javac/api/TestGetScopeResult.java b/test/langtools/tools/javac/api/TestGetScopeResult.java index a46cc6478011b..533c640096e61 100644 --- a/test/langtools/tools/javac/api/TestGetScopeResult.java +++ b/test/langtools/tools/javac/api/TestGetScopeResult.java @@ -74,7 +74,12 @@ import com.sun.tools.javac.tree.JCTree.JCStatement; import com.sun.tools.javac.util.Context; import com.sun.tools.javac.util.Context.Factory; +import java.util.Objects; import javax.lang.model.element.TypeElement; +import javax.lang.model.type.DeclaredType; +import javax.lang.model.type.TypeKind; +import javax.lang.model.type.TypeMirror; +import javax.lang.model.type.TypeVariable; import javax.lang.model.util.ElementFilter; import javax.tools.JavaFileObject; @@ -892,7 +897,9 @@ void testClassTypeSetInEnterGetScope() throws IOException { JavacTool c = JavacTool.create(); try (StandardJavaFileManager fm = c.getStandardFileManager(null, null, null)) { String code = """ - class Test { + import java.util.*; + class Test extends ArrayList + implements List { private int test(boolean b) { int v = b ? test(!b) : 0; return v; @@ -919,11 +926,18 @@ public void finished(TaskEvent e) { new TreePathScanner() { @Override public Void visitClass(ClassTree node, Void p) { - String clazzType = - String.valueOf(trees.getTypeMirror(getCurrentPath())); - if (!"Test".equals(clazzType)) { - throw new AssertionError("Expected class type 'Test', but got: " + clazzType); + TypeMirror type = trees.getTypeMirror(getCurrentPath()); + if (type == null) { + throw new AssertionError("Expected class type 'Test', but got: null"); } + assertEquals(TypeKind.DECLARED, type.getKind()); + DeclaredType decl = (DeclaredType) type; + TypeVariable tvar = (TypeVariable) decl.getTypeArguments().get(0); + assertEquals("T", tvar.asElement().getSimpleName().toString()); + assertEquals("Test&java.lang.CharSequence", tvar.getUpperBound().toString()); + TypeElement clazz = (TypeElement) decl.asElement(); + assertEquals("java.util.ArrayList", clazz.getSuperclass().toString().toString()); + assertEquals("java.util.List", clazz.getInterfaces().toString().toString()); return super.visitClass(node, p); } @Override @@ -941,8 +955,9 @@ public Void visitReturn(ReturnTree rt, Void p) { List> expected = List.of(List.of("v:int", "b:boolean", - "super:java.lang.Object", - "this:Test" + "super:java.util.ArrayList", + "this:Test", + "T:T" )); if (!expected.equals(actual)) { @@ -974,4 +989,10 @@ private void asssertScopeContainsTypeWithFQN(Scope scope, String fqn) { ", but it is missing."); } + private void assertEquals(Object expected, Object actual) { + if (!Objects.equals(expected, actual)) { + throw new AssertionError("Expected: '" + expected + "', " + + "but got: '" + actual + "'"); + } + } }