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 java.io.InputStream;
032: import java.io.PrintWriter;
033: import java.io.StringWriter;
034: import java.lang.instrument.ClassFileTransformer;
035: import java.lang.instrument.IllegalClassFormatException;
036: import java.lang.instrument.Instrumentation;
037: import java.security.ProtectionDomain;
038:
039: import junit.framework.TestSuite;
040:
041: import org.objectweb.asm.AbstractTest;
042: import org.objectweb.asm.ClassAdapter;
043: import org.objectweb.asm.ClassReader;
044: import org.objectweb.asm.ClassVisitor;
045: import org.objectweb.asm.ClassWriter;
046: import org.objectweb.asm.MethodVisitor;
047: import org.objectweb.asm.util.TraceClassVisitor;
048:
049: /**
050: * LocalVariablesSorter tests.
051: *
052: * @author Eric Bruneton
053: */
054: public class LocalVariablesSorterTest2 extends AbstractTest {
055:
056: public static void premain(final String agentArgs,
057: final Instrumentation inst) {
058: inst.addTransformer(new ClassFileTransformer() {
059: public byte[] transform(final ClassLoader loader,
060: final String className,
061: final Class classBeingRedefined,
062: final ProtectionDomain domain,
063: final byte[] classFileBuffer)
064: throws IllegalClassFormatException {
065: String n = className.replace('/', '.');
066: if (agentArgs.length() == 0
067: || n.indexOf(agentArgs) != -1) {
068: return transformClass(classFileBuffer);
069: } else {
070: return null;
071: }
072: }
073: });
074: }
075:
076: private static byte[] transformClass(final byte[] clazz) {
077: ClassReader cr = new ClassReader(clazz);
078: ClassWriter cw = new ClassWriter(0);
079: cr.accept(new ClassAdapter(cw) {
080:
081: public MethodVisitor visitMethod(final int access,
082: final String name, final String desc,
083: final String signature, final String[] exceptions) {
084: return new LocalVariablesSorter(access, desc, cv
085: .visitMethod(access, name, desc, signature,
086: exceptions));
087: }
088:
089: }, ClassReader.EXPAND_FRAMES);
090: return cw.toByteArray();
091: }
092:
093: public static TestSuite suite() throws Exception {
094: return new LocalVariablesSorterTest2().getSuite();
095: }
096:
097: public void test() throws Exception {
098: try {
099: Class.forName(n, true, getClass().getClassLoader());
100: } catch (NoClassDefFoundError ncdfe) {
101: // ignored
102: } catch (UnsatisfiedLinkError ule) {
103: // ignored
104: } catch (ClassFormatError cfe) {
105: fail(cfe.getMessage());
106: } catch (VerifyError ve) {
107: String s = n.replace('.', '/') + ".class";
108: InputStream is = getClass().getClassLoader()
109: .getResourceAsStream(s);
110: ClassReader cr = new ClassReader(is);
111: byte[] b = transformClass(cr.b);
112: StringWriter sw1 = new StringWriter();
113: StringWriter sw2 = new StringWriter();
114: sw2.write(ve.toString() + "\n");
115: ClassVisitor cv1 = new TraceClassVisitor(new PrintWriter(
116: sw1));
117: ClassVisitor cv2 = new TraceClassVisitor(new PrintWriter(
118: sw2));
119: cr.accept(cv1, 0);
120: new ClassReader(b).accept(cv2, 0);
121: String s1 = sw1.toString();
122: String s2 = sw2.toString();
123: assertEquals("different data", s1, s2);
124: }
125: }
126: }
|