From 663631f0456fcc245dd835889f86541d75161c53 Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Thu, 28 Aug 2014 20:52:43 +0400 Subject: java-decompiler: post-import cleanup (classes moved) --- .../decompiler/struct/consts/ConstantPool.java | 316 +++++++++++++++++++++ .../decompiler/struct/consts/LinkConstant.java | 159 +++++++++++ .../decompiler/struct/consts/PooledConstant.java | 116 ++++++++ .../struct/consts/PrimitiveConstant.java | 126 ++++++++ .../decompiler/struct/consts/VariableTypeEnum.java | 47 +++ 5 files changed, 764 insertions(+) create mode 100644 src/org/jetbrains/java/decompiler/struct/consts/ConstantPool.java create mode 100644 src/org/jetbrains/java/decompiler/struct/consts/LinkConstant.java create mode 100644 src/org/jetbrains/java/decompiler/struct/consts/PooledConstant.java create mode 100644 src/org/jetbrains/java/decompiler/struct/consts/PrimitiveConstant.java create mode 100644 src/org/jetbrains/java/decompiler/struct/consts/VariableTypeEnum.java (limited to 'src/org/jetbrains/java/decompiler/struct/consts') diff --git a/src/org/jetbrains/java/decompiler/struct/consts/ConstantPool.java b/src/org/jetbrains/java/decompiler/struct/consts/ConstantPool.java new file mode 100644 index 0000000..8951cd1 --- /dev/null +++ b/src/org/jetbrains/java/decompiler/struct/consts/ConstantPool.java @@ -0,0 +1,316 @@ +/* + * Fernflower - The Analytical Java Decompiler + * http://www.reversed-java.com + * + * (C) 2008 - 2010, Stiver + * + * This software is NEITHER public domain NOR free software + * as per GNU License. See license.txt for more details. + * + * This software is distributed WITHOUT ANY WARRANTY; without + * even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. + */ + +package org.jetbrains.java.decompiler.struct.consts; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.jetbrains.java.decompiler.code.CodeConstants; +import org.jetbrains.java.decompiler.main.DecompilerContext; +import org.jetbrains.java.decompiler.modules.renamer.PoolInterceptor; +import org.jetbrains.java.decompiler.struct.gen.FieldDescriptor; +import org.jetbrains.java.decompiler.struct.gen.MethodDescriptor; +import org.jetbrains.java.decompiler.struct.gen.VarType; + +public class ConstantPool { + + public static final int FIELD = 1; + + public static final int METHOD = 2; + + // ***************************************************************************** + // private fields + // ***************************************************************************** + + private List pool = new ArrayList(); + + private PoolInterceptor interceptor; + + // ***************************************************************************** + // constructors + // ***************************************************************************** + + public ConstantPool(DataInputStream in) throws IOException { + + int size = in.readUnsignedShort(); + + int[] pass = new int[size]; + + // first dummy constant + pool.add(null); + + // first pass: read the elements + for (int i = 1; i < size; i++) { + + byte tag = (byte)in.readUnsignedByte(); + + switch (tag) { + case CodeConstants.CONSTANT_Utf8: + pool.add(new PrimitiveConstant(CodeConstants.CONSTANT_Utf8, in.readUTF())); + break; + case CodeConstants.CONSTANT_Integer: + pool.add(new PrimitiveConstant(CodeConstants.CONSTANT_Integer, new Integer(in.readInt()))); + break; + case CodeConstants.CONSTANT_Float: + pool.add(new PrimitiveConstant(CodeConstants.CONSTANT_Float, new Float(in.readFloat()))); + break; + case CodeConstants.CONSTANT_Long: + pool.add(new PrimitiveConstant(CodeConstants.CONSTANT_Long, new Long(in.readLong()))); + pool.add(null); + i++; + break; + case CodeConstants.CONSTANT_Double: + pool.add(new PrimitiveConstant(CodeConstants.CONSTANT_Double, new Double(in.readDouble()))); + pool.add(null); + i++; + break; + case CodeConstants.CONSTANT_Class: + case CodeConstants.CONSTANT_String: + case CodeConstants.CONSTANT_MethodType: + pool.add(new PrimitiveConstant(tag, in.readUnsignedShort())); + pass[i] = 1; + break; + case CodeConstants.CONSTANT_Fieldref: + case CodeConstants.CONSTANT_Methodref: + case CodeConstants.CONSTANT_InterfaceMethodref: + case CodeConstants.CONSTANT_NameAndType: + case CodeConstants.CONSTANT_InvokeDynamic: + pool.add(new LinkConstant(tag, in.readUnsignedShort(), in.readUnsignedShort())); + if(tag == CodeConstants.CONSTANT_NameAndType) { + pass[i] = 1; + } else { + pass[i] = 2; + } + break; + case CodeConstants.CONSTANT_MethodHandle: + pool.add(new LinkConstant(tag, in.readUnsignedByte(), in.readUnsignedShort())); + pass[i] = 3; + break; + } + } + + + // resolving complex pool elements + for(int pass_index = 1; pass_index <= 3; pass_index++) { + for(int i = 1; i < size; i++) { + if(pass[i] == pass_index) { + pool.get(i).resolveConstant(this); + } + } + } + + // get global constant pool interceptor instance, if any available + interceptor = DecompilerContext.getPoolInterceptor(); + } + + // ***************************************************************************** + // public methods + // ***************************************************************************** + + public void writeToOutputStream(DataOutputStream out) throws FileNotFoundException, IOException { + + out.writeShort(pool.size()); + for(int i=1;i 0) { + for(int i=0;i ldc class + return REFERENCE; + default: + throw new RuntimeException("Huh?? What are you trying to load?"); + } + } + + public Object getValue(int index){ + return values[index]; + } + + + // ***************************************************************************** + // getter and setter methods + // ***************************************************************************** + + public Object[] getValues() { + return values; + } + + public void setValues(Object[] values) { + this.values = values; + } + +} diff --git a/src/org/jetbrains/java/decompiler/struct/consts/PrimitiveConstant.java b/src/org/jetbrains/java/decompiler/struct/consts/PrimitiveConstant.java new file mode 100644 index 0000000..f01a695 --- /dev/null +++ b/src/org/jetbrains/java/decompiler/struct/consts/PrimitiveConstant.java @@ -0,0 +1,126 @@ +/* + * Fernflower - The Analytical Java Decompiler + * http://www.reversed-java.com + * + * (C) 2008 - 2010, Stiver + * + * This software is NEITHER public domain NOR free software + * as per GNU License. See license.txt for more details. + * + * This software is distributed WITHOUT ANY WARRANTY; without + * even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. + */ + +package org.jetbrains.java.decompiler.struct.consts; + +import java.io.DataOutputStream; +import java.io.IOException; + +/* + * Integer, Long, Float, Double, String, Class, UTF8 + */ + +public class PrimitiveConstant extends PooledConstant { + + // ***************************************************************************** + // public fields + // ***************************************************************************** + + public int index; + + public Object value; + + public boolean isArray; + + // ***************************************************************************** + // constructors + // ***************************************************************************** + + public PrimitiveConstant(int type, Object value) { + this.type = type; + this.value = value; + + initConstant(); + } + + public PrimitiveConstant(int type, int index) { + this.type = type; + this.index = index; + } + + // ***************************************************************************** + // public methods + // ***************************************************************************** + + public int getInt() { + return ((Integer)value).intValue(); + } + + public long getLong() { + return ((Long)value).longValue(); + } + + public float getFloat() { + return ((Float)value).floatValue(); + } + + public double getDouble() { + return ((Double)value).doubleValue(); + } + + public String getString() { + return (String)value; + } + + public void resolveConstant(ConstantPool pool) { + + if(type == CONSTANT_Class || type == CONSTANT_String || type == CONSTANT_MethodType) { + value = pool.getPrimitiveConstant(index).getString(); + initConstant(); + } + } + + public void writeToStream(DataOutputStream out) throws IOException { + + out.writeByte(type); + switch(type) { + case CONSTANT_Integer: + out.writeInt(getInt()); + break; + case CONSTANT_Float: + out.writeFloat(getFloat()); + break; + case CONSTANT_Long: + out.writeLong(getLong()); + break; + case CONSTANT_Double: + out.writeDouble(getDouble()); + break; + case CONSTANT_Utf8: + out.writeUTF(getString()); + break; + default: // CONSTANT_Class, CONSTANT_String, CONSTANT_MethodType + out.writeShort(index); + } + } + + public boolean equals(Object o) { + if(o == this) return true; + if(o == null || !(o instanceof PrimitiveConstant)) return false; + + PrimitiveConstant cn = (PrimitiveConstant)o; + return this.type == cn.type && + this.isArray == cn.isArray && + this.value.equals(cn.value); + + } + + private void initConstant() { + if(type == CONSTANT_Class) { + String className = getString(); + isArray = (className.length() > 0 && className.charAt(0)=='['); // empty string for a class name seems to be possible in some android files + } + } + +} diff --git a/src/org/jetbrains/java/decompiler/struct/consts/VariableTypeEnum.java b/src/org/jetbrains/java/decompiler/struct/consts/VariableTypeEnum.java new file mode 100644 index 0000000..fc58c7e --- /dev/null +++ b/src/org/jetbrains/java/decompiler/struct/consts/VariableTypeEnum.java @@ -0,0 +1,47 @@ +/* + * Fernflower - The Analytical Java Decompiler + * http://www.reversed-java.com + * + * (C) 2008 - 2010, Stiver + * + * This software is NEITHER public domain NOR free software + * as per GNU License. See license.txt for more details. + * + * This software is distributed WITHOUT ANY WARRANTY; without + * even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. + */ + +package org.jetbrains.java.decompiler.struct.consts; + +public interface VariableTypeEnum { + + public final static int BOOLEAN = 1; + public final static int BYTE = 2; + public final static int CHAR = 3; + public final static int SHORT = 4; + public final static int INT = 5; + public final static int FLOAT = 6; + public final static int LONG = 7; + public final static int DOUBLE = 8; + public final static int RETURN_ADDRESS = 9; + public final static int REFERENCE = 10; + public final static int INSTANCE_UNINITIALIZED = 11; + public final static int VALUE_UNKNOWN = 12; + public final static int VOID = 13; + + public final static Integer BOOLEAN_OBJ = new Integer(BOOLEAN); + public final static Integer BYTE_OBJ = new Integer(BYTE); + public final static Integer CHAR_OBJ = new Integer(CHAR); + public final static Integer SHORT_OBJ = new Integer(SHORT); + public final static Integer INT_OBJ = new Integer(INT); + public final static Integer FLOAT_OBJ = new Integer(FLOAT); + public final static Integer LONG_OBJ = new Integer(LONG); + public final static Integer DOUBLE_OBJ = new Integer(DOUBLE); + public final static Integer RETURN_ADDRESS_OBJ = new Integer(RETURN_ADDRESS); + public final static Integer REFERENCE_OBJ = new Integer(REFERENCE); + public final static Integer INSTANCE_UNINITIALIZED_OBJ = new Integer(INSTANCE_UNINITIALIZED); + public final static Integer VALUE_UNKNOWN_OBJ = new Integer(VALUE_UNKNOWN); + public final static Integer VOID_OBJ = new Integer(VOID); + +} -- cgit v1.2.3