001: /***
002: * ASM: a very small and fast Java bytecode manipulation framework
003: * Copyright (c) 2000-2007 INRIA, France Telecom
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: * 2. Redistributions in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in the
013: * documentation and/or other materials provided with the distribution.
014: * 3. Neither the name of the copyright holders nor the names of its
015: * contributors may be used to endorse or promote products derived from
016: * this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
028: * THE POSSIBILITY OF SUCH DAMAGE.
029: */package org.objectweb.asm.commons;
030:
031: import java.util.Arrays;
032: import java.util.Collections;
033: import java.util.HashMap;
034: import java.util.Iterator;
035: import java.util.Map;
036:
037: import junit.framework.TestCase;
038:
039: import org.objectweb.asm.ClassVisitor;
040: import org.objectweb.asm.Label;
041: import org.objectweb.asm.MethodVisitor;
042: import org.objectweb.asm.Opcodes;
043: import org.objectweb.asm.Type;
044: import org.objectweb.asm.tree.ClassNode;
045: import org.objectweb.asm.tree.FieldInsnNode;
046: import org.objectweb.asm.tree.FieldNode;
047: import org.objectweb.asm.tree.FrameNode;
048: import org.objectweb.asm.tree.InnerClassNode;
049: import org.objectweb.asm.tree.LdcInsnNode;
050: import org.objectweb.asm.tree.MethodInsnNode;
051: import org.objectweb.asm.tree.MethodNode;
052: import org.objectweb.asm.tree.MultiANewArrayInsnNode;
053: import org.objectweb.asm.tree.TryCatchBlockNode;
054: import org.objectweb.asm.tree.TypeInsnNode;
055:
056: public class RemappingClassAdapterTest extends TestCase implements
057: Opcodes {
058:
059: public void testRemappingClassAdapter() throws Exception {
060: Map map = new HashMap();
061: map.put("Boo", "B1");
062: map.put("Coo", "C1");
063: map.put("Doo", "D1");
064: Remapper remapper = new SimpleRemapper(map);
065:
066: ClassNode cn = new ClassNode();
067: dump(new RemappingClassAdapter(cn, remapper));
068:
069: assertEquals("D1", cn.name);
070: assertEquals("B1", cn.super Name);
071: assertEquals(Arrays.asList(new String[] { "I", "I", "C1", "J",
072: "B1" }), cn.interfaces);
073:
074: assertEquals("LB1;", field(cn, 0).desc);
075: assertEquals("[LB1;", field(cn, 1).desc);
076:
077: assertEquals("D1", innerClass(cn, 0).name);
078: assertEquals("B1", innerClass(cn, 0).outerName);
079: // assertEquals("Doo", innerClass(cn, 0).innerName);
080:
081: assertEquals("B1", cn.outerClass);
082: assertEquals("([[LB1;LC1;LD1;)LC1;", cn.outerMethodDesc);
083:
084: MethodNode mn0 = (MethodNode) cn.methods.get(0);
085: Iterator it = mn0.instructions.iterator();
086:
087: FieldInsnNode n0 = (FieldInsnNode) it.next();
088: assertEquals("D1", n0.owner);
089: assertEquals("LB1;", n0.desc);
090:
091: assertEquals(Type.getType("LB1;"),
092: ((LdcInsnNode) it.next()).cst);
093: assertEquals(Type.getType("[LD1;"),
094: ((LdcInsnNode) it.next()).cst);
095: assertEquals(Type.getType("[I"), ((LdcInsnNode) it.next()).cst);
096: assertEquals(Type.getType("J"), ((LdcInsnNode) it.next()).cst);
097:
098: assertEquals("B1", ((TypeInsnNode) it.next()).desc);
099: assertEquals("[LD1;", ((TypeInsnNode) it.next()).desc);
100: assertEquals("[I", ((TypeInsnNode) it.next()).desc);
101: assertEquals("J", ((TypeInsnNode) it.next()).desc);
102:
103: MultiANewArrayInsnNode n3 = (MultiANewArrayInsnNode) it.next();
104: assertEquals("[[LB1;", n3.desc);
105:
106: MethodInsnNode n4 = (MethodInsnNode) it.next();
107: assertEquals("D1", n4.owner);
108: assertEquals("([[LB1;LC1;LD1;)LC1;", n4.desc);
109:
110: FrameNode fn0 = (FrameNode) it.next();
111: assertEquals(Collections.EMPTY_LIST, fn0.local);
112: assertEquals(Collections.EMPTY_LIST, fn0.stack);
113:
114: assertEquals(Arrays.asList(new Object[] { "B1", "C1", "D1" }),
115: ((FrameNode) it.next()).local);
116: assertEquals(Arrays.asList(new Object[] { Opcodes.INTEGER,
117: "C1", Opcodes.INTEGER, "D1" }),
118: ((FrameNode) it.next()).local);
119: assertEquals(Arrays.asList(new Object[] { Opcodes.INTEGER,
120: Opcodes.INTEGER }), ((FrameNode) it.next()).local);
121: // assertEquals(Collections.EMPTY_LIST, fn0.stack);
122:
123: TryCatchBlockNode tryCatchBlockNode = (TryCatchBlockNode) mn0.tryCatchBlocks
124: .get(0);
125: assertEquals("C1", tryCatchBlockNode.type);
126:
127: MethodNode mn1 = (MethodNode) cn.methods.get(1);
128: assertEquals("([[LB1;LC1;LD1;)V", mn1.desc);
129: assertEquals(Arrays.asList(new String[] { "I", "J" }),
130: mn1.exceptions);
131: }
132:
133: private FieldNode field(ClassNode cn, int n) {
134: return (FieldNode) cn.fields.get(n);
135: }
136:
137: private InnerClassNode innerClass(ClassNode cn, int n) {
138: return (InnerClassNode) cn.innerClasses.get(n);
139: }
140:
141: public static void dump(ClassVisitor cv) throws Exception {
142: cv.visit(V1_5, 0, "Doo", null, "Boo", new String[] { "I", "I",
143: "Coo", "J", "Boo" });
144:
145: cv.visitInnerClass("Doo", "Boo", "Doo", 0);
146:
147: cv.visitOuterClass("Boo", "foo", "([[LBoo;LCoo;LDoo;)LCoo;");
148:
149: cv.visitField(0, "boo", "LBoo;", null, null).visitEnd();
150: cv.visitField(0, "boo1", "[LBoo;", null, null).visitEnd();
151: cv.visitField(0, "s", "Ljava/lang/String;", null, null)
152: .visitEnd();
153: cv.visitField(0, "i", "I", null, null).visitEnd();
154:
155: MethodVisitor mv;
156:
157: mv = cv.visitMethod(0, "foo", "()V", null, null);
158: mv.visitCode();
159: mv.visitFieldInsn(GETFIELD, "Doo", "boo", "LBoo;");
160:
161: mv.visitLdcInsn(Type.getType("LBoo;"));
162: mv.visitLdcInsn(Type.getType("[LDoo;"));
163: mv.visitLdcInsn(Type.getType("[I"));
164: mv.visitLdcInsn(Type.getType("J"));
165:
166: mv.visitTypeInsn(ANEWARRAY, "Boo");
167: mv.visitTypeInsn(ANEWARRAY, "[LDoo;");
168: mv.visitTypeInsn(ANEWARRAY, "[I");
169: mv.visitTypeInsn(ANEWARRAY, "J");
170:
171: mv.visitMultiANewArrayInsn("[[LBoo;", 2);
172: mv.visitMethodInsn(INVOKEVIRTUAL, "Doo", "goo",
173: "([[LBoo;LCoo;LDoo;)LCoo;");
174:
175: mv.visitFrame(Opcodes.F_NEW, 0, new Object[5], 0,
176: new Object[10]);
177: mv.visitFrame(Opcodes.F_NEW, 3, new Object[] { "Boo", "Coo",
178: "Doo" }, 0, new Object[0]);
179: mv.visitFrame(Opcodes.F_NEW, 4, new Object[] { Opcodes.INTEGER,
180: "Coo", Opcodes.INTEGER, "Doo" }, 0, new Object[0]);
181: mv.visitFrame(Opcodes.F_NEW, 2, new Object[] { Opcodes.INTEGER,
182: Opcodes.INTEGER }, 0, new Object[0]);
183:
184: Label l = new Label();
185:
186: mv.visitLocalVariable("boo", "LBoo;", null, l, l, 1);
187: mv.visitLocalVariable("boo1", "[LBoo;", null, l, l, 3);
188: mv.visitLocalVariable("boo2", "[[LBoo;", null, l, l, 4);
189: mv.visitMaxs(0, 0);
190:
191: mv.visitTryCatchBlock(l, l, l, "Coo");
192:
193: mv.visitEnd();
194:
195: mv = cv.visitMethod(0, "goo", "([[LBoo;LCoo;LDoo;)V", null,
196: new String[] { "I", "J" });
197: mv.visitEnd();
198:
199: cv.visitEnd();
200: }
201:
202: // /*
203: public static class Boo {
204: }
205:
206: public static interface Coo {
207: }
208:
209: public static class Doo extends Boo implements Coo {
210: Boo boo = new Boo();
211: Boo[] boo1 = new Boo[2];
212: String s = "";
213: int i = 5;
214:
215: static final Class c1 = Boo.class;
216: static final Class c2 = Boo[].class;
217:
218: public Doo() {
219: }
220:
221: public Doo(int i, Coo coo, Boo boo) {
222: }
223:
224: void foo() {
225: class Eoo {
226: String s;
227: }
228:
229: Eoo e = new Eoo();
230: e.s = "aaa";
231:
232: // visitFieldInsn(int, String, String, String)
233: // visitLocalVariable(String, String, String, Label, Label, int)
234: Boo boo = this .boo;
235:
236: // visitLdcInsn(Object)
237: Class cc = Boo.class;
238:
239: // visitTypeInsn(int, String)
240: Boo[] boo1 = new Boo[2];
241:
242: // visitMultiANewArrayInsn(String, int)
243: Boo[][] boo2 = new Boo[2][2];
244:
245: // visitMethodInsn(int, String, String, String)
246: goo(boo2, this , this );
247: }
248:
249: Coo goo(Boo[][] boo2, Coo coo, Doo doo) {
250: return null;
251: }
252: }
253: // */
254: }
|