Skip to content

Commit 48918ed

Browse files
update
1 parent dc8882e commit 48918ed

File tree

16 files changed

+449
-4
lines changed

16 files changed

+449
-4
lines changed

src/main/java/org/cnt/java/classfile/Broccoli.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
*/
88
public class Broccoli extends Vegetable implements Organic {
99

10+
private double price;
11+
private double weight;
12+
1013
@Override
1114
public String getCertification() {
1215
return "china";
@@ -24,4 +27,13 @@ public double bloom() {
2427
public String fruit() {
2528
return "perfect";
2629
}
30+
31+
public double getPrice() {
32+
return price;
33+
}
34+
35+
public double getWeight() {
36+
return weight;
37+
}
38+
2739
}

src/main/java/org/cnt/java/classfile/bytecode/ByteCode.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ public class ByteCode {
1717
private InterfacesCount interfacesCount;
1818
private Interfaces interfaces;
1919
private FieldsCount fieldsCount;
20-
20+
private Fields fields;
2121
private MethodsCount methodsCount;
22-
22+
private Methods methods;
2323
private AttributesCount attributesCount;
2424

2525
private int offset;
@@ -41,6 +41,9 @@ public void parse() {
4141
parseInterfacesCount();
4242
parseInterfaces();
4343
parseFieldsCount();
44+
parseFields();
45+
parseMethodsCount();
46+
parseMethods();
4447
}
4548

4649
public void toString(StringBuilder sb) {
@@ -55,6 +58,9 @@ public void toString(StringBuilder sb) {
5558
sb.append(interfacesCount).append("\r\n");
5659
sb.append(interfaces).append("\r\n");
5760
sb.append(fieldsCount).append("\r\n");
61+
sb.append(fields).append("\r\n");
62+
sb.append(methodsCount).append("\r\n");
63+
sb.append(methods).append("\r\n");
5864
}
5965

6066
public void parseMagic() {
@@ -112,11 +118,21 @@ public void parseFieldsCount() {
112118
offset = fieldsCount.parse();
113119
}
114120

121+
public void parseFields() {
122+
fields = new Fields(bytes, offset, fieldsCount.getCount(), constantPool);
123+
offset = fields.parse();
124+
}
125+
115126
public void parseMethodsCount() {
116127
methodsCount = new MethodsCount(bytes, offset);
117128
offset = methodsCount.parse();
118129
}
119130

131+
public void parseMethods() {
132+
methods = new Methods(bytes, offset, methodsCount.getCount(), constantPool);
133+
offset = methods.parse();
134+
}
135+
120136
public void parseAttributesCount() {
121137
attributesCount = new AttributesCount(bytes, offset);
122138
offset = attributesCount.parse();

src/main/java/org/cnt/java/classfile/bytecode/Fields.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,15 @@ public int parse() {
3030
}
3131
return offset;
3232
}
33+
34+
@Override
35+
public String toString() {
36+
StringBuilder sb = new StringBuilder("Fields [\r\n");
37+
for (int i = 0, len = fields.length; i < len; i++) {
38+
sb.append("#" + i).append(" = ")
39+
.append(fields[i]).append("\r\n");
40+
}
41+
sb.append("]");
42+
return sb.toString();
43+
}
3344
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,44 @@
11
package org.cnt.java.classfile.bytecode;
22

3+
import org.cnt.java.classfile.bytecode.method.MethodInfo;
4+
35
/**
46
* @author lixinjie
57
* @since 2019-07-11
68
*/
79
public class Methods {
810

11+
private int methodsCount;
12+
private int offset;
13+
private byte[] bytes;
14+
private ConstantPool constantPool;
15+
16+
private MethodInfo[] methods;
17+
18+
public Methods(byte[] bytes, int offset, int methodsCount, ConstantPool constantPool) {
19+
this.bytes = bytes;
20+
this.offset = offset;
21+
this.methodsCount = methodsCount;
22+
this.constantPool = constantPool;
23+
}
24+
25+
public int parse() {
26+
methods = new MethodInfo[methodsCount];
27+
for (int i = 0; i < methodsCount; i++) {
28+
methods[i] = new MethodInfo(bytes, offset, constantPool);
29+
offset = methods[i].parse();
30+
}
31+
return offset;
32+
}
33+
34+
@Override
35+
public String toString() {
36+
StringBuilder sb = new StringBuilder("Methods [\r\n");
37+
for (int i = 0, len = methods.length; i < len; i++) {
38+
sb.append("#" + i).append(" = ")
39+
.append(methods[i]).append("\r\n");
40+
}
41+
sb.append("]");
42+
return sb.toString();
43+
}
944
}

src/main/java/org/cnt/java/classfile/bytecode/MethodsCount.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,9 @@ public int parse() {
2121
public int getCount() {
2222
return u2.getValue();
2323
}
24+
25+
@Override
26+
public String toString() {
27+
return "MethodsCount [getCount()=" + getCount() + "]";
28+
}
2429
}

src/main/java/org/cnt/java/classfile/bytecode/attribute/AttributeInfoBuilder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public static AttributeInfoBuilder newBuilder(byte[] bytes, ConstantPool constan
2424
public AttributeInfo build(int offset) {
2525
int attributeNameIndex = Byter.toUnsigned(bytes[offset], bytes[offset + 1]);
2626
String attributeName = constantPool.getConstantUtf8String(attributeNameIndex);
27+
System.out.println(attributeName);
2728
switch (attributeName) {
2829
case ConstantValue_Attribute:
2930
return new ConstantValue(bytes, offset);

src/main/java/org/cnt/java/classfile/bytecode/attribute/RuntimeInvisibleTypeAnnotations.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.cnt.java.classfile.bytecode.attribute;
22

3+
import org.cnt.java.classfile.bytecode.attribute.annotation.TypeAnnotation;
34
import org.cnt.java.classfile.bytecode.attribute.util.Num2;
45

56
/**
@@ -9,6 +10,7 @@
910
public class RuntimeInvisibleTypeAnnotations extends AttributeInfo {
1011

1112
private Num2 numAnnotations;
13+
private TypeAnnotation[] annotations;
1214

1315
public RuntimeInvisibleTypeAnnotations(byte[] bytes, int offset) {
1416
super(bytes, offset);
@@ -18,7 +20,11 @@ public int parse() {
1820
offset = super.parse();
1921
numAnnotations = new Num2(bytes, offset);
2022
offset = numAnnotations.parse();
21-
23+
annotations = new TypeAnnotation[numAnnotations.getNum()];
24+
for (int i = 0, len = annotations.length; i < len; i++) {
25+
annotations[i] = new TypeAnnotation(bytes, offset);
26+
offset = annotations[i].parse();
27+
}
2228
return offset;
2329
}
2430
}

src/main/java/org/cnt/java/classfile/bytecode/attribute/RuntimeVisibleTypeAnnotations.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.cnt.java.classfile.bytecode.attribute;
22

3+
import org.cnt.java.classfile.bytecode.attribute.annotation.TypeAnnotation;
34
import org.cnt.java.classfile.bytecode.attribute.util.Num2;
45

56
/**
@@ -9,6 +10,7 @@
910
public class RuntimeVisibleTypeAnnotations extends AttributeInfo {
1011

1112
private Num2 numAnnotations;
13+
private TypeAnnotation[] annotations;
1214

1315
public RuntimeVisibleTypeAnnotations(byte[] bytes, int offset) {
1416
super(bytes, offset);
@@ -18,7 +20,11 @@ public int parse() {
1820
offset = super.parse();
1921
numAnnotations = new Num2(bytes, offset);
2022
offset = numAnnotations.parse();
21-
23+
annotations = new TypeAnnotation[numAnnotations.getNum()];
24+
for (int i = 0, len = annotations.length; i < len; i++) {
25+
annotations[i] = new TypeAnnotation(bytes, offset);
26+
offset = annotations[i].parse();
27+
}
2228
return offset;
2329
}
2430
}

src/main/java/org/cnt/java/classfile/bytecode/attribute/annotation/TargetInfoUnion.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
package org.cnt.java.classfile.bytecode.attribute.annotation;
22

3+
import org.cnt.java.classfile.bytecode.attribute.util.CatchTarget;
4+
import org.cnt.java.classfile.bytecode.attribute.util.EmptyTarget;
5+
import org.cnt.java.classfile.bytecode.attribute.util.FormalParameterTarget;
6+
import org.cnt.java.classfile.bytecode.attribute.util.LocalVarTarget;
7+
import org.cnt.java.classfile.bytecode.attribute.util.OffsetTarget;
8+
import org.cnt.java.classfile.bytecode.attribute.util.SupertypeTarget;
39
import org.cnt.java.classfile.bytecode.attribute.util.TargetType;
10+
import org.cnt.java.classfile.bytecode.attribute.util.ThrowsTarget;
11+
import org.cnt.java.classfile.bytecode.attribute.util.TypeArgumentTarget;
12+
import org.cnt.java.classfile.bytecode.attribute.util.TypeParameterBoundTarget;
13+
import org.cnt.java.classfile.bytecode.attribute.util.TypeParameterTarget;
414

515
/**
616
* @author lixinjie
@@ -12,13 +22,55 @@ public class TargetInfoUnion {
1222
private int offset;
1323
private TargetType targetType;
1424

25+
private TypeParameterTarget typeParameterTarget;
26+
private SupertypeTarget supertypeTarget;
27+
private TypeParameterBoundTarget typeParameterBoundTarget;
28+
private EmptyTarget emptyTarget;
29+
private FormalParameterTarget formalParameterTarget;
30+
private ThrowsTarget throwsTarget;
31+
private LocalVarTarget localVarTarget;
32+
private CatchTarget catchTarget;
33+
private OffsetTarget offsetTarget;
34+
private TypeArgumentTarget typeArgumentTarget;
35+
1536
public TargetInfoUnion(byte[] bytes, int offset, TargetType targetType) {
1637
this.bytes = bytes;
1738
this.offset = offset;
1839
this.targetType = targetType;
1940
}
2041

2142
public int parse() {
43+
if (targetType.isTypeParameterTarget()) {
44+
typeParameterTarget = new TypeParameterTarget(bytes, offset);
45+
offset = typeParameterTarget.parse();
46+
} else if (targetType.isSupertypeTarget()) {
47+
supertypeTarget = new SupertypeTarget(bytes, offset);
48+
offset = supertypeTarget.parse();
49+
} else if (targetType.isTypeParameterBoundTarget()) {
50+
typeParameterBoundTarget = new TypeParameterBoundTarget(bytes, offset);
51+
offset = typeParameterBoundTarget.parse();
52+
} else if (targetType.isEmptyTarget()) {
53+
emptyTarget = new EmptyTarget(bytes, offset);
54+
offset = emptyTarget.parse();
55+
} else if (targetType.isFormalParameterTarget()) {
56+
formalParameterTarget = new FormalParameterTarget(bytes, offset);
57+
offset = formalParameterTarget.parse();
58+
} else if (targetType.isThrowsTarget()) {
59+
throwsTarget = new ThrowsTarget(bytes, offset);
60+
offset = throwsTarget.parse();
61+
} else if (targetType.isLocalVarTarget()) {
62+
localVarTarget = new LocalVarTarget(bytes, offset);
63+
offset = localVarTarget.parse();
64+
} else if (targetType.isCatchTarget()) {
65+
catchTarget = new CatchTarget(bytes, offset);
66+
offset = catchTarget.parse();
67+
} else if (targetType.isOffsetTarget()) {
68+
offsetTarget = new OffsetTarget(bytes, offset);
69+
offset = offsetTarget.parse();
70+
} else if (targetType.isTypeArgumentTarget()) {
71+
typeArgumentTarget = new TypeArgumentTarget(bytes, offset);
72+
offset = typeArgumentTarget.parse();
73+
}
2274
return offset;
2375
}
2476
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.cnt.java.classfile.bytecode.attribute.util;
2+
3+
import org.cnt.java.classfile.bytecode.constantpool.util.Length2;
4+
5+
/**
6+
* @author lixinjie
7+
* @since 2019-07-16
8+
*/
9+
public class LocalVarTarget {
10+
11+
private byte[] bytes;
12+
private int offset;
13+
14+
private Length2 tableLength;
15+
private Table[] tables;
16+
17+
public LocalVarTarget(byte[] bytes, int offset) {
18+
this.bytes = bytes;
19+
this.offset = offset;
20+
}
21+
22+
public int parse() {
23+
tableLength = new Length2(bytes, offset);
24+
offset = tableLength.parse();
25+
tables = new Table[tableLength.getLength()];
26+
for (int i = 0, len = tables.length; i < len; i++) {
27+
tables[i] = new Table(bytes, offset);
28+
offset = tables[i].parse();
29+
}
30+
return offset;
31+
}
32+
}

0 commit comments

Comments
 (0)