From 2df49d32a71818227e80a8628688906f2ede8a6a Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Tue, 2 Sep 2014 20:56:03 +0400 Subject: java-decompiler: fixes and cleanups - unified attribute loading code - common methods for checking member flags - verifying skip() - correct resource closing - typos --- .../java/decompiler/struct/StructClass.java | 298 +++++---------------- 1 file changed, 74 insertions(+), 224 deletions(-) (limited to 'src/org/jetbrains/java/decompiler/struct/StructClass.java') diff --git a/src/org/jetbrains/java/decompiler/struct/StructClass.java b/src/org/jetbrains/java/decompiler/struct/StructClass.java index 78e941d..9afe081 100644 --- a/src/org/jetbrains/java/decompiler/struct/StructClass.java +++ b/src/org/jetbrains/java/decompiler/struct/StructClass.java @@ -16,7 +16,6 @@ package org.jetbrains.java.decompiler.struct; import org.jetbrains.java.decompiler.code.CodeConstants; -import org.jetbrains.java.decompiler.struct.attr.StructGeneralAttribute; import org.jetbrains.java.decompiler.struct.consts.ConstantPool; import org.jetbrains.java.decompiler.struct.consts.PrimitiveConstant; import org.jetbrains.java.decompiler.struct.lazy.LazyLoader; @@ -24,272 +23,127 @@ import org.jetbrains.java.decompiler.util.DataInputFullStream; import org.jetbrains.java.decompiler.util.InterpreterUtil; import org.jetbrains.java.decompiler.util.VBStyleCollection; -import java.io.*; +import java.io.IOException; +import java.io.InputStream; /* - ClassFile { - u4 magic; - u2 minor_version; - u2 major_version; - u2 constant_pool_count; - cp_info constant_pool[constant_pool_count-1]; - u2 access_flags; - u2 this_class; - u2 super_class; - u2 interfaces_count; - u2 interfaces[interfaces_count]; - u2 fields_count; - field_info fields[fields_count]; - u2 methods_count; - method_info methods[methods_count]; - u2 attributes_count; - attribute_info attributes[attributes_count]; - } + class_file { + u4 magic; + u2 minor_version; + u2 major_version; + u2 constant_pool_count; + cp_info constant_pool[constant_pool_count-1]; + u2 access_flags; + u2 this_class; + u2 super_class; + u2 interfaces_count; + u2 interfaces[interfaces_count]; + u2 fields_count; + field_info fields[fields_count]; + u2 methods_count; + method_info methods[methods_count]; + u2 attributes_count; + attribute_info attributes[attributes_count]; + } */ +public class StructClass extends StructMember { -public class StructClass { - - // ***************************************************************************** - // public fields - // ***************************************************************************** - - public int minor_version; - - public int major_version; - - public int access_flags; - - public int this_class; + public final String qualifiedName; + public final PrimitiveConstant superClass; - public int super_class; - - public PrimitiveConstant thisClass; - - public PrimitiveConstant superClass; - - public String qualifiedName; - - - // ***************************************************************************** - // private fields - // ***************************************************************************** + private final boolean own; + private final LazyLoader loader; + private final int minorVersion; + private final int majorVersion; + private final int[] interfaces; + private final String[] interfaceNames; + private final VBStyleCollection fields; + private final VBStyleCollection methods; private ConstantPool pool; - private int[] interfaces; - - private String[] interfaceNames; - - private VBStyleCollection fields = new VBStyleCollection(); - - private VBStyleCollection methods = new VBStyleCollection(); - - private VBStyleCollection attributes = new VBStyleCollection(); - - private boolean own = true; - - private LazyLoader loader; - - // ***************************************************************************** - // constructors - // ***************************************************************************** - - public StructClass(String filename, boolean own, LazyLoader loader) throws IOException { - this(new FileInputStream(filename), own, loader); - } - public StructClass(InputStream inStream, boolean own, LazyLoader loader) throws IOException { this(new DataInputFullStream(inStream), own, loader); } - public StructClass(DataInputFullStream inStream, boolean own, LazyLoader loader) throws IOException { + public StructClass(DataInputFullStream in, boolean own, LazyLoader loader) throws IOException { this.own = own; this.loader = loader; - initStruct(inStream); - } - - // ***************************************************************************** - // public methods - // ***************************************************************************** - - public boolean hasField(String name, String descriptor) { - return getField(name, descriptor) != null; - } - - public StructField getField(String name, String descriptor) { - return fields.getWithKey(InterpreterUtil.makeUniqueKey(name, descriptor)); - } - - public StructMethod getMethod(String key) { - return methods.getWithKey(key); - } - - public StructMethod getMethod(String name, String descriptor) { - return methods.getWithKey(InterpreterUtil.makeUniqueKey(name, descriptor)); - } - - public void writeToFile(File file) throws IOException { - DataOutputStream out = new DataOutputStream(new FileOutputStream(file)); - writeToOutputStream(out); - out.close(); - } - - public void writeToOutputStream(DataOutputStream out) throws IOException { - - out.writeInt(0xCAFEBABE); - out.writeShort(minor_version); - out.writeShort(major_version); - - getPool().writeToOutputStream(out); - - out.writeShort(access_flags); - out.writeShort(this_class); - out.writeShort(super_class); - - out.writeShort(interfaces.length); - for (int i = 0; i < interfaces.length; i++) { - out.writeShort(interfaces[i]); - } - - out.writeShort(fields.size()); - for (int i = 0; i < fields.size(); i++) { - fields.get(i).writeToStream(out); - } - - out.writeShort(methods.size()); - for (int i = 0; i < methods.size(); i++) { - methods.get(i).writeToStream(out); - } - - out.writeShort(attributes.size()); - for (StructGeneralAttribute attr : attributes) { - attr.writeToStream(out); - } - } - - public String getInterface(int i) { - return interfaceNames[i]; - } - - public void releaseResources() { - if (loader != null) { - pool = null; - } - } - - // ***************************************************************************** - // private methods - // ***************************************************************************** - - private void initStruct(DataInputFullStream in) throws IOException { + in.discard(4); - in.skip(4); - - this.minor_version = in.readUnsignedShort(); - this.major_version = in.readUnsignedShort(); + minorVersion = in.readUnsignedShort(); + majorVersion = in.readUnsignedShort(); pool = new ConstantPool(in); - this.access_flags = in.readUnsignedShort(); - - this_class = in.readUnsignedShort(); - thisClass = pool.getPrimitiveConstant(this_class); - qualifiedName = thisClass.getString(); - - super_class = in.readUnsignedShort(); - superClass = pool.getPrimitiveConstant(super_class); + accessFlags = in.readUnsignedShort(); + int thisClassIdx = in.readUnsignedShort(); + int superClassIdx = in.readUnsignedShort(); + qualifiedName = pool.getPrimitiveConstant(thisClassIdx).getString(); + superClass = pool.getPrimitiveConstant(superClassIdx); // interfaces int length = in.readUnsignedShort(); - int[] arrInterfaces = new int[length]; - String[] arrInterfaceNames = new String[length]; - + interfaces = new int[length]; + interfaceNames = new String[length]; for (int i = 0; i < length; i++) { - arrInterfaces[i] = in.readUnsignedShort(); - arrInterfaceNames[i] = pool.getPrimitiveConstant(arrInterfaces[i]).getString(); + interfaces[i] = in.readUnsignedShort(); + interfaceNames[i] = pool.getPrimitiveConstant(interfaces[i]).getString(); } - this.interfaces = arrInterfaces; - this.interfaceNames = arrInterfaceNames; // fields - VBStyleCollection lstFields = new VBStyleCollection(); length = in.readUnsignedShort(); + fields = new VBStyleCollection(); for (int i = 0; i < length; i++) { - StructField field = new StructField(); - field.access_flags = in.readUnsignedShort(); - field.name_index = in.readUnsignedShort(); - field.descriptor_index = in.readUnsignedShort(); - - field.initStrings(pool, this_class); - - field.setAttributes(readAttributes(in)); - - lstFields.addWithKey(field, InterpreterUtil.makeUniqueKey(field.getName(), field.getDescriptor())); + StructField field = new StructField(in, this); + fields.addWithKey(field, InterpreterUtil.makeUniqueKey(field.getName(), field.getDescriptor())); } - this.fields = lstFields; // methods length = in.readUnsignedShort(); + methods = new VBStyleCollection(); for (int i = 0; i < length; i++) { - StructMethod meth = new StructMethod(in, own, this); - - //if(qualifiedName.endsWith("JUnitStatusLine") && !meth.getName().equals("onProcessStarted") && !meth.getName().startsWith("access")) { - //if(!meth.getName().equals("run")) { - // continue; - //} - - methods.addWithKey(meth, InterpreterUtil.makeUniqueKey(meth.getName(), meth.getDescriptor())); + StructMethod method = new StructMethod(in, this); + methods.addWithKey(method, InterpreterUtil.makeUniqueKey(method.getName(), method.getDescriptor())); } // attributes - this.attributes = readAttributes(in); - + attributes = readAttributes(in, pool); - // release memory - if (loader != null) { - pool = null; - } + releaseResources(); } - private VBStyleCollection readAttributes(DataInputFullStream in) throws IOException { + public boolean hasField(String name, String descriptor) { + return getField(name, descriptor) != null; + } - VBStyleCollection lstAttribute = new VBStyleCollection(); + public StructField getField(String name, String descriptor) { + return fields.getWithKey(InterpreterUtil.makeUniqueKey(name, descriptor)); + } - int length = in.readUnsignedShort(); - for (int i = 0; i < length; i++) { - int attr_nameindex = in.readUnsignedShort(); - String attrname = pool.getPrimitiveConstant(attr_nameindex).getString(); - - StructGeneralAttribute attr = StructGeneralAttribute.getMatchingAttributeInstance(attr_nameindex, attrname); - - if (attr != null) { - byte[] arr = new byte[in.readInt()]; - in.readFull(arr); - attr.setInfo(arr); - - attr.initContent(pool); - lstAttribute.addWithKey(attr, attr.getName()); - } - else { - in.skip(in.readInt()); - } - } + public StructMethod getMethod(String key) { + return methods.getWithKey(key); + } - return lstAttribute; + public StructMethod getMethod(String name, String descriptor) { + return methods.getWithKey(InterpreterUtil.makeUniqueKey(name, descriptor)); } + public String getInterface(int i) { + return interfaceNames[i]; + } - // ***************************************************************************** - // getter and setter methods - // ***************************************************************************** + public void releaseResources() { + if (loader != null) { + pool = null; + } + } public ConstantPool getPool() { - if (pool == null && loader != null) { pool = loader.loadPool(qualifiedName); } - return pool; } @@ -309,10 +163,6 @@ public class StructClass { return fields; } - public VBStyleCollection getAttributes() { - return attributes; - } - public boolean isOwn() { return own; } @@ -322,15 +172,15 @@ public class StructClass { } public boolean isVersionGE_1_5() { - return (major_version > 48 || (major_version == 48 && minor_version > 0)); // FIXME: check second condition + return (majorVersion > 48 || (majorVersion == 48 && minorVersion > 0)); // FIXME: check second condition } public boolean isVersionGE_1_7() { - return (major_version >= 51); + return (majorVersion >= 51); } public int getBytecodeVersion() { - switch (major_version) { + switch (majorVersion) { case 52: return CodeConstants.BYTECODE_JAVA_8; case 51: -- cgit v1.2.3