From 250e74e3398b6b93e8cd81e9a051e841f42ea300 Mon Sep 17 00:00:00 2001 From: Charlie Groves Date: Sat, 16 Oct 2010 23:16:14 +0000 Subject: [PATCH] Move all the fileset and classloading wanging into GenTask git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@6213 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../presents/tools/GenActionScriptTask.java | 61 +------------------ .../presents/tools/GenDObjectTask.java | 46 +------------- .../presents/tools/GenReceiverTask.java | 2 +- .../presents/tools/GenServiceTask.java | 2 +- .../threerings/presents/tools/GenTask.java | 57 +++++++++++++++++ .../presents/tools/InvocationTask.java | 50 +-------------- 6 files changed, 68 insertions(+), 150 deletions(-) diff --git a/src/java/com/threerings/presents/tools/GenActionScriptTask.java b/src/java/com/threerings/presents/tools/GenActionScriptTask.java index f43668903..5aeb456ec 100644 --- a/src/java/com/threerings/presents/tools/GenActionScriptTask.java +++ b/src/java/com/threerings/presents/tools/GenActionScriptTask.java @@ -24,7 +24,6 @@ package com.threerings.presents.tools; import java.lang.reflect.Field; import java.lang.reflect.Modifier; -import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Set; @@ -35,14 +34,9 @@ import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; -import org.apache.tools.ant.BuildException; -import org.apache.tools.ant.DirectoryScanner; -import org.apache.tools.ant.types.FileSet; - import com.google.common.base.Charsets; import com.google.common.base.Splitter; import com.google.common.collect.Iterables; -import com.google.common.collect.Lists; import com.google.common.io.Files; import com.threerings.io.ObjectInputStream; @@ -60,14 +54,6 @@ import com.threerings.presents.dobj.DObject; */ public class GenActionScriptTask extends GenTask { - /** - * Adds a nested <fileset> element which enumerates streamable source files. - */ - public void addFileset (FileSet set) - { - _filesets.add(set); - } - /** * Configures the path to our ActionScript source files. */ @@ -88,50 +74,11 @@ public class GenActionScriptTask extends GenTask } } - /** - * Performs the actual work of the task. - */ - @Override - public void execute () - { - for (FileSet fs : _filesets) { - DirectoryScanner ds = fs.getDirectoryScanner(getProject()); - File fromDir = fs.getDir(getProject()); - String[] srcFiles = ds.getIncludedFiles(); - for (String srcFile : srcFiles) { - processClass(new File(fromDir, srcFile)); - } - } - } - - /** - * Processes a Streamable source file. - */ - protected void processClass (File source) - { - // System.err.println("Processing " + source + "..."); - - // load up the file and determine it's package and classname - String name = null; - try { - name = GenUtil.readClassName(source); - } catch (Exception e) { - System.err.println("Failed to parse " + source + ": " + e.getMessage()); - return; - } - - Class sclass = loadClass(name); - try { - processClass(source, sclass); - } catch (Exception e) { - throw new BuildException(e); - } - } - /** * Processes a resolved Streamable class instance. */ - protected void processClass (File source, Class sclass) + @Override + public void processClass (File source, Class sclass) throws Exception { // make sure we implement Streamable but don't extend DObject or InvocationMarshaller and @@ -397,9 +344,6 @@ public class GenActionScriptTask extends GenTask } } - /** A list of filesets that contain tile images. */ - protected ArrayList _filesets = Lists.newArrayList(); - /** A header to put on all generated source files. */ protected String _header; @@ -412,4 +356,5 @@ public class GenActionScriptTask extends GenTask "public function writeObject (out :ObjectOutputStream) :void"; protected static final Splitter DOT_SPLITTER = Splitter.on('.'); + } diff --git a/src/java/com/threerings/presents/tools/GenDObjectTask.java b/src/java/com/threerings/presents/tools/GenDObjectTask.java index 039461bb6..02ba723dd 100644 --- a/src/java/com/threerings/presents/tools/GenDObjectTask.java +++ b/src/java/com/threerings/presents/tools/GenDObjectTask.java @@ -31,11 +31,6 @@ import java.util.HashMap; import java.util.Map; import java.io.File; -import java.io.IOException; - -import org.apache.tools.ant.BuildException; -import org.apache.tools.ant.DirectoryScanner; -import org.apache.tools.ant.types.FileSet; import com.google.common.collect.Lists; import com.samskivert.util.StringUtil; @@ -50,14 +45,6 @@ import com.threerings.presents.dobj.OidList; */ public class GenDObjectTask extends GenTask { - /** - * Adds a nested <fileset> element which enumerates service declaration source files. - */ - public void addFileset (FileSet set) - { - _filesets.add(set); - } - @Override public void execute () { @@ -66,36 +53,12 @@ public class GenDObjectTask extends GenTask _dsclass = loadClass(DSet.class.getName()); _olclass = loadClass(OidList.class.getName()); - for (FileSet fs : _filesets) { - DirectoryScanner ds = fs.getDirectoryScanner(getProject()); - File fromDir = fs.getDir(getProject()); - String[] srcFiles = ds.getIncludedFiles(); - for (String srcFile : srcFiles) { - processObject(new File(fromDir, srcFile)); - } - } - } - - /** Processes a distributed object source file. */ - protected void processObject (File source) - { - // load up the file and determine it's package and classname - String name; - try{ - name = GenUtil.readClassName(source); - } catch (IOException io) { - throw new BuildException("Couldn't read class name: " + source, io); - } - Class klass = loadClass(name); - try { - processObject(source, klass); - } catch (Exception e) { - throw new BuildException("Failed to process " + source.getName(), e); - } + super.execute(); } /** Processes a resolved distributed object class instance. */ - protected void processObject (File source, Class oclass) + @Override + public void processClass (File source, Class oclass) throws Exception { // make sure we extend distributed object @@ -203,9 +166,6 @@ public class GenDObjectTask extends GenTask sfile.writeTo(source, fsection.toString(), msection.toString()); } - /** A list of filesets that contain tile images. */ - protected ArrayList _filesets = Lists.newArrayList(); - /** {@link DObject} resolved with the proper classloader so that we * can compare it to loaded derived classes. */ protected Class _doclass; diff --git a/src/java/com/threerings/presents/tools/GenReceiverTask.java b/src/java/com/threerings/presents/tools/GenReceiverTask.java index 77e6ba602..6b94d8400 100644 --- a/src/java/com/threerings/presents/tools/GenReceiverTask.java +++ b/src/java/com/threerings/presents/tools/GenReceiverTask.java @@ -44,7 +44,7 @@ import com.threerings.presents.server.InvocationSender; public class GenReceiverTask extends InvocationTask { @Override - protected void processService (File source, Class receiver) + public void processClass (File source, Class receiver) throws Exception { System.out.println("Processing " + receiver.getName() + "..."); diff --git a/src/java/com/threerings/presents/tools/GenServiceTask.java b/src/java/com/threerings/presents/tools/GenServiceTask.java index 54a1f6c78..3c7a80089 100644 --- a/src/java/com/threerings/presents/tools/GenServiceTask.java +++ b/src/java/com/threerings/presents/tools/GenServiceTask.java @@ -191,7 +191,7 @@ public class GenServiceTask extends InvocationTask // documentation inherited @Override - protected void processService (File source, Class service) + public void processClass (File source, Class service) throws Exception { System.out.println("Processing " + service.getName() + "..."); diff --git a/src/java/com/threerings/presents/tools/GenTask.java b/src/java/com/threerings/presents/tools/GenTask.java index c66e76640..2b62c5821 100644 --- a/src/java/com/threerings/presents/tools/GenTask.java +++ b/src/java/com/threerings/presents/tools/GenTask.java @@ -21,18 +21,24 @@ package com.threerings.presents.tools; +import java.util.List; import java.util.Map; +import java.io.File; import java.io.StringWriter; import org.apache.tools.ant.AntClassLoader; import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.DirectoryScanner; import org.apache.tools.ant.Task; +import org.apache.tools.ant.types.FileSet; import org.apache.tools.ant.types.Reference; import org.apache.tools.ant.util.ClasspathUtils; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.VelocityEngine; +import com.google.common.collect.Lists; + import com.samskivert.velocity.VelocityUtil; public abstract class GenTask extends Task @@ -46,6 +52,14 @@ public abstract class GenTask extends Task } } + /** + * Adds a nested <fileset> element which enumerates service declaration source files. + */ + public void addFileset (FileSet set) + { + _filesets.add(set); + } + /** * Configures to output extra information when generating code. */ @@ -65,6 +79,27 @@ public abstract class GenTask extends Task ((AntClassLoader)_cloader).setParent(getClass().getClassLoader()); } + /** + * Performs the actual work of the task. + */ + @Override + public void execute () + { + for (FileSet fs : _filesets) { + DirectoryScanner ds = fs.getDirectoryScanner(getProject()); + File fromDir = fs.getDir(getProject()); + String[] srcFiles = ds.getIncludedFiles(); + for (String srcFile : srcFiles) { + File source = new File(fromDir, srcFile); + try { + processClass(source, loadClass(source)); + } catch (Exception e) { + throw new BuildException(e); + } + } + } + } + /** * Merges the specified template using the supplied mapping of keys to objects. * @@ -108,6 +143,25 @@ public abstract class GenTask extends Task return writer.toString(); } + /** + * Process a class found from the given source file that was on the filesets given to this + * task. + */ + protected abstract void processClass (File source, Class klass) + throws Exception; + + protected Class loadClass (File source) + { + // load up the file and determine it's package and classname + String name; + try { + name = GenUtil.readClassName(source); + } catch (Exception e) { + throw new BuildException("Failed to parse " + source + ": " + e.getMessage()); + } + return loadClass(name); + } + protected Class loadClass (String name) { if (_cloader == null) { @@ -123,6 +177,9 @@ public abstract class GenTask extends Task } } + /** A list of filesets that contain java source to be processed. */ + protected List _filesets = Lists.newArrayList(); + /** Show extra output if set. */ protected boolean _verbose; diff --git a/src/java/com/threerings/presents/tools/InvocationTask.java b/src/java/com/threerings/presents/tools/InvocationTask.java index 878930d2e..59b86ce6b 100644 --- a/src/java/com/threerings/presents/tools/InvocationTask.java +++ b/src/java/com/threerings/presents/tools/InvocationTask.java @@ -36,8 +36,6 @@ import java.io.IOException; import java.io.PrintWriter; import org.apache.tools.ant.BuildException; -import org.apache.tools.ant.DirectoryScanner; -import org.apache.tools.ant.types.FileSet; import com.google.common.base.Predicate; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; @@ -50,8 +48,8 @@ import com.threerings.presents.net.Transport; import com.threerings.presents.client.InvocationService.InvocationListener; /** - * A base Ant task for generating invocation service related marshalling - * and unmarshalling classes. + * A base Ant task for generating invocation service related marshalling and unmarshalling + * classes. */ public abstract class InvocationTask extends GenTask { @@ -340,15 +338,6 @@ public abstract class InvocationTask extends GenTask } } - /** - * Adds a nested <fileset> element which enumerates service - * declaration source files. - */ - public void addFileset (FileSet set) - { - _filesets.add(set); - } - /** * Configures us with a header file that we'll prepend to all * generated source files. @@ -369,39 +358,9 @@ public abstract class InvocationTask extends GenTask // resolve the InvocationListener class using our classloader _ilistener = loadClass(InvocationListener.class.getName()); - for (FileSet fs : _filesets) { - DirectoryScanner ds = fs.getDirectoryScanner(getProject()); - File fromDir = fs.getDir(getProject()); - String[] srcFiles = ds.getIncludedFiles(); - for (String srcFile : srcFiles) { - processService(new File(fromDir, srcFile)); - } - } + super.execute(); } - /** Processes an invocation service source file. */ - protected void processService (File source) - { - // System.err.println("Processing " + source + "..."); - // load up the file and determine it's package and classname - String name = null; - try { - name = GenUtil.readClassName(source); - } catch (Exception e) { - throw new BuildException("Failed to parse " + source + ": " + e.getMessage()); - } - - Class serviceClass = loadClass(name); - try { - processService(source, serviceClass); - } catch (Exception e) { - throw new BuildException("Failed to process " + source.getName() + ": " + e, e); - } - } - - /** Processes a resolved invocation service class instance. */ - protected abstract void processService (File source, Class service) throws Exception; - protected void writeFile (String path, String data) throws IOException { @@ -431,9 +390,6 @@ public abstract class InvocationTask extends GenTask newstr.replace('/', File.separatorChar)); } - /** A list of filesets that contain tile images. */ - protected List _filesets = Lists.newArrayList(); - /** A header to put on all generated source files. */ protected String _header;