|
| 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