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.CHECKCAST;
034: import static org.objectweb.asm.Opcodes.INVOKEVIRTUAL;
035: import static org.objectweb.asm.Opcodes.IRETURN;
036:
037: import org.objectweb.asm.ClassAdapter;
038: import org.objectweb.asm.ClassVisitor;
039: import org.objectweb.asm.MethodVisitor;
040: import org.objectweb.asm.commons.AnalyzerAdapter;
041: import org.objectweb.asm.util.TraceMethodVisitor;
042:
043: import util.AbstractTestCase;
044:
045: /**
046: * ASM Guide example test class.
047: *
048: * @author Eric Bruneton
049: */
050: public class RemoveUnusedCastAdapterTest extends AbstractTestCase {
051:
052: public void test() {
053: TraceMethodVisitor tmv = new TraceMethodVisitor(null);
054: RemoveUnusedCastAdapter rc = new RemoveUnusedCastAdapter(tmv);
055: AnalyzerAdapter aa = new AnalyzerAdapter("C", ACC_PUBLIC, "m",
056: "(Ljava/lang/Integer;)I", rc);
057: rc.aa = aa;
058: aa.visitCode();
059: aa.visitVarInsn(ALOAD, 1);
060: aa.visitTypeInsn(CHECKCAST, "java/lang/Number");
061: aa.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Number",
062: "intValue", "()I");
063: aa.visitInsn(IRETURN);
064: aa.visitMaxs(1, 2);
065: aa.visitEnd();
066: checkMethod(tmv);
067: }
068:
069: protected void checkMethod(TraceMethodVisitor tmv) {
070: TraceMethodVisitor mv = new TraceMethodVisitor(null);
071: mv.visitCode();
072: mv.visitVarInsn(ALOAD, 1);
073: mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Number",
074: "intValue", "()I");
075: mv.visitInsn(IRETURN);
076: mv.visitMaxs(1, 2);
077: mv.visitEnd();
078: assertEquals(mv, tmv);
079: }
080:
081: @Override
082: protected ClassVisitor getClassAdapter(final ClassVisitor cv) {
083: return new ClassAdapter(cv) {
084: private String owner;
085:
086: @Override
087: public void visit(int version, int access, String name,
088: String signature, String super Name,
089: String[] interfaces) {
090: cv.visit(version, access, name, signature, super Name,
091: interfaces);
092: owner = name;
093: }
094:
095: @Override
096: public MethodVisitor visitMethod(int access, String name,
097: String desc, String signature, String[] exceptions) {
098: RemoveUnusedCastAdapter rc = new RemoveUnusedCastAdapter(
099: cv.visitMethod(access, name, desc, signature,
100: exceptions));
101: AnalyzerAdapter aa = new AnalyzerAdapter(owner, access,
102: name, desc, rc);
103: rc.aa = aa;
104: return aa;
105: }
106: };
107: }
108: }
|