[Jsptest-svn-commits] SF.net SVN: jsptest:[255] trunk
Status: Alpha
Brought to you by:
lkoskela
From: <lko...@us...> - 2009-11-13 00:01:23
|
Revision: 255 http://jsptest.svn.sourceforge.net/jsptest/?rev=255&view=rev Author: lkoskela Date: 2009-11-13 00:00:35 +0000 (Fri, 13 Nov 2009) Log Message: ----------- Added JavaCompiler implementation for Java 6 (JSR 199) Modified Paths: -------------- trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/compiler/java/CommandLineJavac.java trunk/jsptest-jsp20/src/main/java/net/sf/jsptest/compiler/jsp20/JasperCompiler.java Added Paths: ----------- trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/compiler/java/Java6Compiler.java trunk/jsptest-generic/jsptest-common/src/test/java/net/sf/jsptest/compiler/ trunk/jsptest-generic/jsptest-common/src/test/java/net/sf/jsptest/compiler/java/ trunk/jsptest-generic/jsptest-common/src/test/java/net/sf/jsptest/compiler/java/Java6CompilerTest.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 2009-11-12 23:48:30 UTC (rev 254) +++ trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/compiler/java/CommandLineJavac.java 2009-11-13 00:00:35 UTC (rev 255) @@ -35,7 +35,7 @@ return execute(command); } - private String[] buildCommandLine(String pathToJavaSource, String outputDirectory, + protected String[] buildCommandLine(String pathToJavaSource, String outputDirectory, String classpathString) { return new String[] { "javac", "-classpath", classpathString, "-d", outputDirectory, pathToJavaSource }; @@ -52,7 +52,7 @@ return s.toString(); } - private boolean execute(String[] commandLine) throws IOException, InterruptedException { + protected boolean execute(String[] commandLine) throws IOException, InterruptedException { Process p = Runtime.getRuntime().exec(commandLine); String processOutput = readOutput(p); boolean success = (p.waitFor() == 0); Added: trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/compiler/java/Java6Compiler.java =================================================================== --- trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/compiler/java/Java6Compiler.java (rev 0) +++ trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/compiler/java/Java6Compiler.java 2009-11-13 00:00:35 UTC (rev 255) @@ -0,0 +1,65 @@ +package net.sf.jsptest.compiler.java; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.lang.reflect.Method; + +import net.sf.jsptest.utils.IO; + +public class Java6Compiler extends CommandLineJavac { + private static final String TMPDIR = System.getProperty("java.io.tmpdir"); + + protected boolean execute(String[] arguments) throws IOException, + InterruptedException { + ByteArrayOutputStream output = new ByteArrayOutputStream(); + try { + return executeWithReflection(arguments, output) == 0; + } finally { + File logFile = new File(TMPDIR, getClass().getName() + ".log"); + IO.append(output.toString(), logFile); + } + } + + private int executeWithReflection(String[] arguments, + ByteArrayOutputStream output) { + try { + Object systemJavaCompiler = acquireCompilerImplementation(); + Method[] methods = systemJavaCompiler.getClass().getMethods(); + for (int i = 0; i < methods.length; i++) { + if (methods[i].getName().equals("run")) { + Object returnValue = methods[i].invoke(systemJavaCompiler, + new Object[] { + new ByteArrayInputStream(new byte[0]), + output, output, arguments }); + return Integer.parseInt(String.valueOf(returnValue)); + } + } + return -1; + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + protected String[] buildCommandLine(String pathToJavaSource, + String outputDirectory, String classpathString) { + return new String[] { "-verbose", "-classpath", classpathString, "-d", + outputDirectory, pathToJavaSource }; + } + + public boolean isAvailable() { + return acquireCompilerImplementation() != null; + } + + private Object acquireCompilerImplementation() { + try { + Class toolProvider = Class.forName("javax.tools.ToolProvider"); + Method method = toolProvider.getMethod("getSystemJavaCompiler", + null); + return method.invoke(null, null); + } catch (Exception e) { + return null; + } + } +} Added: trunk/jsptest-generic/jsptest-common/src/test/java/net/sf/jsptest/compiler/java/Java6CompilerTest.java =================================================================== --- trunk/jsptest-generic/jsptest-common/src/test/java/net/sf/jsptest/compiler/java/Java6CompilerTest.java (rev 0) +++ trunk/jsptest-generic/jsptest-common/src/test/java/net/sf/jsptest/compiler/java/Java6CompilerTest.java 2009-11-13 00:00:35 UTC (rev 255) @@ -0,0 +1,14 @@ +package net.sf.jsptest.compiler.java; + +import junit.framework.TestCase; + +public class Java6CompilerTest extends TestCase { + public void testIsAvailableWhenRunningUnderJava6() throws Exception { + assertEquals(isRunningWithJava6(), new Java6Compiler().isAvailable()); + } + + private boolean isRunningWithJava6() { + String versionString = System.getProperty("java.specification.version"); + return Float.parseFloat(versionString) >= 1.6f; + } +} Modified: trunk/jsptest-jsp20/src/main/java/net/sf/jsptest/compiler/jsp20/JasperCompiler.java =================================================================== --- trunk/jsptest-jsp20/src/main/java/net/sf/jsptest/compiler/jsp20/JasperCompiler.java 2009-11-12 23:48:30 UTC (rev 254) +++ trunk/jsptest-jsp20/src/main/java/net/sf/jsptest/compiler/jsp20/JasperCompiler.java 2009-11-13 00:00:35 UTC (rev 255) @@ -30,6 +30,7 @@ import javax.servlet.jsp.tagext.TagInfo; import net.sf.jsptest.compiler.JspCompilationInfo; import net.sf.jsptest.compiler.java.CommandLineJavac; +import net.sf.jsptest.compiler.java.Java6Compiler; import net.sf.jsptest.compiler.java.JavaCompiler; import net.sf.jsptest.compiler.java.SunJavaC; import net.sf.jsptest.compiler.jsp20.mock.MockOptions; @@ -351,6 +352,7 @@ // this doesn't work because with Maven we need to set the classpath // explicitly as the "current" classpath does not include our // dependencies + compilers.add(new Java6Compiler()); compilers.add(new SunJavaC()); compilers.add(new CommandLineJavac()); for (Iterator i = compilers.iterator(); i.hasNext();) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |