KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jbet > cmd > property


1 /*
2  * JBET - Java Binary Enhancement Tool
3  * Copyright (c) 2003 Networks Associates Technology, Inc.
4  *
5  * This software was developed under DARPA/SPAWAR contract
6  * N66001-00-C-8602 "SPMA" as part of the
7  * DARPA OASIS research program.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */

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 JavaDoc shorthelp() { return "implement property"; }
40     public String JavaDoc longhelp() { return "jbet propterty <class> <class> ....\n"; }
41     
42
43     static public void makeProperty(String JavaDoc 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 JavaDoc []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 JavaDoc {
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 JavaDoc) 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             //cr.removeField(fi);
93
Jbet.info.println("property " + cr.name()+ "." +instr.immediate_s());
94             /*Instruction instrdotnext = instr.next;
95             clinit.code.remove(instr);
96             clinit.code.remove(instrdotnext); */

97             //cr.setDirty();
98
}
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