java-decompiler: optimization (empty lists allocation avoided)

This commit is contained in:
Roman Shevchenko 2014-09-04 18:16:16 +04:00
parent 1cea85e49a
commit 686b5abef9
15 changed files with 345 additions and 343 deletions

View File

@ -61,10 +61,10 @@ public class ClassesProcessor {
StructInnerClassesAttribute inner = (StructInnerClassesAttribute)cl.getAttributes().getWithKey("InnerClasses"); StructInnerClassesAttribute inner = (StructInnerClassesAttribute)cl.getAttributes().getWithKey("InnerClasses");
if (inner != null) { if (inner != null) {
for (int i = 0; i < inner.getClassentries().size(); i++) { for (int i = 0; i < inner.getClassEntries().size(); i++) {
int[] entry = inner.getClassentries().get(i); int[] entry = inner.getClassEntries().get(i);
String[] strentry = inner.getStringentries().get(i); String[] strentry = inner.getStringEntries().get(i);
Object[] arr = new Object[4]; // arr[0] not used Object[] arr = new Object[4]; // arr[0] not used
@ -165,8 +165,8 @@ public class ClassesProcessor {
StructClass scl = supernode.classStruct; StructClass scl = supernode.classStruct;
StructInnerClassesAttribute inner = (StructInnerClassesAttribute)scl.getAttributes().getWithKey("InnerClasses"); StructInnerClassesAttribute inner = (StructInnerClassesAttribute)scl.getAttributes().getWithKey("InnerClasses");
for (int i = 0; i < inner.getStringentries().size(); i++) { for (int i = 0; i < inner.getStringEntries().size(); i++) {
String nestedClass = inner.getStringentries().get(i)[0]; String nestedClass = inner.getStringEntries().get(i)[0];
if (!setNestedClasses.contains(nestedClass)) { if (!setNestedClasses.contains(nestedClass)) {
continue; continue;
} }

View File

@ -201,7 +201,7 @@ public class NestedClassProcessor {
StructEnclosingMethodAttribute attr = StructEnclosingMethodAttribute attr =
(StructEnclosingMethodAttribute)child.classStruct.getAttributes().getWithKey("EnclosingMethod"); (StructEnclosingMethodAttribute)child.classStruct.getAttributes().getWithKey("EnclosingMethod");
if (attr != null && attr.getMethodName() != null) { if (attr != null && attr.getMethodName() != null) {
if (node.classStruct.qualifiedName.equals(attr.getClassname()) && if (node.classStruct.qualifiedName.equals(attr.getClassName()) &&
node.classStruct.getMethod(attr.getMethodName(), attr.getMethodDescriptor()) != null) { node.classStruct.getMethod(attr.getMethodName(), attr.getMethodDescriptor()) != null) {
child.enclosingMethod = InterpreterUtil.makeUniqueKey(attr.getMethodName(), attr.getMethodDescriptor()); child.enclosingMethod = InterpreterUtil.makeUniqueKey(attr.getMethodName(), attr.getMethodDescriptor());
continue; continue;

View File

@ -51,8 +51,7 @@ public class VarProcessor {
defproc.setVarDefinitions(); defproc.setVarDefinitions();
} }
public void setDebugVarNames(HashMap<Integer, String> mapDebugVarNames) { public void setDebugVarNames(Map<Integer, String> mapDebugVarNames) {
if (varvers == null) { if (varvers == null) {
return; return;
} }

View File

@ -18,20 +18,15 @@ package org.jetbrains.java.decompiler.struct.attr;
import org.jetbrains.java.decompiler.modules.decompiler.exps.Exprent; import org.jetbrains.java.decompiler.modules.decompiler.exps.Exprent;
import org.jetbrains.java.decompiler.struct.consts.ConstantPool; import org.jetbrains.java.decompiler.struct.consts.ConstantPool;
import java.io.ByteArrayInputStream; import java.io.IOException;
import java.io.DataInputStream;
public class StructAnnDefaultAttribute extends StructGeneralAttribute { public class StructAnnDefaultAttribute extends StructGeneralAttribute {
private Exprent defaultValue; private Exprent defaultValue;
public void initContent(ConstantPool pool) { @Override
public void initContent(ConstantPool pool) throws IOException {
name = ATTRIBUTE_ANNOTATION_DEFAULT; defaultValue = StructAnnotationAttribute.parseAnnotationElement(stream(), pool);
DataInputStream data = new DataInputStream(new ByteArrayInputStream(info));
defaultValue = StructAnnotationAttribute.parseAnnotationElement(data, pool);
} }
public Exprent getDefaultValue() { public Exprent getDefaultValue() {

View File

@ -22,160 +22,162 @@ import org.jetbrains.java.decompiler.struct.consts.PrimitiveConstant;
import org.jetbrains.java.decompiler.struct.gen.FieldDescriptor; import org.jetbrains.java.decompiler.struct.gen.FieldDescriptor;
import org.jetbrains.java.decompiler.struct.gen.VarType; import org.jetbrains.java.decompiler.struct.gen.VarType;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
public class StructAnnotationAttribute extends StructGeneralAttribute { public class StructAnnotationAttribute extends StructGeneralAttribute {
private List<AnnotationExprent> annotations; private List<AnnotationExprent> annotations;
public void initContent(ConstantPool pool) { @Override
public void initContent(ConstantPool pool) throws IOException {
super.initContent(pool); annotations = parseAnnotations(pool, stream());
annotations = new ArrayList<AnnotationExprent>();
DataInputStream data = new DataInputStream(new ByteArrayInputStream(info, 2, info.length));
int len = (((info[0] & 0xFF) << 8) | (info[1] & 0xFF));
for (int i = 0; i < len; i++) {
annotations.add(parseAnnotation(data, pool));
}
} }
public static AnnotationExprent parseAnnotation(DataInputStream data, ConstantPool pool) { public static List<AnnotationExprent> parseAnnotations(ConstantPool pool, DataInputStream data) throws IOException {
int len = data.readUnsignedShort();
try { if (len > 0) {
List<AnnotationExprent> annotations = new ArrayList<AnnotationExprent>(len);
String classname = pool.getPrimitiveConstant(data.readUnsignedShort()).getString();
VarType cltype = new VarType(classname);
int len = data.readUnsignedShort();
List<String> parnames = new ArrayList<String>();
List<Exprent> parvalues = new ArrayList<Exprent>();
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
parnames.add(pool.getPrimitiveConstant(data.readUnsignedShort()).getString()); annotations.add(parseAnnotation(data, pool));
parvalues.add(parseAnnotationElement(data, pool));
} }
return annotations;
return new AnnotationExprent(cltype.value, parnames, parvalues);
} }
catch (IOException ex) { else {
throw new RuntimeException(ex); return Collections.emptyList();
} }
} }
public static Exprent parseAnnotationElement(DataInputStream data, ConstantPool pool) { public static AnnotationExprent parseAnnotation(DataInputStream data, ConstantPool pool) throws IOException {
String className = pool.getPrimitiveConstant(data.readUnsignedShort()).getString();
try { List<String> names;
int tag = data.readUnsignedByte(); List<Exprent> values;
int len = data.readUnsignedShort();
if (len > 0) {
names = new ArrayList<String>(len);
values = new ArrayList<Exprent>(len);
for (int i = 0; i < len; i++) {
names.add(pool.getPrimitiveConstant(data.readUnsignedShort()).getString());
values.add(parseAnnotationElement(data, pool));
}
}
else {
names = Collections.emptyList();
values = Collections.emptyList();
}
switch (tag) { return new AnnotationExprent(new VarType(className).value, names, values);
case 'e': // enum constant }
String classname = pool.getPrimitiveConstant(data.readUnsignedShort()).getString();
String constname = pool.getPrimitiveConstant(data.readUnsignedShort()).getString();
FieldDescriptor descr = FieldDescriptor.parseDescriptor(classname); public static Exprent parseAnnotationElement(DataInputStream data, ConstantPool pool) throws IOException {
return new FieldExprent(constname, descr.type.value, true, null, descr); int tag = data.readUnsignedByte();
case 'c': // class
String descriptor = pool.getPrimitiveConstant(data.readUnsignedShort()).getString();
VarType type = FieldDescriptor.parseDescriptor(descriptor).type;
String value; switch (tag) {
switch (type.type) { case 'e': // enum constant
case CodeConstants.TYPE_OBJECT: String className = pool.getPrimitiveConstant(data.readUnsignedShort()).getString();
value = type.value; String constName = pool.getPrimitiveConstant(data.readUnsignedShort()).getString();
break; FieldDescriptor descr = FieldDescriptor.parseDescriptor(className);
case CodeConstants.TYPE_BYTE: return new FieldExprent(constName, descr.type.value, true, null, descr);
value = byte.class.getName();
break;
case CodeConstants.TYPE_CHAR:
value = char.class.getName();
break;
case CodeConstants.TYPE_DOUBLE:
value = double.class.getName();
break;
case CodeConstants.TYPE_FLOAT:
value = float.class.getName();
break;
case CodeConstants.TYPE_INT:
value = int.class.getName();
break;
case CodeConstants.TYPE_LONG:
value = long.class.getName();
break;
case CodeConstants.TYPE_SHORT:
value = short.class.getName();
break;
case CodeConstants.TYPE_BOOLEAN:
value = boolean.class.getName();
break;
case CodeConstants.TYPE_VOID:
value = void.class.getName();
break;
default:
throw new RuntimeException("invalid class type: " + type.type);
}
return new ConstExprent(VarType.VARTYPE_CLASS, value);
case '[': // array
int len = data.readUnsignedShort();
List<Exprent> lst = new ArrayList<Exprent>();
case 'c': // class
String descriptor = pool.getPrimitiveConstant(data.readUnsignedShort()).getString();
VarType type = FieldDescriptor.parseDescriptor(descriptor).type;
String value;
switch (type.type) {
case CodeConstants.TYPE_OBJECT:
value = type.value;
break;
case CodeConstants.TYPE_BYTE:
value = byte.class.getName();
break;
case CodeConstants.TYPE_CHAR:
value = char.class.getName();
break;
case CodeConstants.TYPE_DOUBLE:
value = double.class.getName();
break;
case CodeConstants.TYPE_FLOAT:
value = float.class.getName();
break;
case CodeConstants.TYPE_INT:
value = int.class.getName();
break;
case CodeConstants.TYPE_LONG:
value = long.class.getName();
break;
case CodeConstants.TYPE_SHORT:
value = short.class.getName();
break;
case CodeConstants.TYPE_BOOLEAN:
value = boolean.class.getName();
break;
case CodeConstants.TYPE_VOID:
value = void.class.getName();
break;
default:
throw new RuntimeException("invalid class type: " + type.type);
}
return new ConstExprent(VarType.VARTYPE_CLASS, value);
case '[': // array
List<Exprent> elements = Collections.emptyList();
int len = data.readUnsignedShort();
if (len > 0) {
elements = new ArrayList<Exprent>(len);
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
lst.add(parseAnnotationElement(data, pool)); elements.add(parseAnnotationElement(data, pool));
} }
}
VarType newtype; VarType newType;
if (lst.isEmpty()) { if (elements.isEmpty()) {
newtype = new VarType(CodeConstants.TYPE_OBJECT, 1, "java/lang/Object"); newType = new VarType(CodeConstants.TYPE_OBJECT, 1, "java/lang/Object");
} }
else { else {
VarType eltype = lst.get(0).getExprType(); VarType elementType = elements.get(0).getExprType();
newtype = new VarType(eltype.type, 1, eltype.value); newType = new VarType(elementType.type, 1, elementType.value);
} }
NewExprent newexpr = new NewExprent(newtype, new ArrayList<Exprent>()); NewExprent newExpr = new NewExprent(newType, Collections.<Exprent>emptyList());
newexpr.setDirectArrayInit(true); newExpr.setDirectArrayInit(true);
newexpr.setLstArrayElements(lst); newExpr.setLstArrayElements(elements);
return newexpr; return newExpr;
case '@': // annotation
return parseAnnotation(data, pool); case '@': // annotation
default: return parseAnnotation(data, pool);
PrimitiveConstant cn = pool.getPrimitiveConstant(data.readUnsignedShort());
switch (tag) { default:
case 'B': PrimitiveConstant cn = pool.getPrimitiveConstant(data.readUnsignedShort());
return new ConstExprent(VarType.VARTYPE_BYTE, cn.value); switch (tag) {
case 'C': case 'B':
return new ConstExprent(VarType.VARTYPE_CHAR, cn.value); return new ConstExprent(VarType.VARTYPE_BYTE, cn.value);
case 'D': case 'C':
return new ConstExprent(VarType.VARTYPE_DOUBLE, cn.value); return new ConstExprent(VarType.VARTYPE_CHAR, cn.value);
case 'F': case 'D':
return new ConstExprent(VarType.VARTYPE_FLOAT, cn.value); return new ConstExprent(VarType.VARTYPE_DOUBLE, cn.value);
case 'I': case 'F':
return new ConstExprent(VarType.VARTYPE_INT, cn.value); return new ConstExprent(VarType.VARTYPE_FLOAT, cn.value);
case 'J': case 'I':
return new ConstExprent(VarType.VARTYPE_LONG, cn.value); return new ConstExprent(VarType.VARTYPE_INT, cn.value);
case 'S': case 'J':
return new ConstExprent(VarType.VARTYPE_SHORT, cn.value); return new ConstExprent(VarType.VARTYPE_LONG, cn.value);
case 'Z': case 'S':
return new ConstExprent(VarType.VARTYPE_BOOLEAN, cn.value); return new ConstExprent(VarType.VARTYPE_SHORT, cn.value);
case 's': case 'Z':
return new ConstExprent(VarType.VARTYPE_STRING, cn.value); return new ConstExprent(VarType.VARTYPE_BOOLEAN, cn.value);
default: case 's':
throw new RuntimeException("invalid element type!"); return new ConstExprent(VarType.VARTYPE_STRING, cn.value);
} default:
} throw new RuntimeException("invalid element type!");
} }
catch (IOException ex) {
throw new RuntimeException(ex);
} }
} }
public List<AnnotationExprent> getAnnotations() { public List<AnnotationExprent> getAnnotations() {
return annotations; return annotations;
} }

View File

@ -18,37 +18,30 @@ package org.jetbrains.java.decompiler.struct.attr;
import org.jetbrains.java.decompiler.modules.decompiler.exps.AnnotationExprent; import org.jetbrains.java.decompiler.modules.decompiler.exps.AnnotationExprent;
import org.jetbrains.java.decompiler.struct.consts.ConstantPool; import org.jetbrains.java.decompiler.struct.consts.ConstantPool;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
public class StructAnnotationParameterAttribute extends StructGeneralAttribute { public class StructAnnotationParameterAttribute extends StructGeneralAttribute {
private List<List<AnnotationExprent>> paramAnnotations; private List<List<AnnotationExprent>> paramAnnotations;
public void initContent(ConstantPool pool) { @Override
public void initContent(ConstantPool pool) throws IOException {
DataInputStream data = stream();
super.initContent(pool); int len = data.readUnsignedByte();
if (len > 0) {
paramAnnotations = new ArrayList<List<AnnotationExprent>>(); paramAnnotations = new ArrayList<List<AnnotationExprent>>(len);
DataInputStream data = new DataInputStream(new ByteArrayInputStream(info));
try {
int len = data.readUnsignedByte();
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
List<AnnotationExprent> lst = new ArrayList<AnnotationExprent>(); List<AnnotationExprent> annotations = StructAnnotationAttribute.parseAnnotations(pool, data);
int annsize = data.readUnsignedShort(); paramAnnotations.add(annotations);
for (int j = 0; j < annsize; j++) {
lst.add(StructAnnotationAttribute.parseAnnotation(data, pool));
}
paramAnnotations.add(lst);
} }
} }
catch (IOException ex) { else {
throw new RuntimeException(ex); paramAnnotations = Collections.emptyList();
} }
} }

View File

@ -18,81 +18,77 @@ package org.jetbrains.java.decompiler.struct.attr;
import org.jetbrains.java.decompiler.modules.decompiler.exps.AnnotationExprent; import org.jetbrains.java.decompiler.modules.decompiler.exps.AnnotationExprent;
import org.jetbrains.java.decompiler.struct.consts.ConstantPool; import org.jetbrains.java.decompiler.struct.consts.ConstantPool;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
public class StructAnnotationTypeAttribute extends StructGeneralAttribute { public class StructAnnotationTypeAttribute extends StructGeneralAttribute {
public static final int ANNOTATION_TARGET_TYPE_GENERIC_CLASS = 0x00; private static final int ANNOTATION_TARGET_TYPE_GENERIC_CLASS = 0x00;
public static final int ANNOTATION_TARGET_TYPE_GENERIC_METHOD = 0x01; private static final int ANNOTATION_TARGET_TYPE_GENERIC_METHOD = 0x01;
public static final int ANNOTATION_TARGET_TYPE_EXTENDS_IMPLEMENTS = 0x10; private static final int ANNOTATION_TARGET_TYPE_EXTENDS_IMPLEMENTS = 0x10;
public static final int ANNOTATION_TARGET_TYPE_GENERIC_CLASS_BOUND = 0x11; private static final int ANNOTATION_TARGET_TYPE_GENERIC_CLASS_BOUND = 0x11;
public static final int ANNOTATION_TARGET_TYPE_GENERIC_METHOD_BOUND = 0x12; private static final int ANNOTATION_TARGET_TYPE_GENERIC_METHOD_BOUND = 0x12;
public static final int ANNOTATION_TARGET_TYPE_FIELD = 0x13; private static final int ANNOTATION_TARGET_TYPE_FIELD = 0x13;
public static final int ANNOTATION_TARGET_TYPE_RETURN = 0x14; private static final int ANNOTATION_TARGET_TYPE_RETURN = 0x14;
public static final int ANNOTATION_TARGET_TYPE_RECEIVER = 0x15; private static final int ANNOTATION_TARGET_TYPE_RECEIVER = 0x15;
public static final int ANNOTATION_TARGET_TYPE_FORMAL = 0x16; private static final int ANNOTATION_TARGET_TYPE_FORMAL = 0x16;
public static final int ANNOTATION_TARGET_TYPE_THROWS = 0x17; private static final int ANNOTATION_TARGET_TYPE_THROWS = 0x17;
public static final int ANNOTATION_TARGET_TYPE_LOCAL_VARIABLE = 0x40; private static final int ANNOTATION_TARGET_TYPE_LOCAL_VARIABLE = 0x40;
public static final int ANNOTATION_TARGET_TYPE_RESOURCE_VARIABLE = 0x41; private static final int ANNOTATION_TARGET_TYPE_RESOURCE_VARIABLE = 0x41;
public static final int ANNOTATION_TARGET_TYPE_EXCEPTION = 0x42; private static final int ANNOTATION_TARGET_TYPE_EXCEPTION = 0x42;
public static final int ANNOTATION_TARGET_TYPE_INSTANCEOF = 0x43; private static final int ANNOTATION_TARGET_TYPE_INSTANCEOF = 0x43;
public static final int ANNOTATION_TARGET_TYPE_NEW = 0x44; private static final int ANNOTATION_TARGET_TYPE_NEW = 0x44;
public static final int ANNOTATION_TARGET_TYPE_DOUBLECOLON_NEW = 0x45; private static final int ANNOTATION_TARGET_TYPE_DOUBLE_COLON_NEW = 0x45;
public static final int ANNOTATION_TARGET_TYPE_DOUBLECOLON_ID = 0x46; private static final int ANNOTATION_TARGET_TYPE_DOUBLE_COLON_ID = 0x46;
public static final int ANNOTATION_TARGET_TYPE_CAST = 0x47; private static final int ANNOTATION_TARGET_TYPE_CAST = 0x47;
public static final int ANNOTATION_TARGET_TYPE_INVOKATION_CONSTRUCTOR = 0x48; private static final int ANNOTATION_TARGET_TYPE_INVOCATION_CONSTRUCTOR = 0x48;
public static final int ANNOTATION_TARGET_TYPE_INVOKATION_METHOD = 0x49; private static final int ANNOTATION_TARGET_TYPE_INVOCATION_METHOD = 0x49;
public static final int ANNOTATION_TARGET_TYPE_GENERIC_DOUBLECOLON_NEW = 0x4A; private static final int ANNOTATION_TARGET_TYPE_GENERIC_DOUBLE_COLON_NEW = 0x4A;
public static final int ANNOTATION_TARGET_TYPE_GENERIC_DOUBLECOLON_ID = 0x4B; private static final int ANNOTATION_TARGET_TYPE_GENERIC_DOUBLE_COLON_ID = 0x4B;
public static final int ANNOTATION_TARGET_UNION_TYPE_PARAMETER = 1; private static final int ANNOTATION_TARGET_UNION_TYPE_PARAMETER = 1;
public static final int ANNOTATION_TARGET_UNION_SUPERTYPE = 2; private static final int ANNOTATION_TARGET_UNION_SUPERTYPE = 2;
public static final int ANNOTATION_TARGET_UNION_TYPE_PARAMETER_BOUND = 3; private static final int ANNOTATION_TARGET_UNION_TYPE_PARAMETER_BOUND = 3;
public static final int ANNOTATION_TARGET_UNION_EMPTY = 4; private static final int ANNOTATION_TARGET_UNION_EMPTY = 4;
public static final int ANNOTATION_TARGET_UNION_FORMAL_PARAMETER = 5; private static final int ANNOTATION_TARGET_UNION_FORMAL_PARAMETER = 5;
public static final int ANNOTATION_TARGET_UNION_THROWS = 6; private static final int ANNOTATION_TARGET_UNION_THROWS = 6;
public static final int ANNOTATION_TARGET_UNION_LOCALVAR = 7; private static final int ANNOTATION_TARGET_UNION_LOCAL_VAR = 7;
public static final int ANNOTATION_TARGET_UNION_CATCH = 8; private static final int ANNOTATION_TARGET_UNION_CATCH = 8;
public static final int ANNOTATION_TARGET_UNION_OFFSET = 9; private static final int ANNOTATION_TARGET_UNION_OFFSET = 9;
public static final int ANNOTATION_TARGET_UNION_TYPE_ARGUMENT = 10; private static final int ANNOTATION_TARGET_UNION_TYPE_ARGUMENT = 10;
@SuppressWarnings("FieldCanBeLocal") private List<AnnotationLocation> locations;
@SuppressWarnings("FieldCanBeLocal") private List<AnnotationExprent> annotations;
List<AnnotationLocation> locations = new ArrayList<AnnotationLocation>(); @Override
List<AnnotationExprent> annotations = new ArrayList<AnnotationExprent>(); public void initContent(ConstantPool pool) throws IOException {
DataInputStream data = stream();
public void initContent(ConstantPool pool) { int len = data.readUnsignedByte();
if (len > 0) {
super.initContent(pool); locations = new ArrayList<AnnotationLocation>(len);
annotations = new ArrayList<AnnotationExprent>(len);
DataInputStream data = new DataInputStream(new ByteArrayInputStream(info)); for (int i = 0; i < len; i++) {
try {
int ann_number = data.readUnsignedByte();
for (int i = 0; i < ann_number; i++) {
locations.add(parseAnnotationLocation(data)); locations.add(parseAnnotationLocation(data));
annotations.add(StructAnnotationAttribute.parseAnnotation(data, pool)); annotations.add(StructAnnotationAttribute.parseAnnotation(data, pool));
} }
} }
catch (IOException ex) { else {
throw new RuntimeException(ex); locations = Collections.emptyList();
annotations = Collections.emptyList();
} }
} }
public AnnotationLocation parseAnnotationLocation(DataInputStream data) throws IOException { private static AnnotationLocation parseAnnotationLocation(DataInputStream data) throws IOException {
AnnotationLocation ann_location = new AnnotationLocation(); AnnotationLocation ann_location = new AnnotationLocation();
// target type // target type
ann_location.target_type = data.readUnsignedByte(); ann_location.target_type = data.readUnsignedByte();
// target union // target union
switch (ann_location.target_type) { switch (ann_location.target_type) {
case ANNOTATION_TARGET_TYPE_GENERIC_CLASS: case ANNOTATION_TARGET_TYPE_GENERIC_CLASS:
case ANNOTATION_TARGET_TYPE_GENERIC_METHOD: case ANNOTATION_TARGET_TYPE_GENERIC_METHOD:
@ -118,22 +114,22 @@ public class StructAnnotationTypeAttribute extends StructGeneralAttribute {
break; break;
case ANNOTATION_TARGET_TYPE_LOCAL_VARIABLE: case ANNOTATION_TARGET_TYPE_LOCAL_VARIABLE:
case ANNOTATION_TARGET_TYPE_RESOURCE_VARIABLE: case ANNOTATION_TARGET_TYPE_RESOURCE_VARIABLE:
ann_location.target_union = ANNOTATION_TARGET_UNION_LOCALVAR; ann_location.target_union = ANNOTATION_TARGET_UNION_LOCAL_VAR;
break; break;
case ANNOTATION_TARGET_TYPE_EXCEPTION: case ANNOTATION_TARGET_TYPE_EXCEPTION:
ann_location.target_union = ANNOTATION_TARGET_UNION_CATCH; ann_location.target_union = ANNOTATION_TARGET_UNION_CATCH;
break; break;
case ANNOTATION_TARGET_TYPE_INSTANCEOF: case ANNOTATION_TARGET_TYPE_INSTANCEOF:
case ANNOTATION_TARGET_TYPE_NEW: case ANNOTATION_TARGET_TYPE_NEW:
case ANNOTATION_TARGET_TYPE_DOUBLECOLON_NEW: case ANNOTATION_TARGET_TYPE_DOUBLE_COLON_NEW:
case ANNOTATION_TARGET_TYPE_DOUBLECOLON_ID: case ANNOTATION_TARGET_TYPE_DOUBLE_COLON_ID:
ann_location.target_union = ANNOTATION_TARGET_UNION_OFFSET; ann_location.target_union = ANNOTATION_TARGET_UNION_OFFSET;
break; break;
case ANNOTATION_TARGET_TYPE_CAST: case ANNOTATION_TARGET_TYPE_CAST:
case ANNOTATION_TARGET_TYPE_INVOKATION_CONSTRUCTOR: case ANNOTATION_TARGET_TYPE_INVOCATION_CONSTRUCTOR:
case ANNOTATION_TARGET_TYPE_INVOKATION_METHOD: case ANNOTATION_TARGET_TYPE_INVOCATION_METHOD:
case ANNOTATION_TARGET_TYPE_GENERIC_DOUBLECOLON_NEW: case ANNOTATION_TARGET_TYPE_GENERIC_DOUBLE_COLON_NEW:
case ANNOTATION_TARGET_TYPE_GENERIC_DOUBLECOLON_ID: case ANNOTATION_TARGET_TYPE_GENERIC_DOUBLE_COLON_ID:
ann_location.target_union = ANNOTATION_TARGET_UNION_TYPE_ARGUMENT; ann_location.target_union = ANNOTATION_TARGET_UNION_TYPE_ARGUMENT;
break; break;
default: default:
@ -158,7 +154,7 @@ public class StructAnnotationTypeAttribute extends StructGeneralAttribute {
break; break;
case ANNOTATION_TARGET_UNION_EMPTY: case ANNOTATION_TARGET_UNION_EMPTY:
break; break;
case ANNOTATION_TARGET_UNION_LOCALVAR: case ANNOTATION_TARGET_UNION_LOCAL_VAR:
int table_length = data.readUnsignedShort(); int table_length = data.readUnsignedShort();
ann_location.data = new int[table_length * 3 + 1]; ann_location.data = new int[table_length * 3 + 1];
@ -175,7 +171,6 @@ public class StructAnnotationTypeAttribute extends StructGeneralAttribute {
} }
// target path // target path
int path_length = data.readUnsignedByte(); int path_length = data.readUnsignedByte();
ann_location.target_path_kind = new int[path_length]; ann_location.target_path_kind = new int[path_length];
@ -190,14 +185,10 @@ public class StructAnnotationTypeAttribute extends StructGeneralAttribute {
} }
private static class AnnotationLocation { private static class AnnotationLocation {
public int target_type; public int target_type;
public int target_union; public int target_union;
public int[] data; public int[] data;
public int[] target_path_kind; public int[] target_path_kind;
public int[] target_argument_index; public int[] target_argument_index;
} }
} }

View File

@ -19,7 +19,6 @@ import org.jetbrains.java.decompiler.struct.consts.ConstantPool;
import org.jetbrains.java.decompiler.struct.consts.LinkConstant; import org.jetbrains.java.decompiler.struct.consts.LinkConstant;
import org.jetbrains.java.decompiler.struct.consts.PooledConstant; import org.jetbrains.java.decompiler.struct.consts.PooledConstant;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
@ -27,49 +26,41 @@ import java.util.List;
public class StructBootstrapMethodsAttribute extends StructGeneralAttribute { public class StructBootstrapMethodsAttribute extends StructGeneralAttribute {
private List<LinkConstant> method_refs = new ArrayList<LinkConstant>(); private List<LinkConstant> methodRefs = new ArrayList<LinkConstant>();
private List<List<PooledConstant>> method_arguments = new ArrayList<List<PooledConstant>>(); private List<List<PooledConstant>> methodArguments = new ArrayList<List<PooledConstant>>();
public void initContent(ConstantPool pool) { @Override
public void initContent(ConstantPool pool) throws IOException {
DataInputStream data = stream();
name = ATTRIBUTE_BOOTSTRAP_METHODS; int method_number = data.readUnsignedShort();
try { for (int i = 0; i < method_number; ++i) {
int bootstrap_method_ref = data.readUnsignedShort();
int num_bootstrap_arguments = data.readUnsignedShort();
DataInputStream data = new DataInputStream(new ByteArrayInputStream(info, 0, info.length)); List<PooledConstant> list_arguments = new ArrayList<PooledConstant>();
int method_number = data.readUnsignedShort(); for (int j = 0; j < num_bootstrap_arguments; ++j) {
int bootstrap_argument_ref = data.readUnsignedShort();
for (int i = 0; i < method_number; ++i) { list_arguments.add(pool.getConstant(bootstrap_argument_ref));
int bootstrap_method_ref = data.readUnsignedShort();
int num_bootstrap_arguments = data.readUnsignedShort();
List<PooledConstant> list_arguments = new ArrayList<PooledConstant>();
for (int j = 0; j < num_bootstrap_arguments; ++j) {
int bootstrap_argument_ref = data.readUnsignedShort();
list_arguments.add(pool.getConstant(bootstrap_argument_ref));
}
method_refs.add(pool.getLinkConstant(bootstrap_method_ref));
method_arguments.add(list_arguments);
} }
}
catch (IOException ex) { methodRefs.add(pool.getLinkConstant(bootstrap_method_ref));
throw new RuntimeException(ex); methodArguments.add(list_arguments);
} }
} }
public int getMethodsNumber() { public int getMethodsNumber() {
return method_refs.size(); return methodRefs.size();
} }
public LinkConstant getMethodReference(int index) { public LinkConstant getMethodReference(int index) {
return method_refs.get(index); return methodRefs.get(index);
} }
public List<PooledConstant> getMethodArguments(int index) { public List<PooledConstant> getMethodArguments(int index) {
return method_arguments.get(index); return methodArguments.get(index);
} }
} }

View File

@ -17,14 +17,15 @@ package org.jetbrains.java.decompiler.struct.attr;
import org.jetbrains.java.decompiler.struct.consts.ConstantPool; import org.jetbrains.java.decompiler.struct.consts.ConstantPool;
import java.io.IOException;
public class StructConstantValueAttribute extends StructGeneralAttribute { public class StructConstantValueAttribute extends StructGeneralAttribute {
private int index; private int index;
public void initContent(ConstantPool pool) { @Override
public void initContent(ConstantPool pool) throws IOException {
name = ATTRIBUTE_CONSTANT_VALUE; index = stream().readUnsignedShort();
index = ((info[0] & 0xFF) << 8) | (info[1] & 0xFF);
} }
public int getIndex() { public int getIndex() {

View File

@ -18,32 +18,31 @@ package org.jetbrains.java.decompiler.struct.attr;
import org.jetbrains.java.decompiler.struct.consts.ConstantPool; import org.jetbrains.java.decompiler.struct.consts.ConstantPool;
import org.jetbrains.java.decompiler.struct.consts.LinkConstant; import org.jetbrains.java.decompiler.struct.consts.LinkConstant;
import java.io.DataInputStream;
import java.io.IOException;
public class StructEnclosingMethodAttribute extends StructGeneralAttribute { public class StructEnclosingMethodAttribute extends StructGeneralAttribute {
private String classname; private String className;
private String methodName;
private String mtname;
private String methodDescriptor; private String methodDescriptor;
public void initContent(ConstantPool pool) { @Override
public void initContent(ConstantPool pool) throws IOException {
DataInputStream data = stream();
int classIndex = data.readUnsignedShort();
int methodIndex = data.readUnsignedShort();
name = ATTRIBUTE_ENCLOSING_METHOD; className = pool.getPrimitiveConstant(classIndex).getString();
if (methodIndex != 0) {
int clindex = (((info[0] & 0xFF) << 8) | (info[1] & 0xFF)); LinkConstant lk = pool.getLinkConstant(methodIndex);
int mtindex = (((info[2] & 0xFF) << 8) | (info[3] & 0xFF)); methodName = lk.elementname;
classname = pool.getPrimitiveConstant(clindex).getString();
if (mtindex != 0) {
LinkConstant lk = pool.getLinkConstant(mtindex);
mtname = lk.elementname;
methodDescriptor = lk.descriptor; methodDescriptor = lk.descriptor;
} }
} }
public String getClassname() { public String getClassName() {
return classname; return className;
} }
public String getMethodDescriptor() { public String getMethodDescriptor() {
@ -51,6 +50,6 @@ public class StructEnclosingMethodAttribute extends StructGeneralAttribute {
} }
public String getMethodName() { public String getMethodName() {
return mtname; return methodName;
} }
} }

View File

@ -17,21 +17,28 @@ package org.jetbrains.java.decompiler.struct.attr;
import org.jetbrains.java.decompiler.struct.consts.ConstantPool; import org.jetbrains.java.decompiler.struct.consts.ConstantPool;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
public class StructExceptionsAttribute extends StructGeneralAttribute { public class StructExceptionsAttribute extends StructGeneralAttribute {
private List<Integer> throwsExceptions = new ArrayList<Integer>(); private List<Integer> throwsExceptions;
public void initContent(ConstantPool pool) { @Override
public void initContent(ConstantPool pool) throws IOException {
name = ATTRIBUTE_EXCEPTIONS; DataInputStream data = stream();
int len = data.readUnsignedShort();
int length = 2 + (((info[0] & 0xFF) << 8) | (info[1] & 0xFF)) * 2; if (len > 0) {
for (int i = 2; i < length; i += 2) { throwsExceptions = new ArrayList<Integer>(len);
int index = ((info[i] & 0xFF) << 8) | (info[i + 1] & 0xFF); for (int i = 0; i < len; i++) {
throwsExceptions.add(index); throwsExceptions.add(data.readUnsignedShort());
}
}
else {
throwsExceptions = Collections.emptyList();
} }
} }
@ -42,8 +49,4 @@ public class StructExceptionsAttribute extends StructGeneralAttribute {
public List<Integer> getThrowsExceptions() { public List<Integer> getThrowsExceptions() {
return throwsExceptions; return throwsExceptions;
} }
public void setThrowsExceptions(List<Integer> throwsExceptions) {
this.throwsExceptions = throwsExceptions;
}
} }

View File

@ -16,6 +16,10 @@
package org.jetbrains.java.decompiler.struct.attr; package org.jetbrains.java.decompiler.struct.attr;
import org.jetbrains.java.decompiler.struct.consts.ConstantPool; import org.jetbrains.java.decompiler.struct.consts.ConstantPool;
import org.jetbrains.java.decompiler.util.DataInputFullStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
/* /*
attribute_info { attribute_info {
@ -44,9 +48,8 @@ public class StructGeneralAttribute {
public static final String ATTRIBUTE_SYNTHETIC = "Synthetic"; public static final String ATTRIBUTE_SYNTHETIC = "Synthetic";
public static final String ATTRIBUTE_DEPRECATED = "Deprecated"; public static final String ATTRIBUTE_DEPRECATED = "Deprecated";
protected String name; private String name;
protected byte[] info; private byte[] info;
public static StructGeneralAttribute createAttribute(String name) { public static StructGeneralAttribute createAttribute(String name) {
StructGeneralAttribute attr; StructGeneralAttribute attr;
@ -100,7 +103,11 @@ public class StructGeneralAttribute {
return attr; return attr;
} }
public void initContent(ConstantPool pool) { } protected DataInputFullStream stream() {
return new DataInputFullStream(new ByteArrayInputStream(info));
}
public void initContent(ConstantPool pool) throws IOException { }
public void setInfo(byte[] info) { public void setInfo(byte[] info) {
this.info = info; this.info = info;

View File

@ -17,14 +17,16 @@ package org.jetbrains.java.decompiler.struct.attr;
import org.jetbrains.java.decompiler.struct.consts.ConstantPool; import org.jetbrains.java.decompiler.struct.consts.ConstantPool;
import java.io.IOException;
public class StructGenericSignatureAttribute extends StructGeneralAttribute { public class StructGenericSignatureAttribute extends StructGeneralAttribute {
private String signature; private String signature;
public void initContent(ConstantPool pool) { @Override
public void initContent(ConstantPool pool) throws IOException {
name = ATTRIBUTE_SIGNATURE; int index = stream().readUnsignedShort();
signature = pool.getPrimitiveConstant(((info[0] & 0xFF) << 8) | (info[1] & 0xFF)).getString(); signature = pool.getPrimitiveConstant(index).getString();
} }
public String getSignature() { public String getSignature() {

View File

@ -17,56 +17,56 @@ package org.jetbrains.java.decompiler.struct.attr;
import org.jetbrains.java.decompiler.struct.consts.ConstantPool; import org.jetbrains.java.decompiler.struct.consts.ConstantPool;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
public class StructInnerClassesAttribute extends StructGeneralAttribute { public class StructInnerClassesAttribute extends StructGeneralAttribute {
private List<int[]> classentries = new ArrayList<int[]>(); private List<int[]> classEntries;
private List<String[]> stringEntries;
private List<String[]> stringentries = new ArrayList<String[]>(); @Override
public void initContent(ConstantPool pool) throws IOException {
DataInputStream data = stream();
public void initContent(ConstantPool pool) { int len = data.readUnsignedShort();
if (len > 0) {
classEntries = new ArrayList<int[]>(len);
stringEntries = new ArrayList<String[]>(len);
name = ATTRIBUTE_INNER_CLASSES; for (int i = 0; i < len; i++) {
int[] classEntry = new int[4];
for (int j = 0; j < 4; j++) {
classEntry[j] = data.readUnsignedShort();
}
classEntries.add(classEntry);
int length = 2 + (((info[0] & 0xFF) << 8) | (info[1] & 0xFF)) * 8; // inner name, enclosing class, original simple name
int i = 2; String[] stringEntry = new String[3];
stringEntry[0] = pool.getPrimitiveConstant(classEntry[0]).getString();
while (i < length) { if (classEntry[1] != 0) {
stringEntry[1] = pool.getPrimitiveConstant(classEntry[1]).getString();
int[] arr = new int[4]; }
for (int j = 0; j < 4; j++) { if (classEntry[2] != 0) {
arr[j] = ((info[i] & 0xFF) << 8) | (info[i + 1] & 0xFF); stringEntry[2] = pool.getPrimitiveConstant(classEntry[2]).getString();
i += 2; }
stringEntries.add(stringEntry);
} }
classentries.add(arr);
} }
else {
for (int[] entry : classentries) { classEntries = Collections.emptyList();
stringEntries = Collections.emptyList();
String[] arr = new String[3];
// inner name
arr[0] = pool.getPrimitiveConstant(entry[0]).getString();
//enclosing class
if (entry[1] != 0) {
arr[1] = pool.getPrimitiveConstant(entry[1]).getString();
}
// original simple name
if (entry[2] != 0) {
arr[2] = pool.getPrimitiveConstant(entry[2]).getString();
}
stringentries.add(arr);
} }
} }
public List<int[]> getClassentries() { public List<int[]> getClassEntries() {
return classentries; return classEntries;
} }
public List<String[]> getStringentries() { public List<String[]> getStringEntries() {
return stringentries; return stringEntries;
} }
} }

View File

@ -16,25 +16,44 @@
package org.jetbrains.java.decompiler.struct.attr; package org.jetbrains.java.decompiler.struct.attr;
import org.jetbrains.java.decompiler.struct.consts.ConstantPool; import org.jetbrains.java.decompiler.struct.consts.ConstantPool;
import org.jetbrains.java.decompiler.util.DataInputFullStream;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map;
/*
u2 local_variable_table_length;
local_variable {
u2 start_pc;
u2 length;
u2 name_index;
u2 descriptor_index;
u2 index;
}
*/
public class StructLocalVariableTableAttribute extends StructGeneralAttribute { public class StructLocalVariableTableAttribute extends StructGeneralAttribute {
private HashMap<Integer, String> mapVarNames = new HashMap<Integer, String>(); private Map<Integer, String> mapVarNames = new HashMap<Integer, String>();
public void initContent(ConstantPool pool) { @Override
public void initContent(ConstantPool pool) throws IOException {
DataInputFullStream data = stream();
name = ATTRIBUTE_LOCAL_VARIABLE_TABLE; int len = data.readUnsignedShort();
if (len > 0) {
int len = ((info[0] & 0xFF) << 8) | (info[1] & 0xFF); mapVarNames = new HashMap<Integer, String>(len);
for (int i = 0; i < len; i++) {
int ind = 6; data.discard(4);
for (int i = 0; i < len; i++, ind += 10) { int nameIndex = data.readUnsignedShort();
int nindex = ((info[ind] & 0xFF) << 8) | (info[ind + 1] & 0xFF); data.discard(2);
int vindex = ((info[ind + 4] & 0xFF) << 8) | (info[ind + 5] & 0xFF); int varIndex = data.readUnsignedShort();
mapVarNames.put(varIndex, pool.getPrimitiveConstant(nameIndex).getString());
mapVarNames.put(vindex, pool.getPrimitiveConstant(nindex).getString()); }
}
else {
mapVarNames = Collections.emptyMap();
} }
} }
@ -42,7 +61,7 @@ public class StructLocalVariableTableAttribute extends StructGeneralAttribute {
mapVarNames.putAll(attr.getMapVarNames()); mapVarNames.putAll(attr.getMapVarNames());
} }
public HashMap<Integer, String> getMapVarNames() { public Map<Integer, String> getMapVarNames() {
return mapVarNames; return mapVarNames;
} }
} }