KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > asm > tree > TreeClassAdapter


1 /***
2  * ASM: a very small and fast Java bytecode manipulation framework
3  * Copyright (c) 2000,2002,2003 INRIA, 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  * Contact: Eric.Bruneton@rd.francetelecom.com
31  *
32  * Author: Eric Bruneton
33  */

34
35 package org.logicalcobwebs.asm.tree;
36
37 import org.logicalcobwebs.asm.ClassAdapter;
38 import org.logicalcobwebs.asm.CodeVisitor;
39 import org.logicalcobwebs.asm.ClassVisitor;
40 import org.logicalcobwebs.asm.Attribute;
41
42 /**
43  * A {@link ClassAdapter ClassAdapter} that constructs a tree representation of
44  * the classes it vists. Each <tt>visit</tt><i>XXX</i> method of this class
45  * constructs an <i>XXX</i><tt>Node</tt> and adds it to the {@link #classNode
46  * classNode} node (except the {@link #visitEnd visitEnd} method, which just
47  * makes the {@link #cv cv} class visitor visit the tree that has just been
48  * constructed).
49  * <p>
50  * In order to implement a usefull class adapter based on a tree representation
51  * of classes, one just need to override the {@link #visitEnd visitEnd} method
52  * with a method of the following form:
53  * <pre>
54  * public void visitEnd () {
55  * // ...
56  * // code to modify the classNode tree, can be arbitrary complex
57  * // ...
58  * // makes the cv visitor visit this modified class:
59  * classNode.accept(cv);
60  * }
61  * </pre>
62  */

63
64 public class TreeClassAdapter extends ClassAdapter {
65
66   /**
67    * A tree representation of the class that is being visited by this visitor.
68    */

69
70   public ClassNode classNode;
71
72   /**
73    * Constructs a new {@link TreeClassAdapter TreeClassAdapter} object.
74    *
75    * @param cv the class visitor to which this adapter must delegate calls.
76    */

77
78   public TreeClassAdapter (final ClassVisitor cv) {
79     super(cv);
80   }
81
82   public void visit (
83     final int access,
84     final String JavaDoc name,
85     final String JavaDoc superName,
86     final String JavaDoc[] interfaces,
87     final String JavaDoc sourceFile)
88   {
89     classNode = new ClassNode(access, name, superName, interfaces, sourceFile);
90   }
91
92   public void visitInnerClass (
93     final String JavaDoc name,
94     final String JavaDoc outerName,
95     final String JavaDoc innerName,
96     final int access)
97   {
98     InnerClassNode icn = new InnerClassNode(name, outerName, innerName, access);
99     classNode.innerClasses.add(icn);
100   }
101
102   public void visitField (
103     final int access,
104     final String JavaDoc name,
105     final String JavaDoc desc,
106     final Object JavaDoc value,
107     final Attribute attrs)
108   {
109     FieldNode fn = new FieldNode(access, name, desc, value, attrs);
110     classNode.fields.add(fn);
111   }
112
113   public CodeVisitor visitMethod (
114     final int access,
115     final String JavaDoc name,
116     final String JavaDoc desc,
117     final String JavaDoc[] exceptions,
118     final Attribute attrs)
119   {
120     MethodNode mn = new MethodNode(access, name, desc, exceptions, attrs);
121     classNode.methods.add(mn);
122     return new TreeCodeAdapter(mn);
123   }
124
125   public void visitAttribute (final Attribute attr) {
126     attr.next = classNode.attrs;
127     classNode.attrs = attr;
128   }
129
130   public void visitEnd () {
131     classNode.accept(cv);
132   }
133 }
134
Popular Tags