2222import java .util .List ;
2323import java .util .Map ;
2424import java .util .Objects ;
25+ import java .util .Optional ;
2526import java .util .function .Function ;
2627import 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 ();
0 commit comments