Skip to content

Commit 8e03ff9

Browse files
committed
sourcecode builder update
1 parent dc03adf commit 8e03ff9

File tree

7 files changed

+138
-65
lines changed

7 files changed

+138
-65
lines changed

hasnaer/java/bytecode/ClassFile.java

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.io.IOException;
88
import java.io.InputStream;
99
import java.util.ArrayList;
10+
import java.util.Collections;
1011
import java.util.List;
1112
import javax.swing.JFileChooser;
1213

@@ -26,9 +27,7 @@ public static int getMagic() {
2627
}
2728
private int minor_version;
2829
private int major_version;
29-
3030
private ConstantPool constant_pool;
31-
3231
private int access_flags;
3332
private int this_class;
3433
private int super_class;
@@ -137,7 +136,8 @@ private List<AttributeInfo> loadAttributes(int attributes_count)
137136

138137

139138
for (int j = 0; j < number_of_exceptions; j++) {
140-
((Exceptions) attribute).addExceptionIndex(j, this.readUnsignedShort());
139+
((Exceptions) attribute).addExceptionIndex(j,
140+
this.readUnsignedShort());
141141
}
142142

143143
break;
@@ -162,7 +162,8 @@ private List<AttributeInfo> loadAttributes(int attributes_count)
162162

163163
case SourceFile:
164164
// System.err.println("SourceFile attribute");
165-
attribute = new SourceFile(attribute_name_index, attribute_length, this.readUnsignedShort());
165+
attribute = new SourceFile(attribute_name_index, attribute_length,
166+
this.readUnsignedShort());
166167
break;
167168

168169
case LineNumberTable:
@@ -181,7 +182,8 @@ private List<AttributeInfo> loadAttributes(int attributes_count)
181182
// System.err.println("LocalVariableTable attribute");
182183
int local_variable_table_length = this.readUnsignedShort();
183184
attribute = new LocalVariableTable(attribute_name_index,
184-
attribute_length, local_variable_table_length);
185+
attribute_length, local_variable_table_length,
186+
constant_pool);
185187
for (int j = 0; j < local_variable_table_length; j++) {
186188
((LocalVariableTable) attribute).addEntry(
187189
new LocalVariableTable.Entry(
@@ -191,6 +193,8 @@ private List<AttributeInfo> loadAttributes(int attributes_count)
191193
this.readUnsignedShort(),
192194
this.readUnsignedShort()));
193195
}
196+
Collections.sort(((LocalVariableTable) attribute).getTable());
197+
194198
break;
195199
case Deprecated:
196200
// System.err.println("Deprecated attribute");
@@ -214,8 +218,8 @@ private List<AttributeInfo> loadAttributes(int attributes_count)
214218
private void loadMethods(int methods_count) throws IOException {
215219
for (int i = 0; i < methods_count; i++) {
216220
MethodInfo method = new MethodInfo(this.readUnsignedShort(),
217-
this.readUnsignedShort(), this.readUnsignedShort());
218-
221+
this.readUnsignedShort(), this.readUnsignedShort(), constant_pool);
222+
219223
method.setName(constant_pool.getUTF8_Info(method.getName_index()).getValue());
220224
method.setDescriptor(constant_pool.getUTF8_Info(method.getDescriptor_index()).getValue());
221225
int attribute_count = this.readUnsignedShort();
@@ -227,7 +231,7 @@ private void loadMethods(int methods_count) throws IOException {
227231
private void loadFields(int fields_count) throws IOException {
228232
for (int i = 0; i < fields_count; i++) {
229233
FieldInfo field = new FieldInfo(this.readUnsignedShort(),
230-
this.readUnsignedShort(), this.readUnsignedShort());
234+
this.readUnsignedShort(), this.readUnsignedShort(), constant_pool);
231235

232236
field.setName(constant_pool.getUTF8_Info(field.getName_index()).getValue());
233237
field.setDescriptor(constant_pool.getUTF8_Info(field.getDescriptor_index()).getValue());
@@ -237,7 +241,6 @@ private void loadFields(int fields_count) throws IOException {
237241
}
238242
}
239243

240-
241244
private void loadInterfaces(int interfaces_count) throws IOException {
242245
for (int i = 0; i < interfaces_count; i++) {
243246
int index = this.readUnsignedShort();
@@ -307,29 +310,29 @@ public String getClassName() {
307310
return getConstant_pool().getUTF8_Info(utf).getValue().replace("/", ".");
308311
}
309312

310-
public String getSuperClassName(){
313+
public String getSuperClassName() {
311314
int utf = getConstant_pool().getClass_Info(getSuper_class()).getName_index();
312315
return getConstant_pool().getUTF8_Info(utf).getValue().replace("/", ".");
313316
}
314-
315-
public MethodInfo getMethod(String name){
316-
for(MethodInfo method : methods){
317-
if(method.getName().equals(name)){
317+
318+
public MethodInfo getMethod(String name) {
319+
for (MethodInfo method : methods) {
320+
if (method.getName().equals(name)) {
318321
return method;
319322
}
320323
}
321324
return null;
322325
}
323-
324-
public FieldInfo getField(String name){
325-
for(FieldInfo field : fields){
326-
if(field.getName().equals(name)){
326+
327+
public FieldInfo getField(String name) {
328+
for (FieldInfo field : fields) {
329+
if (field.getName().equals(name)) {
327330
return field;
328331
}
329332
}
330333
return null;
331334
}
332-
335+
333336
public static void main(String[] args) throws Exception {
334337
JFileChooser browser = new JFileChooser();
335338
int result = browser.showOpenDialog(null);
@@ -409,8 +412,8 @@ public List<MethodInfo> getMethods() {
409412
public List<AttributeInfo> getAttributes() {
410413
return attributes;
411414
}
412-
413-
public boolean isInterface(){
415+
416+
public boolean isInterface() {
414417
return AccessFlags.isInterface(this.access_flags);
415418
}
416419
}

hasnaer/java/bytecode/ClassMemberInfo.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package hasnaer.java.bytecode;
22

33
import hasnaer.java.bytecode.attribute.AttributeInfo;
4+
import hasnaer.java.bytecode.cp.ConstantPool;
45
import java.util.List;
56

67
/**
@@ -15,15 +16,17 @@ public abstract class ClassMemberInfo {
1516

1617
private List<AttributeInfo> attributes;
1718

19+
private ConstantPool constant_pool;
20+
1821
private String name;
1922
private String descriptor;
2023

2124
public ClassMemberInfo(int access_flags,
22-
int name_index, int descriptor_index){
25+
int name_index, int descriptor_index, ConstantPool constant_pool){
2326
this.access_flags = access_flags;
2427
this.name_index = name_index;
2528
this.descriptor_index = descriptor_index;
26-
29+
this.constant_pool = constant_pool;
2730
}
2831

2932
/**

hasnaer/java/bytecode/Descriptor.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,17 @@ public static String getReturnDescriptor(String descriptor){
5151
int index = descriptor.indexOf(")") + 1;
5252
return fieldDataType(descriptor.substring(index));
5353
}
54+
55+
public static int getParamCount(String descriptor) {
56+
int start_index = descriptor.indexOf("(");
57+
int end_index = descriptor.indexOf(")");
58+
59+
String params = descriptor.substring(start_index + 1, end_index);
60+
61+
if(params.length() == 0){
62+
return 0;
63+
}
64+
65+
return params.split(";").length;
66+
}
5467
}

hasnaer/java/bytecode/FieldInfo.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package hasnaer.java.bytecode;
22

3+
import hasnaer.java.bytecode.cp.ConstantPool;
4+
35
/**
46
*
57
* @author hasnae rehioui
@@ -8,8 +10,8 @@ public class FieldInfo extends ClassMemberInfo {
810

911

1012
public FieldInfo(int access_flags, int name_index,
11-
int descriptor_index){
12-
super(access_flags, name_index, descriptor_index);
13+
int descriptor_index, ConstantPool constant_pool){
14+
super(access_flags, name_index, descriptor_index, constant_pool);
1315
}
1416

1517
}

hasnaer/java/bytecode/MethodInfo.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import hasnaer.java.bytecode.attribute.AttributeInfo;
44
import hasnaer.java.bytecode.attribute.Code;
5+
import hasnaer.java.bytecode.cp.ConstantPool;
56

67
/**
78
*
@@ -10,8 +11,8 @@
1011
public class MethodInfo extends ClassMemberInfo {
1112

1213
public MethodInfo(int access_flags,
13-
int name_index, int descriptor_index){
14-
super(access_flags, name_index, descriptor_index);
14+
int name_index, int descriptor_index, ConstantPool constant_pool){
15+
super(access_flags, name_index, descriptor_index, constant_pool);
1516
}
1617

1718
public Code getCodeAttribute(){

hasnaer/java/bytecode/SourceCodeBuilder.java

Lines changed: 50 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import hasnaer.java.bytecode.attribute.Code;
44
import hasnaer.java.bytecode.attribute.LocalVariableTable;
5-
import hasnaer.java.bytecode.cp.ConstantPool;
65
import java.io.FileInputStream;
6+
import java.util.Arrays;
77
import java.util.List;
88
import javax.swing.JFileChooser;
99

@@ -51,23 +51,16 @@ public static String visitClass(ClassFile class_file) {
5151
}
5252
builder.append(" ");
5353
}
54-
5554
builder.append("{\n");
56-
57-
builder.append(visitFields(class_file.getFields(),
58-
class_file.getConstant_pool()));
59-
60-
builder.append(visitMethods(class_file.getMethods(),
61-
class_file.getConstant_pool()));
62-
55+
builder.append(visitFields(class_file.getFields()));
56+
builder.append(visitMethods(class_file.getMethods()));
6357
builder.append("}");
6458

6559

6660
return builder.toString();
6761
}
6862

69-
public static String visitFields(List<FieldInfo> fields,
70-
ConstantPool constant_pool) {
63+
public static String visitFields(List<FieldInfo> fields) {
7164

7265
StringBuilder builder = new StringBuilder("\n");
7366

@@ -84,45 +77,64 @@ public static String visitFields(List<FieldInfo> fields,
8477
return builder.toString();
8578
}
8679

87-
public static String visitMethods(List<MethodInfo> methods,
88-
ConstantPool constant_pool) {
80+
public static String visitMethods(List<MethodInfo> methods) {
8981

9082
StringBuilder builder = new StringBuilder("\n");
9183

9284
for (MethodInfo method : methods) {
9385

94-
builder.append(INDENT);
95-
builder.append(AccessFlags.methodAccess(method.getAccess_flags()));
96-
builder.append(Descriptor.getReturnDescriptor(method.getDescriptor()));
97-
builder.append(method.getName());
98-
builder.append("(){\n");
99-
builder.append(INDENT);
100-
101-
builder.append("}\n\n");
86+
builder.append(visitMethod(method));
10287

10388
}
10489

10590
return builder.toString();
10691
}
10792

108-
public static String visitMethod(MethodInfo method,
109-
ConstantPool constant_pool) {
93+
public static String visitMethod(MethodInfo method) {
11094

11195
StringBuilder builder = new StringBuilder();
11296

11397
Code code_attribute = method.getCodeAttribute();
11498
LocalVariableTable lvt_attribute = code_attribute.getLocalVariableTableAttribute();
115-
116-
System.err.println(lvt_attribute.getTable());
117-
118-
builder.append(INDENT);
119-
builder.append(AccessFlags.methodAccess(method.getAccess_flags()));
120-
builder.append(Descriptor.getReturnDescriptor(method.getDescriptor()));
121-
builder.append(method.getName());
122-
builder.append("(){\n");
123-
builder.append(INDENT);
124-
125-
builder.append("}\n\n");
99+
100+
// System.err.println(method.getName());
101+
// System.err.println(lvt_attribute);
102+
103+
if (lvt_attribute != null) {
104+
105+
// System.err.println("doMethod");
106+
lvt_attribute.init();
107+
108+
builder.append(INDENT);
109+
builder.append(AccessFlags.methodAccess(method.getAccess_flags()));
110+
builder.append(Descriptor.getReturnDescriptor(method.getDescriptor()));
111+
builder.append(method.getName());
112+
int numOfParameters = Descriptor.getParamCount(method.getDescriptor());
113+
builder.append(" (");
114+
if (numOfParameters > 0) {
115+
String[] variable = lvt_attribute.getVariable(lvt_attribute.THIS_INDEX + 1);
116+
builder.append(variable[0]);
117+
builder.append(" ");
118+
builder.append(variable[1]);
119+
120+
for (int i = 2; i <= numOfParameters; i++) {
121+
builder.append(", ");
122+
variable = lvt_attribute.getVariable(lvt_attribute.THIS_INDEX + i);
123+
builder.append(variable[0]);
124+
builder.append(" ");
125+
builder.append(variable[1]);
126+
}
127+
}
128+
129+
builder.append(") {\n");
130+
builder.append(INDENT);
131+
builder.append("}\n\n");
132+
133+
134+
}
135+
136+
137+
126138

127139
return builder.toString();
128140
}
@@ -132,8 +144,10 @@ public static void main(String[] args) throws Exception {
132144
int result = browser.showOpenDialog(null);
133145
if (result == JFileChooser.APPROVE_OPTION) {
134146
ClassFile c_file = new ClassFile(new FileInputStream(browser.getSelectedFile()));
135-
// System.err.println("this_class=" + c_file.getName());
136-
System.out.println(SourceCodeBuilder.visitMethod(c_file.getMethod("visitMethod"), c_file.getConstant_pool()));
147+
148+
// System.out.println(SourceCodeBuilder.visitMethod(c_file.getMethod("visitMethod")));
149+
System.out.println(SourceCodeBuilder.visitClass(c_file));
150+
137151
}
138152

139153
}

0 commit comments

Comments
 (0)