[java decompiler] "new" expression writer optimization

master
Roman Shevchenko 9 years ago
parent a725e5b7b6
commit 811494ccb2
  1. 119
      src/org/jetbrains/java/decompiler/modules/decompiler/exps/NewExprent.java

@ -159,7 +159,32 @@ public class NewExprent extends Exprent {
if (anonymous) { if (anonymous) {
ClassNode child = DecompilerContext.getClassProcessor().getMapRootClasses().get(newType.value); ClassNode child = DecompilerContext.getClassProcessor().getMapRootClasses().get(newType.value);
buf.append("("); if (!enumConst) {
String enclosing = null;
if (!lambda && constructor != null) {
enclosing = getQualifiedNewInstance(child.anonymousClassType.value, constructor.getLstParameters(), indent, tracer);
if (enclosing != null) {
buf.append(enclosing).append('.');
}
}
buf.append("new ");
String typename = ExprProcessor.getCastTypeName(child.anonymousClassType);
if (enclosing != null) {
ClassNode anonymousNode = DecompilerContext.getClassProcessor().getMapRootClasses().get(child.anonymousClassType.value);
if (anonymousNode != null) {
typename = anonymousNode.simpleName;
}
else {
typename = typename.substring(typename.lastIndexOf('.') + 1);
}
}
buf.append(typename);
}
buf.append('(');
if (!lambda && constructor != null) { if (!lambda && constructor != null) {
InvocationExprent invSuper = child.superInvocation; InvocationExprent invSuper = child.superInvocation;
@ -207,32 +232,7 @@ public class NewExprent extends Exprent {
} }
} }
if (!enumConst) { buf.append(')');
String enclosing = null;
if (!lambda && constructor != null) {
enclosing = getQualifiedNewInstance(child.anonymousClassType.value, constructor.getLstParameters(), indent, tracer);
}
String typename = ExprProcessor.getCastTypeName(child.anonymousClassType);
if (enclosing != null) {
ClassNode anonymousNode = DecompilerContext.getClassProcessor().getMapRootClasses().get(child.anonymousClassType.value);
if (anonymousNode != null) {
typename = anonymousNode.simpleName;
}
else {
typename = typename.substring(typename.lastIndexOf('.') + 1);
}
}
buf.prepend("new " + typename);
if (enclosing != null) {
buf.prepend(enclosing + ".");
}
}
buf.append(")");
if (enumConst && buf.length() == 2) { if (enumConst && buf.length() == 2) {
buf.setLength(0); buf.setLength(0);
@ -257,16 +257,41 @@ public class NewExprent extends Exprent {
} }
else if (directArrayInit) { else if (directArrayInit) {
VarType leftType = newType.decreaseArrayDim(); VarType leftType = newType.decreaseArrayDim();
buf.append("{"); buf.append('{');
for (int i = 0; i < lstArrayElements.size(); i++) { for (int i = 0; i < lstArrayElements.size(); i++) {
if (i > 0) { if (i > 0) {
buf.append(", "); buf.append(", ");
} }
ExprProcessor.getCastedExprent(lstArrayElements.get(i), leftType, buf, indent, false, tracer); ExprProcessor.getCastedExprent(lstArrayElements.get(i), leftType, buf, indent, false, tracer);
} }
buf.append("}"); buf.append('}');
} }
else if (newType.arrayDim == 0) { else if (newType.arrayDim == 0) {
if (!enumConst) {
String enclosing = null;
if (constructor != null) {
enclosing = getQualifiedNewInstance(newType.value, constructor.getLstParameters(), indent, tracer);
if (enclosing != null) {
buf.append(enclosing).append('.');
}
}
buf.append("new ");
String typename = ExprProcessor.getTypeName(newType);
if (enclosing != null) {
ClassNode newNode = DecompilerContext.getClassProcessor().getMapRootClasses().get(newType.value);
if (newNode != null) {
typename = newNode.simpleName;
}
else {
typename = typename.substring(typename.lastIndexOf('.') + 1);
}
}
buf.append(typename);
}
if (constructor != null) { if (constructor != null) {
List<Exprent> lstParameters = constructor.getLstParameters(); List<Exprent> lstParameters = constructor.getLstParameters();
@ -286,7 +311,7 @@ public class NewExprent extends Exprent {
int start = enumConst ? 2 : 0; int start = enumConst ? 2 : 0;
if (!enumConst || start < lstParameters.size()) { if (!enumConst || start < lstParameters.size()) {
buf.append("("); buf.append('(');
boolean firstParam = true; boolean firstParam = true;
for (int i = start; i < lstParameters.size(); i++) { for (int i = start; i < lstParameters.size(); i++) {
@ -311,31 +336,7 @@ public class NewExprent extends Exprent {
} }
} }
buf.append(")"); buf.append(')');
}
}
if (!enumConst) {
String enclosing = null;
if (constructor != null) {
enclosing = getQualifiedNewInstance(newType.value, constructor.getLstParameters(), indent, tracer);
}
String typename = ExprProcessor.getTypeName(newType);
if (enclosing != null) {
ClassNode newNode = DecompilerContext.getClassProcessor().getMapRootClasses().get(newType.value);
if (newNode != null) {
typename = newNode.simpleName;
}
else {
typename = typename.substring(typename.lastIndexOf('.') + 1);
}
}
buf.prepend("new " + typename);
if (enclosing != null) {
buf.prepend(enclosing + ".");
} }
} }
} }
@ -344,11 +345,11 @@ public class NewExprent extends Exprent {
if (lstArrayElements.isEmpty()) { if (lstArrayElements.isEmpty()) {
for (int i = 0; i < newType.arrayDim; i++) { for (int i = 0; i < newType.arrayDim; i++) {
buf.append("["); buf.append('[');
if (i < lstDims.size()) { if (i < lstDims.size()) {
buf.append(lstDims.get(i).toJava(indent, tracer)); buf.append(lstDims.get(i).toJava(indent, tracer));
} }
buf.append("]"); buf.append(']');
} }
} }
else { else {
@ -357,14 +358,14 @@ public class NewExprent extends Exprent {
} }
VarType leftType = newType.decreaseArrayDim(); VarType leftType = newType.decreaseArrayDim();
buf.append("{"); buf.append('{');
for (int i = 0; i < lstArrayElements.size(); i++) { for (int i = 0; i < lstArrayElements.size(); i++) {
if (i > 0) { if (i > 0) {
buf.append(", "); buf.append(", ");
} }
ExprProcessor.getCastedExprent(lstArrayElements.get(i), leftType, buf, indent, false, tracer); ExprProcessor.getCastedExprent(lstArrayElements.get(i), leftType, buf, indent, false, tracer);
} }
buf.append("}"); buf.append('}');
} }
} }

Loading…
Cancel
Save