eclipse-jdt / eclipse-jdt/eclipse.jdt.core

Wrong type hierarchy computed when working copy is opened

Open
#4,707 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Java
Stars
237
Forks
195
Avg merge
1d 12h
Merged PRs (30d)
47

Description

Seen in: https://github.com/eclipse-jdt/eclipse.jdt.core/issues/4704

Add the following tests to `org.eclipse.jdt.ui.tests.core.TypeHierarchyTest`:

```
@Test
public void hierarchyGH4704NoWorkingCopy() throws Exception {
String a=
"""
public abstract class A {
public A() {}
public void m() {}
}
""";
String b=
"""
public class B {
public void t() {
Runnable r = () -> new A() {};
}
}
""";

IPackageFragmentRoot root1= JavaProjectHelper.addSourceContainer(fJavaProject1, "src");
IPackageFragment pack1= root1.createPackageFragment("pack", true, null);
ICompilationUnit cu1= pack1.getCompilationUnit("A.java");
IType type1= cu1.createType(a, null, true, null);
IPackageFragment pack2= root1.createPackageFragment("pack", true, null);
ICompilationUnit cu2= pack2.getCompilationUnit("B.java");
cu2.createType(b, null, true, null);

ITypeHierarchy hierarchy= type1.newTypeHierarchy(null);
IType[] allTypes= hierarchy.getAllTypes();
Set typeNames = Stream.of(allTypes).map(IType::getFullyQualifiedName).collect(Collectors.toSet());
Set expected = Set.of(Object.class.getName(), "pack.A", "pack.B$1$1");
assertEquals("Unexpected types in hierarchy", expected, typeNames);
}
```
```
@Test
public void hierarchyGH4704WorkingCopy() throws Exception {
String a=
"""
public abstract class A {
public A() {}
public void m() {}
}
""";
String b=
"""
public class B {
public void t() {
Runnable r = () -> new A() {};
}
}
""";

IPackageFragmentRoot root1= JavaProjectHelper.addSourceContainer(fJavaProject1, "src");
IPackageFragment pack1= root1.createPackageFragment("pack", true, null);
ICompilationUnit cu1= pack1.getCompilationUnit("A.java");
IType type1= cu1.createType(a, null, true, null);
IPackageFragment pack2= root1.createPackageFragment("pack", true, null);
ICompilationUnit cu2= pack2.getCompilationUnit("B.java");
IType type2= cu2.createType(b, null, true, null);

ITypeHierarchy hierarchy= null;
try {
JavaUI.openInEditor(type2, false, false);
hierarchy= type1.newTypeHierarchy(null);
} finally {
JavaPlugin.getActivePage().closeAllEditors(false);
}
IType[] allTypes= hierarchy.getAllTypes();
Set typeNames = Stream.of(allTypes).map(IType::getFullyQualifiedName).collect(Collectors.toSet());
Set expected = Set.of(Object.class.getName(), "pack.A", "pack.B$1$1");
assertEquals("Unexpected types in hierarchy", expected, typeNames);
}
```

Observe one of them fails:

```
java.lang.AssertionError: Unexpected types in hierarchy expected:<[pack.A, pack.B$1$1, java.lang.Object]> but was:<[pack.A, java.lang.Object]>
at org.junit.Assert.fail(Assert.java:89)
at org.junit.Assert.failNotEquals(Assert.java:835)
at org.junit.Assert.assertEquals(Assert.java:120)
at org.eclipse.jdt.ui.tests.core.TypeHierarchyTest.hierarchyGH4704WorkingCopy(TypeHierarchyTest.java:345)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
```

The type hierarchy is not different here. Both tests should be seeing the same type hierarchy. I assume the anonymous type should be in the type hierarchy.

For #4704 this results in hiding the error when a Java editor is opened with the type `B`.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.