KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > dependencyfinder > ant > ListSymbols


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

32
33 package com.jeantessier.dependencyfinder.ant;
34
35 import java.io.*;
36 import java.util.*;
37
38 import org.apache.tools.ant.*;
39 import org.apache.tools.ant.types.*;
40
41 import com.jeantessier.classreader.*;
42 import com.jeantessier.dependencyfinder.*;
43
44 public class ListSymbols extends Task {
45     private boolean classNames = false;
46     private boolean fieldNames = false;
47     private boolean methodNames = false;
48     private boolean localNames = false;
49     private File destfile;
50     private Path path;
51
52     public boolean getClassnames() {
53         return classNames;
54     }
55     
56     public void setClassnames(boolean classNames) {
57         this.classNames = classNames;
58     }
59
60     public boolean getFieldnames() {
61         return fieldNames;
62     }
63     
64     public void setFieldnames(boolean fieldNames) {
65         this.fieldNames = fieldNames;
66     }
67
68     public boolean getMethodnames() {
69         return methodNames;
70     }
71     
72     public void setMethodnames(boolean methodNames) {
73         this.methodNames = methodNames;
74     }
75
76     public boolean getLocalnames() {
77         return localNames;
78     }
79     
80     public void setLocalnames(boolean localNames) {
81         this.localNames = localNames;
82     }
83
84     public File getDestfile() {
85         return destfile;
86     }
87     
88     public void setDestfile(File destfile) {
89         this.destfile = destfile;
90     }
91     
92     public Path createPath() {
93         if (path == null) {
94             path = new Path(getProject());
95         }
96
97         return path;
98     }
99     
100     public Path getPath() {
101         return path;
102     }
103     
104     public void execute() throws BuildException {
105         // first off, make sure that we've got what we need
106

107         if (getPath() == null) {
108             throw new BuildException("path must be set!");
109         }
110
111         if (getDestfile() == null) {
112             throw new BuildException("destfile must be set!");
113         }
114
115         log("Reading classes from path " + getPath());
116
117         VerboseListener verboseListener = new VerboseListener(this);
118
119         SymbolGatherer collector = new SymbolGatherer();
120
121         // Since SymbolGatherer lists everything by default,
122
// we turn them all off if any of the switches are
123
// present. This way, if you pass nothing, you get
124
// the default behavior and the tool shows everything.
125
// If you pass in one or more, you only see symbols
126
// of the kind(s) you specified.
127
if (getClassnames() || getFieldnames() || getMethodnames() || getLocalnames()) {
128             collector.setCollectingClassNames(false);
129             collector.setCollectingFieldNames(false);
130             collector.setCollectingMethodNames(false);
131             collector.setCollectingLocalNames(false);
132         }
133
134         if (getClassnames()) {
135             collector.setCollectingClassNames(true);
136         }
137
138         if (getFieldnames()) {
139             collector.setCollectingFieldNames(true);
140         }
141
142         if (getMethodnames()) {
143             collector.setCollectingMethodNames(true);
144         }
145
146         if (getLocalnames()) {
147             collector.setCollectingLocalNames(true);
148         }
149
150         ClassfileLoader loader = new TransientClassfileLoader();
151         loader.addLoadListener(new LoadListenerVisitorAdapter(collector));
152         loader.addLoadListener(verboseListener);
153         loader.load(Arrays.asList(getPath().list()));
154
155         log("Saving symbols to " + getDestfile().getAbsolutePath());
156         
157         try {
158             PrintWriter out = new PrintWriter(new FileWriter(getDestfile()));
159
160             Iterator i = collector.getCollection().iterator();
161             while (i.hasNext()) {
162                 out.println(i.next());
163             }
164             
165             out.close();
166         } catch (IOException ex) {
167             throw new BuildException(ex);
168         }
169     }
170 }
171
Popular Tags