KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > cglib > transform > DumpFieldsTask


1 /*
2  * Copyright 2003 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package net.sf.cglib.transform;
17
18
19 import java.io.*;
20
21 import org.apache.tools.ant.BuildException;
22
23 import org.objectweb.asm.ClassReader;
24
25 import org.objectweb.asm.ClassVisitor;
26
27 import org.objectweb.asm.CodeVisitor;
28
29 import org.objectweb.asm.Attribute;
30
31
32 public class DumpFieldsTask extends AbstractProcessTask {
33
34     private File outfile;
35
36     private PrintStream out;
37
38
39
40     public void setOutputFile(File outfile) {
41
42         this.outfile = outfile;
43
44     }
45
46     
47
48     public void execute() throws BuildException {
49
50         try {
51
52             out = new PrintStream(new FileOutputStream(outfile));
53             try{
54                 
55              super.execute();
56              
57             }finally{
58                out.close();
59             }
60
61         } catch (IOException e) {
62
63             throw new BuildException(e);
64
65         }
66
67     }
68
69
70
71     protected void processFile(File file) throws Exception JavaDoc {
72
73         InputStream in = new BufferedInputStream(new FileInputStream(file));
74
75         ClassReader r = new ClassReader(in);
76
77         r.accept(new ClassVisitor() {
78
79             private String JavaDoc className;
80
81
82
83             public void visitEnd() { }
84
85             public void visitInnerClass(String JavaDoc name, String JavaDoc outerName, String JavaDoc innerName, int access) { }
86
87             public void visitAttribute(Attribute attrs) { }
88
89
90
91             public CodeVisitor visitMethod(int access, String JavaDoc name, String JavaDoc desc, String JavaDoc[] exceptions, Attribute attrs) {
92
93                 return null;
94
95             }
96
97
98
99             public void visit(int version, int access, String JavaDoc name, String JavaDoc superName, String JavaDoc[] interfaces, String JavaDoc sourceFile) {
100
101                 className = name.replace('/', '.');
102
103             }
104
105
106
107             public void visitField(int access, String JavaDoc name, String JavaDoc desc, Object JavaDoc value, Attribute attrs) {
108
109                 out.println("class=" + className + ", field=" + name);
110
111             }
112
113         }, true);
114
115     }
116
117 }
118
119
Popular Tags