3232public class InitInnerClassVisitor extends AbstractJavaSyntaxVisitor {
3333 protected UpdateFieldDeclarationsAndReferencesVisitor updateFieldDeclarationsAndReferencesVisitor = new UpdateFieldDeclarationsAndReferencesVisitor ();
3434 protected DefaultList <String > syntheticInnerFieldNames = new DefaultList <>();
35- protected ObjectType outerType ;
35+ protected String outerTypeFieldName ;
3636
3737 @ Override
3838 public void visit (AnnotationDeclaration declaration ) {
@@ -59,18 +59,18 @@ public void visit(BodyDeclaration declaration) {
5959 ClassFileBodyDeclaration bodyDeclaration = (ClassFileBodyDeclaration )declaration ;
6060
6161 // Init attributes
62- outerType = null ;
62+ outerTypeFieldName = null ;
6363 syntheticInnerFieldNames .clear ();
6464 // Visit methods
6565 safeAcceptListDeclaration (bodyDeclaration .getMethodDeclarations ());
6666 // Init values
67- bodyDeclaration .setOuterType ( outerType );
67+ bodyDeclaration .setOuterTypeFieldName ( outerTypeFieldName );
6868
6969 if (!syntheticInnerFieldNames .isEmpty ()) {
7070 bodyDeclaration .setSyntheticInnerFieldNames (new DefaultList <>(syntheticInnerFieldNames ));
7171 }
7272
73- if ((outerType != null ) || !syntheticInnerFieldNames .isEmpty ()) {
73+ if ((outerTypeFieldName != null ) || !syntheticInnerFieldNames .isEmpty ()) {
7474 updateFieldDeclarationsAndReferencesVisitor .visit (bodyDeclaration );
7575 }
7676 }
@@ -81,6 +81,7 @@ public void visit(ConstructorDeclaration declaration) {
8181 ClassFileConstructorDeclaration cfcd = (ClassFileConstructorDeclaration )declaration ;
8282 ClassFile classFile = cfcd .getClassFile ();
8383 ClassFile outerClassFile = classFile .getOuterClassFile ();
84+ boolean removeFirstParameter = false ;
8485
8586 syntheticInnerFieldNames .clear ();
8687
@@ -103,7 +104,7 @@ public void visit(ConstructorDeclaration declaration) {
103104 // 'this(...)'
104105 if ((outerClassFile != null ) && !classFile .matchAccessFlags (ACC_STATIC )) {
105106 // Inner non-static class --> First parameter is the synthetic outer reference
106- outerType = ( ObjectType ) cfcd . getParameterTypes (). getFirst () ;
107+ removeFirstParameter = true ;
107108 }
108109 break ;
109110 }
@@ -115,7 +116,8 @@ public void visit(ConstructorDeclaration declaration) {
115116 String name = e .getName ();
116117
117118 if (name .startsWith ("this$" )) {
118- outerType = (ObjectType ) expression .getRightExpression ().getType ();
119+ outerTypeFieldName = name ;
120+ removeFirstParameter = true ;
119121 } else if (name .startsWith ("val$" )) {
120122 syntheticInnerFieldNames .add (name );
121123 }
@@ -134,7 +136,7 @@ public void visit(ConstructorDeclaration declaration) {
134136 if (parameters .isList ()) {
135137 List <FormalParameter > list = parameters .getList ();
136138
137- if (outerType != null ) {
139+ if (removeFirstParameter ) {
138140 // Remove outer this
139141 list .remove (0 );
140142 }
@@ -146,7 +148,7 @@ public void visit(ConstructorDeclaration declaration) {
146148 int size = list .size ();
147149 list .subList (size - count , size ).clear ();
148150 }
149- } else if (( outerType != null ) || !syntheticInnerFieldNames .isEmpty ()) {
151+ } else if (removeFirstParameter || !syntheticInnerFieldNames .isEmpty ()) {
150152 // Remove outer this and outer local variable reference
151153 cfcd .setFormalParameters (null );
152154 }
@@ -211,7 +213,7 @@ public void visit(FieldDeclaration declaration) {
211213 public void visit (FieldDeclarator declarator ) {
212214 String name = declarator .getName ();
213215
214- if (name .startsWith ( "this$" ) || syntheticInnerFieldNames .contains (name )) {
216+ if (name .equals ( outerTypeFieldName ) || syntheticInnerFieldNames .contains (name )) {
215217 syntheticField = true ;
216218 }
217219 }
@@ -235,24 +237,25 @@ public void visit(NewExpression expression) {
235237 @ Override
236238 public void visit (FieldReferenceExpression expression ) {
237239 if (expression .getName ().startsWith ("this$" )) {
238- if (expression .getType ().getDescriptor ().equals (outerType .getDescriptor ())) {
239- Expression exp = (expression .getExpression () == null ) ? expression : expression .getExpression ();
240- expression .setExpression (new ObjectTypeReferenceExpression (exp .getLineNumber (), outerType .createType (null )));
241- expression .setName ("this" );
240+ if (expression .getInternalTypeName ().equals (bodyDeclaration .getInternalTypeName ())) {
241+ if (expression .getName ().equals (outerTypeFieldName )) {
242+ ObjectType objectType = (ObjectType )expression .getType ();
243+ Expression exp = (expression .getExpression () == null ) ? expression : expression .getExpression ();
244+ expression .setExpression (new ObjectTypeReferenceExpression (exp .getLineNumber (), objectType .createType (null )));
245+ expression .setName ("this" );
246+ }
242247 } else {
243248 ClassFileTypeDeclaration typeDeclaration = bodyDeclaration .getInnerTypeDeclaration (expression .getInternalTypeName ());
244249
245250 if ((typeDeclaration != null ) && typeDeclaration .isClassDeclaration ()) {
246- if (typeDeclaration .getInternalTypeName ().equals (expression .getInternalTypeName ())) {
247- ClassFileBodyDeclaration cfbd = (ClassFileBodyDeclaration ) typeDeclaration .getBodyDeclaration ();
248- String outerInternalTypeName = cfbd .getOuterBodyDeclaration ().getInternalTypeName ();
249- ObjectType objectType = (ObjectType )expression .getType ();
250-
251- if (outerInternalTypeName .equals (objectType .getInternalName ())) {
252- Expression exp = (expression .getExpression () == null ) ? expression : expression .getExpression ();
253- expression .setExpression (new ObjectTypeReferenceExpression (exp .getLineNumber (), objectType .createType (null )));
254- expression .setName ("this" );
255- }
251+ ClassFileBodyDeclaration cfbd = (ClassFileBodyDeclaration ) typeDeclaration .getBodyDeclaration ();
252+ String outerInternalTypeName = cfbd .getOuterBodyDeclaration ().getInternalTypeName ();
253+ ObjectType objectType = (ObjectType )expression .getType ();
254+
255+ if (outerInternalTypeName .equals (objectType .getInternalName ())) {
256+ Expression exp = (expression .getExpression () == null ) ? expression : expression .getExpression ();
257+ expression .setExpression (new ObjectTypeReferenceExpression (exp .getLineNumber (), objectType .createType (null )));
258+ expression .setName ("this" );
256259 }
257260 }
258261 }
@@ -267,10 +270,11 @@ public void visit(FieldReferenceExpression expression) {
267270 @ Override
268271 protected Expression updateExpression (Expression expression ) {
269272 if (expression .isLocalVariableReferenceExpression ()) {
270- ClassFileLocalVariableReferenceExpression cdlvre = (ClassFileLocalVariableReferenceExpression ) expression ;
271-
272- if ((cdlvre .getName () != null ) && cdlvre .getName ().startsWith ("this$" ) && cdlvre .getType ().getDescriptor ().equals (outerType .getDescriptor ())) {
273- return new FieldReferenceExpression (outerType , new ObjectTypeReferenceExpression (cdlvre .getLineNumber (), outerType .createType (null )), outerType .getInternalName (), "this" , outerType .getDescriptor ());
273+ if ((expression .getName () != null ) && expression .getName ().equals (outerTypeFieldName ) && expression .getType ().isObjectType ()) {
274+ ObjectType objectType = (ObjectType )expression .getType ();
275+ if (bodyDeclaration .getOuterBodyDeclaration ().getInternalTypeName ().equals (objectType .getInternalName ())) {
276+ return new FieldReferenceExpression (objectType , new ObjectTypeReferenceExpression (expression .getLineNumber (), objectType .createType (null )), objectType .getInternalName (), "this" , objectType .getDescriptor ());
277+ }
274278 }
275279 }
276280
@@ -434,7 +438,7 @@ public void visit(NewExpression expression) {
434438 DefaultList <Expression > list = parameters .getList ();
435439 DefaultList <Type > types = parameterTypes .getList ();
436440
437- if (cfbd .getOuterType () != null ) {
441+ if (cfbd .getOuterTypeFieldName () != null ) {
438442 // Remove outer this
439443 list .removeFirst ();
440444 types .removeFirst ();
@@ -466,7 +470,7 @@ public void visit(NewExpression expression) {
466470 lastParameters .clear ();
467471 types .subList (size - count , size ).clear ();
468472 }
469- } else if (cfbd .getOuterType () != null ) {
473+ } else if (cfbd .getOuterTypeFieldName () != null ) {
470474 // Remove outer this
471475 ne .setParameters (null );
472476 ne .setParameterTypes (null );
@@ -520,7 +524,7 @@ public void visit(SuperConstructorInvocationExpression expression) {
520524 // Remove outer 'this' reference parameter
521525 Type firstParameterType = parameters .getFirst ().getType ();
522526
523- if (firstParameterType .isObjectType () && !classFile .matchAccessFlags (ACC_STATIC ) && (bodyDeclaration .getOuterType () != null )) {
527+ if (firstParameterType .isObjectType () && !classFile .matchAccessFlags (ACC_STATIC ) && (bodyDeclaration .getOuterTypeFieldName () != null )) {
524528 TypeMaker .TypeTypes superTypeTypes = typeMaker .makeTypeTypes (classFile .getSuperTypeName ());
525529
526530 if ((superTypeTypes != null ) && superTypeTypes .thisType .isInnerObjectType ()) {
@@ -543,7 +547,7 @@ public void visit(ConstructorInvocationExpression expression) {
543547
544548 if ((parameters != null ) && (parameters .size () > 0 )) {
545549 // Remove outer this reference parameter
546- if (parameters . getFirst (). getType (). equals ( bodyDeclaration .getOuterType ()) ) {
550+ if (bodyDeclaration .getOuterTypeFieldName () != null ) {
547551 cie .setParameters (removeFirstItem (parameters ));
548552 cie .setParameterTypes (removeFirstItem (cie .getParameterTypes ()));
549553 }
0 commit comments