Removed unnecessary boxing of primitive types

master
Dmitry Cherniachenko 8 years ago committed by Egor.Ushakov
parent cd1acfa266
commit 7e1cb88fe2
  1. 49
      src/org/jetbrains/java/decompiler/modules/decompiler/exps/InvocationExprent.java
  2. 18
      testData/results/TestPrimitives.dec

@ -216,6 +216,11 @@ public class InvocationExprent extends Exprent {
tracer.addMapping(bytecode); tracer.addMapping(bytecode);
if (isStatic) { if (isStatic) {
if (isBoxingCall()) {
ExprProcessor.getCastedExprent(lstParameters.get(0), descriptor.params[0], buf, indent, false, false, tracer);
return buf;
}
ClassNode node = (ClassNode)DecompilerContext.getProperty(DecompilerContext.CURRENT_CLASS_NODE); ClassNode node = (ClassNode)DecompilerContext.getProperty(DecompilerContext.CURRENT_CLASS_NODE);
if (node == null || !classname.equals(node.classStruct.qualifiedName)) { if (node == null || !classname.equals(node.classStruct.qualifiedName)) {
buf.append(DecompilerContext.getImportCollector().getShortName(ExprProcessor.buildJavaClassName(classname))); buf.append(DecompilerContext.getImportCollector().getShortName(ExprProcessor.buildJavaClassName(classname)));
@ -358,6 +363,50 @@ public class InvocationExprent extends Exprent {
return buf; return buf;
} }
private boolean isBoxingCall() {
if (isStatic && "valueOf".equals(name) && lstParameters.size() == 1) {
int paramType = lstParameters.get(0).getExprType().type;
// special handling for ambiguous types
if (lstParameters.get(0).type == Exprent.EXPRENT_CONST) {
if (paramType == CodeConstants.TYPE_BYTECHAR || paramType == CodeConstants.TYPE_SHORTCHAR) {
if (classname.equals("java/lang/Character")) {
return true;
}
}
}
return classname.equals(getClassNameForPrimitiveType(paramType));
}
return false;
}
// TODO: move to CodeConstants ???
private static String getClassNameForPrimitiveType(int type) {
switch (type) {
case CodeConstants.TYPE_BOOLEAN:
return "java/lang/Boolean";
case CodeConstants.TYPE_BYTE:
case CodeConstants.TYPE_BYTECHAR:
return "java/lang/Byte";
case CodeConstants.TYPE_CHAR:
return "java/lang/Character";
case CodeConstants.TYPE_SHORT:
case CodeConstants.TYPE_SHORTCHAR:
return "java/lang/Short";
case CodeConstants.TYPE_INT:
return "java/lang/Integer";
case CodeConstants.TYPE_LONG:
return "java/lang/Long";
case CodeConstants.TYPE_FLOAT:
return "java/lang/Float";
case CodeConstants.TYPE_DOUBLE:
return "java/lang/Double";
}
return null;
}
private BitSet getAmbiguousParameters() { private BitSet getAmbiguousParameters() {
StructClass cl = DecompilerContext.getStructContext().getClass(classname); StructClass cl = DecompilerContext.getStructContext().getClass(classname);
if (cl == null) return EMPTY_BIT_SET; if (cl == null) return EMPTY_BIT_SET;

@ -10,39 +10,39 @@ public class TestPrimitives {
this.printFloat(1.23F);// 11 this.printFloat(1.23F);// 11
this.printDouble(1.23D);// 12 this.printDouble(1.23D);// 12
this.printChar('Z');// 13 this.printChar('Z');// 13
System.out.println(String.format("%b, %d, %d, %d", new Object[]{Boolean.valueOf(this.getBoolean()), Byte.valueOf(this.getByte()), Short.valueOf(this.getShort()), Integer.valueOf(this.getInt())}));// 15 System.out.println(String.format("%b, %d, %d, %d", new Object[]{this.getBoolean(), this.getByte(), this.getShort(), this.getInt()}));// 15
}// 16 }// 16
public void printBoolean(boolean b) { public void printBoolean(boolean b) {
System.out.println(String.format("%b", new Object[]{Boolean.valueOf(b)}));// 19 System.out.println(String.format("%b", new Object[]{b}));// 19
}// 20 }// 20
public void printByte(byte b) { public void printByte(byte b) {
System.out.println(String.format("%d", new Object[]{Byte.valueOf(b)}));// 23 System.out.println(String.format("%d", new Object[]{b}));// 23
}// 24 }// 24
public void printShort(short s) { public void printShort(short s) {
System.out.println(String.format("%d", new Object[]{Short.valueOf(s)}));// 27 System.out.println(String.format("%d", new Object[]{s}));// 27
}// 28 }// 28
public void printInt(int i) { public void printInt(int i) {
System.out.println(String.format("%d", new Object[]{Integer.valueOf(i)}));// 31 System.out.println(String.format("%d", new Object[]{i}));// 31
}// 32 }// 32
public void printLong(long l) { public void printLong(long l) {
System.out.println(String.format("%d", new Object[]{Long.valueOf(l)}));// 35 System.out.println(String.format("%d", new Object[]{l}));// 35
}// 36 }// 36
public void printFloat(float f) { public void printFloat(float f) {
System.out.println(String.format("%f", new Object[]{Float.valueOf(f)}));// 39 System.out.println(String.format("%f", new Object[]{f}));// 39
}// 40 }// 40
public void printDouble(double d) { public void printDouble(double d) {
System.out.println(String.format("%f", new Object[]{Double.valueOf(d)}));// 43 System.out.println(String.format("%f", new Object[]{d}));// 43
}// 44 }// 44
public void printChar(char c) { public void printChar(char c) {
System.out.println(String.format("%c", new Object[]{Character.valueOf(c)}));// 47 System.out.println(String.format("%c", new Object[]{c}));// 47
}// 48 }// 48
public boolean getBoolean() { public boolean getBoolean() {

Loading…
Cancel
Save