Author: cziegeler
Date: Wed Mar 24 07:54:50 2010
New Revision: 926965
URL: http://svn.apache.org/viewvc?rev=926965&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/CompilationResult.java
- copied, changed from r926092,
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/CompilerMessage.java
(with props)
sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/impl/CompilationResultImpl.java
(with props)
Removed:
sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/ErrorHandler.java
Modified:
sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/CompilationUnit.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/test/java/org/apache/sling/commons/compiler/impl/CompilerJava5Test.java
Copied:
sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/CompilationResult.java
(from r926092,
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/CompilationResult.java?p2=sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/CompilationResult.java&p1=sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/ErrorHandler.java&r1=926092&r2=926965&rev=926965&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/CompilationResult.java
Wed Mar 24 07:54:50 2010
@@ -16,26 +16,49 @@
*/
package org.apache.sling.commons.compiler;
+import java.util.List;
+
/**
- * The error handler for the compilation.
+ * The compilation result allows clients of the java compiler
+ * to check for error messages, warnings (if not disabled by
+ * the options) and allows to access the compiled classes.
+ * @since 2.0
*/
-public interface ErrorHandler {
+public interface CompilationResult {
+
+ /**
+ * Return a list of error messages that occured during
+ * compilation. If no errors occured <code>null</code>
+ * is returned.
+ * @return A list of error messages or <code>null</code>.
+ */
+ List<CompilerMessage> getErrors();
+
+ /**
+ * Return a list of warnings that occured during
+ * compilation. If no warnings occured <code>null</code>
+ * is returned.
+ * @return A list of warnings or <code>null</code>.
+ */
+ List<CompilerMessage> getWarnings();
/**
- * 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
+ * Was a compilation required or were all classes recent?
+ * @return <code>true>/code> if classes were compiled.
*/
- void onError(String msg, String sourceFile, int line, int position);
+ boolean didCompile();
/**
- * 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
+ * Try to load the compiled class.
+ * The class loading might fail if the className is not
+ * one of the compiled sources, if the compilation failed
+ * or if a class loader writer has been used in combination
+ * with a class loader that is not able to load the classes
+ * written by the class loader writer.
+ * @return The compiled class
+ * @throws ClassNotFoundException If the class could not be found
+ * or compilation failed.
*/
- void onWarning(String msg, String sourceFile, int line, int position);
+ Class<?> loadCompiledClass(final String className)
+ throws ClassNotFoundException;
}
Modified:
sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/CompilationUnit.java
URL:
http://svn.apache.org/viewvc/sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/CompilationUnit.java?rev=926965&r1=926964&r2=926965&view=diff
==============================================================================
---
sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/CompilationUnit.java
(original)
+++
sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/CompilationUnit.java
Wed Mar 24 07:54:50 2010
@@ -38,4 +38,11 @@ public interface CompilationUnit {
* @return the name of the top level public type.
*/
String getMainClassName();
+
+ /**
+ * Return the last modified for the compilation unit.
+ * @return The last modified information or <code>-1</code> if
+ * the information can't be detected.
+ */
+ long getLastModified();
}
Added:
sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/CompilerMessage.java
URL:
http://svn.apache.org/viewvc/sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/CompilerMessage.java?rev=926965&view=auto
==============================================================================
---
sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/CompilerMessage.java
(added)
+++
sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/CompilerMessage.java
Wed Mar 24 07:54:50 2010
@@ -0,0 +1,101 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.commons.compiler;
+
+/**
+ * This class encapsulates a message produced the compiler.
+ * A message is either a warning or an error.
+ * The messages are retrieved from the {...@link CompilationResult}.
+ *
+ * @since 2.0
+ */
+public class CompilerMessage {
+
+ /**
+ * The line number of the offending program text
+ */
+ private final int line;
+
+ /**
+ * The column number of the offending program text
+ */
+ private final int column;
+
+ /**
+ * The name of the file containing the offending program text
+ */
+ private final String file;
+
+ /**
+ * The actual text
+ */
+ private final String message;
+
+ /**
+ * The error message constructor.
+ *
+ * @param file The name of the file containing the offending program text
+ * @param line The line number of the offending program text
+ * @param column The column number of the offending program text
+ * @param message The actual text
+ */
+ public CompilerMessage(final String file,
+ final int line,
+ final int column,
+ final String message) {
+ this.file = file;
+ this.line = line;
+ this.column = column;
+ this.message = message;
+ }
+
+ /**
+ * Return the filename associated with this compiler message.
+ *
+ * @return The filename associated with this compiler message
+ */
+ public String getFile() {
+ return file;
+ }
+
+ /**
+ * Return the line number of the program text originating this message
+ *
+ * @return The line number of the program text originating this message
+ */
+ public int getLine() {
+ return this.line;
+ }
+
+ /**
+ * Return the column number of the program text originating this message
+ *
+ * @return The column number of the program text originating this message
+ */
+ public int getColumn() {
+ return this.column;
+ }
+
+ /**
+ * Return the message
+ *
+ * @return The message
+ */
+ public String getMessage() {
+ return message;
+ }
+}
Propchange:
sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/CompilerMessage.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/CompilerMessage.java
------------------------------------------------------------------------------
svn:keywords = author date id revision rev url
Propchange:
sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/CompilerMessage.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
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=926965&r1=926964&r2=926965&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
Wed Mar 24 07:54:50 2010
@@ -24,13 +24,17 @@ public interface JavaCompiler {
/**
* Compile the compilation units.
+ * This method checks if the compilation is necessary by using
+ * last modified check of the source to compile and the class
+ * file (if available).
+ * The compiler compiles all sources if at least one of the
+ * class files is out dated!
+ *
* @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
+ * @return The compilation result with more information.
* @since 2.0
*/
- boolean compile(CompilationUnit[] units,
- ErrorHandler errorHandler,
- Options options);
+ CompilationResult compile(CompilationUnit[] units,
+ 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=926965&r1=926964&r2=926965&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
Wed Mar 24 07:54:50 2010
@@ -63,6 +63,16 @@ public class Options extends HashMap<Str
*/
public static final String KEY_ADDITIONAL_CLASS_LOADER = "classLoader";
+ /** The key to force the compilation - even if the class files are more
recent.
+ * The value should be of type Boolean. */
+ public static final String KEY_FORCE_COMPILATION = "forceCompilation";
+
+ /** The key to ignore warnings - if this option is turned on, the
+ * resulting compilation result does not get the warnings issued
+ * by the compiler.
+ * The value should be of type Boolean. */
+ public static final String KEY_IGNORE_WARNINGS = "ignoreWarnings";
+
/**
* Default options with the following presets:
* - generate debug info : true
Added:
sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/impl/CompilationResultImpl.java
URL:
http://svn.apache.org/viewvc/sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/impl/CompilationResultImpl.java?rev=926965&view=auto
==============================================================================
---
sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/impl/CompilationResultImpl.java
(added)
+++
sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/impl/CompilationResultImpl.java
Wed Mar 24 07:54:50 2010
@@ -0,0 +1,106 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.commons.compiler.impl;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.sling.commons.compiler.CompilationResult;
+import org.apache.sling.commons.compiler.CompilerMessage;
+
+/**
+ * Implementation of the compilation result
+ */
+public class CompilationResultImpl implements CompilationResult {
+
+ private List<CompilerMessage> errors;
+
+ private List<CompilerMessage> warnings;
+
+ private final boolean ignoreWarnings;
+
+ private final boolean compilationRequired;
+
+ private final ClassLoader classLoader;
+
+ public CompilationResultImpl(final ClassLoader classLoader) {
+ this.ignoreWarnings = true;
+ this.classLoader = classLoader;
+ this.compilationRequired = false;
+ }
+
+ public CompilationResultImpl(final boolean ignoreWarnings,
+ final ClassLoader classLoader) {
+ this.ignoreWarnings = ignoreWarnings;
+ this.classLoader = classLoader;
+ this.compilationRequired = true;
+ }
+
+ /**
+ * @see org.apache.sling.commons.compiler.CompilationResult#getErrors()
+ */
+ public List<CompilerMessage> getErrors() {
+ return this.errors;
+ }
+
+ /**
+ * @see org.apache.sling.commons.compiler.CompilationResult#getWarnings()
+ */
+ public List<CompilerMessage> getWarnings() {
+ return this.warnings;
+ }
+
+ /**
+ * @see
org.apache.sling.commons.compiler.CompilationResult#loadCompiledClass(java.lang.String)
+ */
+ public Class<?> loadCompiledClass(final String className)
+ throws ClassNotFoundException {
+ if ( errors != null ) {
+ throw new ClassNotFoundException(className);
+ }
+ return this.classLoader.loadClass(className);
+ }
+
+ /**
+ * @see org.apache.sling.commons.compiler.CompilationResult#didCompile()
+ */
+ public boolean didCompile() {
+ return this.compilationRequired;
+ }
+
+ /**
+ * Notification of an error
+ */
+ public void onError(String msg, String sourceFile, int line, int position)
{
+ if ( errors == null ) {
+ errors = new ArrayList<CompilerMessage>();
+ }
+ errors.add(new CompilerMessage(sourceFile, line, position, msg));
+ }
+
+ /**
+ * Notification of a warning
+ */
+ public void onWarning(String msg, String sourceFile, int line, int
position) {
+ if ( !this.ignoreWarnings ) {
+ if ( warnings == null ) {
+ warnings = new ArrayList<CompilerMessage>();
+ }
+ warnings.add(new CompilerMessage(sourceFile, line, position, msg));
+ }
+ }
+}
Propchange:
sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/impl/CompilationResultImpl.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/impl/CompilationResultImpl.java
------------------------------------------------------------------------------
svn:keywords = author date id revision rev url
Propchange:
sling/trunk/contrib/commons/compiler/src/main/java/org/apache/sling/commons/compiler/impl/CompilationResultImpl.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
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=926965&r1=926964&r2=926965&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
Wed Mar 24 07:54:50 2010
@@ -32,14 +32,13 @@ import org.apache.felix.scr.annotations.
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.CompilationResult;
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;
import org.eclipse.jdt.core.compiler.CategorizedProblem;
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.internal.compiler.ClassFile;
-import org.eclipse.jdt.internal.compiler.CompilationResult;
import org.eclipse.jdt.internal.compiler.DefaultErrorHandlingPolicies;
import org.eclipse.jdt.internal.compiler.ICompilerRequestor;
import org.eclipse.jdt.internal.compiler.IErrorHandlingPolicy;
@@ -101,7 +100,7 @@ public class EclipseJavaCompiler impleme
}
/**
- * Get the class loader
+ * Get the commons dynamic class loader
*/
private void getClassLoader(final DynamicClassLoaderManager rclp) {
this.dynamicClassLoaderManager = rclp;
@@ -109,7 +108,7 @@ public class EclipseJavaCompiler impleme
}
/**
- * Unget the class loader
+ * Unget the commons dynamic class loader
*/
private void ungetClassLoader() {
this.classLoader = null;
@@ -117,13 +116,109 @@ public class EclipseJavaCompiler impleme
}
/**
- * @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)
+ * Get the classloader for the compilation.
*/
- public boolean compile(final CompilationUnit[] units,
- final ErrorHandler errorHandler,
- final Options compileOptions) {
+ private ClassLoader getClassLoader(final Options options) {
+ 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;
+ }
+ return loader;
+ }
+
+ /**
+ * Get the class loader writer for the compilation.
+ */
+ private ClassLoaderWriter getClassLoaderWriter(final Options options) {
+ if (options.get(Options.KEY_CLASS_LOADER_WRITER) != null ) {
+ return
(ClassLoaderWriter)options.get(Options.KEY_CLASS_LOADER_WRITER);
+ }
+ return this.classLoaderWriter;
+ }
+
+ /**
+ * Check if the compiled class file is older than the source file
+ */
+ private boolean isOutDated(final CompilationUnit unit,
+ final ClassLoaderWriter writer) {
+ final long targetLastModified = writer.getLastModified('/' +
unit.getMainClassName().replace('.', '/') + ".class");
+ if (targetLastModified < 0) {
+ return true;
+ }
+
+ return targetLastModified < unit.getLastModified();
+ }
+
+ /**
+ * Return the force compilation value
+ */
+ private boolean isForceCompilation(final Options options) {
+ final Boolean flag =
(Boolean)options.get(Options.KEY_FORCE_COMPILATION);
+ if ( flag != null ) {
+ return flag;
+ }
+ return false;
+ }
+
+ /**
+ * Return the ignore warnings value
+ */
+ private boolean isIgnoreWarnings(final Options options) {
+ final Boolean flag = (Boolean)options.get(Options.KEY_IGNORE_WARNINGS);
+ if ( flag != null ) {
+ return flag;
+ }
+ return false;
+ }
+
+ private static final Options EMPTY_OPTIONS = new Options();
+
+ /**
+ * @see
org.apache.sling.commons.compiler.JavaCompiler#compile(org.apache.sling.commons.compiler.CompilationUnit[],
org.apache.sling.commons.compiler.Options)
+ */
+ public CompilationResult compile(final CompilationUnit[] units,
+ 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());
+ final Options options = (compileOptions != null ? compileOptions :
EMPTY_OPTIONS);
+
+ // get classloader and classloader writer
+ final ClassLoader loader = this.getClassLoader(options);
+ final ClassLoaderWriter writer = this.getClassLoaderWriter(options);
+
+ // check sources for compilation
+ boolean needsCompilation = isForceCompilation(options);
+ if ( !needsCompilation ) {
+ for(final CompilationUnit unit : units) {
+ if ( this.isOutDated(unit, writer) ) {
+ needsCompilation = true;
+ break;
+ }
+ }
+ }
+ if ( !needsCompilation ) {
+ logger.debug("All source files are recent - no compilation
required.");
+ return new CompilationResultImpl(loader);
+ }
+
+ // delete old class files
+ for(final CompilationUnit unit : units) {
+ final String name = '/' + unit.getMainClassName().replace('.',
'/') + ".class";
+ writer.delete(name);
+ }
// create properties for the settings object
final Map<String, String> props = new HashMap<String, String>();
@@ -146,32 +241,10 @@ public class EclipseJavaCompiler impleme
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;
- }
-
- // classloader writer
- final ClassLoaderWriter writer =
(options.get(Options.KEY_CLASS_LOADER_WRITER) != null ?
-
(ClassLoaderWriter)options.get(Options.KEY_CLASS_LOADER_WRITER) :
this.classLoaderWriter);
-
+ // create the result
+ final CompilationResultImpl result = new
CompilationResultImpl(isIgnoreWarnings(options), loader);
// create the context
- final CompileContext context = new CompileContext(units, errorHandler,
writer, loader);
+ final CompileContext context = new CompileContext(units, result,
writer, loader);
// create the compiler
final org.eclipse.jdt.internal.compiler.Compiler compiler =
@@ -186,31 +259,28 @@ public class EclipseJavaCompiler impleme
// compile
compiler.compile(context.getSourceUnits());
- return !context.hasErrors;
+ return result;
}
//--------------------------------------------------------< inner classes >
- private class CompileContext implements ICompilerRequestor,
INameEnvironment, ErrorHandler {
+ private class CompileContext implements ICompilerRequestor,
INameEnvironment {
private final Map<String,ICompilationUnit> compUnits;
- private final ErrorHandler errorHandler;
+ private final CompilationResultImpl 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 CompilationResultImpl 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], this);
+ CompilationUnitAdapter cua = new
CompilationUnitAdapter(units[i], errorHandler);
char[][] compoundName =
CharOperation.arrayConcat(cua.getPackageName(), cua.getMainTypeName());
- this.compUnits.put(CharOperation.toString(compoundName), new
CompilationUnitAdapter(units[i], this));
+ this.compUnits.put(CharOperation.toString(compoundName), new
CompilationUnitAdapter(units[i], errorHandler));
}
this.errorHandler = errorHandler;
@@ -218,21 +288,6 @@ public class EclipseJavaCompiler impleme
this.classLoader = classLoader;
}
- /**
- * @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()]);
@@ -241,7 +296,7 @@ public class EclipseJavaCompiler impleme
/**
* @see
org.eclipse.jdt.internal.compiler.ICompilerRequestor#acceptResult(org.eclipse.jdt.internal.compiler.CompilationResult)
*/
- public void acceptResult(CompilationResult result) {
+ public void
acceptResult(org.eclipse.jdt.internal.compiler.CompilationResult result) {
if (result.hasProblems()) {
CategorizedProblem[] problems = result.getProblems();
for (int i = 0; i < problems.length; i++) {
@@ -252,9 +307,9 @@ public class EclipseJavaCompiler impleme
int pos = problem.getSourceStart();
if (problem.isError()) {
- this.onError(msg, fileName, line, pos);
+ this.errorHandler.onError(msg, fileName, line, pos);
} else if (problem.isWarning()) {
- this.onWarning(msg, fileName, line, pos);
+ this.errorHandler.onWarning(msg, fileName, line, pos);
} else {
logger.debug("unknown problem category: {}", problem);
}
@@ -267,7 +322,7 @@ public class EclipseJavaCompiler impleme
try {
this.write(className, classFile.getBytes());
} catch (IOException e) {
- this.onError("Unable to write class file: " +
e.getMessage(), className, 0, 0);
+ this.errorHandler.onError("Unable to write class file: " +
e.getMessage(), className, 0, 0);
}
}
}
@@ -368,12 +423,12 @@ public class EclipseJavaCompiler impleme
private class CompilationUnitAdapter implements ICompilationUnit {
- private final ErrorHandler errorHandler;
+ private final CompilationResultImpl errorHandler;
private final CompilationUnit compUnit;
private final String mainTypeName;
private final String packageName;
- public CompilationUnitAdapter(final CompilationUnit compUnit, final
ErrorHandler errorHandler) {
+ public CompilationUnitAdapter(final CompilationUnit compUnit, final
CompilationResultImpl errorHandler) {
this.compUnit = compUnit;
this.errorHandler = errorHandler;
final int pos = compUnit.getMainClassName().lastIndexOf('.');
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=926965&r1=926964&r2=926965&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
Wed Mar 24 07:54:50 2010
@@ -26,15 +26,15 @@ import java.io.Reader;
import junit.framework.TestCase;
import org.apache.sling.commons.classloader.ClassLoaderWriter;
+import org.apache.sling.commons.compiler.CompilationResult;
import org.apache.sling.commons.compiler.CompilationUnit;
-import org.apache.sling.commons.compiler.ErrorHandler;
import org.apache.sling.commons.compiler.Options;
/**
* Test case for java 5 support
*/
public class CompilerJava5Test extends TestCase
- implements ErrorHandler, ClassLoaderWriter {
+ implements ClassLoaderWriter {
public void testJava5Support() throws Exception {
String sourceFile = "Java5Test";
@@ -45,21 +45,11 @@ public class CompilerJava5Test extends T
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));
+ final CompilationResult result = new EclipseJavaCompiler().compile(new
CompilationUnit[]{unit}, options);
+ assertNotNull(result);
+ assertNull(result.getErrors());
}
- //---------------------------------------------------------< ErrorHandler >
-
- public void onError(String msg, String sourceFile, int line, int position)
{
- System.out.println("Error in " + sourceFile + ", line " + line + ",
pos. " + position + ": " + msg);
- }
-
- public void onWarning(String msg, String sourceFile, int line, int
position) {
- System.out.println("Warning in " + sourceFile + ", line " + line + ",
pos. " + position + ": " + msg);
- }
-
- //----------------------------------------------------------<
ClassLoaderWriter >
-
//--------------------------------------------------------< misc. helpers >
private CompilationUnit createCompileUnit(final String sourceFile) throws
Exception {
@@ -79,6 +69,13 @@ public class CompilerJava5Test extends T
InputStream in =
getClass().getClassLoader().getResourceAsStream(sourceFile);
return new InputStreamReader(in, "UTF-8");
}
+
+ /**
+ * @see
org.apache.sling.commons.compiler.CompilationUnit#getLastModified()
+ */
+ public long getLastModified() {
+ return 0;
+ }
};
}
@@ -100,7 +97,7 @@ public class CompilerJava5Test extends T
* @see
org.apache.sling.commons.classloader.ClassLoaderWriter#getLastModified(java.lang.String)
*/
public long getLastModified(String path) {
- return 0;
+ return -1;
}
/**