[Jsptest-svn-commits] SF.net SVN: jsptest: [223] trunk/jsptest-generic/jsptest-common/src/main/ ja
Status: Alpha
Brought to you by:
lkoskela
From: <lko...@us...> - 2008-04-16 20:12:55
|
Revision: 223 http://jsptest.svn.sourceforge.net/jsptest/?rev=223&view=rev Author: lkoskela Date: 2008-04-16 13:12:52 -0700 (Wed, 16 Apr 2008) Log Message: ----------- Formatting and minor refactoring Modified Paths: -------------- trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/compiler/java/CommandLineJavac.java trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/compiler/java/JavaCompiler.java trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/compiler/java/SunJavaC.java Modified: trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/compiler/java/CommandLineJavac.java =================================================================== --- trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/compiler/java/CommandLineJavac.java 2008-04-16 18:05:56 UTC (rev 222) +++ trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/compiler/java/CommandLineJavac.java 2008-04-16 20:12:52 UTC (rev 223) @@ -28,73 +28,73 @@ */ public class CommandLineJavac implements JavaCompiler { - private static final String SEPARATOR = System - .getProperty("path.separator"); + private static final String SEPARATOR = System + .getProperty("path.separator"); - public boolean compile(String srcFile, String destDir, - String[] classpath) throws Exception { - srcFile = new File(srcFile).getCanonicalPath(); - String cp = join(classpath); - String[] command = buildCommandLine(srcFile, destDir, cp); - return execute(command); - } + public boolean compile(String srcFile, String destDir, String[] classpath) + throws Exception { + srcFile = new File(srcFile).getCanonicalPath(); + String cp = join(classpath); + String[] command = buildCommandLine(srcFile, destDir, cp); + return execute(command); + } - private String[] buildCommandLine(String pathToJavaSource, - String outputDirectory, String classpathString) { - return new String[] { "javac", "-classpath", classpathString, - "-d", outputDirectory, pathToJavaSource }; - } + private String[] buildCommandLine(String pathToJavaSource, + String outputDirectory, String classpathString) { + return new String[] { "javac", "-classpath", classpathString, "-d", + outputDirectory, pathToJavaSource }; + } - private String join(String[] classpath) { - StringBuffer s = new StringBuffer(); - for (int i = 0; i < classpath.length; i++) { - if (s.length() > 0) { - s.append(SEPARATOR); - } - s.append(classpath[i]); - } - return s.toString(); - } + private String join(String[] classpath) { + StringBuffer s = new StringBuffer(5000); + for (int i = 0; i < classpath.length; i++) { + if (s.length() > 0) { + s.append(SEPARATOR); + } + s.append(classpath[i]); + } + return s.toString(); + } - private boolean execute(String[] commandLine) throws IOException, - InterruptedException { - Process p = Runtime.getRuntime().exec(commandLine); - String processOutput = readOutput(p); - boolean success = (p.waitFor() == 0); - if (!success) { - System.err.println(processOutput); - } - return success; - } + private boolean execute(String[] commandLine) throws IOException, + InterruptedException { + Process p = Runtime.getRuntime().exec(commandLine); + String processOutput = readOutput(p); + boolean success = (p.waitFor() == 0); + if (!success) { + System.err.println(processOutput); + } + return success; + } - private String readOutput(final Process p) throws IOException { - try { - StringWriter output = new StringWriter(); - final PrintWriter ps = new PrintWriter(output); - Thread stderrThread = new Thread(new StreamConsumer( - "STDERR", p.getErrorStream(), ps)); - Thread stdoutThread = new Thread(new StreamConsumer( - "STDOUT", p.getInputStream(), ps)); - p.getOutputStream().close(); - stderrThread.start(); - stdoutThread.start(); - stderrThread.join(); - stdoutThread.join(); - return output.toString(); - } catch (Exception e) { - StringWriter sw = new StringWriter(); - e.printStackTrace(new PrintWriter(sw)); - return sw.toString(); - } - } + private String readOutput(final Process p) throws IOException { + try { + StringWriter output = new StringWriter(); + final PrintWriter ps = new PrintWriter(output); + Thread stderrThread = new Thread(new StreamConsumer("STDERR", p + .getErrorStream(), ps)); + Thread stdoutThread = new Thread(new StreamConsumer("STDOUT", p + .getInputStream(), ps)); + p.getOutputStream().close(); + stderrThread.start(); + stdoutThread.start(); + stderrThread.join(); + stdoutThread.join(); + return output.toString(); + } catch (Exception e) { + StringWriter sw = new StringWriter(); + e.printStackTrace(new PrintWriter(sw)); + return sw.toString(); + } + } - public boolean isAvailable() { - try { - Process p = Runtime.getRuntime().exec("javac"); - String s = readOutput(p); - return s.indexOf("Usage: javac") != -1; - } catch (Exception e) { - return false; - } - } + public boolean isAvailable() { + try { + Process p = Runtime.getRuntime().exec("javac"); + String s = readOutput(p); + return s.indexOf("Usage: javac") != -1; + } catch (Exception e) { + return false; + } + } } Modified: trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/compiler/java/JavaCompiler.java =================================================================== --- trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/compiler/java/JavaCompiler.java 2008-04-16 18:05:56 UTC (rev 222) +++ trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/compiler/java/JavaCompiler.java 2008-04-16 20:12:52 UTC (rev 223) @@ -23,26 +23,25 @@ */ public interface JavaCompiler { - /** - * Compiles the given .java source file into the given .class file using an - * explicitly set class path. - * - * @param pathToJavaSource - * Path to the source file to compile. - * @param outputDirectory - * Path to the output directory under which to generate the - * .class file - * @param classpath - * The class path to use for compilation. - */ - public abstract boolean compile(String pathToJavaSource, - String outputDirectory, String[] classpath) - throws Exception; + /** + * Compiles the given .java source file into the given .class file using an + * explicitly set class path. + * + * @param pathToJavaSource + * Path to the source file to compile. + * @param outputDirectory + * Path to the output directory under which to generate the + * .class file + * @param classpath + * The class path to use for compilation. + */ + boolean compile(String pathToJavaSource, String outputDirectory, + String[] classpath) throws Exception; - /** - * Indicates whether this Java compiler implementation is available on the - * current system. - */ - public abstract boolean isAvailable(); + /** + * Indicates whether this Java compiler implementation is available on the + * current system. + */ + boolean isAvailable(); } \ No newline at end of file Modified: trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/compiler/java/SunJavaC.java =================================================================== --- trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/compiler/java/SunJavaC.java 2008-04-16 18:05:56 UTC (rev 222) +++ trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/compiler/java/SunJavaC.java 2008-04-16 20:12:52 UTC (rev 223) @@ -6,65 +6,75 @@ import java.lang.reflect.Method; /** - * This implementation uses the java compiler from sun which ships with since the jdk since java 1.3. - * The code is inspired by the javac ant task implementation. - * + * This implementation uses the java compiler from sun which ships with since + * the jdk since java 1.3. The code is inspired by the javac ant task + * implementation. + * * @author mathias.broekelmann - * + * */ public class SunJavaC implements JavaCompiler { - private static final String SEPARATOR = System.getProperty("path.separator"); + private static final String SEPARATOR = System + .getProperty("path.separator"); - public boolean compile(String pathToJavaSource, String outputDirectory, String[] classpath) throws Exception { - String classpathString = join(classpath); - String[] args = buildArgs(pathToJavaSource, outputDirectory, classpathString); - return compile(args); - } + public boolean compile(String pathToJavaSource, String outputDirectory, + String[] classpath) throws Exception { + String classpathString = join(classpath); + String[] args = buildArgs(pathToJavaSource, outputDirectory, + classpathString); + return compile(args); + } - private boolean compile(String[] args) { - try { - Class compilerClass = getCompileClass(); - Method compilerMethod = resolveCompilerMethod(compilerClass); - return ((Integer)compilerMethod.invoke(compilerClass.newInstance(), new Object[] { args })).intValue() == 0; - } catch (Throwable e) { - System.err.println(e.getMessage()); - return false; - } - } + private boolean compile(String[] args) { + try { + Class compilerClass = getCompileClass(); + Method compilerMethod = resolveCompilerMethod(compilerClass); + Object compilerInstance = compilerClass.newInstance(); + Object[] compilerArguments = new Object[] { args }; + int returnCode = ((Integer) compilerMethod.invoke(compilerInstance, + compilerArguments)).intValue(); + return returnCode == 0; + } catch (Throwable e) { + System.err.println(e.getMessage()); + return false; + } + } - private String[] buildArgs(String pathToJavaSource, String outputDirectory, String classpathString) { - return new String[] { "-classpath", classpathString, "-d", outputDirectory, pathToJavaSource }; - } + private String[] buildArgs(String pathToJavaSource, String outputDirectory, + String classpathString) { + return new String[] { "-classpath", classpathString, "-d", + outputDirectory, pathToJavaSource }; + } - private String join(String[] classpath) { - StringBuffer s = new StringBuffer(); - for (int i = 0; i < classpath.length; i++) { - if (s.length() > 0) { - s.append(SEPARATOR); - } - s.append(classpath[i]); - } - return s.toString(); - } + private String join(String[] classpath) { + StringBuffer s = new StringBuffer(5000); + for (int i = 0; i < classpath.length; i++) { + if (s.length() > 0) { + s.append(SEPARATOR); + } + s.append(classpath[i]); + } + return s.toString(); + } - public boolean isAvailable() { - try { - Class c = getCompileClass(); - resolveCompilerMethod(c); - return true; - } catch (Throwable ex) { - return false; - } - } + public boolean isAvailable() { + try { + resolveCompilerMethod(getCompileClass()); + return true; + } catch (Throwable ex) { + return false; + } + } - private Method resolveCompilerMethod(Class compilerClass) throws NoSuchMethodException { - return compilerClass.getMethod("compile", new Class[] { (new String[] {}).getClass() }); - } + private Method resolveCompilerMethod(Class compilerClass) + throws NoSuchMethodException { + return compilerClass.getMethod("compile", new Class[] { (new String[0]) + .getClass() }); + } - private Class getCompileClass() throws ClassNotFoundException { - Class c = Class.forName("com.sun.tools.javac.Main"); - return c; - } + private Class getCompileClass() throws ClassNotFoundException { + return Class.forName("com.sun.tools.javac.Main"); + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |