Skip to content

Commit

Permalink
Fix JD-GUI java-decompiler#252 : incorrect integer constant type
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanue1 committed Jul 7, 2019
1 parent 09f5e60 commit 4ca89c9
Show file tree
Hide file tree
Showing 14 changed files with 43 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
public class NewExpression extends AbstractLineNumberExpression {
protected BaseTypeArgument nonWildcardTypeArguments;
protected ObjectType type;
protected String descriptor;
protected BaseExpression parameters;
protected BodyDeclaration bodyDeclaration;

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

public NewExpression(int lineNumber, BaseTypeArgument nonWildcardTypeArguments, ObjectType type, BaseExpression parameters, BodyDeclaration bodyDeclaration) {
public NewExpression(int lineNumber, BaseTypeArgument nonWildcardTypeArguments, ObjectType type, String descriptor, BaseExpression parameters, BodyDeclaration bodyDeclaration) {
super(lineNumber);
this.nonWildcardTypeArguments = nonWildcardTypeArguments;
this.type = type;
this.descriptor = descriptor;
this.parameters = parameters;
this.bodyDeclaration = bodyDeclaration;
}
Expand All @@ -59,6 +61,10 @@ public int getPriority() {
return 3;
}

public String getDescriptor() {
return descriptor;
}

public BaseExpression getParameters() {
return parameters;
}
Expand All @@ -67,6 +73,11 @@ public void setParameters(BaseExpression parameters) {
this.parameters = parameters;
}

public void setDescriptorAndParameters(String descriptor, BaseExpression parameters) {
this.descriptor = descriptor;
this.parameters = parameters;
}

public BodyDeclaration getBodyDeclaration() {
return bodyDeclaration;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
public class NewInnerExpression extends NewExpression {
protected Expression expression;

public NewInnerExpression(int lineNumber, BaseTypeArgument nonWildcardTypeArguments, ObjectType type, BaseExpression parameters, BodyDeclaration bodyDeclaration, Expression expression) {
super(lineNumber, nonWildcardTypeArguments, type, parameters, bodyDeclaration);
public NewInnerExpression(int lineNumber, BaseTypeArgument nonWildcardTypeArguments, ObjectType type, String descriptor, BaseExpression parameters, BodyDeclaration bodyDeclaration, Expression expression) {
super(lineNumber, nonWildcardTypeArguments, type, descriptor, parameters, bodyDeclaration);
this.expression = expression;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ public void parse(BasicBlock basicBlock, Statements<Statement> statements, Defau
"<init>".equals(name)) {

if (expression1.getClass() == NewExpression.class) {
((NewExpression) expression1).setParameters(parameters);
((NewExpression)expression1).setDescriptorAndParameters(descriptor, parameters);
} else if (expression1.getType().equals(ot)) {
statements.add(new ExpressionStatement(new ConstructorInvocationExpression(lineNumber, ot, descriptor, parameters)));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,23 @@ public void visit(MethodInvocationExpression expression) {
expression.getExpression().accept(this);
}

@Override
public void visit(NewExpression expression) {
BaseExpression parameters = expression.getParameters();

if (parameters != null) {
String internalTypeName = expression.getObjectType().getInternalName();
String descriptor = expression.getDescriptor();
List<Type> types = TYPES.get(internalTypeName + ":<init>" + descriptor);

if (types == null) {
types = signatureParser.parseParameterTypes(descriptor);
}

expression.setParameters(updateExpressions(types, parameters));
}
}

@Override
public void visit(NewArray expression) {
BaseExpression dimensions = expression.getDimensionExpressionList();
Expand Down
9 changes: 8 additions & 1 deletion src/test/java/org/jd/core/v1/ClassFileToJavaSourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ public void testJdk170Basic() throws Exception {

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

assertTrue(source.matches(PatternMaker.make("[ 186: 186]", "protected static final Integer INTEGER_255 = new Integer(255);")));

assertTrue(source.indexOf("<init>()") == -1);
assertTrue(source.indexOf("null = ") == -1);
assertTrue(source.indexOf("NaND") == -1);
Expand Down Expand Up @@ -144,7 +146,7 @@ public void testJdk170NoDebugBasic() throws Exception {
assertTrue(source.matches(PatternMaker.make("System.out.println(\"hello\");")));

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

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

assertTrue(source.indexOf("protected static final Integer INTEGER_255 = new Integer(255);") != -1);

assertTrue(source.indexOf("<init>()") == -1);
assertTrue(source.indexOf("null = ") == -1);
assertTrue(source.indexOf("NaND") == -1);
Expand Down Expand Up @@ -2285,6 +2289,7 @@ public void testJdk142Basic() throws Exception {
assertTrue(source.matches(PatternMaker.make("[ 171: 171]", "return str + str;")));
assertTrue(source.matches(PatternMaker.make("[ 174: 174]", "return str;")));
assertTrue(source.matches(PatternMaker.make("[ 183: 183]", "return ((Basic)objects[index]).int78;")));
assertTrue(source.matches(PatternMaker.make("[ 186: 186]", "protected static final Integer INTEGER_255 = new Integer(255);")));

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

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

assertTrue(source.indexOf("// Byte code:") == -1);
assertTrue(source.indexOf(".null.") == -1 && source.indexOf(".null ") == -1 && source.indexOf("null = ") == -1);
Expand Down
4 changes: 3 additions & 1 deletion src/test/resources/java/org/jd/core/test/Basic.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,6 @@ public Basic getLast(Object[] objects) {
public int getInt78(Object[] objects, short index) {
return ((Basic)objects[index]).int78;
}
}

protected final static Integer INTEGER_255 = new Integer(255);
}
Binary file modified src/test/resources/zip/data-java-jdk-1.4.2.zip
Binary file not shown.
Binary file modified src/test/resources/zip/data-java-jdk-1.5.0.zip
Binary file not shown.
Binary file modified src/test/resources/zip/data-java-jdk-1.6.0.zip
Binary file not shown.
Binary file modified src/test/resources/zip/data-java-jdk-1.7.0-no-debug-info.zip
Binary file not shown.
Binary file modified src/test/resources/zip/data-java-jdk-1.7.0.zip
Binary file not shown.
Binary file modified src/test/resources/zip/data-java-jdk-1.8.0.zip
Binary file not shown.
Binary file modified src/test/resources/zip/data-java-jdk-10.0.2.zip
Binary file not shown.
Binary file modified src/test/resources/zip/data-java-jdk-9.0.1.zip
Binary file not shown.

0 comments on commit 4ca89c9

Please sign in to comment.