Skip to content

Commit 73f08cf

Browse files
author
emmanue1
committed
Fix bug on parameters in 'this' and 'super' method invocations
1 parent c2962e6 commit 73f08cf

File tree

4 files changed

+51
-79
lines changed

4 files changed

+51
-79
lines changed

src/main/java/org/jd/core/v1/model/javasyntax/declaration/ClassDeclaration.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008-2019 Emmanuel Dupuy.
2+
* Copyright (c) 2008, 2019 Emmanuel Dupuy.
33
* This project is distributed under the GPLv3 license.
44
* This is a Copyleft license that gives the user the right to use,
55
* copy and modify the code freely for non-commercial purposes.
@@ -10,21 +10,21 @@
1010
import org.jd.core.v1.model.javasyntax.reference.BaseAnnotationReference;
1111
import org.jd.core.v1.model.javasyntax.type.BaseType;
1212
import org.jd.core.v1.model.javasyntax.type.BaseTypeParameter;
13-
import org.jd.core.v1.model.javasyntax.type.Type;
13+
import org.jd.core.v1.model.javasyntax.type.ObjectType;
1414

1515
public class ClassDeclaration extends InterfaceDeclaration {
16-
protected Type superType;
16+
protected ObjectType superType;
1717

1818
public ClassDeclaration(int flags, String internalName, String name, BodyDeclaration bodyDeclaration) {
1919
super(null, flags, internalName, name, null, null, bodyDeclaration);
2020
}
2121

22-
public ClassDeclaration(BaseAnnotationReference annotationReferences, int flags, String internalName, String name, BaseTypeParameter typeParameters, Type superType, BaseType interfaces, BodyDeclaration bodyDeclaration) {
22+
public ClassDeclaration(BaseAnnotationReference annotationReferences, int flags, String internalName, String name, BaseTypeParameter typeParameters, ObjectType superType, BaseType interfaces, BodyDeclaration bodyDeclaration) {
2323
super(annotationReferences, flags, internalName, name, typeParameters, interfaces, bodyDeclaration);
2424
this.superType = superType;
2525
}
2626

27-
public Type getSuperType() {
27+
public ObjectType getSuperType() {
2828
return superType;
2929
}
3030

src/main/java/org/jd/core/v1/service/converter/classfiletojavasyntax/model/javasyntax/declaration/ClassFileClassDeclaration.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008-2019 Emmanuel Dupuy.
2+
* Copyright (c) 2008, 2019 Emmanuel Dupuy.
33
* This project is distributed under the GPLv3 license.
44
* This is a Copyleft license that gives the user the right to use,
55
* copy and modify the code freely for non-commercial purposes.
@@ -11,12 +11,12 @@
1111
import org.jd.core.v1.model.javasyntax.reference.BaseAnnotationReference;
1212
import org.jd.core.v1.model.javasyntax.type.BaseType;
1313
import org.jd.core.v1.model.javasyntax.type.BaseTypeParameter;
14-
import org.jd.core.v1.model.javasyntax.type.Type;
14+
import org.jd.core.v1.model.javasyntax.type.ObjectType;
1515

1616
public class ClassFileClassDeclaration extends ClassDeclaration implements ClassFileMemberDeclaration {
1717
protected int firstLineNumber;
1818

19-
public ClassFileClassDeclaration(BaseAnnotationReference annotationReferences, int flags, String internalName, String name, BaseTypeParameter typeParameters, Type superType, BaseType interfaces, ClassFileBodyDeclaration bodyDeclaration) {
19+
public ClassFileClassDeclaration(BaseAnnotationReference annotationReferences, int flags, String internalName, String name, BaseTypeParameter typeParameters, ObjectType superType, BaseType interfaces, ClassFileBodyDeclaration bodyDeclaration) {
2020
super(annotationReferences, flags, internalName, name, typeParameters, superType, interfaces, bodyDeclaration);
2121
this.firstLineNumber = bodyDeclaration==null ? 0 : bodyDeclaration.getFirstLineNumber();
2222
}

src/main/java/org/jd/core/v1/service/converter/classfiletojavasyntax/util/SignatureParser.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ protected boolean isAReferenceTypeSignature(SignatureReader reader) {
510510
* SimpleClassTypeSignature: Identifier TypeArguments?
511511
* ClassTypeSignatureSuffix: '.' SimpleClassTypeSignature
512512
*/
513-
protected Type parseClassTypeSignature(SignatureReader reader, int dimension) {
513+
protected ObjectType parseClassTypeSignature(SignatureReader reader, int dimension) {
514514
if (reader.nextEqualsTo('L')) {
515515
// Skip 'L'. Parse 'PackageSpecifier* SimpleClassTypeSignature'
516516
int index = ++reader.index;
@@ -570,7 +570,7 @@ protected Type parseClassTypeSignature(SignatureReader reader, int dimension) {
570570
// Skip ';'
571571
reader.index++;
572572

573-
return (dimension==0) ? ot : ot.createType(dimension);
573+
return (dimension==0) ? ot : (ObjectType)ot.createType(dimension);
574574
} else {
575575
return null;
576576
}
@@ -778,7 +778,7 @@ public String toString() {
778778
public static class TypeTypes {
779779
public ObjectType thisType;
780780
public BaseTypeParameter typeParameters;
781-
public Type superType;
781+
public ObjectType superType;
782782
public BaseType interfaces;
783783
}
784784

src/main/java/org/jd/core/v1/service/converter/classfiletojavasyntax/visitor/InitInnerClassVisitor.java

Lines changed: 40 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import java.util.*;
2626

2727
public class InitInnerClassVisitor extends AbstractJavaSyntaxVisitor {
28-
protected UpdateReferencesVisitor updateReferencesVisitor = new UpdateReferencesVisitor();
28+
protected UpdateFieldReferencesVisitor updateFieldReferencesVisitor = new UpdateFieldReferencesVisitor();
2929
protected ObjectType outerType;
3030
protected DefaultList<String> outerLocalVariableNames = new DefaultList<>();
3131

@@ -63,7 +63,7 @@ public void visit(BodyDeclaration declaration) {
6363
bodyDeclaration.setOuterLocalVariableNames(outerLocalVariableNames.isEmpty() ? null : new DefaultList<>(outerLocalVariableNames));
6464

6565
if ((outerType != null) || !outerLocalVariableNames.isEmpty()) {
66-
updateReferencesVisitor.visit(bodyDeclaration);
66+
updateFieldReferencesVisitor.visit(bodyDeclaration);
6767
}
6868
}
6969

@@ -176,7 +176,7 @@ public void visit(MethodDeclaration declaration) {}
176176
@Override
177177
public void visit(StaticInitializerDeclaration declaration) {}
178178

179-
protected class UpdateReferencesVisitor extends AbstractJavaSyntaxVisitor {
179+
protected class UpdateFieldReferencesVisitor extends AbstractJavaSyntaxVisitor {
180180
@Override
181181
public void visit(BodyDeclaration declaration) {
182182
ClassFileBodyDeclaration bodyDeclaration = (ClassFileBodyDeclaration)declaration;
@@ -188,71 +188,6 @@ public void visit(MethodDeclaration declaration) {
188188
safeAccept(declaration.getStatements());
189189
}
190190

191-
@Override
192-
public void visit(StaticInitializerDeclaration declaration) {
193-
safeAccept(declaration.getStatements());
194-
}
195-
196-
@Override
197-
public void visit(SuperConstructorInvocationExpression expression) {
198-
SuperConstructorInvocationExpression cfscie = expression;
199-
200-
if (cfscie.getParameters() != null) {
201-
if (cfscie.getParameters().isList()) {
202-
visitParameters(cfscie.getParameters().getList());
203-
} else {
204-
cfscie.setParameters(visitParameter(cfscie.getParameters().getFirst()));
205-
}
206-
}
207-
}
208-
209-
@Override
210-
public void visit(ConstructorInvocationExpression expression) {
211-
ConstructorInvocationExpression cie = expression;
212-
213-
assert cie.getParameters() != null;
214-
215-
if (cie.getParameters().isList()) {
216-
DefaultList<Expression> parameters = cie.getParameters().getList();
217-
218-
parameters.remove(0);
219-
assert parameters.size() > 0;
220-
221-
if (parameters.size() == 1) {
222-
cie.setParameters(visitParameter(parameters.getFirst()));
223-
} else {
224-
visitParameters(parameters);
225-
}
226-
} else {
227-
cie.setParameters(null);
228-
}
229-
}
230-
231-
@SuppressWarnings("unchecked")
232-
protected void visitParameters(DefaultList<Expression> list) {
233-
ListIterator<Expression> iterator = list.listIterator();
234-
235-
while (iterator.hasNext()) {
236-
iterator.set(visitParameter(iterator.next()));
237-
}
238-
}
239-
240-
protected Expression visitParameter(Expression expression) {
241-
if (expression.getClass() == ClassFileLocalVariableReferenceExpression.class) {
242-
ClassFileLocalVariableReferenceExpression cflvre = (ClassFileLocalVariableReferenceExpression)expression;
243-
244-
if (outerLocalVariableNames.contains(cflvre.getName())) {
245-
return new FieldReferenceExpression(cflvre.getType(), new ObjectTypeReferenceExpression(cflvre.getLineNumber(), outerType), outerType.getInternalName(), cflvre.getName().substring(4), cflvre.getType().getDescriptor());
246-
} else if ((cflvre.getName() != null) && cflvre.getName().startsWith("this$") && cflvre.getType().getDescriptor().equals(outerType.getDescriptor())) {
247-
return new FieldReferenceExpression(outerType, new ObjectTypeReferenceExpression(cflvre.getLineNumber(), outerType), outerType.getInternalName(), "this", outerType.getDescriptor());
248-
}
249-
} else if (expression.getClass() == FieldReferenceExpression.class) {
250-
expression.accept(this);
251-
}
252-
253-
return expression;
254-
}
255-
256191
@Override
257192
public void visit(FieldReferenceExpression expression) {
258193
FieldReferenceExpression cffre = expression;
@@ -299,6 +234,7 @@ public void visit(NewExpression expression) {
299234

300235
public static class UpdateNewExpressionVisitor extends AbstractJavaSyntaxVisitor {
301236
protected ClassFileBodyDeclaration bodyDeclaration;
237+
protected String superTypeName;
302238
protected HashMap<String, String> finalLocalVariableNameMap = new HashMap<>();
303239
protected DefaultList<ClassFileClassDeclaration> localClassDeclarations = new DefaultList<>();
304240
protected HashSet<NewExpression> newExpressions = new HashSet<>();
@@ -312,8 +248,10 @@ public void visit(BodyDeclaration declaration) {
312248

313249
@Override
314250
public void visit(ConstructorDeclaration declaration) {
251+
superTypeName = ((ClassFileConstructorDeclaration)declaration).getClassFile().getSuperTypeName();
315252
finalLocalVariableNameMap.clear();
316253
localClassDeclarations.clear();
254+
317255
safeAccept(declaration.getStatements());
318256

319257
if (! finalLocalVariableNameMap.isEmpty()) {
@@ -507,6 +445,40 @@ public void visit(NewExpression expression) {
507445
}
508446
}
509447

448+
@Override
449+
public void visit(SuperConstructorInvocationExpression expression) {
450+
ClassFileMemberDeclaration memberDeclaration = bodyDeclaration.getInnerTypeDeclaration(superTypeName);
451+
452+
if ((memberDeclaration != null) && (memberDeclaration.getClass() == ClassFileClassDeclaration.class)) {
453+
ClassFileClassDeclaration cfcd = (ClassFileClassDeclaration) memberDeclaration;
454+
ClassFileBodyDeclaration cfbd = (ClassFileBodyDeclaration) cfcd.getBodyDeclaration();
455+
456+
if (cfbd.getOuterType() != null) {
457+
assert expression.getParameters().getFirst().getType().equals(cfbd.getOuterType());
458+
expression.setParameters(removeOuterThisParameter(expression.getParameters()));
459+
}
460+
}
461+
}
462+
463+
@Override
464+
public void visit(ConstructorInvocationExpression expression) {
465+
if (bodyDeclaration.getOuterType() != null) {
466+
assert expression.getParameters().getFirst().getType().equals(bodyDeclaration.getOuterType());
467+
expression.setParameters(removeOuterThisParameter(expression.getParameters()));
468+
}
469+
}
470+
471+
protected BaseExpression removeOuterThisParameter(BaseExpression parameters) {
472+
// Remove outer this
473+
if (parameters.isList()) {
474+
parameters.getList().removeFirst();
475+
} else {
476+
parameters = null;
477+
}
478+
479+
return parameters;
480+
}
481+
510482
protected class UpdateFinalFieldReferenceVisitor extends AbstractJavaSyntaxVisitor {
511483
protected boolean fina1;
512484

0 commit comments

Comments
 (0)