KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jbet > Reflection


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;
32
33 /* ways of doing stuff easily with reflection */
34
35 public class Reflection
36 {
37     /* Make an array of java/lang/Class corresponding to the arguments in the descriptor. The array is
38        left at the top of stack. 4 stack slots are needed.
39     */

40
41     public static void descriptor2classes (Snippit out, Descriptor descriptor)
42     {
43     out.push (new Instruction().setIpush (descriptor.args.length));
44     out.push (new Instruction().setANewArray ("java/lang/Class"));
45     
46     for (int i = 0; i < descriptor.args.length; i++) {
47         out.push (new Instruction().setDup());
48         out.push (new Instruction().setIpush (i));
49         
50         Type arg = descriptor.args[i];
51         
52         if (arg.base == 'L' || arg.arraydepth > 0) {
53         out.push (new Instruction().setSpush (arg.toClassRef().replace ('/', '.')));
54         out.push (new Instruction().setInvokeStatic ("java/lang/Class", "forName",
55                                  new Descriptor (Type.STRING, Type.CLASS)));
56         } else
57         out.push (new Instruction().setGetstatic (arg.wrapperClass(), "TYPE", Type.CLASS));
58
59         out.push (new Instruction().setAastore ());
60     }
61     }
62
63     /* Make an array of java/lang/Object containing the elements on the stack corresponding to
64        the descriptor. This could probably be more efficient. Needs up to 6 extra stack slots.
65     */

66
67     public static void arguments2array (Snippit out, Descriptor descriptor)
68     {
69     out.push (new Instruction().setIpush (descriptor.args.length));
70     out.push (new Instruction().setANewArray ("java/lang/Object"));
71
72     for (int i = descriptor.args.length - 1; i >= 0; i--) {
73         Type arg = descriptor.args[i];
74
75         out.push (new Instruction().setDup_x (Type.OBJECT, arg));
76         if (arg.category() == 2) {
77         out.push (new Instruction().setDup_x2());
78         out.push (new Instruction().setPop());
79         }
80         else
81         out.push (new Instruction().setSwap());
82
83         if (arg.base != 'L' && arg.arraydepth == 0) {
84         out.push (new Instruction().setNew (arg.wrapperClass()));
85         out.push (new Instruction().setDup_x (Type.OBJECT, arg));
86         if (arg.category() == 2) {
87             out.push (new Instruction().setDup_x2());
88             out.push (new Instruction().setPop());
89         }
90         else
91             out.push (new Instruction().setSwap());
92         out.push (new Instruction().setInvokeSpecial (arg.wrapperClass(), "<init>", new Descriptor (arg, Type.VOID)));
93         }
94
95         out.push (new Instruction().setIpush (i));
96         out.push (new Instruction().setSwap());
97         out.push (new Instruction().setAastore());
98     }
99     }
100
101     /* convert a java/lang/Object on the stack into the specified type */
102
103     public static void getReturn (Snippit out, Type ret)
104     {
105     if (ret.base != 'V') {
106         if (ret.base != 'L' && ret.arraydepth == 0) {
107         out.push (new Instruction().setCheckcast (ret.wrapperClass()));
108         out.push (new Instruction().setInvokeVirtual (ret.wrapperClass(), ret.unpackMethod(),
109                                   new Descriptor (ret)));
110         } else
111         out.push (new Instruction().setCheckcast (ret));
112     } else
113         out.push (new Instruction().setPop());
114     }
115 }
116
Popular Tags