001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.dependencyfinder.ant;
034:
035: import java.io.*;
036: import java.util.*;
037:
038: import org.apache.tools.ant.*;
039: import org.apache.tools.ant.types.*;
040:
041: import com.jeantessier.classreader.*;
042:
043: public class ListSymbols extends Task {
044: private boolean classNames = false;
045: private boolean fieldNames = false;
046: private boolean methodNames = false;
047: private boolean localNames = false;
048: private File destfile;
049: private Path path;
050:
051: public boolean getClassnames() {
052: return classNames;
053: }
054:
055: public void setClassnames(boolean classNames) {
056: this .classNames = classNames;
057: }
058:
059: public boolean getFieldnames() {
060: return fieldNames;
061: }
062:
063: public void setFieldnames(boolean fieldNames) {
064: this .fieldNames = fieldNames;
065: }
066:
067: public boolean getMethodnames() {
068: return methodNames;
069: }
070:
071: public void setMethodnames(boolean methodNames) {
072: this .methodNames = methodNames;
073: }
074:
075: public boolean getLocalnames() {
076: return localNames;
077: }
078:
079: public void setLocalnames(boolean localNames) {
080: this .localNames = localNames;
081: }
082:
083: public File getDestfile() {
084: return destfile;
085: }
086:
087: public void setDestfile(File destfile) {
088: this .destfile = destfile;
089: }
090:
091: public Path createPath() {
092: if (path == null) {
093: path = new Path(getProject());
094: }
095:
096: return path;
097: }
098:
099: public Path getPath() {
100: return path;
101: }
102:
103: public void execute() throws BuildException {
104: // first off, make sure that we've got what we need
105:
106: if (getPath() == null) {
107: throw new BuildException("path must be set!");
108: }
109:
110: if (getDestfile() == null) {
111: throw new BuildException("destfile must be set!");
112: }
113:
114: log("Reading classes from path " + getPath());
115:
116: VerboseListener verboseListener = new VerboseListener(this );
117:
118: SymbolGatherer collector = new SymbolGatherer();
119:
120: // Since SymbolGatherer lists everything by default,
121: // we turn them all off if any of the switches are
122: // present. This way, if you pass nothing, you get
123: // the default behavior and the tool shows everything.
124: // If you pass in one or more, you only see symbols
125: // of the kind(s) you specified.
126: if (getClassnames() || getFieldnames() || getMethodnames()
127: || 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
152: .addLoadListener(new LoadListenerVisitorAdapter(
153: collector));
154: loader.addLoadListener(verboseListener);
155: loader.load(Arrays.asList(getPath().list()));
156:
157: log("Saving symbols to " + getDestfile().getAbsolutePath());
158:
159: try {
160: PrintWriter out = new PrintWriter(new FileWriter(
161: getDestfile()));
162: for (String symbol : collector.getCollection()) {
163: out.println(symbol);
164: }
165: out.close();
166: } catch (IOException ex) {
167: throw new BuildException(ex);
168: }
169: }
170: }
|