Skip to content

Commit 055e846

Browse files
committed
Attempt to fix testFieldReference07
Signed-off-by: Rob Stryker <[email protected]>
1 parent 2f089a5 commit 055e846

File tree

2 files changed

+95
-2
lines changed

2 files changed

+95
-2
lines changed

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

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.List;
2323
import java.util.Map;
2424
import java.util.Objects;
25+
import java.util.Optional;
2526
import java.util.function.Function;
2627
import java.util.stream.Stream;
2728

@@ -1385,7 +1386,22 @@ IBinding resolveNameToJavac(Name name, JCTree tree) {
13851386
return this.bindings.getTypeBinding(variableDecl.type);
13861387
}
13871388
if (tree instanceof JCFieldAccess fieldAccess) {
1388-
return this.getFieldAccessBinding(fieldAccess);
1389+
IBinding b = this.getFieldAccessBinding(fieldAccess);
1390+
if( b != null ) {
1391+
return b;
1392+
}
1393+
if( fieldAccess.selected instanceof JCIdent jcid && jcid.sym == null && jcid.type == null) {
1394+
// Some bug at connecting this to an existing class in this same file?
1395+
IBinding bind = this.searchForFieldAccessBindingViaTopLevelType(jcid, name);
1396+
if( bind != null ) {
1397+
return bind;
1398+
}
1399+
bind = this.searchForFieldAccessBindingViaFieldsInCurrentType(jcid, name);
1400+
if( bind != null ) {
1401+
return bind;
1402+
}
1403+
}
1404+
return null;
13891405
}
13901406
if (tree instanceof JCMethodInvocation methodInvocation && methodInvocation.meth.type != null) {
13911407
return this.bindings.getBinding(((JCFieldAccess)methodInvocation.meth).sym, methodInvocation.meth.type);
@@ -1405,9 +1421,78 @@ IBinding resolveNameToJavac(Name name, JCTree tree) {
14051421
if (tree instanceof JCModuleDecl variableDecl && variableDecl.sym != null && variableDecl.sym.type instanceof ModuleType) {
14061422
return this.bindings.getModuleBinding(variableDecl);
14071423
}
1424+
1425+
if (name.getParent() instanceof Name parentName && tree instanceof JCIdent ident && ident.sym == null) {
1426+
tree = this.converter.domToJavac.get(parentName);
1427+
IBinding b3 = resolveNameToJavac(parentName, tree);
1428+
return b3;
1429+
}
1430+
return null;
1431+
}
1432+
1433+
private IBinding searchForFieldAccessBindingViaTopLevelType(JCIdent jcid, Name name) {
1434+
CompilationUnit cu = null;
1435+
ASTNode working = name;
1436+
while( working != null && !(working instanceof CompilationUnit) ) {
1437+
working = working.getParent();
1438+
}
1439+
if( working != null ) {
1440+
cu = (CompilationUnit)working;
1441+
List<AbstractTypeDeclaration> types = cu.types();
1442+
for( AbstractTypeDeclaration t1 : types ) {
1443+
if( t1.getName().toString().equals(jcid.getName().toString())) {
1444+
IBinding t1Binding = t1.resolveBinding();
1445+
if( t1Binding != null && t1Binding instanceof ITypeBinding t2) {
1446+
IVariableBinding[] fields = t2.getDeclaredFields();
1447+
for( IVariableBinding vb1 : fields ) {
1448+
if( vb1.getName().toString().equals(name.toString())) {
1449+
// we found a match
1450+
return vb1;
1451+
}
1452+
}
1453+
}
1454+
}
1455+
}
1456+
}
14081457
return null;
14091458
}
14101459

1460+
private IBinding searchForFieldAccessBindingViaFieldsInCurrentType(JCIdent jcid, Name name) {
1461+
String soughtName = name instanceof SimpleName sn ? sn.toString() :
1462+
name instanceof QualifiedName qn ? qn.getName().toString() : null;
1463+
if( soughtName == null )
1464+
return null;
1465+
1466+
AbstractTypeDeclaration type = null;
1467+
ASTNode working = name;
1468+
while( working != null && !(working instanceof AbstractTypeDeclaration) ) {
1469+
working = working.getParent();
1470+
}
1471+
if( working != null ) {
1472+
type = (AbstractTypeDeclaration)working;
1473+
List<FieldDeclaration> fields = type.bodyDeclarations().stream().filter(x -> x instanceof FieldDeclaration).toList();
1474+
for( FieldDeclaration t1 : fields ) {
1475+
Optional frags = t1.fragments().stream().filter(
1476+
x -> ((VariableDeclarationFragment)x).getName().toString().equals(jcid.getName().toString()))
1477+
.findFirst();
1478+
if( frags.isPresent() ) {
1479+
VariableDeclarationFragment frag = (VariableDeclarationFragment)frags.get();
1480+
IBinding t1Binding = t1.getType().resolveBinding();
1481+
if( t1Binding != null && t1Binding instanceof ITypeBinding t2) {
1482+
IVariableBinding[] referencedTypeFields = t2.getDeclaredFields();
1483+
for( IVariableBinding vb1 : referencedTypeFields ) {
1484+
if( vb1.getName().toString().equals(soughtName)) {
1485+
// we found a match
1486+
return vb1;
1487+
}
1488+
}
1489+
}
1490+
System.out.println("Break");
1491+
}
1492+
}
1493+
}
1494+
return null;
1495+
}
14111496
@Override
14121497
IVariableBinding resolveVariable(EnumConstantDeclaration enumConstant) {
14131498
resolve();

org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/JavacUtils.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,15 @@
3636

3737
import org.eclipse.core.resources.IProject;
3838
import org.eclipse.core.resources.IResource;
39+
import org.eclipse.core.runtime.CoreException;
3940
import org.eclipse.core.runtime.ILog;
4041
import org.eclipse.core.runtime.IPath;
4142
import org.eclipse.core.runtime.Path;
4243
import org.eclipse.jdt.core.IClasspathAttribute;
4344
import org.eclipse.jdt.core.IClasspathEntry;
4445
import org.eclipse.jdt.core.IJavaElement;
4546
import org.eclipse.jdt.core.IJavaProject;
47+
import org.eclipse.jdt.core.IModuleDescription;
4648
import org.eclipse.jdt.core.IType;
4749
import org.eclipse.jdt.core.JavaCore;
4850
import org.eclipse.jdt.core.JavaModelException;
@@ -399,7 +401,13 @@ private static Collection<File> classpathEntriesToFiles(JavaProject project, boo
399401
// return res;
400402

401403
ArrayList<IClasspathEntry> seen = new ArrayList<>();
402-
if (project.getModuleDescription() == null) {
404+
IModuleDescription modDesc = null;
405+
try {
406+
modDesc = project.getModuleDescription();
407+
} catch(CoreException ce) {
408+
// ignore
409+
}
410+
if (modDesc == null) {
403411
IPath outputLocation = project.getOutputLocation();
404412
if (outputLocation != null) {
405413
addPath(project, outputLocation, res);

0 commit comments

Comments
 (0)