You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by bh...@apache.org on 2009/12/20 21:26:07 UTC
svn commit: r892656 - in /myfaces/extensions/scripting/trunk/core/core/src:
main/java/org/apache/myfaces/extensions/scripting/compiler/
test/java/org/apache/myfaces/extensions/scripting/compiler/
Author: bhuemer
Date: Sun Dec 20 20:26:07 2009
New Revision: 892656
URL: http://svn.apache.org/viewvc?rev=892656&view=rev
Log:
- Added a Groovy compiler implementation.
Added:
myfaces/extensions/scripting/trunk/core/core/src/main/java/org/apache/myfaces/extensions/scripting/compiler/GroovyCompiler.java
myfaces/extensions/scripting/trunk/core/core/src/test/java/org/apache/myfaces/extensions/scripting/compiler/GroovyCompilerTest.java
Modified:
myfaces/extensions/scripting/trunk/core/core/src/main/java/org/apache/myfaces/extensions/scripting/compiler/JavacCompiler.java
Added: myfaces/extensions/scripting/trunk/core/core/src/main/java/org/apache/myfaces/extensions/scripting/compiler/GroovyCompiler.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/scripting/trunk/core/core/src/main/java/org/apache/myfaces/extensions/scripting/compiler/GroovyCompiler.java?rev=892656&view=auto
==============================================================================
--- myfaces/extensions/scripting/trunk/core/core/src/main/java/org/apache/myfaces/extensions/scripting/compiler/GroovyCompiler.java (added)
+++ myfaces/extensions/scripting/trunk/core/core/src/main/java/org/apache/myfaces/extensions/scripting/compiler/GroovyCompiler.java Sun Dec 20 20:26:07 2009
@@ -0,0 +1,75 @@
+package org.apache.myfaces.extensions.scripting.compiler;
+
+import org.apache.myfaces.extensions.scripting.loader.ClassLoaderUtils;
+import org.codehaus.groovy.control.CompilationUnit;
+import org.codehaus.groovy.control.CompilerConfiguration;
+
+import java.io.File;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+
+/**
+ *
+ *
+ */
+public class GroovyCompiler implements Compiler {
+
+ // ------------------------------------------ Compiler methods
+
+ /**
+ * <p>Compiles the given file and creates an according class file in the given target path.</p>
+ *
+ * @param sourcePath the path to the source directory
+ * @param targetPath the path to the target directory
+ * @param file the relative file name of the class you want to compile
+ * @return the compilation result, i.e. as of now only the compiler output
+ */
+ public CompilationResult compile(File sourcePath, File targetPath, String file, ClassLoader classLoader)
+ throws CompilationException {
+ return compile(sourcePath, targetPath, new File(sourcePath, file), classLoader);
+ }
+
+ /**
+ * <p>Compiles the given file and creates an according class file in the given target path.</p>
+ *
+ * @param sourcePath the path to the source directory
+ * @param targetPath the path to the target directory
+ * @param file the file of the class you want to compile
+ * @return the compilation result, i.e. as of now only the compiler output
+ */
+ public CompilationResult compile(File sourcePath, File targetPath, File file, ClassLoader classLoader)
+ throws CompilationException {
+ StringWriter compilerOutput = new StringWriter();
+
+ CompilationUnit compilationUnit = new CompilationUnit(
+ buildCompilerConfiguration(sourcePath, targetPath, file, classLoader));
+ compilationUnit.getConfiguration().setOutput(new PrintWriter(compilerOutput));
+ compilationUnit.addSource(file);
+ compilationUnit.compile();
+
+ return new CompilationResult(compilerOutput.toString());
+ }
+
+ // ------------------------------------------ Utility methods
+
+ protected CompilerConfiguration buildCompilerConfiguration(File sourcePath, File targetPath, File file, ClassLoader classLoader) {
+ CompilerConfiguration configuration = new CompilerConfiguration();
+
+ // Set the destination / target directory for the compiled .class files.
+ configuration.setTargetDirectory(targetPath.getAbsoluteFile());
+
+ // Specify the classpath of the given class loader. This enables the user to write new Java
+ // "scripts" that depend on classes that have already been loaded previously. Otherwise he
+ // wouldn't be able to use for example classes that are available in a library.
+ configuration.setClasspath(ClassLoaderUtils.buildClasspath(classLoader));
+
+ // Enable verbose output.
+ configuration.setVerbose(true);
+
+ // Generate debugging information.
+ configuration.setDebug(true);
+
+ return configuration;
+ }
+
+}
Modified: myfaces/extensions/scripting/trunk/core/core/src/main/java/org/apache/myfaces/extensions/scripting/compiler/JavacCompiler.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/scripting/trunk/core/core/src/main/java/org/apache/myfaces/extensions/scripting/compiler/JavacCompiler.java?rev=892656&r1=892655&r2=892656&view=diff
==============================================================================
--- myfaces/extensions/scripting/trunk/core/core/src/main/java/org/apache/myfaces/extensions/scripting/compiler/JavacCompiler.java (original)
+++ myfaces/extensions/scripting/trunk/core/core/src/main/java/org/apache/myfaces/extensions/scripting/compiler/JavacCompiler.java Sun Dec 20 20:26:07 2009
@@ -85,7 +85,7 @@
classLoader = createJavacAwareClassLoader(toolsJar);
}
catch (MalformedURLException ex) {
- throw new IllegalStateException("An error occured while trying to load the Javac compiler class.", ex);
+ throw new IllegalStateException("An error occurred while trying to load the Javac compiler class.", ex);
}
try {
@@ -105,7 +105,7 @@
*
* @param sourcePath the path to the source directory
* @param targetPath the path to the target directory
- * @param file the file of the class you want to compile
+ * @param file the relative file name of the class you want to compile
* @return the compilation result, i.e. as of now only the compiler output
*/
public CompilationResult compile(File sourcePath, File targetPath, String file, ClassLoader classLoader)
@@ -118,7 +118,7 @@
*
* @param sourcePath the path to the source directory
* @param targetPath the path to the target directory
- * @param file the relative file name of the class you want to compile
+ * @param file the file of the class you want to compile
* @return the compilation result, i.e. as of now only the compiler output
*/
public CompilationResult compile(File sourcePath, File targetPath, File file, ClassLoader classLoader)
@@ -140,7 +140,7 @@
// Invoke the Javac compiler
Method compile = compilerClass.getMethod("compile", new Class[]{String[].class, PrintWriter.class});
Integer returnCode = (Integer) compile.invoke(null,
- new Object[]{buildCompilerArguments(sourcePath, targetPath, file, classLoader),
+ new Object[]{ buildCompilerArguments(sourcePath, targetPath, file, classLoader),
new PrintWriter(compilerOutput)});
CompilationResult result = new CompilationResult(compilerOutput.toString());
@@ -192,7 +192,7 @@
arguments.add("-d");
arguments.add(targetPath.getAbsolutePath());
- // Specify the classpath of the given classloader. This enables the user to write new Java
+ // Specify the classpath of the given class loader. This enables the user to write new Java
// "scripts" that depend on classes that have already been loaded previously. Otherwise he
// wouldn't be able to use for example classes that are available in a library.
arguments.add("-classpath");
@@ -251,7 +251,7 @@
if (logger.isDebugEnabled()) {
logger.debug(
"The required JAR file '$JAVA_HOME$/lib/tools.jar' has been found ['" + toolsJarFile.getAbsolutePath()
- + "']. A custom URL classloader will be created for the Javac compiler.");
+ + "']. A custom URL class loader will be created for the Javac compiler.");
}
return new URLClassLoader(
@@ -264,7 +264,7 @@
} else {
if (logger.isDebugEnabled()) {
logger.debug("The user has specified the required JAR file '$JAVA_HOME$/lib/tools.jar' ['"
- + toolsJar.toExternalForm() + "']. A custom URL classloader will be created for the Javac compiler.");
+ + toolsJar.toExternalForm() + "']. A custom URL class loader will be created for the Javac compiler.");
}
return new URLClassLoader(new URL[]{toolsJar}, ClassLoaderUtils.getDefaultClassLoader());
Added: myfaces/extensions/scripting/trunk/core/core/src/test/java/org/apache/myfaces/extensions/scripting/compiler/GroovyCompilerTest.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/scripting/trunk/core/core/src/test/java/org/apache/myfaces/extensions/scripting/compiler/GroovyCompilerTest.java?rev=892656&view=auto
==============================================================================
--- myfaces/extensions/scripting/trunk/core/core/src/test/java/org/apache/myfaces/extensions/scripting/compiler/GroovyCompilerTest.java (added)
+++ myfaces/extensions/scripting/trunk/core/core/src/test/java/org/apache/myfaces/extensions/scripting/compiler/GroovyCompilerTest.java Sun Dec 20 20:26:07 2009
@@ -0,0 +1,44 @@
+package org.apache.myfaces.extensions.scripting.compiler;
+
+import org.apache.myfaces.extensions.scripting.AbstractGeneratorTestCase;
+
+import java.io.File;
+
+/**
+ * <p>Test class for
+ * <code>org.apache.myfaces.extensions.scripting.compiler.GroovyCompiler</code></p>
+ */
+public class GroovyCompilerTest extends AbstractGeneratorTestCase {
+
+ // ------------------------------------------ Test methods
+
+ public void testCompileGeneratedFile() throws Exception {
+ writeFile("/src/main/groovy/org/apache/myfaces/extensions/scripting/HelloWorld.groovy", new String[]{
+ "package org.apache.myfaces.extensions.scripting; ",
+ " ",
+ "def class HelloWorld { ",
+ " ",
+ " def static main(String[] args) { ",
+ " println(\"Hello World\"); ",
+ " } ",
+ "} "
+ });
+
+ Compiler compiler = new GroovyCompiler();
+ CompilationResult result = compiler.compile(
+ new File(buildAbsolutePath("/src/main/groovy")),
+ new File(buildAbsolutePath("/target/test-classes")),
+ "org/apache/myfaces/extensions/scripting/HelloWorld.groovy", getCurrentClassLoader());
+
+ assertFalse(result.hasErrors());
+ assertTrue(new File(
+ buildAbsolutePath("/target/test-classes/"), "org/apache/myfaces/extensions/scripting/HelloWorld.class").exists());
+ }
+
+ // ------------------------------------------ Utility methods
+
+ protected ClassLoader getCurrentClassLoader() {
+ return getClass().getClassLoader();
+ }
+
+}