sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From romb...@apache.org
Subject [sling-org-apache-sling-commons-compiler] 07/14: SLING-1451 : Clean up compiler API and use classloading infrastructure
Date Tue, 07 Nov 2017 09:20:39 GMT
This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag org.apache.sling.commons.compiler-2.0.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-compiler.git

commit d8d0a63c9b7fdbd39ed3ae673280a5fcd9e407f7
Author: Carsten Ziegeler <cziegeler@apache.org>
AuthorDate: Wed Mar 24 07:54:50 2010 +0000

    SLING-1451 : Clean up compiler API and use classloading infrastructure
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/contrib/commons/compiler@926965
13f79535-47bb-0310-9956-ffa450edef68
---
 .../sling/commons/compiler/CompilationResult.java  |  64 +++++++
 .../sling/commons/compiler/CompilationUnit.java    |   7 +
 .../sling/commons/compiler/CompilerMessage.java    | 101 ++++++++++++
 .../sling/commons/compiler/ErrorHandler.java       |  41 -----
 .../sling/commons/compiler/JavaCompiler.java       |  14 +-
 .../org/apache/sling/commons/compiler/Options.java |  10 ++
 .../compiler/impl/CompilationResultImpl.java       | 106 ++++++++++++
 .../commons/compiler/impl/EclipseJavaCompiler.java | 183 ++++++++++++++-------
 .../commons/compiler/impl/CompilerJava5Test.java   |  29 ++--
 9 files changed, 429 insertions(+), 126 deletions(-)

diff --git a/src/main/java/org/apache/sling/commons/compiler/CompilationResult.java b/src/main/java/org/apache/sling/commons/compiler/CompilationResult.java
new file mode 100644
index 0000000..4a72cbd
--- /dev/null
+++ b/src/main/java/org/apache/sling/commons/compiler/CompilationResult.java
@@ -0,0 +1,64 @@
+/*
+ * 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;
+
+import java.util.List;
+
+/**
+ * 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 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();
+
+    /**
+     * Was a compilation required or were all classes recent?
+     * @return <code>true>/code> if classes were compiled.
+     */
+    boolean didCompile();
+
+    /**
+     * 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.
+     */
+    Class<?> loadCompiledClass(final String className)
+    throws ClassNotFoundException;
+}
diff --git a/src/main/java/org/apache/sling/commons/compiler/CompilationUnit.java b/src/main/java/org/apache/sling/commons/compiler/CompilationUnit.java
index fd060c2..415c237 100644
--- a/src/main/java/org/apache/sling/commons/compiler/CompilationUnit.java
+++ b/src/main/java/org/apache/sling/commons/compiler/CompilationUnit.java
@@ -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();
 }
diff --git a/src/main/java/org/apache/sling/commons/compiler/CompilerMessage.java b/src/main/java/org/apache/sling/commons/compiler/CompilerMessage.java
new file mode 100644
index 0000000..fd98d7a
--- /dev/null
+++ b/src/main/java/org/apache/sling/commons/compiler/CompilerMessage.java
@@ -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;
+    }
+}
diff --git a/src/main/java/org/apache/sling/commons/compiler/ErrorHandler.java b/src/main/java/org/apache/sling/commons/compiler/ErrorHandler.java
deleted file mode 100644
index cebb678..0000000
--- a/src/main/java/org/apache/sling/commons/compiler/ErrorHandler.java
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * 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;
-
-/**
- * The error handler for the compilation.
- */
-public interface ErrorHandler {
-
-    /**
-     * 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);
-
-    /**
-     * 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);
-}
diff --git a/src/main/java/org/apache/sling/commons/compiler/JavaCompiler.java b/src/main/java/org/apache/sling/commons/compiler/JavaCompiler.java
index 033b027..8d2d629 100644
--- a/src/main/java/org/apache/sling/commons/compiler/JavaCompiler.java
+++ b/src/main/java/org/apache/sling/commons/compiler/JavaCompiler.java
@@ -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);
 }
diff --git a/src/main/java/org/apache/sling/commons/compiler/Options.java b/src/main/java/org/apache/sling/commons/compiler/Options.java
index 4085e04..6a24fa3 100644
--- a/src/main/java/org/apache/sling/commons/compiler/Options.java
+++ b/src/main/java/org/apache/sling/commons/compiler/Options.java
@@ -63,6 +63,16 @@ public class Options extends HashMap<String, Object> {
      */
     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
diff --git a/src/main/java/org/apache/sling/commons/compiler/impl/CompilationResultImpl.java
b/src/main/java/org/apache/sling/commons/compiler/impl/CompilationResultImpl.java
new file mode 100644
index 0000000..83f2e58
--- /dev/null
+++ b/src/main/java/org/apache/sling/commons/compiler/impl/CompilationResultImpl.java
@@ -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));
+        }
+    }
+}
diff --git a/src/main/java/org/apache/sling/commons/compiler/impl/EclipseJavaCompiler.java
b/src/main/java/org/apache/sling/commons/compiler/impl/EclipseJavaCompiler.java
index bac1a1d..641be1c 100644
--- a/src/main/java/org/apache/sling/commons/compiler/impl/EclipseJavaCompiler.java
+++ b/src/main/java/org/apache/sling/commons/compiler/impl/EclipseJavaCompiler.java
@@ -32,14 +32,13 @@ 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.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 implements JavaCompiler {
     }
 
     /**
-     * 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 implements JavaCompiler {
     }
 
     /**
-     * Unget the class loader
+     * Unget the commons dynamic class loader
      */
     private void ungetClassLoader() {
         this.classLoader = null;
@@ -117,13 +116,109 @@ public class EclipseJavaCompiler implements JavaCompiler {
     }
 
     /**
-     * @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 implements JavaCompiler {
         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 implements JavaCompiler {
         // 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 implements JavaCompiler {
             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 implements JavaCompiler {
         /**
          * @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 implements JavaCompiler {
                     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 implements JavaCompiler {
                 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 implements JavaCompiler {
 
     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('.');
diff --git a/src/test/java/org/apache/sling/commons/compiler/impl/CompilerJava5Test.java b/src/test/java/org/apache/sling/commons/compiler/impl/CompilerJava5Test.java
index c42b9b6..c409ca0 100644
--- a/src/test/java/org/apache/sling/commons/compiler/impl/CompilerJava5Test.java
+++ b/src/test/java/org/apache/sling/commons/compiler/impl/CompilerJava5Test.java
@@ -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 TestCase
         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 TestCase
                 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 TestCase
      * @see org.apache.sling.commons.classloader.ClassLoaderWriter#getLastModified(java.lang.String)
      */
     public long getLastModified(String path) {
-        return 0;
+        return -1;
     }
 
     /**

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <commits@sling.apache.org>.

Mime
View raw message