commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tcu...@apache.org
Subject svn commit: r233365 - in /jakarta/commons/sandbox/jci/trunk: TODO project.xml src/java/org/apache/commons/jci/CompilingClassLoader.java src/java/org/apache/commons/jci/compilers/janino/JaninoJavaCompiler.java
Date Thu, 18 Aug 2005 19:41:26 GMT
Author: tcurdt
Date: Thu Aug 18 12:41:18 2005
New Revision: 233365

URL: http://svn.apache.org/viewcvs?rev=233365&view=rev
Log:
fixed the deps,
CCL cleanups,
updated the TODO

again thanks to Joerk

Modified:
    jakarta/commons/sandbox/jci/trunk/TODO
    jakarta/commons/sandbox/jci/trunk/project.xml
    jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/CompilingClassLoader.java
    jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/compilers/janino/JaninoJavaCompiler.java

Modified: jakarta/commons/sandbox/jci/trunk/TODO
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/jci/trunk/TODO?rev=233365&r1=233364&r2=233365&view=diff
==============================================================================
--- jakarta/commons/sandbox/jci/trunk/TODO (original)
+++ jakarta/commons/sandbox/jci/trunk/TODO Thu Aug 18 12:41:18 2005
@@ -6,4 +6,5 @@
   o jikes
 o transformation "pipelines" (more than just one transformation)
 o make suffixes matching (*.java, *.class) configurable
+o turn the JavaCompilerFactory into an interface and maybe provide a simple default impl
 o documentation

Modified: jakarta/commons/sandbox/jci/trunk/project.xml
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/jci/trunk/project.xml?rev=233365&r1=233364&r2=233365&view=diff
==============================================================================
--- jakarta/commons/sandbox/jci/trunk/project.xml (original)
+++ jakarta/commons/sandbox/jci/trunk/project.xml Thu Aug 18 12:41:18 2005
@@ -104,6 +104,12 @@
     </dependency>
 
     <dependency>
+      <groupId>commons-logging</groupId>
+      <artifactId>commons-logging-api</artifactId>
+      <version>1.0.4</version>
+    </dependency>
+
+    <dependency>
       <groupId>commons-collections</groupId>
       <artifactId>commons-collections</artifactId>
       <version>3.1</version>
@@ -160,6 +166,12 @@
       <groupId>antlr</groupId>
       <artifactId>antlr</artifactId>
       <version>2.7.5</version>
+    </dependency>
+
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>3.8.1</version>
     </dependency>
 
   </dependencies>

Modified: jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/CompilingClassLoader.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/CompilingClassLoader.java?rev=233365&r1=233364&r2=233365&view=diff
==============================================================================
--- jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/CompilingClassLoader.java
(original)
+++ jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/CompilingClassLoader.java
Thu Aug 18 12:41:18 2005
@@ -20,23 +20,21 @@
 import org.apache.commons.jci.compilers.eclipse.EclipseJavaCompiler;
 import org.apache.commons.jci.listeners.CompilingListener;
 import org.apache.commons.jci.monitor.FilesystemAlterationMonitor;
+import org.apache.commons.jci.problems.CompilationProblemHandler;
 import org.apache.commons.jci.problems.ConsoleCompilationProblemHandler;
 import org.apache.commons.jci.stores.MemoryResourceStore;
 import org.apache.commons.jci.stores.TransactionalResourceStore;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
 
 /**
  * @author tcurdt
  *
  */
 public class CompilingClassLoader extends ReloadingClassLoader {
-    
-    private final static Log log = LogFactory.getLog(CompilingClassLoader.class);
-    
+
     private final TransactionalResourceStore transactionalStore;
-    private final JavaCompiler compiler; 
-    
+    private final JavaCompiler compiler;
+    private final CompilationProblemHandler problemHandler;
+
     public CompilingClassLoader(final ClassLoader pParent, final File pRepository) {
         this(pParent, pRepository, new TransactionalResourceStore(
                 new MemoryResourceStore()) {
@@ -47,32 +45,34 @@
                 }
         );
     }
-    
+
     public CompilingClassLoader(final ClassLoader pParent, final File pRepository, final
TransactionalResourceStore pStore) {
-        this(pParent, pRepository, pStore, new EclipseJavaCompiler());
+        this(pParent, pRepository, pStore, new EclipseJavaCompiler(), new ConsoleCompilationProblemHandler());
     }
-    
-    public CompilingClassLoader(final ClassLoader pParent, final File pRepository, final
TransactionalResourceStore pStore, final JavaCompiler pCompiler) {
-        super(pParent, pRepository, pStore);
 
+    public CompilingClassLoader(final ClassLoader pParent, final File pRepository,
+            final TransactionalResourceStore pStore, final JavaCompiler pCompiler,
+            final CompilationProblemHandler pProblemHandler) {
+        super(pParent, pRepository, pStore);
         transactionalStore = pStore;
-        compiler = pCompiler;                
+        compiler = pCompiler;
+        problemHandler = pProblemHandler;
     }
 
     public void start() {
-        fam = new FilesystemAlterationMonitor(); 
+        fam = new FilesystemAlterationMonitor();
         fam.addListener(new CompilingListener(
                 reader,
                 compiler,
                 transactionalStore,
-                new ConsoleCompilationProblemHandler()
+                problemHandler
                 ) {
             public void reload() {
                 super.reload();
                 CompilingClassLoader.this.reload();
             }
         }, repository);
-        thread = new Thread(fam);         
+        thread = new Thread(fam);
         thread.start();
     }
 }

Modified: jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/compilers/janino/JaninoJavaCompiler.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/compilers/janino/JaninoJavaCompiler.java?rev=233365&r1=233364&r2=233365&view=diff
==============================================================================
--- jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/compilers/janino/JaninoJavaCompiler.java
(original)
+++ jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/compilers/janino/JaninoJavaCompiler.java
Thu Aug 18 12:41:18 2005
@@ -15,32 +15,39 @@
  */
 package org.apache.commons.jci.compilers.janino;
 
-import java.io.ByteArrayInputStream;
+import java.io.BufferedReader;
+import java.io.CharArrayReader;
 import java.io.File;
 import java.io.IOException;
+import java.io.Reader;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
+
 import org.apache.commons.jci.compilers.JavaCompiler;
 import org.apache.commons.jci.problems.CompilationProblemHandler;
 import org.apache.commons.jci.readers.ResourceReader;
 import org.apache.commons.jci.stores.ResourceStore;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+
 import org.codehaus.janino.ClassLoaderIClassLoader;
+import org.codehaus.janino.CompileException;
 import org.codehaus.janino.DebuggingInformation;
 import org.codehaus.janino.Descriptor;
 import org.codehaus.janino.IClass;
 import org.codehaus.janino.IClassLoader;
 import org.codehaus.janino.Java;
+import org.codehaus.janino.Location;
 import org.codehaus.janino.Parser;
 import org.codehaus.janino.Scanner;
 import org.codehaus.janino.UnitCompiler;
+import org.codehaus.janino.WarningHandler;
+import org.codehaus.janino.UnitCompiler.ErrorHandler;
 import org.codehaus.janino.Scanner.LocatedException;
 import org.codehaus.janino.util.ClassFile;
 
 /**
- *
  * @author art@gramlich-net.com
  */
 public class JaninoJavaCompiler implements JavaCompiler {
@@ -54,18 +61,20 @@
         private final CompilationProblemHandler problemHandler;
         private final Map classes;
 
-        private CompilingIClassLoader(ResourceReader resourceReader, CompilationProblemHandler
problemHandler, Map classes) {
+        private CompilingIClassLoader(final ResourceReader pResourceReader,
+                                      final CompilationProblemHandler pProblemHandler,
+                                      final Map pClasses) {
             super(new ClassLoaderIClassLoader());
-            this.resourceReader = resourceReader;
-            this.problemHandler = problemHandler;
-            this.classes = classes;
+            resourceReader = pResourceReader;
+            problemHandler = pProblemHandler;
+            classes = pClasses;
             super.postConstruct();
         }
 
-        protected IClass findIClass(final String type) {
-            final String className = Descriptor.toClassName(type);
-            if (types.containsKey(type)) {
-                return (IClass) types.get(type);
+        protected IClass findIClass(final String pType) {
+            final String className = Descriptor.toClassName(pType);
+            if (types.containsKey(pType)) {
+                return (IClass) types.get(pType);
             }
             final String fileNameForClass = className.replace('.', File.separatorChar) +
".java";
 
@@ -73,12 +82,15 @@
             if (content == null) {
                 return null;
             }
-            final ByteArrayInputStream instream = new ByteArrayInputStream(new String(content).getBytes());
+            final Reader reader = new BufferedReader(new CharArrayReader(content));
             Scanner scanner = null;
             try {
-                scanner = new Scanner(fileNameForClass, instream, "UTF-8");
+                scanner = new Scanner(fileNameForClass, reader);
                 final Java.CompilationUnit unit = new Parser(scanner).parseCompilationUnit();
                 final UnitCompiler uc = new UnitCompiler(unit, this);
+                CompilationProblemHandlerAdapter adapter = new CompilationProblemHandlerAdapter(problemHandler);
+                uc.setCompileErrorHandler(adapter);
+                uc.setWarningHandler(adapter);
                 log.debug("compile " + className);
                 final ClassFile[] classFiles = uc.compileUnit(DebuggingInformation.ALL);
                 for (int i = 0; i < classFiles.length; i++) {
@@ -87,7 +99,7 @@
                 }
                 final IClass ic = uc.findClass(className);
                 if (null != ic) {
-                    types.put(type, ic);
+                    types.put(pType, ic);
                 }
                 return ic;
             } catch (final LocatedException e) {
@@ -107,19 +119,36 @@
         }
     }
 
-    public void compile(final String[] classes, final ResourceReader in,
-            final ResourceStore store, final CompilationProblemHandler problemHandler) {
+    public void compile(final String[] pClasses, final ResourceReader pResourceReader,
+                        final ResourceStore pStore, final CompilationProblemHandler pProblemHandler)
{
         final Map classFilesByName = new HashMap();
-        final IClassLoader icl = new CompilingIClassLoader(in, problemHandler, classFilesByName);
-        for (int i = 0; i < classes.length; i++) {
-            log.debug("compiling " + classes[i]);
-            icl.loadIClass(Descriptor.fromClassName(classes[i]));
+        final IClassLoader icl = new CompilingIClassLoader(pResourceReader, pProblemHandler,
classFilesByName);
+        for (int i = 0; i < pClasses.length; i++) {
+            log.debug("compiling " + pClasses[i]);
+            icl.loadIClass(Descriptor.fromClassName(pClasses[i]));
         }
         // Store all fully compiled classes
-        for (Iterator i=classFilesByName.keySet().iterator(); i.hasNext();) {
-            final String name = (String)i.next();
-            final byte[] bytes = (byte[]) classFilesByName.get(name);
-            store.write(name,bytes);
+        for (Iterator i = classFilesByName.entrySet().iterator(); i.hasNext();) {
+            final Map.Entry entry = (Map.Entry)i.next();
+            pStore.write((String)entry.getKey(), (byte[])entry.getValue());
         }
     }
+
+    private static final class CompilationProblemHandlerAdapter implements ErrorHandler,
WarningHandler {
+        private final CompilationProblemHandler problemHandler;
+
+        public CompilationProblemHandlerAdapter(final CompilationProblemHandler pProblemHandler)
{
+            problemHandler = pProblemHandler;
+        }
+
+        public void handleError(final String pMessage, final Location pOptionalLocation)
throws CompileException {
+            problemHandler.handle(new JaninoCompilationProblem(pOptionalLocation, pMessage,
true));
+        }
+
+        public void handleWarning(final String pHandle, final String pMessage, final Location
pOptionalLocation) {
+            problemHandler.handle(new JaninoCompilationProblem(pOptionalLocation, pMessage,
false));
+        }
+
+    }
+
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message