001: /***
002: * ASM Guide
003: * Copyright (c) 2007 Eric Bruneton
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 ch7.sec2;
030:
031: import static org.objectweb.asm.Opcodes.ACC_PUBLIC;
032: import static org.objectweb.asm.Opcodes.ALOAD;
033: import static org.objectweb.asm.Opcodes.ATHROW;
034: import static org.objectweb.asm.Opcodes.DUP;
035: import static org.objectweb.asm.Opcodes.F_SAME;
036: import static org.objectweb.asm.Opcodes.IFLT;
037: import static org.objectweb.asm.Opcodes.ILOAD;
038: import static org.objectweb.asm.Opcodes.INVOKESPECIAL;
039: import static org.objectweb.asm.Opcodes.NEW;
040: import static org.objectweb.asm.Opcodes.PUTFIELD;
041: import static org.objectweb.asm.Opcodes.RETURN;
042:
043: import org.objectweb.asm.ClassAdapter;
044: import org.objectweb.asm.ClassVisitor;
045: import org.objectweb.asm.Label;
046: import org.objectweb.asm.MethodVisitor;
047: import org.objectweb.asm.util.TraceMethodVisitor;
048:
049: import util.AbstractTestCase;
050:
051: /**
052: * ASM Guide example test class.
053: *
054: * @author Eric Bruneton
055: */
056: public class RemoveDeadCodeAdapterTest extends AbstractTestCase {
057:
058: public void test() {
059: TraceMethodVisitor tmv = new TraceMethodVisitor(null);
060: RemoveDeadCodeAdapter mv = new RemoveDeadCodeAdapter(
061: "pkg/Bean", ACC_PUBLIC, "checkAndSetF", "(I)V", tmv);
062: mv.visitCode();
063: mv.visitVarInsn(ILOAD, 1);
064: Label label = new Label();
065: mv.visitJumpInsn(IFLT, label);
066: mv.visitVarInsn(ALOAD, 0);
067: mv.visitVarInsn(ILOAD, 1);
068: mv.visitFieldInsn(PUTFIELD, "pkg/Bean", "f", "I");
069: Label end = new Label();
070: mv.visitInsn(RETURN);
071: mv.visitLabel(label);
072: mv.visitFrame(F_SAME, 0, null, 0, null);
073: mv.visitTypeInsn(NEW, "java/lang/IllegalArgumentException");
074: mv.visitInsn(DUP);
075: mv.visitMethodInsn(INVOKESPECIAL,
076: "java/lang/IllegalArgumentException", "<init>", "()V");
077: mv.visitInsn(ATHROW);
078: mv.visitLabel(end);
079: mv.visitFrame(F_SAME, 0, null, 0, null);
080: mv.visitInsn(RETURN);
081: mv.visitMaxs(2, 2);
082: mv.visitEnd();
083: checkMethod(tmv);
084: }
085:
086: protected void checkMethod(TraceMethodVisitor tmv) {
087: TraceMethodVisitor mv = new TraceMethodVisitor(null);
088: mv.visitCode();
089: mv.visitVarInsn(ILOAD, 1);
090: Label label = new Label();
091: mv.visitJumpInsn(IFLT, label);
092: mv.visitVarInsn(ALOAD, 0);
093: mv.visitVarInsn(ILOAD, 1);
094: mv.visitFieldInsn(PUTFIELD, "pkg/Bean", "f", "I");
095: mv.visitInsn(RETURN);
096: mv.visitLabel(label);
097: mv.visitFrame(F_SAME, 0, null, 0, null);
098: mv.visitTypeInsn(NEW, "java/lang/IllegalArgumentException");
099: mv.visitInsn(DUP);
100: mv.visitMethodInsn(INVOKESPECIAL,
101: "java/lang/IllegalArgumentException", "<init>", "()V");
102: mv.visitInsn(ATHROW);
103: mv.visitLabel(new Label());
104: mv.visitMaxs(2, 2);
105: mv.visitEnd();
106: assertEquals(mv, tmv);
107: }
108:
109: @Override
110: protected ClassVisitor getClassAdapter(final ClassVisitor cv) {
111: return new ClassAdapter(cv) {
112: private String owner;
113:
114: @Override
115: public void visit(int version, int access, String name,
116: String signature, String super Name,
117: String[] interfaces) {
118: cv.visit(version, access, name, signature, super Name,
119: interfaces);
120: owner = name;
121: }
122:
123: @Override
124: public MethodVisitor visitMethod(int access, String name,
125: String desc, String signature, String[] exceptions) {
126: return new RemoveDeadCodeAdapter(owner, access, name,
127: desc, cv.visitMethod(access, name, desc,
128: signature, exceptions));
129: }
130: };
131: }
132: }
|