1 package polyglot.types.reflect; 2 3 import polyglot.types.*; 4 import java.util.*; 5 import java.io.*; 6 7 21 class Method 22 { 23 ClassFile clazz; 24 int modifiers; 25 int name; 26 int type; 27 Attribute[] attrs; 28 Exceptions exceptions; 29 boolean synthetic; 30 31 41 Method(DataInputStream in, ClassFile clazz) throws IOException 42 { 43 this.clazz = clazz; 44 45 modifiers = in.readUnsignedShort(); 46 47 name = in.readUnsignedShort(); 48 type = in.readUnsignedShort(); 49 50 int numAttributes = in.readUnsignedShort(); 51 52 attrs = new Attribute[numAttributes]; 53 54 for (int i = 0; i < numAttributes; i++) { 55 int nameIndex = in.readUnsignedShort(); 56 int length = in.readInt(); 57 58 Constant name = clazz.constants[nameIndex]; 59 60 if (name != null) { 61 if ("Exceptions".equals(name.value())) { 62 exceptions = new Exceptions(clazz, in, nameIndex, length); 63 attrs[i] = exceptions; 64 } 65 if ("Synthetic".equals(name.value())) { 66 synthetic = true; 67 } 68 } 69 70 if (attrs[i] == null) { 71 long n = in.skip(length); 72 if (n != length) { 73 throw new EOFException(); 74 } 75 } 76 } 77 } 78 79 boolean isSynthetic() { 80 return synthetic; 81 } 82 83 String name() { 84 return (String ) clazz.constants[this.name].value(); 85 } 86 87 MethodInstance methodInstance(TypeSystem ts, ClassType ct) { 88 String name = (String ) clazz.constants[this.name].value(); 89 String type = (String ) clazz.constants[this.type].value(); 90 91 if (type.charAt(0) != '(') { 92 throw new ClassFormatError ("Bad method type descriptor."); 93 } 94 95 int index = type.indexOf(')', 1); 96 List argTypes = clazz.typeListForString(ts, type.substring(1, index)); 97 Type returnType = clazz.typeForString(ts, type.substring(index+1)); 98 99 List excTypes = new ArrayList(); 100 101 if (exceptions != null) { 102 for (int i = 0; i < exceptions.exceptions.length; i++) { 103 String s = clazz.classNameCP(exceptions.exceptions[i]); 104 excTypes.add(clazz.quietTypeForName(ts, s)); 105 } 106 } 107 108 return ts.methodInstance(ct.position(), ct, 109 ts.flagsForBits(modifiers), returnType, name, 110 argTypes, excTypes); 111 } 112 113 ConstructorInstance constructorInstance(TypeSystem ts, ClassType ct, 114 Field[] fields) { 115 MethodInstance mi = methodInstance(ts, ct); 117 118 List formals = mi.formalTypes(); 119 120 if (ct.isInnerClass()) { 121 124 int numSynthetic = 0; 126 127 for (int i = 0; i < fields.length; i++) { 128 if (fields[i].isSynthetic()) { 129 numSynthetic++; 130 } 131 } 132 133 if (numSynthetic <= formals.size()) { 136 formals = formals.subList(numSynthetic, formals.size()); 137 } 138 } 139 140 return ts.constructorInstance(mi.position(), ct, 141 mi.flags(), formals, 142 mi.throwTypes()); 143 } 144 } 145 | Popular Tags |