KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Attributes


1 /***
2  * ASM examples: examples showing how ASM can be used
3  * Copyright (c) 2000-2005 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 import org.objectweb.asm.Attribute;
31 import org.objectweb.asm.FieldVisitor;
32 import org.objectweb.asm.ClassAdapter;
33 import org.objectweb.asm.ClassReader;
34 import org.objectweb.asm.ClassVisitor;
35 import org.objectweb.asm.ClassWriter;
36 import org.objectweb.asm.MethodVisitor;
37 import org.objectweb.asm.Opcodes;
38 import org.objectweb.asm.Label;
39 import org.objectweb.asm.ByteVector;
40 import org.objectweb.asm.util.TraceClassVisitor;
41
42 import java.io.FileOutputStream JavaDoc;
43 import java.io.PrintWriter JavaDoc;
44
45 /**
46  * @author Eric Bruneton
47  */

48 public class Attributes extends ClassLoader JavaDoc {
49
50     public static void main(final String JavaDoc args[]) throws Exception JavaDoc {
51         // loads the original class and adapts it
52
ClassReader cr = new ClassReader("CommentAttribute");
53         ClassWriter cw = new ClassWriter(0);
54         ClassVisitor cv = new AddCommentClassAdapter(cw);
55         cr.accept(cv, new Attribute[] { new CommentAttribute("") }, 0);
56         byte[] b = cw.toByteArray();
57
58         // stores the adapted class on disk
59
FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc("CommentAttribute.class.new");
60         fos.write(b);
61         fos.close();
62
63         // "disassembles" the adapted class
64
cr = new ClassReader(b);
65         cv = new TraceClassVisitor(new PrintWriter JavaDoc(System.out));
66         cr.accept(cv, new Attribute[] { new CommentAttribute("") }, 0);
67     }
68 }
69
70 class AddCommentClassAdapter extends ClassAdapter implements Opcodes {
71
72     public AddCommentClassAdapter(final ClassVisitor cv) {
73         super(cv);
74     }
75
76     public void visit(
77         final int version,
78         final int access,
79         final String JavaDoc name,
80         final String JavaDoc signature,
81         final String JavaDoc superName,
82         final String JavaDoc[] interfaces)
83     {
84         super.visit(version, access, name, signature, superName, interfaces);
85         visitAttribute(new CommentAttribute("this is a class comment"));
86     }
87
88     public FieldVisitor visitField(
89         final int access,
90         final String JavaDoc name,
91         final String JavaDoc desc,
92         final String JavaDoc signature,
93         final Object JavaDoc value)
94     {
95         FieldVisitor fv = super.visitField(access, name, desc, signature, value);
96         fv.visitAttribute(new CommentAttribute("this is a field comment"));
97         return fv;
98     }
99
100     public MethodVisitor visitMethod(
101         final int access,
102         final String JavaDoc name,
103         final String JavaDoc desc,
104         final String JavaDoc signature,
105         final String JavaDoc[] exceptions)
106     {
107         MethodVisitor mv = cv.visitMethod(access,
108                 name,
109                 desc,
110                 signature,
111                 exceptions);
112         if (mv != null) {
113             mv.visitAttribute(new CommentAttribute("this is a method comment"));
114         }
115         return mv;
116     }
117 }
118
119 class CommentAttribute extends Attribute {
120
121     private String JavaDoc comment;
122
123     public CommentAttribute(final String JavaDoc comment) {
124         super("Comment");
125         this.comment = comment;
126     }
127
128     public String JavaDoc getComment() {
129         return comment;
130     }
131
132     public boolean isUnknown() {
133         return false;
134     }
135
136     protected Attribute read(
137         final ClassReader cr,
138         final int off,
139         final int len,
140         final char[] buf,
141         final int codeOff,
142         final Label[] labels)
143     {
144         return new CommentAttribute(cr.readUTF8(off, buf));
145     }
146
147     protected ByteVector write(
148         final ClassWriter cw,
149         final byte[] code,
150         final int len,
151         final int maxStack,
152         final int maxLocals)
153     {
154         return new ByteVector().putShort(cw.newUTF8(comment));
155     }
156 }
157
Popular Tags