java-decompiler: hide synthetic field assignment
This commit is contained in:
@@ -15,97 +15,58 @@
|
|||||||
*/
|
*/
|
||||||
package org.jetbrains.java.decompiler.main;
|
package org.jetbrains.java.decompiler.main;
|
||||||
|
|
||||||
import org.jetbrains.java.decompiler.code.CodeConstants;
|
|
||||||
import org.jetbrains.java.decompiler.main.ClassesProcessor.ClassNode;
|
|
||||||
import org.jetbrains.java.decompiler.main.rels.ClassWrapper;
|
import org.jetbrains.java.decompiler.main.rels.ClassWrapper;
|
||||||
import org.jetbrains.java.decompiler.main.rels.MethodWrapper;
|
import org.jetbrains.java.decompiler.main.rels.MethodWrapper;
|
||||||
import org.jetbrains.java.decompiler.modules.decompiler.exps.Exprent;
|
import org.jetbrains.java.decompiler.modules.decompiler.exps.Exprent;
|
||||||
import org.jetbrains.java.decompiler.modules.decompiler.exps.InvocationExprent;
|
import org.jetbrains.java.decompiler.modules.decompiler.exps.InvocationExprent;
|
||||||
import org.jetbrains.java.decompiler.modules.decompiler.exps.NewExprent;
|
|
||||||
import org.jetbrains.java.decompiler.modules.decompiler.exps.VarExprent;
|
import org.jetbrains.java.decompiler.modules.decompiler.exps.VarExprent;
|
||||||
import org.jetbrains.java.decompiler.modules.decompiler.stats.Statement;
|
import org.jetbrains.java.decompiler.modules.decompiler.stats.Statement;
|
||||||
import org.jetbrains.java.decompiler.modules.decompiler.vars.VarVersionPaar;
|
import org.jetbrains.java.decompiler.modules.decompiler.vars.VarVersionPaar;
|
||||||
import org.jetbrains.java.decompiler.struct.StructClass;
|
import org.jetbrains.java.decompiler.struct.StructClass;
|
||||||
import org.jetbrains.java.decompiler.struct.StructField;
|
import org.jetbrains.java.decompiler.struct.StructField;
|
||||||
import org.jetbrains.java.decompiler.struct.StructMethod;
|
import org.jetbrains.java.decompiler.struct.StructMethod;
|
||||||
import org.jetbrains.java.decompiler.struct.gen.FieldDescriptor;
|
|
||||||
import org.jetbrains.java.decompiler.struct.gen.VarType;
|
|
||||||
import org.jetbrains.java.decompiler.util.InterpreterUtil;
|
import org.jetbrains.java.decompiler.util.InterpreterUtil;
|
||||||
|
|
||||||
public class EnumProcessor {
|
public class EnumProcessor {
|
||||||
|
|
||||||
public static void clearEnum(ClassWrapper wrapper) {
|
public static void clearEnum(ClassWrapper wrapper) {
|
||||||
|
|
||||||
StructClass cl = wrapper.getClassStruct();
|
StructClass cl = wrapper.getClassStruct();
|
||||||
|
|
||||||
// hide values() and valueOf()
|
// hide values/valueOf methods and super() invocations
|
||||||
for (StructMethod meth : cl.getMethods()) {
|
for (MethodWrapper method : wrapper.getMethods()) {
|
||||||
|
StructMethod mt = method.methodStruct;
|
||||||
String name = meth.getName();
|
String name = mt.getName();
|
||||||
int flag = 0;
|
String descriptor = mt.getDescriptor();
|
||||||
|
|
||||||
if ("values".equals(name)) {
|
if ("values".equals(name)) {
|
||||||
flag = 1;
|
if (descriptor.equals("()[L" + cl.qualifiedName + ";")) {
|
||||||
|
wrapper.getHideMembers().add(InterpreterUtil.makeUniqueKey(name, descriptor));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if ("valueOf".equals(name)) {
|
else if ("valueOf".equals(name)) {
|
||||||
flag = 2;
|
if (descriptor.equals("(Ljava/lang/String;)L" + cl.qualifiedName + ";")) {
|
||||||
}
|
wrapper.getHideMembers().add(InterpreterUtil.makeUniqueKey(name, descriptor));
|
||||||
|
|
||||||
if (flag > 0) {
|
|
||||||
String[] arr = meth.getDescriptor().split("[()]");
|
|
||||||
String par = arr[1];
|
|
||||||
|
|
||||||
if ((flag == 1 && par.length() == 0) ||
|
|
||||||
flag == 2 && "Ljava/lang/String;".equals(par)) {
|
|
||||||
wrapper.getHideMembers().add(InterpreterUtil.makeUniqueKey(name, meth.getDescriptor()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
else if ("<init>".equals(name)) {
|
||||||
|
Statement firstData = findFirstData(method.root);
|
||||||
// hide all super invocations
|
if (firstData != null && !firstData.getExprents().isEmpty()) {
|
||||||
for (MethodWrapper meth : wrapper.getMethods()) {
|
Exprent exprent = firstData.getExprents().get(0);
|
||||||
if ("<init>".equals(meth.methodStruct.getName())) {
|
if (exprent.type == Exprent.EXPRENT_INVOCATION) {
|
||||||
Statement firstdata = findFirstData(meth.root);
|
InvocationExprent invexpr = (InvocationExprent)exprent;
|
||||||
if (firstdata == null || firstdata.getExprents().isEmpty()) {
|
if (isInvocationSuperConstructor(invexpr, method, wrapper)) {
|
||||||
return;
|
firstData.getExprents().remove(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
Exprent exprent = firstdata.getExprents().get(0);
|
|
||||||
if (exprent.type == Exprent.EXPRENT_INVOCATION) {
|
|
||||||
InvocationExprent invexpr = (InvocationExprent)exprent;
|
|
||||||
if (isInvocationSuperConstructor(invexpr, meth, wrapper)) {
|
|
||||||
firstdata.getExprents().remove(0);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// hide dummy synthetic fields of enum constants
|
// hide synthetic fields of enum and it's constants
|
||||||
for (StructField fd : cl.getFields()) {
|
for (StructField fd : cl.getFields()) {
|
||||||
if (fd.hasModifier(CodeConstants.ACC_ENUM)) {
|
String descriptor = fd.getDescriptor();
|
||||||
Exprent initializer =
|
if (fd.isSynthetic() && descriptor.equals("[L" + cl.qualifiedName + ";")) {
|
||||||
wrapper.getStaticFieldInitializers().getWithKey(InterpreterUtil.makeUniqueKey(fd.getName(), fd.getDescriptor()));
|
wrapper.getHideMembers().add(InterpreterUtil.makeUniqueKey(fd.getName(), descriptor));
|
||||||
if (initializer != null && initializer.type == Exprent.EXPRENT_NEW) {
|
|
||||||
NewExprent nexpr = (NewExprent)initializer;
|
|
||||||
if (nexpr.isAnonymous()) {
|
|
||||||
ClassNode child = DecompilerContext.getClassProcessor().getMapRootClasses().get(nexpr.getNewtype().value);
|
|
||||||
hideDummyFieldInConstant(child.wrapper);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void hideDummyFieldInConstant(ClassWrapper wrapper) {
|
|
||||||
StructClass cl = wrapper.getClassStruct();
|
|
||||||
for (StructField fd : cl.getFields()) {
|
|
||||||
if (fd.isSynthetic()) {
|
|
||||||
FieldDescriptor descr = FieldDescriptor.parseDescriptor(fd.getDescriptor());
|
|
||||||
VarType ret = descr.type;
|
|
||||||
|
|
||||||
if (ret.type == CodeConstants.TYPE_OBJECT && ret.arraydim == 1 && cl.qualifiedName.equals(ret.value)) {
|
|
||||||
wrapper.getHideMembers().add(InterpreterUtil.makeUniqueKey(fd.getName(), fd.getDescriptor()));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ import org.jetbrains.java.decompiler.main.ClassesProcessor.ClassNode;
|
|||||||
import org.jetbrains.java.decompiler.main.DecompilerContext;
|
import org.jetbrains.java.decompiler.main.DecompilerContext;
|
||||||
import org.jetbrains.java.decompiler.modules.decompiler.ExprProcessor;
|
import org.jetbrains.java.decompiler.modules.decompiler.ExprProcessor;
|
||||||
import org.jetbrains.java.decompiler.modules.decompiler.vars.CheckTypesResult;
|
import org.jetbrains.java.decompiler.modules.decompiler.vars.CheckTypesResult;
|
||||||
import org.jetbrains.java.decompiler.struct.StructClass;
|
|
||||||
import org.jetbrains.java.decompiler.struct.StructField;
|
import org.jetbrains.java.decompiler.struct.StructField;
|
||||||
import org.jetbrains.java.decompiler.struct.gen.VarType;
|
import org.jetbrains.java.decompiler.struct.gen.VarType;
|
||||||
import org.jetbrains.java.decompiler.util.InterpreterUtil;
|
import org.jetbrains.java.decompiler.util.InterpreterUtil;
|
||||||
@@ -105,26 +104,33 @@ public class AssignmentExprent extends Exprent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String toJava(int indent) {
|
public String toJava(int indent) {
|
||||||
|
|
||||||
VarType leftType = left.getExprType();
|
VarType leftType = left.getExprType();
|
||||||
VarType rightType = right.getExprType();
|
VarType rightType = right.getExprType();
|
||||||
|
|
||||||
boolean fieldInStatInit = false;
|
boolean fieldInClassInit = false, hiddenField = false;
|
||||||
if (left.type == Exprent.EXPRENT_FIELD) { // first assignment to a final field. Field name without "this" in front of it
|
if (left.type == Exprent.EXPRENT_FIELD) { // first assignment to a final field. Field name without "this" in front of it
|
||||||
FieldExprent field = (FieldExprent)left;
|
FieldExprent field = (FieldExprent)left;
|
||||||
ClassNode node = ((ClassNode)DecompilerContext.getProperty(DecompilerContext.CURRENT_CLASS_NODE));
|
ClassNode node = ((ClassNode)DecompilerContext.getProperty(DecompilerContext.CURRENT_CLASS_NODE));
|
||||||
if (node != null) {
|
if (node != null) {
|
||||||
StructClass cl = node.classStruct;
|
StructField fd = node.classStruct.getField(field.getName(), field.getDescriptor().descriptorString);
|
||||||
StructField fd = cl.getField(field.getName(), field.getDescriptor().descriptorString);
|
if (fd != null) {
|
||||||
if (fd != null && field.isStatic() && fd.hasModifier(CodeConstants.ACC_FINAL)) {
|
if (field.isStatic() && fd.hasModifier(CodeConstants.ACC_FINAL)) {
|
||||||
fieldInStatInit = true;
|
fieldInClassInit = true;
|
||||||
|
}
|
||||||
|
if (node.wrapper.getHideMembers().contains(InterpreterUtil.makeUniqueKey(fd.getName(), fd.getDescriptor()))) {
|
||||||
|
hiddenField = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (hiddenField) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
StringBuilder buffer = new StringBuilder();
|
StringBuilder buffer = new StringBuilder();
|
||||||
|
|
||||||
if (fieldInStatInit) {
|
if (fieldInClassInit) {
|
||||||
buffer.append(((FieldExprent)left).getName());
|
buffer.append(((FieldExprent)left).getName());
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ public class SingleClassesTest {
|
|||||||
@Test public void testMethodParameters() { doTest("TestMethodParameters"); }
|
@Test public void testMethodParameters() { doTest("TestMethodParameters"); }
|
||||||
@Test public void testCodeConstructs() { doTest("TestCodeConstructs"); }
|
@Test public void testCodeConstructs() { doTest("TestCodeConstructs"); }
|
||||||
@Test public void testConstants() { doTest("TestConstants"); }
|
@Test public void testConstants() { doTest("TestConstants"); }
|
||||||
//@Test public void testEnum() { doTest("TestEnum"); }
|
@Test public void testEnum() { doTest("TestEnum"); }
|
||||||
|
|
||||||
private void doTest(final String testName) {
|
private void doTest(final String testName) {
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user