001: /***
002: * ASM tests
003: * Copyright (c) 2002-2005 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 junit.framework.TestSuite;
032: import junit.textui.TestRunner;
033:
034: import org.objectweb.asm.AbstractTest;
035: import org.objectweb.asm.ClassAdapter;
036: import org.objectweb.asm.ClassReader;
037: import org.objectweb.asm.ClassVisitor;
038: import org.objectweb.asm.ClassWriter;
039: import org.objectweb.asm.MethodVisitor;
040: import org.objectweb.asm.Opcodes;
041:
042: /**
043: * AdviceAdapter tests.
044: *
045: * @author Eugene Kuleshov
046: */
047: public class AdviceAdapterTest extends AbstractTest {
048:
049: public static void main(final String[] args) throws Exception {
050: TestRunner.run(AdviceAdapterTest.suite());
051: }
052:
053: public static TestSuite suite() throws Exception {
054: return new AdviceAdapterTest().getSuite();
055: }
056:
057: public void test() throws Exception {
058: ClassReader cr = new ClassReader(is);
059: ClassWriter cw1 = new ClassWriter(0);
060: ClassWriter cw2 = new ClassWriter(0);
061: cr.accept(new ReferenceClassAdapter(cw1),
062: ClassReader.EXPAND_FRAMES);
063: cr.accept(new AdviceClassAdapter(cw2),
064: ClassReader.EXPAND_FRAMES);
065: assertEquals(new ClassReader(cw1.toByteArray()),
066: new ClassReader(cw2.toByteArray()));
067: }
068:
069: static class ReferenceClassAdapter extends ClassAdapter {
070:
071: public ReferenceClassAdapter(final ClassVisitor cv) {
072: super (cv);
073: }
074:
075: public MethodVisitor visitMethod(final int access,
076: final String name, final String desc,
077: final String signature, final String[] exceptions) {
078: MethodVisitor mv = cv.visitMethod(access, name, desc,
079: signature, exceptions);
080:
081: if (mv == null
082: || (access & (Opcodes.ACC_ABSTRACT | Opcodes.ACC_NATIVE)) > 0) {
083: return mv;
084: }
085:
086: return new LocalVariablesSorter(access, desc, mv);
087: }
088: }
089:
090: static class AdviceClassAdapter extends ClassAdapter {
091:
092: public AdviceClassAdapter(final ClassVisitor cv) {
093: super (cv);
094: }
095:
096: public MethodVisitor visitMethod(final int access,
097: final String name, final String desc,
098: final String signature, final String[] exceptions) {
099: MethodVisitor mv = cv.visitMethod(access, name, desc,
100: signature, exceptions);
101:
102: if (mv == null
103: || (access & (Opcodes.ACC_ABSTRACT | Opcodes.ACC_NATIVE)) > 0) {
104: return mv;
105: }
106:
107: return new AdviceAdapter(mv, access, name, desc) {
108: protected void onMethodEnter() {
109: // mv.visitInsn(NOP);
110: // mv.visitInsn(NOP);
111: // mv.visitInsn(NOP);
112: }
113:
114: protected void onMethodExit(final int opcode) {
115: // mv.visitInsn(NOP);
116: // mv.visitInsn(NOP);
117: // mv.visitInsn(NOP);
118: // mv.visitInsn(NOP);
119: }
120: };
121: }
122: }
123: }
|