Skip to content

Commit 7f3ebc0

Browse files
committed
第一次提交,项目如何运行参见test文件夹下的测试代码
0 parents  commit 7f3ebc0

File tree

386 files changed

+15474
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

386 files changed

+15474
-0
lines changed

Diff for: .gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
/smalljvm.iml
3+
/out/
4+
/.idea/

Diff for: src/smalljvm/classfile/AttributeInfo.java

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package smalljvm.classfile;
2+
3+
4+
/**
5+
* Created by junling on 2017/4/6.
6+
*/
7+
public interface AttributeInfo {
8+
9+
int length();
10+
11+
short nameindex();
12+
13+
public static boolean isBootstrapMethods(ClassFile clazz, AttributeInfo attribute){ return (clazz.getString(attribute.nameindex()) == Env.ATTRIBUTE_NAME_BootstrapMethods); }
14+
public static boolean isSourceFile(ClassFile clazz, AttributeInfo attribute){ return (clazz.getString(attribute.nameindex()) == Env.ATTRIBUTE_NAME_SourceFile); }
15+
public static boolean isSourceDebugExtension(ClassFile clazz, AttributeInfo attribute){ return (clazz.getString(attribute.nameindex()) == Env.ATTRIBUTE_NAME_SourceDebugExtension); }
16+
public static boolean isInnerClasses(ClassFile clazz, AttributeInfo attribute){ return (clazz.getString(attribute.nameindex()) == Env.ATTRIBUTE_NAME_InnerClasses); }
17+
public static boolean isEnclosingMethod(ClassFile clazz, AttributeInfo attribute){ return (clazz.getString(attribute.nameindex()) == Env.ATTRIBUTE_NAME_EnclosingMethod); }
18+
public static boolean isDeprecated(ClassFile clazz, AttributeInfo attribute){ return (clazz.getString(attribute.nameindex()) == Env.ATTRIBUTE_NAME_Deprecated); }
19+
public static boolean isSynthetic(ClassFile clazz, AttributeInfo attribute){ return (clazz.getString(attribute.nameindex()) == Env.ATTRIBUTE_NAME_Synthetic); }
20+
public static boolean isSignature(ClassFile clazz, AttributeInfo attribute){ return (clazz.getString(attribute.nameindex()) == Env.ATTRIBUTE_NAME_Signature); }
21+
public static boolean isConstantValue(ClassFile clazz, AttributeInfo attribute){ return (clazz.getString(attribute.nameindex()) == Env.ATTRIBUTE_NAME_ConstantValue); }
22+
public static boolean isMethodParameters(ClassFile clazz, AttributeInfo attribute){ return (clazz.getString(attribute.nameindex()) == Env.ATTRIBUTE_NAME_MethodParameters); }
23+
public static boolean isExceptions(ClassFile clazz, AttributeInfo attribute){ return (clazz.getString(attribute.nameindex()) == Env.ATTRIBUTE_NAME_Exceptions); }
24+
public static boolean isCode(ClassFile clazz, AttributeInfo attribute){ return (clazz.getString(attribute.nameindex()) == Env.ATTRIBUTE_NAME_Code); }
25+
public static boolean isStackMapTable(ClassFile clazz, AttributeInfo attribute){ return (clazz.getString(attribute.nameindex()) == Env.ATTRIBUTE_NAME_StackMapTable); }
26+
public static boolean isLineNumberTable(ClassFile clazz, AttributeInfo attribute){ return (clazz.getString(attribute.nameindex()) == Env.ATTRIBUTE_NAME_LineNumberTable); }
27+
public static boolean isLocalVariableTable(ClassFile clazz, AttributeInfo attribute){ return (clazz.getString(attribute.nameindex()) == Env.ATTRIBUTE_NAME_LocalVariableTable); }
28+
public static boolean isRuntimeVisibleAnnotations(ClassFile clazz, AttributeInfo attribute){ return (clazz.getString(attribute.nameindex()) == Env.ATTRIBUTE_NAME_RuntimeVisibleAnnotations); }
29+
public static boolean isRuntimeInvisibleAnnotations(ClassFile clazz, AttributeInfo attribute){ return (clazz.getString(attribute.nameindex()) == Env.ATTRIBUTE_NAME_RuntimeInvisibleAnnotations); }
30+
public static boolean isRuntimeVisibleParameterAnnotations(ClassFile clazz, AttributeInfo attribute){ return (clazz.getString(attribute.nameindex()) == Env.ATTRIBUTE_NAME_RuntimeVisibleParameterAnnotations); }
31+
public static boolean isRuntimeInvisibleParameterAnnotations(ClassFile clazz, AttributeInfo attribute){ return (clazz.getString(attribute.nameindex()) == Env.ATTRIBUTE_NAME_RuntimeInvisibleParameterAnnotations); }
32+
public static boolean isRuntimeVisibleTypeAnnotations(ClassFile clazz, AttributeInfo attribute){ return (clazz.getString(attribute.nameindex()) == Env.ATTRIBUTE_NAME_RuntimeVisibleTypeAnnotations); }
33+
public static boolean isRuntimeInvisibleTypeAnnotations(ClassFile clazz, AttributeInfo attribute){ return (clazz.getString(attribute.nameindex()) == Env.ATTRIBUTE_NAME_RuntimeInvisibleTypeAnnotations); }
34+
public static boolean isAnnotationDefault(ClassFile clazz, AttributeInfo attribute){ return (clazz.getString(attribute.nameindex()) == Env.ATTRIBUTE_NAME_AnnotationDefault); }
35+
36+
}

Diff for: src/smalljvm/classfile/ClassFile.java

+178
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
package smalljvm.classfile;
2+
3+
4+
import smalljvm.classfile.constant.ClassConstant;
5+
import smalljvm.classfile.constant.StringConstant;
6+
import smalljvm.classfile.constant.Utf8Constant;
7+
8+
/**
9+
* Created by junling on 2017/4/6.
10+
*/
11+
public class ClassFile {
12+
public int magic;
13+
public short minor_version;
14+
public short major_version;
15+
public short constant_pool_count;
16+
public ConstantInfo[] constant_pool = new ConstantInfo[0];
17+
public short access_flags;
18+
public short this_class;
19+
public short super_class;
20+
public short interfaces_count;
21+
public short[] interfaces = new short[0];
22+
public short fields_count;
23+
public FieldInfo[] fields = new FieldInfo[0];
24+
public short methods_count;
25+
public MethodInfo[] methods = new MethodInfo[0];
26+
public short attributes_count;
27+
public AttributeInfo[] attributes = new AttributeInfo[0];
28+
29+
30+
31+
@Override
32+
public String toString(){
33+
String s = "ClassFile";
34+
s += "magic " + Integer.toHexString(magic) + "\n";
35+
s += "minor_version " + String.valueOf(minor_version) + "\n";
36+
s += "major_version " + String.valueOf(major_version) + "\n";
37+
38+
s += "constant_pool_count " + String.valueOf(constant_pool_count) + "\n";
39+
for(int i = 0; i < constant_pool_count; i++){
40+
ConstantInfo constant = constant_pool[i];
41+
if (constant != null)
42+
s += "constant[" + String.valueOf(i) + "]: " + constant.toString() + "\n";
43+
}
44+
45+
s += "access_flags " + String.valueOf(access_flags) + "\n";
46+
s += "this_class " + String.valueOf(this_class) + "\n";
47+
s += "super_class " + String.valueOf(super_class) + "\n";
48+
49+
s += "interfaces_count " + String.valueOf(interfaces_count) + "\n";
50+
for(int i = 0; i < interfaces_count; i++){
51+
s += "interfaces[" + String.valueOf(i) + "]: " + interfaces[i] + "\n";
52+
}
53+
54+
s += "fields_count " + String.valueOf(fields_count) + "\n";
55+
for(int i = 0; i < fields_count; i++){
56+
FieldInfo field = fields[i];
57+
s += "fields[" + String.valueOf(i) + "]:";
58+
s += field.toString();
59+
s += "\n";
60+
}
61+
62+
s += "methods_count " + String.valueOf(methods_count) + "\n";
63+
for(int i = 0; i < methods_count; i++){
64+
MethodInfo method = methods[i];
65+
s += "methods[" + String.valueOf(i) + "]: ";
66+
s += method.toString();
67+
s += "\n";
68+
}
69+
70+
s += "attributes_count " + String.valueOf(attributes_count) + "\n";
71+
for(int i = 0; i < attributes_count; i++){
72+
AttributeInfo attribute = attributes[i];
73+
s += "attributes[" + String.valueOf(i) + "]:";
74+
s += attribute.toString();
75+
s += "\n";
76+
}
77+
78+
return s;
79+
}
80+
81+
public String getName() {
82+
return getClassName(this_class);
83+
}
84+
85+
public String getSuperName() {
86+
if (super_class == 0){
87+
if(!getName().equals("java/lang/Object"))
88+
throw new IllegalStateException("");
89+
return null;
90+
}
91+
92+
return getClassName(super_class);
93+
}
94+
95+
public short getInterfacesCount(){
96+
return interfaces_count;
97+
}
98+
public String getInterfaceName(short index){
99+
return getClassName(interfaces[index]);
100+
}
101+
102+
103+
104+
105+
public FieldInfo findField(String name, String descriptor)
106+
{
107+
for (int index = 0; index < fields_count; index++)
108+
{
109+
FieldInfo field = fields[index];
110+
if ((name == null || field.getName().equals(name)) &&
111+
(descriptor == null || field.getDescriptor().equals(descriptor)))
112+
{
113+
return field;
114+
}
115+
}
116+
117+
return null;
118+
}
119+
120+
121+
122+
public MethodInfo findMethod(String name, String descriptor)
123+
{
124+
for (int index = 0; index < methods_count; index++)
125+
{
126+
MethodInfo method = methods[index];
127+
if ((name == null || method.getName().equals(name)) &&
128+
(descriptor == null || method.getDescriptor().equals(descriptor)))
129+
{
130+
return method;
131+
}
132+
}
133+
134+
return null;
135+
}
136+
137+
public String getClassName(short index){
138+
ConstantInfo constant = constant_pool[index];
139+
if (!ConstantInfo.isClass(constant))
140+
throw new IllegalStateException("");
141+
142+
ClassConstant classConstant = (ClassConstant)constant;
143+
short name_index = classConstant.name_index;
144+
145+
constant = constant_pool[name_index];
146+
if (!ConstantInfo.isUtf8(constant))
147+
throw new IllegalStateException("");
148+
149+
Utf8Constant utf8Constant = (Utf8Constant)constant;
150+
return utf8Constant.string();
151+
}
152+
153+
public String getUtf(short index){
154+
ConstantInfo constant = constant_pool[index];
155+
if (!ConstantInfo.isUtf8(constant))
156+
throw new IllegalStateException("");
157+
158+
Utf8Constant utf8Constant = (Utf8Constant)constant;
159+
return utf8Constant.string();
160+
}
161+
162+
public String getString(short index){
163+
ConstantInfo constant = constant_pool[index];
164+
if (!ConstantInfo.isString(constant))
165+
throw new IllegalStateException("");
166+
167+
StringConstant stringConstant = (StringConstant)constant;
168+
short name_index = stringConstant.string_index;
169+
170+
constant = constant_pool[name_index];
171+
if (!ConstantInfo.isUtf8(constant))
172+
throw new IllegalStateException("");
173+
174+
Utf8Constant utf8Constant = (Utf8Constant)constant;
175+
return utf8Constant.string();
176+
}
177+
178+
}

0 commit comments

Comments
 (0)