1 30 31 package jbet.cmd; 32 import jbet.*; 33 import java.util.*; 34 import java.io.*; 35 36 public class property implements Command { 37 38 public int helplevel() { return 3; } 39 public String shorthelp() { return "implement property"; } 40 public String longhelp() { return "jbet propterty <class> <class> ....\n"; } 41 42 43 static public void makeProperty(String s) { 44 return; 45 } 46 47 static boolean diditwork; 48 49 static { 50 makeProperty("diditwork"); 51 } 52 53 static boolean get__diditwork() { return true; } 54 static void set__diditwork(boolean f) {} 55 56 static public void main(String []args) { 57 if (diditwork) 58 System.out.println("Property ok"); 59 else 60 System.out.println("Property failed"); 61 } 62 63 public void run (Lexer lexer) throws Exception { 64 lexer.match(Token.END_OF_OPTS); 65 ClassInfo [] withprops = lexer.getClasses(); 66 lexer.match(':'); 67 ClassInfo [] usesprops = lexer.getClasses(); 68 lexer.term(); 69 70 run (withprops, usesprops); 71 } 72 73 public void run (ClassInfo[] withprops, ClassInfo[] usesprops) throws ClassFileException, ElementNotFoundException 74 { 75 Vector fieldsv = new Vector(); 76 77 for (int i = 0; i < withprops.length; i++) { 78 ClassInfo cr = withprops[i]; 79 MethodInfo clinit = cr.findMethod("<clinit>", (String ) null); 80 if (clinit == null || clinit.code == null) continue; 81 Instruction instr; 82 for (instr = clinit.code.first(); instr != null; instr = instr.next) { 83 if ( instr.opCode() == Instruction.AOP_SPUSH && 84 instr.next != null && 85 instr.next.opCode() == Instruction.OP_INVOKESTATIC && 86 instr.next.classRef().equals("jbet/cmd/property") && 87 instr.next.elemName().equals("makeProperty") && 88 instr.next.descriptor().equals("(Ljava/lang/String;)V") ){ 89 90 FieldInfo fi = cr.getField(instr.immediate_s()); 91 fieldsv.addElement(fi); 92 Jbet.info.println("property " + cr.name()+ "." +instr.immediate_s()); 94 97 } 99 } 100 } 101 102 final FieldInfo [] fields = new FieldInfo [ fieldsv.size() ]; 103 fieldsv.toArray (fields); 104 105 final Instruction [] reads = new Instruction [ fieldsv.size() ]; 106 final Instruction [] writes = new Instruction [ fieldsv.size() ]; 107 108 for (int i = 0; i < reads.length; i++) { 109 reads[i] = new Instruction().setInvoke 110 (fields[i].cr.name(), 111 "get__" + fields[i].name, 112 new Descriptor (fields[i].type), 113 fields[i].isStatic()); 114 writes[i] = new Instruction().setInvoke 115 (fields[i].cr.name(), 116 "set__" + fields[i].name, 117 new Descriptor (fields[i].type, Type.VOID), 118 fields[i].isStatic()); 119 } 120 121 class _util { 122 public _util() {} 123 public boolean isAccessMethod(MethodInfo mi, int k) { 124 FieldInfo fi = fields[k]; 125 Instruction read = reads[k]; 126 Instruction write = writes[k]; 127 if (mi.cr != fi.cr) return false; 128 if (mi.name.equals(read.elemName()) && 129 mi.descriptor.equals(read.descriptor())) return true; 130 if (mi.name.equals(write.elemName()) && 131 mi.descriptor.equals(write.descriptor())) return true; 132 return false; 133 } 134 }; 135 _util util = new _util(); 136 137 for (int i = 0; i < usesprops.length; i++) { 138 ClassInfo cr = usesprops[i]; 139 for (int j = 0; j < cr.numMethods(); j++) { 140 MethodInfo mi = cr.methodAt(j); 141 if (mi.code == null) continue; 142 Instruction instr = mi.code.first(); 143 INSTRLOOP: 144 for (; instr != null; instr = instr.next) { 145 if (instr.isGet()) 146 for (int k = 0; k < fields.length; k++) { 147 FieldInfo fi = fields[k]; 148 if (util.isAccessMethod(mi,k)) continue; 149 if ( fi.cr.name().equals(instr.classRef()) && 150 fi.name.equals(instr.elemName())) { 151 instr.copy( reads[k] ); 152 cr.setDirty(); 153 continue INSTRLOOP; 154 } 155 } 156 else if (instr.isPut()) 157 for (int k = 0; k < fields.length; k++) { 158 FieldInfo fi = fields[k]; 159 if (util.isAccessMethod(mi,k)) continue; 160 if ( fi.cr.name().equals(instr.classRef()) && 161 fi.name.equals(instr.elemName())) { 162 instr.copy( writes[k] ); 163 cr.setDirty(); 164 continue INSTRLOOP; 165 } 166 } 167 } 168 } 169 } 170 Jbet.info.println("Done property"); 171 } 172 173 } 174 175 176 177 178 179 180 | Popular Tags |