Skip to content

Commit 4ca89c9

Browse files
author
emmanue1
committed
Fix JD-GUI #252 : incorrect integer constant type
1 parent 09f5e60 commit 4ca89c9

File tree

14 files changed

+43
-6
lines changed

14 files changed

+43
-6
lines changed

src/main/java/org/jd/core/v1/model/javasyntax/expression/NewExpression.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
public class NewExpression extends AbstractLineNumberExpression {
1616
protected BaseTypeArgument nonWildcardTypeArguments;
1717
protected ObjectType type;
18+
protected String descriptor;
1819
protected BaseExpression parameters;
1920
protected BodyDeclaration bodyDeclaration;
2021

@@ -29,10 +30,11 @@ public NewExpression(int lineNumber, ObjectType type, BodyDeclaration bodyDeclar
2930
this.bodyDeclaration = bodyDeclaration;
3031
}
3132

32-
public NewExpression(int lineNumber, BaseTypeArgument nonWildcardTypeArguments, ObjectType type, BaseExpression parameters, BodyDeclaration bodyDeclaration) {
33+
public NewExpression(int lineNumber, BaseTypeArgument nonWildcardTypeArguments, ObjectType type, String descriptor, BaseExpression parameters, BodyDeclaration bodyDeclaration) {
3334
super(lineNumber);
3435
this.nonWildcardTypeArguments = nonWildcardTypeArguments;
3536
this.type = type;
37+
this.descriptor = descriptor;
3638
this.parameters = parameters;
3739
this.bodyDeclaration = bodyDeclaration;
3840
}
@@ -59,6 +61,10 @@ public int getPriority() {
5961
return 3;
6062
}
6163

64+
public String getDescriptor() {
65+
return descriptor;
66+
}
67+
6268
public BaseExpression getParameters() {
6369
return parameters;
6470
}
@@ -67,6 +73,11 @@ public void setParameters(BaseExpression parameters) {
6773
this.parameters = parameters;
6874
}
6975

76+
public void setDescriptorAndParameters(String descriptor, BaseExpression parameters) {
77+
this.descriptor = descriptor;
78+
this.parameters = parameters;
79+
}
80+
7081
public BodyDeclaration getBodyDeclaration() {
7182
return bodyDeclaration;
7283
}

src/main/java/org/jd/core/v1/model/javasyntax/expression/NewInnerExpression.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
public class NewInnerExpression extends NewExpression {
1515
protected Expression expression;
1616

17-
public NewInnerExpression(int lineNumber, BaseTypeArgument nonWildcardTypeArguments, ObjectType type, BaseExpression parameters, BodyDeclaration bodyDeclaration, Expression expression) {
18-
super(lineNumber, nonWildcardTypeArguments, type, parameters, bodyDeclaration);
17+
public NewInnerExpression(int lineNumber, BaseTypeArgument nonWildcardTypeArguments, ObjectType type, String descriptor, BaseExpression parameters, BodyDeclaration bodyDeclaration, Expression expression) {
18+
super(lineNumber, nonWildcardTypeArguments, type, descriptor, parameters, bodyDeclaration);
1919
this.expression = expression;
2020
}
2121

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ public void parse(BasicBlock basicBlock, Statements<Statement> statements, Defau
763763
"<init>".equals(name)) {
764764

765765
if (expression1.getClass() == NewExpression.class) {
766-
((NewExpression) expression1).setParameters(parameters);
766+
((NewExpression)expression1).setDescriptorAndParameters(descriptor, parameters);
767767
} else if (expression1.getType().equals(ot)) {
768768
statements.add(new ExpressionStatement(new ConstructorInvocationExpression(lineNumber, ot, descriptor, parameters)));
769769
} else {

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,23 @@ public void visit(MethodInvocationExpression expression) {
205205
expression.getExpression().accept(this);
206206
}
207207

208+
@Override
209+
public void visit(NewExpression expression) {
210+
BaseExpression parameters = expression.getParameters();
211+
212+
if (parameters != null) {
213+
String internalTypeName = expression.getObjectType().getInternalName();
214+
String descriptor = expression.getDescriptor();
215+
List<Type> types = TYPES.get(internalTypeName + ":<init>" + descriptor);
216+
217+
if (types == null) {
218+
types = signatureParser.parseParameterTypes(descriptor);
219+
}
220+
221+
expression.setParameters(updateExpressions(types, parameters));
222+
}
223+
}
224+
208225
@Override
209226
public void visit(NewArray expression) {
210227
BaseExpression dimensions = expression.getDimensionExpressionList();

src/test/java/org/jd/core/v1/ClassFileToJavaSourceTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ public void testJdk170Basic() throws Exception {
110110

111111
assertTrue(source.matches(PatternMaker.make("[ 183: 183]", "return ((Basic)objects[index]).int78;")));
112112

113+
assertTrue(source.matches(PatternMaker.make("[ 186: 186]", "protected static final Integer INTEGER_255 = new Integer(255);")));
114+
113115
assertTrue(source.indexOf("<init>()") == -1);
114116
assertTrue(source.indexOf("null = ") == -1);
115117
assertTrue(source.indexOf("NaND") == -1);
@@ -144,7 +146,7 @@ public void testJdk170NoDebugBasic() throws Exception {
144146
assertTrue(source.matches(PatternMaker.make("System.out.println(\"hello\");")));
145147

146148
assertTrue(source.matches(PatternMaker.make("String str1 = \"3 == \" + (paramInt + 1) + \" ?\";")));
147-
assertTrue(source.matches(PatternMaker.make("String str2 = \"abc \\\\b \\\\f \\\\n \\\\r \\\\t \\\\\\\" \\\\007 def\";")));
149+
assertTrue(source.indexOf("String str2 = str1.valueOf(\"abc \\b \\f \\n \\r \\t \\\" \\007 def\");") != -1);
148150
assertTrue(source.matches(PatternMaker.make("char c2 = '€';")));
149151
assertTrue(source.matches(PatternMaker.make("char c4 = c3 = c2 = c1 = Character.toUpperCase('x');")));
150152
assertTrue(source.matches(PatternMaker.make("Class clazz3 = String.class;")));
@@ -156,6 +158,8 @@ public void testJdk170NoDebugBasic() throws Exception {
156158

157159
assertTrue(source.matches(PatternMaker.make("this.int78 = 50 / (25 + (this.int78 = 789));")));
158160

161+
assertTrue(source.indexOf("protected static final Integer INTEGER_255 = new Integer(255);") != -1);
162+
159163
assertTrue(source.indexOf("<init>()") == -1);
160164
assertTrue(source.indexOf("null = ") == -1);
161165
assertTrue(source.indexOf("NaND") == -1);
@@ -2285,6 +2289,7 @@ public void testJdk142Basic() throws Exception {
22852289
assertTrue(source.matches(PatternMaker.make("[ 171: 171]", "return str + str;")));
22862290
assertTrue(source.matches(PatternMaker.make("[ 174: 174]", "return str;")));
22872291
assertTrue(source.matches(PatternMaker.make("[ 183: 183]", "return ((Basic)objects[index]).int78;")));
2292+
assertTrue(source.matches(PatternMaker.make("[ 186: 186]", "protected static final Integer INTEGER_255 = new Integer(255);")));
22882293

22892294
assertTrue(source.indexOf("// Byte code:") == -1);
22902295
assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1);
@@ -2324,6 +2329,7 @@ public void testJdk901Basic() throws Exception {
23242329
assertTrue(source.matches(PatternMaker.make("[ 171: 171]", "return str + str;")));
23252330
assertTrue(source.matches(PatternMaker.make("[ 174: 174]", "return str;")));
23262331
assertTrue(source.matches(PatternMaker.make("[ 183: 183]", "return ((Basic)objects[index]).int78;")));
2332+
assertTrue(source.matches(PatternMaker.make("[ 186: 186]", "protected static final Integer INTEGER_255 = new Integer(255);")));
23272333

23282334
assertTrue(source.indexOf("// Byte code:") == -1);
23292335
assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1);
@@ -2363,6 +2369,7 @@ public void testJdk1002Basic() throws Exception {
23632369
assertTrue(source.matches(PatternMaker.make("[ 171: 171]", "return str + str;")));
23642370
assertTrue(source.matches(PatternMaker.make("[ 174: 174]", "return str;")));
23652371
assertTrue(source.matches(PatternMaker.make("[ 183: 183]", "return ((Basic)objects[index]).int78;")));
2372+
assertTrue(source.matches(PatternMaker.make("[ 186: 186]", "protected static final Integer INTEGER_255 = new Integer(255);")));
23662373

23672374
assertTrue(source.indexOf("// Byte code:") == -1);
23682375
assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1);

src/test/resources/java/org/jd/core/test/Basic.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,6 @@ public Basic getLast(Object[] objects) {
182182
public int getInt78(Object[] objects, short index) {
183183
return ((Basic)objects[index]).int78;
184184
}
185-
}
185+
186+
protected final static Integer INTEGER_255 = new Integer(255);
187+
}
57 Bytes
Binary file not shown.
67 Bytes
Binary file not shown.
70 Bytes
Binary file not shown.
584 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)