001: /***
002: * Retrotranslator: a Java bytecode transformer that translates Java classes
003: * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
004: *
005: * Copyright (c) 2005 - 2008 Taras Puchko
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * 3. Neither the name of the copyright holders nor the names of its
017: * contributors may be used to endorse or promote products derived from
018: * this software without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
021: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
022: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
023: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
024: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
025: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
026: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
027: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
028: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
029: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
030: * THE POSSIBILITY OF SUCH DAMAGE.
031: */package net.sf.retrotranslator.transformer;
032:
033: import java.io.File;
034: import java.util.*;
035: import org.apache.tools.ant.*;
036: import org.apache.tools.ant.types.*;
037:
038: /**
039: * @author Taras Puchko
040: */
041: public class RetrotranslatorTask extends Task {
042:
043: private File srcdir;
044: private File srcjar;
045: private File destdir;
046: private File destjar;
047: private List<FileSet> fileSets = new ArrayList<FileSet>();
048: private List<FileSet> jarFileSets = new ArrayList<FileSet>();
049: private List<DirSet> dirSets = new ArrayList<DirSet>();
050: private boolean verbose;
051: private boolean stripsign;
052: private boolean stripannot;
053: private boolean retainapi;
054: private boolean retainflags;
055: private boolean lazy;
056: private boolean advanced;
057: private boolean verify;
058: private boolean uptodatecheck;
059: private boolean smart;
060: private boolean failonwarning = true;
061: private String srcmask;
062: private String embed;
063: private String support;
064: private String backport;
065: private String target;
066: private String reflection;
067: private Path classpath;
068:
069: public RetrotranslatorTask() {
070: }
071:
072: public void setSrcdir(File srcdir) {
073: this .srcdir = srcdir;
074: }
075:
076: public void setSrcjar(File srcjar) {
077: this .srcjar = srcjar;
078: }
079:
080: public void setDestdir(File destdir) {
081: this .destdir = destdir;
082: }
083:
084: public void setDestjar(File destjar) {
085: this .destjar = destjar;
086: }
087:
088: public void addConfiguredFileset(FileSet fileSet) {
089: fileSets.add(fileSet);
090: }
091:
092: public void addConfiguredJarfileset(FileSet fileSet) {
093: jarFileSets.add(fileSet);
094: }
095:
096: public void addConfiguredDirset(DirSet dirSet) {
097: dirSets.add(dirSet);
098: }
099:
100: public void setVerbose(boolean verbose) {
101: this .verbose = verbose;
102: }
103:
104: public void setStripsign(boolean stripsign) {
105: this .stripsign = stripsign;
106: }
107:
108: public void setStripannot(boolean stripannot) {
109: this .stripannot = stripannot;
110: }
111:
112: public void setRetainapi(boolean retainapi) {
113: this .retainapi = retainapi;
114: }
115:
116: public void setRetainflags(boolean retainflags) {
117: this .retainflags = retainflags;
118: }
119:
120: public void setLazy(boolean lazy) {
121: this .lazy = lazy;
122: }
123:
124: public void setAdvanced(boolean advanced) {
125: this .advanced = advanced;
126: }
127:
128: public void setVerify(boolean verify) {
129: this .verify = verify;
130: }
131:
132: public void setUptodatecheck(boolean uptodatecheck) {
133: this .uptodatecheck = uptodatecheck;
134: }
135:
136: public void setSmart(boolean smart) {
137: this .smart = smart;
138: }
139:
140: public void setFailonwarning(boolean failonwarning) {
141: this .failonwarning = failonwarning;
142: }
143:
144: public void setSrcmask(String srcmask) {
145: this .srcmask = srcmask;
146: }
147:
148: public void setEmbed(String embed) {
149: this .embed = embed;
150: }
151:
152: public void setSupport(String support) {
153: this .support = support;
154: }
155:
156: public void setBackport(String backport) {
157: this .backport = backport;
158: }
159:
160: public void setTarget(String target) {
161: this .target = target;
162: }
163:
164: public void setReflection(String reflection) {
165: this .reflection = reflection;
166: }
167:
168: public void setClasspathref(Reference classpathref) {
169: createClasspath().setRefid(classpathref);
170: }
171:
172: public void setClasspath(Path classpath) {
173: this .getClasspath().append(classpath);
174: }
175:
176: public Path createClasspath() {
177: return getClasspath().createPath();
178: }
179:
180: private Path getClasspath() {
181: return classpath != null ? classpath : (classpath = new Path(
182: getProject()));
183: }
184:
185: public void execute() throws BuildException {
186: try {
187: Retrotranslator retrotranslator = new Retrotranslator();
188: if (srcdir != null)
189: retrotranslator.addSrcdir(srcdir);
190: if (srcjar != null)
191: retrotranslator.addSrcjar(srcjar);
192: if (destdir != null)
193: retrotranslator.setDestdir(destdir);
194: if (destjar != null)
195: retrotranslator.setDestjar(destjar);
196: for (FileSet fileSet : fileSets) {
197: DirectoryScanner scanner = fileSet
198: .getDirectoryScanner(getProject());
199: retrotranslator.addSourceFiles(scanner.getBasedir(),
200: Arrays.asList(scanner.getIncludedFiles()));
201: }
202: for (FileSet jarFileSet : jarFileSets) {
203: DirectoryScanner scanner = jarFileSet
204: .getDirectoryScanner(getProject());
205: File basedir = scanner.getBasedir();
206: for (String jarFile : scanner.getIncludedFiles()) {
207: retrotranslator
208: .addSrcjar(new File(basedir, jarFile));
209: }
210: }
211: for (DirSet dirSet : dirSets) {
212: DirectoryScanner scanner = dirSet
213: .getDirectoryScanner(getProject());
214: File basedir = scanner.getBasedir();
215: for (String subdirectory : scanner
216: .getIncludedDirectories()) {
217: retrotranslator.addSrcdir(new File(basedir,
218: subdirectory));
219: }
220: }
221: retrotranslator.setVerbose(verbose);
222: retrotranslator.setStripsign(stripsign);
223: retrotranslator.setStripannot(stripannot);
224: retrotranslator.setRetainapi(retainapi);
225: retrotranslator.setRetainflags(retainflags);
226: retrotranslator.setLazy(lazy);
227: retrotranslator.setAdvanced(advanced);
228: retrotranslator.setVerify(verify);
229: retrotranslator.setUptodatecheck(uptodatecheck);
230: retrotranslator.setSmart(smart);
231: retrotranslator.setSrcmask(srcmask);
232: retrotranslator.setEmbed(embed);
233: retrotranslator.setSupport(support);
234: retrotranslator.setBackport(backport);
235: if (target != null) {
236: retrotranslator.setTarget(target);
237: }
238: if (reflection != null) {
239: retrotranslator.setReflection(reflection);
240: }
241: for (String fileName : getClasspath().list()) {
242: retrotranslator.addClasspathElement(getProject()
243: .resolveFile(fileName));
244: }
245: retrotranslator.setLogger(new AbstractLogger() {
246: protected void log(String text, Level level) {
247: RetrotranslatorTask.this .log(text, level
248: .isCritical() ? Project.MSG_WARN
249: : Project.MSG_INFO);
250: }
251: });
252: boolean success = retrotranslator.run();
253: if (!success && failonwarning) {
254: throw new BuildException("Translation failed.",
255: getLocation());
256: }
257: } catch (RuntimeException e) {
258: e.printStackTrace();
259: throw e;
260: }
261: }
262:
263: }
|