Skip to content

Commit e853bdf

Browse files
robstrykermickaelistria
authored andcommitted
Fixes JavaSearchMultipleProjectsTests.testPackageReference1
Signed-off-by: Rob Stryker <[email protected]>
1 parent cae5435 commit e853bdf

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010
*******************************************************************************/
1111
package org.eclipse.jdt.internal.core.search.matching;
1212

13+
import org.eclipse.jdt.core.IJavaElement;
1314
import org.eclipse.jdt.core.dom.ASTNode;
1415
import org.eclipse.jdt.core.dom.IBinding;
1516
import org.eclipse.jdt.core.dom.IPackageBinding;
1617
import org.eclipse.jdt.core.dom.Name;
1718
import org.eclipse.jdt.core.dom.PackageDeclaration;
1819
import org.eclipse.jdt.internal.core.search.LocatorResponse;
20+
import org.eclipse.jdt.internal.javac.dom.JavacPackageBinding;
1921

2022
public class DOMPackageReferenceLocator extends DOMPatternLocator {
2123

@@ -44,7 +46,30 @@ public LocatorResponse resolveLevel(org.eclipse.jdt.core.dom.ASTNode node, IBind
4446
String n = ipb.getName();
4547
String patternName = new String(this.locator.pattern.pkgName);
4648
if (patternName.equals(n)) {
47-
return toResponse(ACCURATE_MATCH);
49+
if( this.locator.pattern.focus == null ) {
50+
// good enough
51+
return toResponse(ACCURATE_MATCH);
52+
}
53+
54+
// We have a focus of a specific package fragment.
55+
// We want to make sure the class in this node is
56+
// actually from this fragment, and not a similar package
57+
// in a different project.
58+
ASTNode working = node;
59+
Name fullName = null;
60+
while( working instanceof Name fn) {
61+
fullName = fn;
62+
working = working.getParent();
63+
}
64+
if( fullName != null ) {
65+
String typeName = fullName.toString();
66+
IJavaElement je = binding instanceof JavacPackageBinding jpb ? jpb.findJavaElementForClass(typeName) : ipb.getJavaElement();
67+
if( je != null && !this.locator.pattern.focus.equals(je)) {
68+
return toResponse(IMPOSSIBLE_MATCH);
69+
}
70+
// If we can't find a java element, let's give some leeway
71+
return toResponse(ACCURATE_MATCH);
72+
}
4873
}
4974
}
5075
if (binding == null) {

org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/dom/JavacPackageBinding.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111
package org.eclipse.jdt.internal.javac.dom;
1212

1313
import java.util.Arrays;
14+
import java.util.List;
1415
import java.util.Objects;
16+
import java.util.stream.Collectors;
1517

18+
import org.eclipse.jdt.core.ICompilationUnit;
1619
import org.eclipse.jdt.core.IJavaElement;
1720
import org.eclipse.jdt.core.IPackageFragment;
1821
import org.eclipse.jdt.core.JavaModelException;
@@ -99,6 +102,47 @@ public IJavaElement getJavaElement() {
99102
}
100103
}
101104

105+
public IJavaElement findJavaElementForClass(String fqqn) {
106+
if (this.resolver.javaProject == null) {
107+
return null;
108+
}
109+
String qnInternal = this.getQualifiedNameInternal();
110+
if( fqqn.length() <= qnInternal.length() ) {
111+
// Not a type name
112+
return null;
113+
}
114+
try {
115+
List<IPackageFragment> ret = Arrays.stream(this.resolver.javaProject.getAllPackageFragmentRoots())
116+
.map(root -> root.getPackageFragment(qnInternal))
117+
.filter(Objects::nonNull)
118+
.filter(IPackageFragment::exists)
119+
.collect(Collectors.toList());
120+
int s = ret.size();
121+
if( s == 1 ) {
122+
return ret.get(0);
123+
}
124+
125+
String relative = qnInternal == null || qnInternal.isEmpty() ? fqqn : fqqn.substring(qnInternal.length() + 1);
126+
String relativePackage = relative != null && relative.contains(".") ? relative.substring(0, relative.lastIndexOf(".")) : null;
127+
String cuName = relative != null && relative.contains(".") ? relative.substring(relative.lastIndexOf(".") + 1) : relative;
128+
String soughtFile = cuName + ".java";
129+
for( int i = 0; i < s; i++ ) {
130+
IPackageFragment pf = ret.get(i);
131+
if( relativePackage == null ) {
132+
ICompilationUnit cu = pf.getCompilationUnit(soughtFile);
133+
if( cu != null && cu.exists()) {
134+
return pf;
135+
}
136+
}
137+
}
138+
// TODO need to make sure the package is accessible in the module. :|
139+
return s == 0 ? null : ret.get(0);
140+
} catch (JavaModelException e) {
141+
// TODO Auto-generated catch block
142+
e.printStackTrace();
143+
return null;
144+
}
145+
}
102146
@Override
103147
public IModuleBinding getModule() {
104148
return this.getPackageSymbol() != null ?

0 commit comments

Comments
 (0)