KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > asm > attrs > StackMapTableAttributeTest


1 /***
2  * ASM tests
3  * Copyright (c) 2002-2005 France Telecom
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holders nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package org.objectweb.asm.attrs;
31
32 import java.io.PrintWriter JavaDoc;
33 import java.io.StringWriter JavaDoc;
34 import java.util.Arrays JavaDoc;
35
36 import org.objectweb.asm.AbstractTest;
37 import org.objectweb.asm.Attribute;
38 import org.objectweb.asm.ClassReader;
39 import org.objectweb.asm.ClassVisitor;
40 import org.objectweb.asm.ClassWriter;
41 import org.objectweb.asm.Label;
42 import org.objectweb.asm.util.TraceClassVisitor;
43 import org.objectweb.asm.util.TraceMethodVisitor;
44 import org.objectweb.asm.util.attrs.ASMStackMapAttribute;
45 import org.objectweb.asm.util.attrs.ASMStackMapTableAttribute;
46
47 /**
48  * StackMapTableAttributeTest
49  *
50  * @author Eugene Kuleshov
51  */

52 public class StackMapTableAttributeTest extends AbstractTest {
53
54     public static final String JavaDoc TEST_CLASS = "StackMapTableSample.data";
55
56     public StackMapTableAttributeTest() {
57         super();
58         is = getClass().getResourceAsStream(TEST_CLASS);
59     }
60
61     public void test() throws Exception JavaDoc {
62         Attribute[] tattributes = new Attribute[] { new StubStackMapTableAttribute() };
63         Attribute[] attributes = new Attribute[] { new ASMStackMapTableAttribute() };
64
65         ClassWriter cw = new ClassWriter(false);
66
67         TraceClassVisitor tv1 = new TraceClassVisitor(cw,
68                 new PrintWriter JavaDoc(System.err));
69         TraceClassVisitor tv2 = new TraceClassVisitor(cw,
70                 new PrintWriter JavaDoc(System.err))
71         {
72             protected TraceMethodVisitor createTraceMethodVisitor() {
73                 return new TraceMethodVisitor() {
74                     protected void appendLabel(Label l) {
75                         super.appendLabel(l);
76                         buf.append(" " + System.identityHashCode(l));
77                     }
78                 };
79             }
80         };
81
82         ClassReader cr1 = new ClassReader(is);
83         cr1.accept(cw, attributes, true);
84
85         ClassReader cr2 = new ClassReader(cw.toByteArray());
86
87         if (!Arrays.equals(cr1.b, cr2.b)) {
88             StringWriter JavaDoc sw1 = new StringWriter JavaDoc();
89             StringWriter JavaDoc sw2 = new StringWriter JavaDoc();
90             ClassVisitor cv1 = new TraceClassVisitor(new PrintWriter JavaDoc(sw1));
91             ClassVisitor cv2 = new TraceClassVisitor(new PrintWriter JavaDoc(sw2));
92             cr1.accept(cv1, attributes, true);
93             cr2.accept(cv2, attributes, true);
94             assertEquals("different data", sw1.toString(), sw2.toString());
95         }
96
97     }
98
99     private static final class StubStackMapTableAttribute extends Attribute {
100         private static String JavaDoc s = "0123456789ABCDEF";
101
102         private String JavaDoc data;
103
104         public StubStackMapTableAttribute() {
105             super("StackMapTable");
106         }
107
108         protected Attribute read(
109             ClassReader cr,
110             int off,
111             int len,
112             char[] buf,
113             int codeOff,
114             Label[] labels)
115         {
116             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
117             for (int i = 0; i < len; i++) {
118                 int b = cr.readByte(off + i);
119                 sb.append(s.charAt(b >> 4))
120                         .append(s.charAt(b & 0xF))
121                         .append(" ");
122             }
123             StubStackMapTableAttribute att = new StubStackMapTableAttribute();
124             att.data = sb.toString();
125             return att;
126         }
127
128         public String JavaDoc toString() {
129             return data;
130         }
131
132     }
133
134 }
135
Popular Tags