Skip to content

Commit 1f9e8c6

Browse files
robstrykermickaelistria
authored andcommitted
Fix testTypeReference35 and others
Signed-off-by: Rob Stryker <[email protected]>
1 parent d81441d commit 1f9e8c6

File tree

2 files changed

+59
-12
lines changed

2 files changed

+59
-12
lines changed

org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacBindingResolver.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -755,15 +755,29 @@ public ITypeBinding getTypeDeclaration() {
755755
@Override
756756
public IPackageBinding getPackage() {
757757
if (type instanceof SimpleType simpleType && simpleType.getName() instanceof SimpleName) {
758-
return JavacBindingResolver.this.converter.domToJavac
759-
.values()
760-
.stream()
761-
.filter(CompilationUnit.class::isInstance)
762-
.map(CompilationUnit.class::cast)
763-
.map(CompilationUnit::getPackage)
764-
.map(PackageDeclaration::resolveBinding)
765-
.findAny()
766-
.orElse(bindings.getPackageBinding(Symtab.instance(context).rootPackage));
758+
Collection<JCTree> jctr = JavacBindingResolver.this.converter.domToJavac.values();
759+
Iterator<JCTree> jcit = jctr.iterator();
760+
while(jcit.hasNext()) {
761+
Object o = jcit.next();
762+
if( o instanceof CompilationUnit cuuu ) {
763+
// Should this even be here? Migrated from prior version
764+
PackageDeclaration pd = cuuu.getPackage();
765+
if( pd != null ) {
766+
IPackageBinding pack = pd.resolveBinding();
767+
if( pack != null )
768+
return pack;
769+
}
770+
}
771+
if( o instanceof JCCompilationUnit jcuu) {
772+
JCPackageDecl jcpd = jcuu.getPackage();
773+
if( jcpd != null ) {
774+
JavacPackageBinding pckbind = bindings.getPackageBinding(jcpd.packge);
775+
if( pckbind != null ) {
776+
return pckbind;
777+
}
778+
}
779+
}
780+
}
767781
}
768782
return bindings.getPackageBinding(Symtab.instance(context).rootPackage);
769783
}

org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/core/search/matching/DOMTypeReferenceLocator.java

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.eclipse.jdt.core.compiler.CharOperation;
2929
import org.eclipse.jdt.core.dom.ASTNode;
3030
import org.eclipse.jdt.core.dom.ASTVisitor;
31+
import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
3132
import org.eclipse.jdt.core.dom.AnnotationTypeMemberDeclaration;
3233
import org.eclipse.jdt.core.dom.BreakStatement;
3334
import org.eclipse.jdt.core.dom.CastExpression;
@@ -428,8 +429,29 @@ private String getQualifiedNameFromType(Type query) {
428429
return getQualifiedNameFromType(ptt.getType());
429430
}
430431
if( query instanceof SimpleType st) {
431-
String fqqn = fqqnFromImport(st.getName().toString());
432-
return fqqn != null ? fqqn : st.getName().toString();
432+
String stName = st.getName().toString();
433+
if( stName.contains(".")) {
434+
return stName;
435+
}
436+
String fqqn = fqqnFromImport(stName);
437+
if( fqqn != null ) {
438+
return fqqn;
439+
}
440+
// Check if node's top level type has class of sought name
441+
org.eclipse.jdt.core.dom.CompilationUnit cu = findCompilationUnitAncestor(query);
442+
if( cu != null ) {
443+
List<AbstractTypeDeclaration> types = cu.types();
444+
if( types != null ) {
445+
for( AbstractTypeDeclaration ad : types ) {
446+
if( ad.getName().toString().equals(stName)) {
447+
if( cu.getPackage() != null ) {
448+
String pkg = cu.getPackage().getName().toString();
449+
return pkg + "." + stName;
450+
}
451+
}
452+
}
453+
}
454+
}
433455
}
434456
return null;
435457
}
@@ -721,7 +743,8 @@ private int resolveLevelForTypeBinding(org.eclipse.jdt.core.dom.ASTNode node, IT
721743
}
722744

723745
if (trp.focus != null) {
724-
return Objects.equals(typeBinding.getJavaElement(), locator.pattern.focus) ?
746+
IJavaElement je = typeBinding.getJavaElement();
747+
return Objects.equals(je, locator.pattern.focus) ?
725748
ACCURATE_MATCH : IMPOSSIBLE_MATCH;
726749
}
727750
IImportDiscovery importDiscovery = new IImportDiscovery() {
@@ -937,6 +960,16 @@ private boolean hasImportAncestor(ASTNode node) {
937960
return false;
938961
}
939962

963+
private org.eclipse.jdt.core.dom.CompilationUnit findCompilationUnitAncestor(ASTNode node) {
964+
ASTNode working = node;
965+
while(working != null) {
966+
if( working instanceof org.eclipse.jdt.core.dom.CompilationUnit wk)
967+
return wk;
968+
working = working.getParent();
969+
}
970+
return null;
971+
}
972+
940973
@Override
941974
public void reportSearchMatch(MatchLocator locator, ASTNode node, SearchMatch match) throws CoreException {
942975
IResource resource = match.getResource();

0 commit comments

Comments
 (0)