Author: cziegeler
Date: Fri Mar 19 14:50:41 2010
New Revision: 925247
URL: http://svn.apache.org/viewvc?rev=925247&view=rev
Log:
SLING-1451 : Clean up compiler API and use classloading infrastructure
Added:
sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/CompilationUnit.java
- copied, changed from r924837,
sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/CompileUnit.java
Removed:
sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/ClassWriter.java
sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/CompileUnit.java
sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/CompilerEnvironment.java
Modified:
sling/trunk/contrib/commons/compiler/NOTICE
sling/trunk/contrib/commons/compiler/pom.xml
sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/ErrorHandler.java
sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/JavaCompiler.java
sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/Options.java
sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/impl/EclipseJavaCompiler.java
sling/trunk/contrib/commons/compiler/src/main/resources/META-INF/NOTICE
sling/trunk/contrib/commons/compiler/src/test/java/org/apache/sling/commons/compiler/impl/CompilerJava5Test.java
sling/trunk/contrib/commons/compiler/src/test/resources/Java5Test
Modified: sling/trunk/contrib/commons/compiler/NOTICE
URL:
http://svn.apache.org/viewvc/sling/trunk/contrib/commons/compiler/NOTICE?rev=925247&r1=925246&r2=925247&view=diff
==============================================================================
--- sling/trunk/contrib/commons/compiler/NOTICE (original)
+++ sling/trunk/contrib/commons/compiler/NOTICE Fri Mar 19 14:50:41 2010
@@ -1,5 +1,5 @@
Apache Sling Java Compiler
-Copyright 2008-2009 The Apache Software Foundation
+Copyright 2008-2010 The Apache Software Foundation
Apache Sling is based on source code originally developed
by Day Software (http://www.day.com/).
Modified: sling/trunk/contrib/commons/compiler/pom.xml
URL:
http://svn.apache.org/viewvc/sling/trunk/contrib/commons/compiler/pom.xml?rev=925247&r1=925246&r2=925247&view=diff
==============================================================================
--- sling/trunk/contrib/commons/compiler/pom.xml (original)
+++ sling/trunk/contrib/commons/compiler/pom.xml Fri Mar 19 14:50:41 2010
@@ -59,7 +59,7 @@
!org.eclipse.*,*
</Import-Package>
<Export-Package>
- org.apache.sling.commons.compiler;version=1.0.0
+ org.apache.sling.commons.compiler;version=2.0.0
</Export-Package>
<Private-Package>
org.apache.sling.commons.compiler.impl
@@ -88,6 +88,18 @@
<dependencies>
<dependency>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>org.apache.felix.scr.annotations</artifactId>
+ <version>1.2.0</version>
+ <scope>compile</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.commons.classloader</artifactId>
+ <version>1.1.0</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
<groupId>org.eclipse.jdt</groupId>
<artifactId>core</artifactId>
<version>3.3.0-v_771</version>
@@ -111,12 +123,10 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
- <scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
- <scope>test</scope>
</dependency>
</dependencies>
</project>
Copied:
sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/CompilationUnit.java
(from r924837,
sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/CompileUnit.java)
URL:
http://svn.apache.org/viewvc/sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/CompilationUnit.java?p2=sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/CompilationUnit.java&p1=sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/CompileUnit.java&r1=924837&r2=925247&rev=925247&view=diff
==============================================================================
---
sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/CompileUnit.java
(original)
+++
sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/CompilationUnit.java
Fri Mar 19 14:50:41 2010
@@ -16,16 +16,26 @@
*/
package org.apache.sling.commons.compiler;
-public interface CompileUnit {
+import java.io.IOException;
+import java.io.Reader;
- String getSourceFileName();
+/**
+ * This interface describes a compilation unit - usually a java class.
+ * @since 2.0
+ */
+public interface CompilationUnit {
- char[] getSourceFileContents();
+ /**
+ * Return an input stream for the contents.
+ * The compiler will close this stream in all cases!
+ */
+ Reader getSource()
+ throws IOException;
/**
* Returns the name of the top level public type.
- *
+ * This name includes the package.
* @return the name of the top level public type.
*/
- String getMainTypeName();
+ String getMainClassName();
}
Modified:
sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/ErrorHandler.java
URL:
http://svn.apache.org/viewvc/sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/ErrorHandler.java?rev=925247&r1=925246&r2=925247&view=diff
==============================================================================
---
sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/ErrorHandler.java
(original)
+++
sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/ErrorHandler.java
Fri Mar 19 14:50:41 2010
@@ -17,25 +17,25 @@
package org.apache.sling.commons.compiler;
/**
- *
+ * The error handler for the compilation.
*/
public interface ErrorHandler {
/**
- *
- * @param msg
- * @param sourceFile
- * @param line
- * @param position
+ * Notify the handler of an error.
+ * @param msg The error message.
+ * @param sourceFile The source file the error occured in
+ * @param line The source line number
+ * @param position The column
*/
void onError(String msg, String sourceFile, int line, int position);
/**
- *
- * @param msg
- * @param sourceFile
- * @param line
- * @param position
+ * Notify the handler of a warning.
+ * @param msg The warning message.
+ * @param sourceFile The source file the warning occured in
+ * @param line The source line number
+ * @param position The column
*/
void onWarning(String msg, String sourceFile, int line, int position);
}
Modified:
sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/JavaCompiler.java
URL:
http://svn.apache.org/viewvc/sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/JavaCompiler.java?rev=925247&r1=925246&r2=925247&view=diff
==============================================================================
---
sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/JavaCompiler.java
(original)
+++
sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/JavaCompiler.java
Fri Mar 19 14:50:41 2010
@@ -17,12 +17,20 @@
package org.apache.sling.commons.compiler;
/**
- * The <code>JavaCompiler</code> provides platform independant Java Compilation
- * support.
+ * The <code>JavaCompiler</code> provides platform independant Java
+ * compilation support.
*/
public interface JavaCompiler {
- boolean compile(CompileUnit[] units, CompilerEnvironment env,
- ClassWriter classWriter, ErrorHandler errorHandler,
- Options options);
+ /**
+ * Compile the compilation units.
+ * @param units The compilation units.
+ * @param errorHandler The error handler - this object is mandatory
+ * @param options The compilation options - this object is optional
+ * @return <code>true</code> if compilation was successful
+ * @since 2.0
+ */
+ boolean compile(CompilationUnit[] units,
+ ErrorHandler errorHandler,
+ Options options);
}
Modified:
sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/Options.java
URL:
http://svn.apache.org/viewvc/sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/Options.java?rev=925247&r1=925246&r2=925247&view=diff
==============================================================================
---
sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/Options.java
(original)
+++
sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/Options.java
Fri Mar 19 14:50:41 2010
@@ -16,7 +16,21 @@
*/
package org.apache.sling.commons.compiler;
-public class Options {
+import java.util.HashMap;
+
+/**
+ * Options for the compilation process.
+ */
+public class Options extends HashMap<String, Object> {
+
+ /** The key for the source version. */
+ public static final String KEY_SOURCE_VERSION = "sourceVersion";
+
+ /** The key for the target version. */
+ public static final String KEY_TARGET_VERSION = "targetVersion";
+
+ /** The key for the generate debug info flag. */
+ public static final String KEY_GENERATE_DEBUG_INFO = "generateDebugInfo";
public static final String VERSION_RUNTIME = null;
public static final String VERSION_1_1 = "1.1";
@@ -24,33 +38,54 @@ public class Options {
public static final String VERSION_1_3 = "1.3";
public static final String VERSION_1_4 = "1.4";
public static final String VERSION_1_5 = "1.5";
- public static final String VERSION_1_6 = "1.6";
-
- protected String sourceVersion;
- protected boolean generateDebugInfo;
+ public static final String VERSION_1_6 = "1.6";
+ public static final String VERSION_1_7 = "1.7";
+ /** The key for the class loader writer.
+ * By default the registered class loader writer service is used. */
+ public static final String KEY_CLASS_LOADER_WRITER = "classLoaderWriter";
+
+ /**
+ * The key for the class loader.
+ * By default the commons dynamic classloader is used.
+ * This property overrides the classloader and ignores the
+ * {...@link #KEY_ADDITIONAL_CLASS_LOADER} completly!
+ */
+ public static final String KEY_CLASS_LOADER = "classLoader";
+
+ /**
+ * The key for the additional class loader.
+ * By default the commons dynamic classloader is used.
+ * If this property is used and the {...@link #KEY_CLASS_LOADER}
+ * property is not defined, a classloader with the dynamic
+ * class loader (default) and the class loader specified here
+ * is used.
+ */
+ public static final String KEY_ADDITIONAL_CLASS_LOADER = "classLoader";
+
+ /**
+ * Default options with the following presets:
+ * - generate debug info : true
+ */
public Options() {
- this(VERSION_RUNTIME, true);
- }
-
- public Options(String sourceVersion, boolean generateDebugInfo) {
- this.sourceVersion = sourceVersion;
- this.generateDebugInfo = generateDebugInfo;
+ this.put(KEY_GENERATE_DEBUG_INFO, true);
}
public String getSourceVersion() {
- return sourceVersion;
+ return (String) this.get(KEY_SOURCE_VERSION);
}
- public void setSourceVersion(String sourceVersion) {
- this.sourceVersion = sourceVersion;
+ /**
+ * @since 2.0
+ */
+ public String getTargetVersion() {
+ return (String) this.get(KEY_TARGET_VERSION);
}
public boolean isGenerateDebugInfo() {
- return generateDebugInfo;
- }
-
- public void setGenerateDebugInfo(boolean generateDebugInfo) {
- this.generateDebugInfo = generateDebugInfo;
+ if ( this.get(KEY_GENERATE_DEBUG_INFO) != null ) {
+ return (Boolean) this.get(KEY_GENERATE_DEBUG_INFO);
+ }
+ return false;
}
}
Modified:
sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/impl/EclipseJavaCompiler.java
URL:
http://svn.apache.org/viewvc/sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/impl/EclipseJavaCompiler.java?rev=925247&r1=925246&r2=925247&view=diff
==============================================================================
---
sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/impl/EclipseJavaCompiler.java
(original)
+++
sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/impl/EclipseJavaCompiler.java
Fri Mar 19 14:50:41 2010
@@ -17,16 +17,22 @@
package org.apache.sling.commons.compiler.impl;
import java.io.BufferedReader;
-import java.io.CharArrayReader;
+import java.io.ByteArrayOutputStream;
import java.io.IOException;
-import java.io.PrintWriter;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.Reader;
+import java.net.URL;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
-import org.apache.sling.commons.compiler.ClassWriter;
-import org.apache.sling.commons.compiler.CompileUnit;
-import org.apache.sling.commons.compiler.CompilerEnvironment;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.commons.classloader.ClassLoaderWriter;
+import org.apache.sling.commons.classloader.DynamicClassLoaderManager;
+import org.apache.sling.commons.compiler.CompilationUnit;
import org.apache.sling.commons.compiler.ErrorHandler;
import org.apache.sling.commons.compiler.JavaCompiler;
import org.apache.sling.commons.compiler.Options;
@@ -48,122 +54,194 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
- * The <code>EclipseJavaCompiler</code> provides platform independant Java
Compilation
- * support using the Eclipse Java Compiler (org.eclipse.jdt).
+ * The <code>EclipseJavaCompiler</code> provides platform independant
+ * Java compilation support using the Eclipse Java Compiler (org.eclipse.jdt).
*
- * @scr.component metatype="no"
- * @scr.service interface="org.apache.sling.commons.compiler.JavaCompiler"
*/
+...@component
+...@service(value=JavaCompiler.class)
public class EclipseJavaCompiler implements JavaCompiler {
/** Logger instance */
- private static final Logger log =
LoggerFactory.getLogger(EclipseJavaCompiler.class);
+ private final Logger logger =
LoggerFactory.getLogger(EclipseJavaCompiler.class);
- // the static problem factory
- private static IProblemFactory PROBLEM_FACTORY =
- new DefaultProblemFactory(Locale.getDefault());
+ @Reference
+ private ClassLoaderWriter classLoaderWriter;
- public EclipseJavaCompiler() {
+ @Reference
+ private DynamicClassLoaderManager dynamicClassLoaderManager;
+
+ private ClassLoader classLoader;
+
+ /** the static problem factory */
+ private IProblemFactory problemFactory = new
DefaultProblemFactory(Locale.getDefault());
+
+ /** the static policy. */
+ private final IErrorHandlingPolicy policy =
DefaultErrorHandlingPolicies.proceedWithAllProblems();
+
+ /**
+ * Bind the class load provider.
+ * @param repositoryClassLoaderProvider the new provider
+ */
+ protected void bindDynamicClassLoaderManager(final
DynamicClassLoaderManager rclp) {
+ if ( this.classLoader != null ) {
+ this.ungetClassLoader();
+ }
+ this.getClassLoader(rclp);
}
/**
- * @see
org.apache.sling.commons.compiler.JavaCompiler#compile(org.apache.sling.commons.compiler.CompileUnit[],
org.apache.sling.commons.compiler.CompilerEnvironment,
org.apache.sling.commons.compiler.ClassWriter,
org.apache.sling.commons.compiler.ErrorHandler,
org.apache.sling.commons.compiler.Options)
+ * Unbind the class loader provider.
+ * @param repositoryClassLoaderProvider the old provider
*/
- public boolean compile(CompileUnit[] units, CompilerEnvironment env,
- ClassWriter classWriter, ErrorHandler errorHandler,
- Options options) {
+ protected void unbindDynamicClassLoaderManager(final
DynamicClassLoaderManager rclp) {
+ if ( this.dynamicClassLoaderManager == rclp ) {
+ this.ungetClassLoader();
+ }
+ }
- IErrorHandlingPolicy policy =
- DefaultErrorHandlingPolicies.proceedWithAllProblems();
+ /**
+ * Get the class loader
+ */
+ private void getClassLoader(final DynamicClassLoaderManager rclp) {
+ this.dynamicClassLoaderManager = rclp;
+ this.classLoader = rclp.getDynamicClassLoader();
+ }
- // output for non-error log messages
- PrintWriter logWriter = null;
+ /**
+ * Unget the class loader
+ */
+ private void ungetClassLoader() {
+ this.classLoader = null;
+ this.dynamicClassLoaderManager = null;
+ }
- if (options == null) {
- options = new Options();
- }
+ /**
+ * @see
org.apache.sling.commons.compiler.JavaCompiler#compile(org.apache.sling.commons.compiler.CompilationUnit[],
org.apache.sling.commons.compiler.ErrorHandler,
org.apache.sling.commons.compiler.Options)
+ */
+ public boolean compile(final CompilationUnit[] units,
+ final ErrorHandler errorHandler,
+ final Options compileOptions) {
+ // make sure we have an options object (to avoid null checks all over
the place)
+ final Options options = (compileOptions != null ? compileOptions : new
Options());
- Map<String, String> props = new HashMap<String, String>();
+ // create properties for the settings object
+ final Map<String, String> props = new HashMap<String, String>();
if (options.isGenerateDebugInfo()) {
props.put("org.eclipse.jdt.core.compiler.debug.localVariable",
"generate");
props.put("org.eclipse.jdt.core.compiler.debug.lineNumber",
"generate");
props.put("org.eclipse.jdt.core.compiler.debug.sourceFile",
"generate");
}
- String sourceVersion = options.getSourceVersion();
- if (sourceVersion != null) {
- props.put("org.eclipse.jdt.core.compiler.source", sourceVersion);
- //options.put("org.eclipse.jdt.core.compiler.compliance",
sourceVersion);
-
//options.put("org.eclipse.jdt.core.compiler.codegen.targetPlatform",
sourceVersion);
+ if (options.getSourceVersion() != null) {
+ props.put("org.eclipse.jdt.core.compiler.source",
options.getSourceVersion());
+ //props.put("org.eclipse.jdt.core.compiler.compliance",
options.getSourceVersion());
+
//props.put("org.eclipse.jdt.core.compiler.codegen.targetPlatform",
options.getSourceVersion());
+ }
+ if (options.getTargetVersion() != null) {
+ props.put("org.eclipse.jdt.core.compiler.codegen.targetPlatform",
options.getTargetVersion());
+ }
+ props.put("org.eclipse.jdt.core.encoding", "UTF8");
+
+ // create the settings
+ final CompilerOptions settings = new CompilerOptions(props);
+ logger.debug("Compiling with settings {}.", settings);
+
+ // classloader
+ final ClassLoader loader;
+ if ( options.get(Options.KEY_CLASS_LOADER) != null ) {
+ loader = (ClassLoader)options.get(Options.KEY_CLASS_LOADER);
+ } else if ( options.get(Options.KEY_ADDITIONAL_CLASS_LOADER) != null )
{
+ final ClassLoader additionalClassLoader =
(ClassLoader)options.get(Options.KEY_ADDITIONAL_CLASS_LOADER);
+ loader = new ClassLoader(this.classLoader) {
+ protected Class<?> findClass(String name)
+ throws ClassNotFoundException {
+ return additionalClassLoader.loadClass(name);
+ }
+
+ protected URL findResource(String name) {
+ return additionalClassLoader.getResource(name);
+ }
+ };
+ } else {
+ loader = this.classLoader;
}
- //options.put("org.eclipse.jdt.core.encoding", "UTF8");
- CompilerOptions settings = new CompilerOptions(props);
- CompileContext context = new CompileContext(units, env, errorHandler,
classWriter);
+ // classloader writer
+ final ClassLoaderWriter writer =
(options.get(Options.KEY_CLASS_LOADER_WRITER) != null ?
+
(ClassLoaderWriter)options.get(Options.KEY_CLASS_LOADER_WRITER) :
this.classLoaderWriter);
- if (log.isDebugEnabled()) {
- log.debug(settings.toString());
- }
+ // create the context
+ final CompileContext context = new CompileContext(units, errorHandler,
writer, loader);
- org.eclipse.jdt.internal.compiler.Compiler compiler =
+ // create the compiler
+ final org.eclipse.jdt.internal.compiler.Compiler compiler =
new org.eclipse.jdt.internal.compiler.Compiler(
context,
- policy,
+ this.policy,
settings,
context,
- PROBLEM_FACTORY,
- logWriter);
-
- compiler.compile(context.sourceUnits());
+ this.problemFactory,
+ null);
- context.cleanup();
+ // compile
+ compiler.compile(context.getSourceUnits());
- return !context.hadErrors;
+ return !context.hasErrors;
}
//--------------------------------------------------------< inner classes >
- private class CompileContext implements ICompilerRequestor,
INameEnvironment {
+ private class CompileContext implements ICompilerRequestor,
INameEnvironment, ErrorHandler {
- boolean hadErrors;
- HashMap<String,ICompilationUnit> compUnits;
+ private final Map<String,ICompilationUnit> compUnits;
- ErrorHandler errorHandler;
- ClassWriter classWriter;
-
- CompilerEnvironment compEnv;
-
- CompileContext(CompileUnit[] units,
- CompilerEnvironment compEnv,
- ErrorHandler errorHandler,
- ClassWriter classWriter) {
-
- compUnits = new HashMap<String,ICompilationUnit>(units.length);
+ private final ErrorHandler errorHandler;
+ private final ClassLoaderWriter classLoaderWriter;
+ private final ClassLoader classLoader;
+
+ /** Flag indicating if we have an error. */
+ private boolean hasErrors = false;
+
+ public CompileContext(final CompilationUnit[] units,
+ final ErrorHandler errorHandler,
+ final ClassLoaderWriter classWriter,
+ final ClassLoader classLoader) {
+ this.compUnits = new HashMap<String,ICompilationUnit>();
for (int i = 0; i < units.length; i++) {
- CompilationUnitAdapter cua = new
CompilationUnitAdapter(units[i]);
+ CompilationUnitAdapter cua = new
CompilationUnitAdapter(units[i], this);
char[][] compoundName =
CharOperation.arrayConcat(cua.getPackageName(), cua.getMainTypeName());
- compUnits.put(CharOperation.toString(compoundName), new
CompilationUnitAdapter(units[i]));
+ this.compUnits.put(CharOperation.toString(compoundName), new
CompilationUnitAdapter(units[i], this));
}
- this.compEnv = compEnv;
this.errorHandler = errorHandler;
- this.classWriter = classWriter;
- hadErrors = false;
+ this.classLoaderWriter = classWriter;
+ this.classLoader = classLoader;
}
- ICompilationUnit[] sourceUnits() {
+ /**
+ * @see
org.apache.sling.commons.compiler.ErrorHandler#onError(java.lang.String,
java.lang.String, int, int)
+ */
+ public void onError(String msg, String sourceFile, int line, int
position) {
+ this.errorHandler.onError(msg, sourceFile, line, position);
+ this.hasErrors = true;
+ }
+
+ /**
+ * @see
org.apache.sling.commons.compiler.ErrorHandler#onWarning(java.lang.String,
java.lang.String, int, int)
+ */
+ public void onWarning(String msg, String sourceFile, int line, int
position) {
+ this.errorHandler.onWarning(msg, sourceFile, line, position);
+ }
+
+ public ICompilationUnit[] getSourceUnits() {
return compUnits.values().toArray(
new ICompilationUnit[compUnits.size()]);
}
- //---------------------------------------------------<
ICompilerRequestor >
/**
- * {...@inheritdoc}
+ * @see
org.eclipse.jdt.internal.compiler.ICompilerRequestor#acceptResult(org.eclipse.jdt.internal.compiler.CompilationResult)
*/
public void acceptResult(CompilationResult result) {
- if (result.hasErrors()) {
- hadErrors = true;
- }
-
if (result.hasProblems()) {
CategorizedProblem[] problems = result.getProblems();
for (int i = 0; i < problems.length; i++) {
@@ -174,11 +252,11 @@ public class EclipseJavaCompiler impleme
int pos = problem.getSourceStart();
if (problem.isError()) {
- errorHandler.onError(msg, fileName, line, pos);
+ this.onError(msg, fileName, line, pos);
} else if (problem.isWarning()) {
- errorHandler.onWarning(msg, fileName, line, pos);
+ this.onWarning(msg, fileName, line, pos);
} else {
- log.debug("unknown problem category: " +
problem.toString());
+ logger.debug("unknown problem category: {}", problem);
}
}
}
@@ -187,16 +265,15 @@ public class EclipseJavaCompiler impleme
ClassFile classFile = classFiles[i];
String className =
CharOperation.toString(classFile.getCompoundName());
try {
- classWriter.write(className, classFile.getBytes());
- } catch (Exception e) {
- log.error("failed to persist class " + className, e);
+ this.write(className, classFile.getBytes());
+ } catch (IOException e) {
+ this.onError("Unable to write class file: " +
e.getMessage(), className, 0, 0);
}
}
}
- //-------------------------------------------------< INameEnvironment >
/**
- * {...@inheritdoc}
+ * @see
org.eclipse.jdt.internal.compiler.env.INameEnvironment#findType(char[][])
*/
public NameEnvironmentAnswer findType(char[][] compoundTypeName) {
// check 1st if type corresponds with any of current compilation
units
@@ -208,7 +285,7 @@ public class EclipseJavaCompiler impleme
// locate the class through the class loader
try {
- byte[] bytes =
compEnv.findClass(CharOperation.toString(compoundTypeName));
+ byte[] bytes =
this.findClass(CharOperation.toString(compoundTypeName));
if (bytes == null) {
return null;
}
@@ -221,78 +298,145 @@ public class EclipseJavaCompiler impleme
}
/**
- * {...@inheritdoc}
+ * @see
org.eclipse.jdt.internal.compiler.env.INameEnvironment#findType(char[],
char[][])
*/
public NameEnvironmentAnswer findType(char[] typeName, char[][]
packageName) {
return findType(CharOperation.arrayConcat(packageName, typeName));
}
/**
- * {...@inheritdoc}
+ * @see
org.eclipse.jdt.internal.compiler.env.INameEnvironment#isPackage(char[][],
char[])
*/
public boolean isPackage(char[][] parentPackageName, char[]
packageName) {
String fqn = CharOperation.toString(
CharOperation.arrayConcat(parentPackageName, packageName));
- return compUnits.get(fqn) == null && compEnv.isPackage(fqn);
+ return compUnits.get(fqn) == null && this.isPackage(fqn);
}
/**
- * {...@inheritdoc}
+ * @see
org.eclipse.jdt.internal.compiler.env.INameEnvironment#cleanup()
*/
public void cleanup() {
- compEnv.cleanup();
+ // nothing to do
+ }
+
+ /**
+ * Write the classfile
+ */
+ private void write(String name, byte[] data) throws IOException {
+ final OutputStream os = this.classLoaderWriter.getOutputStream('/'
+ name.replace('.', '/') + ".class");
+ os.write(data);
+ os.close();
+ }
+
+ private boolean isPackage(String result) {
+ String resourceName = result.replace('.', '/') + ".class";
+ if ( resourceName.startsWith("/") ) {
+ resourceName = resourceName.substring(1);
+ }
+ final InputStream is =
this.classLoader.getResourceAsStream(resourceName);
+ if ( is != null ) {
+ try {
+ is.close();
+ } catch (IOException ignore) {}
+ }
+ return is == null;
+ }
+
+ private byte[] findClass(String name) throws Exception {
+ final String resourceName = name.replace('.', '/') + ".class";
+ final InputStream is =
this.classLoader.getResourceAsStream(resourceName);
+ if (is != null) {
+ try {
+ byte[] buf = new byte[8192];
+ ByteArrayOutputStream baos = new
ByteArrayOutputStream(buf.length);
+ int count;
+ while ((count = is.read(buf, 0, buf.length)) > 0) {
+ baos.write(buf, 0, count);
+ }
+ baos.flush();
+ return baos.toByteArray();
+ } finally {
+ try {
+ is.close();
+ } catch (IOException ignore) {}
+ }
+ }
+ return null;
}
}
private class CompilationUnitAdapter implements ICompilationUnit {
- CompileUnit compUnit;
- char[][] packageName;
+ private final ErrorHandler errorHandler;
+ private final CompilationUnit compUnit;
+ private final String mainTypeName;
+ private final String packageName;
- CompilationUnitAdapter(CompileUnit compUnit) {
+ public CompilationUnitAdapter(final CompilationUnit compUnit, final
ErrorHandler errorHandler) {
this.compUnit = compUnit;
+ this.errorHandler = errorHandler;
+ final int pos = compUnit.getMainClassName().lastIndexOf('.');
+ if ( pos == -1 ) {
+ this.packageName = "";
+ this.mainTypeName = compUnit.getMainClassName();
+ } else {
+ this.packageName = compUnit.getMainClassName().substring(0,
pos);
+ this.mainTypeName = compUnit.getMainClassName().substring(pos
+ 1);
+ }
}
- String extractPackageName(char[] contents) {
- BufferedReader reader = new BufferedReader(new
CharArrayReader(contents));
+ /**
+ * @see
org.eclipse.jdt.internal.compiler.env.ICompilationUnit#getContents()
+ */
+ public char[] getContents() {
+ Reader fr = null;
try {
- String line;
- while ((line = reader.readLine()) != null) {
- line = line.trim();
- if (line.startsWith("package")) {
- line = line.substring("package".length());
- line = line.substring(0, line.lastIndexOf(';'));
- return line.trim();
+ fr = this.compUnit.getSource();
+ final Reader reader = new BufferedReader(fr);
+ try {
+ char[] chars = new char[8192];
+ StringBuilder buf = new StringBuilder();
+ int count;
+ while ((count = reader.read(chars, 0, chars.length)) > 0) {
+ buf.append(chars, 0, count);
}
+ final char[] result = new char[buf.length()];
+ buf.getChars(0, result.length, result, 0);
+ return result;
+ } finally {
+ reader.close();
}
} catch (IOException e) {
- // should never get here...
+ this.errorHandler.onError("Unable to read source file " +
this.compUnit.getMainClassName() + " : " + e.getMessage(),
+ this.compUnit.getMainClassName(), 0, 0);
+ return null;
+ } finally {
+ if ( fr != null ) {
+ try { fr.close(); } catch (IOException ignore) {}
+ }
}
-
- // no package declaration found
- return "";
- }
-
- //-------------------------------------------------< ICompilationUnit >
-
- public char[] getContents() {
- return compUnit.getSourceFileContents();
}
+ /**
+ * @see
org.eclipse.jdt.internal.compiler.env.ICompilationUnit#getMainTypeName()
+ */
public char[] getMainTypeName() {
- return compUnit.getMainTypeName().toCharArray();
+ return this.mainTypeName.toCharArray();
}
+ /**
+ * @see
org.eclipse.jdt.internal.compiler.env.ICompilationUnit#getPackageName()
+ */
public char[][] getPackageName() {
- if (packageName == null) {
- String s =
extractPackageName(compUnit.getSourceFileContents());
- packageName = CharOperation.splitOn('.', s.toCharArray());
- }
- return packageName;
+ return CharOperation.splitOn('.', this.packageName.toCharArray());
}
+ /**
+ * @see org.eclipse.jdt.internal.compiler.env.IDependent#getFileName()
+ */
public char[] getFileName() {
- return compUnit.getSourceFileName().toCharArray();
+ return (this.mainTypeName + ".java").toCharArray();
}
}
}
Modified:
sling/trunk/contrib/commons/compiler/src/main/resources/META-INF/NOTICE
URL:
http://svn.apache.org/viewvc/sling/trunk/contrib/commons/compiler/src/main/resources/META-INF/NOTICE?rev=925247&r1=925246&r2=925247&view=diff
==============================================================================
--- sling/trunk/contrib/commons/compiler/src/main/resources/META-INF/NOTICE
(original)
+++ sling/trunk/contrib/commons/compiler/src/main/resources/META-INF/NOTICE Fri
Mar 19 14:50:41 2010
@@ -1,5 +1,5 @@
Apache Sling Java Compiler
-Copyright 2008-2009 The Apache Software Foundation
+Copyright 2008-2010 The Apache Software Foundation
Apache Sling is based on source code originally developed
by Day Software (http://www.day.com/).
Modified:
sling/trunk/contrib/commons/compiler/src/test/java/org/apache/sling/commons/compiler/impl/CompilerJava5Test.java
URL:
http://svn.apache.org/viewvc/sling/trunk/contrib/commons/compiler/src/test/java/org/apache/sling/commons/compiler/impl/CompilerJava5Test.java?rev=925247&r1=925246&r2=925247&view=diff
==============================================================================
---
sling/trunk/contrib/commons/compiler/src/test/java/org/apache/sling/commons/compiler/impl/CompilerJava5Test.java
(original)
+++
sling/trunk/contrib/commons/compiler/src/test/java/org/apache/sling/commons/compiler/impl/CompilerJava5Test.java
Fri Mar 19 14:50:41 2010
@@ -17,17 +17,16 @@
package org.apache.sling.commons.compiler.impl;
import java.io.ByteArrayOutputStream;
-import java.io.CharArrayWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
+import java.io.OutputStream;
import java.io.Reader;
import junit.framework.TestCase;
-import org.apache.sling.commons.compiler.ClassWriter;
-import org.apache.sling.commons.compiler.CompileUnit;
-import org.apache.sling.commons.compiler.CompilerEnvironment;
+import org.apache.sling.commons.classloader.ClassLoaderWriter;
+import org.apache.sling.commons.compiler.CompilationUnit;
import org.apache.sling.commons.compiler.ErrorHandler;
import org.apache.sling.commons.compiler.Options;
@@ -35,13 +34,18 @@ import org.apache.sling.commons.compiler
* Test case for java 5 support
*/
public class CompilerJava5Test extends TestCase
- implements CompilerEnvironment, ErrorHandler, ClassWriter {
+ implements ErrorHandler, ClassLoaderWriter {
public void testJava5Support() throws Exception {
String sourceFile = "Java5Test";
- CompileUnit unit = createCompileUnit(sourceFile);
- new EclipseJavaCompiler().compile(new CompileUnit[]{unit}, this, this,
this, new Options(Options.VERSION_1_5, true));
+ CompilationUnit unit = createCompileUnit(sourceFile);
+ final Options options = new Options();
+ options.put(Options.KEY_SOURCE_VERSION, Options.VERSION_1_5);
+ options.put(Options.KEY_CLASS_LOADER_WRITER, this);
+ options.put(Options.KEY_CLASS_LOADER,
this.getClass().getClassLoader());
+
+ assertTrue(new EclipseJavaCompiler().compile(new
CompilationUnit[]{unit}, this, options));
}
//---------------------------------------------------------< ErrorHandler >
@@ -54,102 +58,62 @@ public class CompilerJava5Test extends T
System.out.println("Warning in " + sourceFile + ", line " + line + ",
pos. " + position + ": " + msg);
}
- //--------------------------------------------------< CompilerEnvironment >
-
- public byte[] findClass(String className) throws Exception {
- ClassLoader cl = Thread.currentThread().getContextClassLoader();
- InputStream in = cl.getResourceAsStream(className.replace('.', '/') +
".class");
- if (in == null) {
- return null;
- }
- ByteArrayOutputStream out = new ByteArrayOutputStream(0x7fff);
-
- try {
- byte[] buffer = new byte[0x1000];
- int read = 0;
- while ((read = in.read(buffer)) > 0) {
- out.write(buffer, 0, read);
- }
- } finally {
- //out.close();
- in.close();
- }
-
- return out.toByteArray();
- }
-
- public char[] findSource(String className) throws Exception {
- return new char[0];
- }
-
- public boolean isPackage(String packageName) {
- ClassLoader cl = Thread.currentThread().getContextClassLoader();
- InputStream in = cl.getResourceAsStream(packageName.replace('.', '/')
+ ".class");
- if (in != null) {
- try {
- in.close();
- } catch (IOException ignore) {
- }
- return false;
- }
- return true;
- }
-
- public void cleanup() {
- }
-
- //----------------------------------------------------------< ClassWriter >
-
- public void write(String className, byte[] data) throws Exception {
- // nothing to do
- }
+ //----------------------------------------------------------<
ClassLoaderWriter >
//--------------------------------------------------------< misc. helpers >
- private CompileUnit createCompileUnit(final String sourceFile) throws
Exception {
- final char[] chars = readTextResource(sourceFile);
-
- return new CompileUnit() {
+ private CompilationUnit createCompileUnit(final String sourceFile) throws
Exception {
+ return new CompilationUnit() {
- public String getSourceFileName() {
- return sourceFile;
- }
-
- public char[] getSourceFileContents() {
- return chars;
- }
-
- public String getMainTypeName() {
- String className;
- int pos = sourceFile.lastIndexOf(".java");
- if (pos != -1) {
- className = sourceFile.substring(0, pos).trim();
- } else {
- className = sourceFile.trim();
- }
- pos = className.lastIndexOf('/');
- return (pos == -1) ? className : className.substring(pos);
+ /**
+ * @see
org.apache.sling.commons.compiler.CompilationUnit#getMainClassName()
+ */
+ public String getMainClassName() {
+ return "org.apache.sling.commons.compiler.test." + sourceFile;
+ }
+
+ /**
+ * @see
org.apache.sling.commons.compiler.CompilationUnit#getSource()
+ */
+ public Reader getSource() throws IOException {
+ InputStream in =
getClass().getClassLoader().getResourceAsStream(sourceFile);
+ return new InputStreamReader(in, "UTF-8");
}
};
}
- private char[] readTextResource(String resourcePath) throws IOException {
- InputStream in =
getClass().getClassLoader().getResourceAsStream(resourcePath);
- if (in == null) {
- throw new IOException("resource not found");
- }
- Reader reader = new InputStreamReader(in);
- CharArrayWriter writer = new CharArrayWriter(0x7fff);
- try {
- char[] buffer = new char[0x1000];
- int read = 0;
- while ((read = reader.read(buffer)) > 0) {
- writer.write(buffer, 0, read);
- }
- return writer.toCharArray();
- } finally {
- //writer.close();
- reader.close();
- }
+ /**
+ * @see
org.apache.sling.commons.classloader.ClassLoaderWriter#delete(java.lang.String)
+ */
+ public boolean delete(String path) {
+ return false;
+ }
+
+ /**
+ * @see
org.apache.sling.commons.classloader.ClassLoaderWriter#getInputStream(java.lang.String)
+ */
+ public InputStream getInputStream(String path) throws IOException {
+ return null;
+ }
+
+ /**
+ * @see
org.apache.sling.commons.classloader.ClassLoaderWriter#getLastModified(java.lang.String)
+ */
+ public long getLastModified(String path) {
+ return 0;
+ }
+
+ /**
+ * @see
org.apache.sling.commons.classloader.ClassLoaderWriter#getOutputStream(java.lang.String)
+ */
+ public OutputStream getOutputStream(String path) {
+ return new ByteArrayOutputStream();
+ }
+
+ /**
+ * @see
org.apache.sling.commons.classloader.ClassLoaderWriter#rename(java.lang.String,
java.lang.String)
+ */
+ public boolean rename(String oldPath, String newPath) {
+ return false;
}
}
Modified: sling/trunk/contrib/commons/compiler/src/test/resources/Java5Test
URL:
http://svn.apache.org/viewvc/sling/trunk/contrib/commons/compiler/src/test/resources/Java5Test?rev=925247&r1=925246&r2=925247&view=diff
==============================================================================
--- sling/trunk/contrib/commons/compiler/src/test/resources/Java5Test (original)
+++ sling/trunk/contrib/commons/compiler/src/test/resources/Java5Test Fri Mar
19 14:50:41 2010
@@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.sling.commons.compiler.test ;
+package org.apache.sling.commons.compiler.test;
import java.util.List;