Skip to content

Commit 87ecfad

Browse files
author
emmanue1
committed
Fix bugs on local variable declaration
1 parent 5ae9e8a commit 87ecfad

14 files changed

+245
-55
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright (c) 2008-2019 Emmanuel Dupuy.
3+
* This project is distributed under the GPLv3 license.
4+
* This is a Copyleft license that gives the user the right to use,
5+
* copy and modify the code freely for non-commercial purposes.
6+
*/
7+
8+
package org.jd.core.v1.service.converter.classfiletojavasyntax.model.javasyntax.declaration;
9+
10+
import org.jd.core.v1.model.javasyntax.declaration.FormalParameter;
11+
import org.jd.core.v1.model.javasyntax.reference.BaseAnnotationReference;
12+
import org.jd.core.v1.model.javasyntax.type.Type;
13+
import org.jd.core.v1.service.converter.classfiletojavasyntax.model.localvariable.AbstractLocalVariable;
14+
import org.jd.core.v1.service.converter.classfiletojavasyntax.model.localvariable.LocalVariableReference;
15+
16+
17+
public class ClassFileFormalParameter extends FormalParameter implements LocalVariableReference {
18+
protected AbstractLocalVariable localVariable;
19+
20+
public ClassFileFormalParameter(AbstractLocalVariable localVariable) {
21+
super(null, null);
22+
this.localVariable = localVariable;
23+
}
24+
25+
public ClassFileFormalParameter(AbstractLocalVariable localVariable, boolean varargs) {
26+
super(null, varargs, null);
27+
this.localVariable = localVariable;
28+
}
29+
30+
public ClassFileFormalParameter(BaseAnnotationReference annotationReferences, AbstractLocalVariable localVariable, boolean varargs) {
31+
super(annotationReferences, null, varargs, null);
32+
this.localVariable = localVariable;
33+
}
34+
35+
public Type getType() {
36+
return localVariable.getType();
37+
}
38+
39+
public String getName() {
40+
return localVariable.getName();
41+
}
42+
43+
public void setName(String name) {
44+
localVariable.setName(name);
45+
}
46+
47+
@Override
48+
public AbstractLocalVariable getLocalVariable() {
49+
return localVariable;
50+
}
51+
52+
@Override
53+
public void setLocalVariable(AbstractLocalVariable localVariable) {
54+
this.localVariable = localVariable;
55+
}
56+
57+
@Override
58+
public String toString() {
59+
String s = "ClassFileFormalParameter{";
60+
61+
if (annotationReferences != null)
62+
s += annotationReferences + " ";
63+
64+
Type type = localVariable.getType();
65+
66+
if (varargs)
67+
s += type.createType(type.getDimension()-1) + "... ";
68+
else
69+
s += type + " ";
70+
71+
return s + localVariable.getName() + "}";
72+
}
73+
}

src/main/java/org/jd/core/v1/service/converter/classfiletojavasyntax/model/javasyntax/expression/ClassFileLocalVariableReferenceExpression.java

Lines changed: 5 additions & 2 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,8 +10,9 @@
1010
import org.jd.core.v1.model.javasyntax.expression.LocalVariableReferenceExpression;
1111
import org.jd.core.v1.model.javasyntax.type.Type;
1212
import org.jd.core.v1.service.converter.classfiletojavasyntax.model.localvariable.AbstractLocalVariable;
13+
import org.jd.core.v1.service.converter.classfiletojavasyntax.model.localvariable.LocalVariableReference;
1314

14-
public class ClassFileLocalVariableReferenceExpression extends LocalVariableReferenceExpression {
15+
public class ClassFileLocalVariableReferenceExpression extends LocalVariableReferenceExpression implements LocalVariableReference {
1516
protected AbstractLocalVariable localVariable;
1617

1718
public ClassFileLocalVariableReferenceExpression(int lineNumber, AbstractLocalVariable localVariable) {
@@ -30,10 +31,12 @@ public String getName() {
3031
return localVariable.getName();
3132
}
3233

34+
@Override
3335
public AbstractLocalVariable getLocalVariable() {
3436
return localVariable;
3537
}
3638

39+
@Override
3740
public void setLocalVariable(AbstractLocalVariable localVariable) {
3841
this.localVariable = localVariable;
3942
}

src/main/java/org/jd/core/v1/service/converter/classfiletojavasyntax/model/localvariable/AbstractLocalVariable.java

Lines changed: 8 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.
@@ -20,7 +20,7 @@ public abstract class AbstractLocalVariable implements LocalVariable {
2020
protected int toOffset;
2121
protected String name;
2222
protected int dimension;
23-
protected DefaultList<ClassFileLocalVariableReferenceExpression> references = new DefaultList<>();
23+
protected DefaultList<LocalVariableReference> references = new DefaultList<>();
2424

2525
public AbstractLocalVariable(int index, int offset, int dimension) {
2626
this.declared = (offset == 0);
@@ -106,9 +106,13 @@ public void setToOffset(int offset) {
106106

107107
@Override public String getName() { return name; }
108108

109+
public void setName(String name) {
110+
this.name = name;
111+
}
112+
109113
public int getDimension() { return dimension; }
110114

111-
public DefaultList<ClassFileLocalVariableReferenceExpression> getReferences() {
115+
public DefaultList<LocalVariableReference> getReferences() {
112116
return references;
113117
}
114118

@@ -121,8 +125,7 @@ public DefaultList<ClassFileLocalVariableReferenceExpression> getReferences() {
121125
public abstract void rightReduce(Type otherType);
122126
public abstract void rightReduce(AbstractLocalVariable other);
123127

124-
@Override
125-
public void addReference(ClassFileLocalVariableReferenceExpression reference) {
128+
public void addReference(LocalVariableReference reference) {
126129
references.add(reference);
127130
}
128131
}

src/main/java/org/jd/core/v1/service/converter/classfiletojavasyntax/model/localvariable/Frame.java

Lines changed: 25 additions & 4 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.
@@ -98,12 +98,34 @@ public void mergeLocalVariable(AbstractLocalVariable lv) {
9898
}
9999
}
100100
} else if (lv != alvToMerge) {
101-
for (ClassFileLocalVariableReferenceExpression reference : alvToMerge.getReferences()) {
101+
for (LocalVariableReference reference : alvToMerge.getReferences()) {
102102
reference.setLocalVariable(lv);
103103
}
104104

105105
lv.getReferences().addAll(alvToMerge.getReferences());
106106
lv.setFromOffset(alvToMerge.getFromOffset());
107+
108+
if (!lv.isAssignable(alvToMerge)) {
109+
Type type = lv.getType();
110+
Type alvToMergeType = alvToMerge.getType();
111+
112+
assert (type.isPrimitive() == alvToMergeType.isPrimitive()) && (type.isObject() == alvToMergeType.isObject()) && (type.isGeneric() == alvToMergeType.isGeneric());
113+
114+
if (type.isPrimitive()) {
115+
if (alvToMerge.isAssignable(lv)) {
116+
((PrimitiveLocalVariable)lv).setPrimitiveType((PrimitiveType)type);
117+
} else {
118+
((PrimitiveLocalVariable)lv).setPrimitiveType(PrimitiveType.TYPE_INT);
119+
}
120+
} else if (type.isObject()) {
121+
if (alvToMerge.isAssignable(lv)) {
122+
((ObjectLocalVariable)lv).setObjectType((ObjectType)type);
123+
} else {
124+
((ObjectLocalVariable)lv).setObjectType(ObjectType.TYPE_OBJECT);
125+
}
126+
}
127+
}
128+
107129
localVariableArray[index] = alvToMerge.getNext();
108130
}
109131
}
@@ -474,8 +496,7 @@ protected void updateForStatement(
474496
}
475497

476498
@SuppressWarnings("unchecked")
477-
protected void updateForStatement(
478-
HashSet<AbstractLocalVariable> variablesToDeclare, HashSet<AbstractLocalVariable> foundVariables, ClassFileForStatement forStatement, Expressions init) {
499+
protected void updateForStatement(HashSet<AbstractLocalVariable> variablesToDeclare, HashSet<AbstractLocalVariable> foundVariables, ClassFileForStatement forStatement, Expressions init) {
479500

480501
DefaultList<BinaryOperatorExpression> boes = new DefaultList<>();
481502
DefaultList<AbstractLocalVariable> localVariables = new DefaultList<>();
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.
@@ -8,15 +8,12 @@
88
package org.jd.core.v1.service.converter.classfiletojavasyntax.model.localvariable;
99

1010
import org.jd.core.v1.model.javasyntax.type.Type;
11-
import org.jd.core.v1.service.converter.classfiletojavasyntax.model.javasyntax.expression.ClassFileLocalVariableReferenceExpression;
1211

1312

1413
public interface LocalVariable {
1514
Type getType();
1615

1716
String getName();
1817

19-
void addReference(ClassFileLocalVariableReferenceExpression reference);
20-
2118
void accept(LocalVariableVisitor visitor);
2219
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright (c) 2008, 2019 Emmanuel Dupuy.
3+
* This project is distributed under the GPLv3 license.
4+
* This is a Copyleft license that gives the user the right to use,
5+
* copy and modify the code freely for non-commercial purposes.
6+
*/
7+
8+
package org.jd.core.v1.service.converter.classfiletojavasyntax.model.localvariable;
9+
10+
11+
public interface LocalVariableReference {
12+
AbstractLocalVariable getLocalVariable();
13+
14+
void setLocalVariable(AbstractLocalVariable localVariable);
15+
}

src/main/java/org/jd/core/v1/service/converter/classfiletojavasyntax/model/localvariable/LocalVariableSet.java

Lines changed: 18 additions & 1 deletion
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.
@@ -95,6 +95,23 @@ public AbstractLocalVariable remove(int index, int offset) {
9595
return null;
9696
}
9797

98+
public AbstractLocalVariable get(int index, int offset) {
99+
if (index < array.length) {
100+
AbstractLocalVariable lv = array[index];
101+
102+
while (lv != null) {
103+
if (lv.fromOffset <= offset) {
104+
return lv;
105+
}
106+
107+
assert lv != lv.getNext();
108+
lv = lv.getNext();
109+
}
110+
}
111+
112+
return null;
113+
}
114+
98115
public boolean isEmpty() {
99116
return size == 0;
100117
}

src/main/java/org/jd/core/v1/service/converter/classfiletojavasyntax/model/localvariable/ObjectLocalVariable.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ public Type getType() {
7676
return (dimension == 0) ? toType : arrayType;
7777
}
7878

79+
public void setObjectType(ObjectType type) {
80+
dimension = type.getDimension();
81+
82+
if (dimension == 0) {
83+
this.fromType = this.toType = (ObjectType)type;
84+
} else {
85+
this.arrayType = type;
86+
}
87+
}
88+
7989
@Override
8090
public boolean isAssignable(AbstractLocalVariable other) {
8191
if (other.getClass() == ObjectLocalVariable.class) {

src/main/java/org/jd/core/v1/service/converter/classfiletojavasyntax/model/localvariable/PrimitiveLocalVariable.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ public Type getType() {
5757
return PrimitiveTypeUtil.getPrimitiveType(flags, dimension);
5858
}
5959

60+
public void setPrimitiveType(PrimitiveType type) {
61+
assert type.getDimension() == 0;
62+
this.flags = type.getFlags();
63+
}
64+
6065
@Override
6166
public boolean isAssignable(AbstractLocalVariable other) {
6267
if ((other.getDimension() == 0) && (other.getClass() == PrimitiveLocalVariable.class)) {

0 commit comments

Comments
 (0)