KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > common > enhancer > JCheckClassAdapter


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: JCheckClassAdapter.java 19 2006-02-22 13:44:17Z studzine $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.tests.common.enhancer;
27
28 import java.util.List JavaDoc;
29
30 import org.objectweb.asm.ClassReader;
31 import org.objectweb.asm.ClassVisitor;
32 import org.objectweb.asm.Opcodes;
33 import org.objectweb.asm.Type;
34 import org.objectweb.asm.tree.AbstractInsnNode;
35 import org.objectweb.asm.tree.ClassNode;
36 import org.objectweb.asm.tree.MethodNode;
37 import org.objectweb.asm.tree.analysis.Analyzer;
38 import org.objectweb.asm.tree.analysis.AnalyzerException;
39 import org.objectweb.asm.tree.analysis.Frame;
40 import org.objectweb.asm.tree.analysis.SimpleVerifier;
41 import org.objectweb.asm.util.CheckClassAdapter;
42 import org.objectweb.asm.util.TraceMethodVisitor;
43
44 /**
45  * Checks a class.
46  * @author Florent Benoit
47  */

48 public class JCheckClassAdapter extends CheckClassAdapter {
49
50     /**
51      * Start of int (debug traces).
52      */

53     private static final int START_INT = 100000;
54
55     /**
56      * Constructs a new JCheckClassAdapter.
57      * @param cv the class visitor to which this adapter must delegate calls.
58      */

59     public JCheckClassAdapter(final ClassVisitor cv) {
60         super(cv);
61     }
62
63     /**
64      * Check a set of bytes.
65      * @param bytes representing a class.
66      * @throws AnalyzerException if class is incorrect.
67      */

68     public static void checkClass(final byte[] bytes) throws AnalyzerException {
69         ClassReader cr = new ClassReader(bytes);
70         ClassNode cn = new ClassNode();
71         cr.accept(new CheckClassAdapter(cn), true);
72         List JavaDoc methods = cn.methods;
73         for (int i = 0; i < methods.size(); ++i) {
74             MethodNode method = (MethodNode) methods.get(i);
75             if (method.instructions.size() > 0) {
76                 Analyzer a = new Analyzer(new SimpleVerifier(Type.getType("L" + cn.name + ";"), Type.getType("L" + cn.superName
77                         + ";"), (cn.access & Opcodes.ACC_INTERFACE) != 0));
78                 AnalyzerException throwE = null;
79                 try {
80                     a.analyze(cn.name, method);
81                     continue;
82                 } catch (AnalyzerException e) {
83                     throwE = e;
84                 }
85                 final Frame[] frames = a.getFrames();
86
87                 if (throwE != null) {
88                     System.out.println(method.name + method.desc);
89
90                     TraceMethodVisitor mv = new TraceMethodVisitor() {
91
92                         @Override JavaDoc
93                         public void visitMaxs(final int maxStack, final int maxLocals) {
94                             for (int i = 0; i < text.size(); ++i) {
95                                 String JavaDoc s;
96                                 if (frames[i] == null) {
97                                         s = "null";
98                                 } else {
99                                     s = frames[i].toString();
100                                 }
101                                 while (s.length() < maxStack + maxLocals + 1) {
102                                     s += " ";
103                                 }
104                                 System.out.print(Integer.toString(i + START_INT).substring(1));
105                                 System.out.print(" " + s + " : " + text.get(i));
106                             }
107                             System.out.println();
108                         }
109                     };
110                     for (int j = 0; j < method.instructions.size(); ++j) {
111                         ((AbstractInsnNode) method.instructions.get(j)).accept(mv);
112                     }
113                     mv.visitMaxs(method.maxStack, method.maxLocals);
114                     throw throwE;
115                 }
116             }
117         }
118     }
119
120 }
121
Popular Tags