commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tcu...@apache.org
Subject svn commit: r513226 - in /jakarta/commons/sandbox/jci/trunk/compilers: janino/src/main/java/org/apache/commons/jci/compilers/ rhino/src/main/java/org/apache/commons/jci/compilers/ rhino/src/test/java/org/apache/commons/jci/compilers/
Date Thu, 01 Mar 2007 08:33:55 GMT
Author: tcurdt
Date: Thu Mar  1 00:33:54 2007
New Revision: 513226

URL: http://svn.apache.org/viewvc?view=rev&rev=513226
Log:
working js compilation,
needs more testcases though


Removed:
    jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/main/java/org/apache/commons/jci/compilers/RhinoCompilingClassLoader.java
Modified:
    jakarta/commons/sandbox/jci/trunk/compilers/janino/src/main/java/org/apache/commons/jci/compilers/JaninoJavaCompiler.java
    jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/main/java/org/apache/commons/jci/compilers/RhinoCompilationProblem.java
    jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/main/java/org/apache/commons/jci/compilers/RhinoJavaCompiler.java
    jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/test/java/org/apache/commons/jci/compilers/RhinoJavaCompilerTestCase.java

Modified: jakarta/commons/sandbox/jci/trunk/compilers/janino/src/main/java/org/apache/commons/jci/compilers/JaninoJavaCompiler.java
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/jci/trunk/compilers/janino/src/main/java/org/apache/commons/jci/compilers/JaninoJavaCompiler.java?view=diff&rev=513226&r1=513225&r2=513226
==============================================================================
--- jakarta/commons/sandbox/jci/trunk/compilers/janino/src/main/java/org/apache/commons/jci/compilers/JaninoJavaCompiler.java
(original)
+++ jakarta/commons/sandbox/jci/trunk/compilers/janino/src/main/java/org/apache/commons/jci/compilers/JaninoJavaCompiler.java
Thu Mar  1 00:33:54 2007
@@ -138,19 +138,16 @@
         }
     }
 
-    public CompilationResult compile(
-            final String[] pClasses,
-            final ResourceReader pResourceReader,
-            final ResourceStore pStore,
-            final ClassLoader classLoader
-            ) {
-        final Map classFilesByName = new HashMap();       
+    public CompilationResult compile( final String[] pClasses, final ResourceReader pResourceReader,
final ResourceStore pStore, final ClassLoader classLoader ) {
+
+    	final Map classFilesByName = new HashMap();       
         
         final CompilingIClassLoader icl = new CompilingIClassLoader(pResourceReader, classFilesByName,
classLoader);
         for (int i = 0; i < pClasses.length; i++) {
             log.debug("compiling " + pClasses[i]);
             icl.loadIClass(Descriptor.fromClassName(ClassUtils.convertResourceToClassName(pClasses[i])));
         }
+        
         // Store all fully compiled classes
         for (Iterator i = classFilesByName.entrySet().iterator(); i.hasNext();) {
             final Map.Entry entry = (Map.Entry)i.next();

Modified: jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/main/java/org/apache/commons/jci/compilers/RhinoCompilationProblem.java
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/main/java/org/apache/commons/jci/compilers/RhinoCompilationProblem.java?view=diff&rev=513226&r1=513225&r2=513226
==============================================================================
--- jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/main/java/org/apache/commons/jci/compilers/RhinoCompilationProblem.java
(original)
+++ jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/main/java/org/apache/commons/jci/compilers/RhinoCompilationProblem.java
Thu Mar  1 00:33:54 2007
@@ -20,40 +20,48 @@
 
 public final class RhinoCompilationProblem implements CompilationProblem {
 
+	private final String message;
+	private final String fileName;
+	private final int line;
+//	private final String script;
+	private final int column;
+	private final boolean error;
+	
+	public RhinoCompilationProblem( final String pMessage, final String pFileName, final int
pLine, final String pScript, final int pColumn, final boolean pError ) {
+		message = pMessage;
+		fileName = pFileName;
+		line = pLine;
+//		script = pScript;
+		column = pColumn;
+		error = pError;
+	}
+	
 	public int getEndColumn() {
-		// TODO Auto-generated method stub
-		return 0;
+		return column;
 	}
 
 	public int getEndLine() {
-		// TODO Auto-generated method stub
-		return 0;
+		return line;
 	}
 
 	public String getFileName() {
-		// TODO Auto-generated method stub
-		return null;
+		return fileName;
 	}
 
 	public String getMessage() {
-		// TODO Auto-generated method stub
-		return null;
+		return message;
 	}
 
 	public int getStartColumn() {
-		// TODO Auto-generated method stub
-		return 0;
+		return column;
 	}
 
 	public int getStartLine() {
-		// TODO Auto-generated method stub
-		return 0;
+		return line;
 	}
 
 	public boolean isError() {
-		// TODO Auto-generated method stub
-		return false;
+		return error;
 	}
-
 
 }

Modified: jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/main/java/org/apache/commons/jci/compilers/RhinoJavaCompiler.java
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/main/java/org/apache/commons/jci/compilers/RhinoJavaCompiler.java?view=diff&rev=513226&r1=513225&r2=513226
==============================================================================
--- jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/main/java/org/apache/commons/jci/compilers/RhinoJavaCompiler.java
(original)
+++ jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/main/java/org/apache/commons/jci/compilers/RhinoJavaCompiler.java
Thu Mar  1 00:33:54 2007
@@ -16,27 +16,237 @@
  */
 package org.apache.commons.jci.compilers;
 
+import java.io.ByteArrayInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.util.ArrayList;
+import java.util.Collection;
+
 import org.apache.commons.jci.problems.CompilationProblem;
 import org.apache.commons.jci.readers.ResourceReader;
 import org.apache.commons.jci.stores.ResourceStore;
 import org.apache.commons.jci.utils.ClassUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.mozilla.javascript.CompilerEnvirons;
+import org.mozilla.javascript.Context;
+import org.mozilla.javascript.ErrorReporter;
+import org.mozilla.javascript.EvaluatorException;
+import org.mozilla.javascript.GeneratedClassLoader;
+import org.mozilla.javascript.ImporterTopLevel;
+import org.mozilla.javascript.JavaScriptException;
+import org.mozilla.javascript.NativeArray;
+import org.mozilla.javascript.Scriptable;
+import org.mozilla.javascript.ScriptableObject;
+import org.mozilla.javascript.optimizer.ClassCompiler;
 
 /**
- * 
  * @author tcurdt
- * based on code from dev.helma.org
  */
 public final class RhinoJavaCompiler extends AbstractJavaCompiler {
 
     private final Log log = LogFactory.getLog(RhinoJavaCompiler.class);
 
+    /**
+     * based on code from dev.helma.org
+     * http://dev.helma.org/source/file/helma/branches/rhinoloader/src/org/helma/javascript/RhinoLoader.java/?revision=95
+     */
+    private final class RhinoCompilingClassLoader extends ClassLoader {
+
+    	private final ScriptableObject scope;
+    	private final ResourceReader reader;
+    	private final ResourceStore store;
+
+    	private final Collection problems = new ArrayList();
+    	
+    	private final class ProblemCollector implements ErrorReporter {
+
+    		public void error(String pMessage, String pFileName, int pLine, String pScript, int
pColumn) {
+    			
+    			final CompilationProblem problem = new RhinoCompilationProblem(pMessage, pFileName,
pLine, pScript, pColumn, true); 
+    			
+    			if (problemHandler != null) {
+    				problemHandler.handle(problem);
+    			}
+    			
+    			problems.add(problem); 
+    		}
+
+    		public void warning(String pMessage, String pFileName, int pLine, String pScript, int
pColumn) {
+
+    			final CompilationProblem problem = new RhinoCompilationProblem(pMessage, pFileName,
pLine, pScript, pColumn, false); 
+    			
+    			if (problemHandler != null) {
+    				problemHandler.handle(problem);
+    			}
+    			
+    			problems.add(problem); 
+    		}
+
+    		public EvaluatorException runtimeError(String pMessage, String pFileName, int pLine,
String pScript, int pColumn) {
+    			return new EvaluatorException(pMessage, pFileName, pLine, pScript, pColumn);
+    		}		
+    	}
+    	
+    	public RhinoCompilingClassLoader( final ResourceReader pReader, final ResourceStore
pStore, final ClassLoader pClassLoader) {
+    		super(pClassLoader);
+    		
+    		reader = pReader;
+    		store = pStore;
+
+    		final Context context = Context.enter();
+    		scope = new ImporterTopLevel(context);
+    		Context.exit();
+    	}
+
+    	public Collection getProblems() {
+    		return problems;
+    	}
+    	
+    	protected Class findClass( final String pName ) throws ClassNotFoundException {
+    		final Context context = Context.enter();
+    		context.setErrorReporter(new ProblemCollector());
+
+    		try {
+    			return compileClass(context, pName);
+    		} catch( EvaluatorException e ) {
+    			throw new ClassNotFoundException(e.getMessage(), e);
+    		} catch (IOException e) {
+    			throw new ClassNotFoundException(e.getMessage(), e);
+    		} finally {
+    			Context.exit();
+    		}
+    	}
+
+
+    	private Class compileClass( final Context pContext, final String pClassName) throws
IOException, ClassNotFoundException {
+
+    		Class superclass = null;
+
+    		final String pSourceName = pClassName.replace('.', '/') + ".js";
+    		
+    		final Scriptable target = evaluate(pContext, pSourceName);
+
+    		final Object baseClassName = ScriptableObject.getProperty(target, "__extends__");
+
+    		if (baseClassName instanceof String) {
+    			superclass = Class.forName((String) baseClassName);
+    		}
+
+    		final ArrayList interfaceClasses = new ArrayList();
+
+    		final Object interfaceNames = ScriptableObject.getProperty(target, "__implements__");
+
+    		if (interfaceNames instanceof NativeArray) {
+
+    			final NativeArray interfaceNameArray = (NativeArray) interfaceNames;
+
+    			for (int i=0; i<interfaceNameArray.getLength(); i++) {
+    				
+    				final Object obj = interfaceNameArray.get(i, interfaceNameArray);
+
+    				if (obj instanceof String) {
+    					interfaceClasses.add(Class.forName((String) obj));
+    				}
+    			}
+
+    		} else if (interfaceNames instanceof String) {
+    			
+    			interfaceClasses.add(Class.forName((String) interfaceNames));
+    		
+    		}
+
+    		final Class[] interfaces;
+
+    		if (!interfaceClasses.isEmpty()) {
+    			interfaces = new Class[interfaceClasses.size()];
+    			interfaceClasses.toArray(interfaces);
+    		} else {
+    			// FIXME: hm ...really no empty array good enough?
+    			interfaces = null;
+    		}
+
+    		return compileClass(pContext, pSourceName, pClassName, superclass, interfaces);
+
+    	}
+
+
+    	private Class compileClass( final Context pContext, final String pSourceName, final
String pClassName, final Class pSuperClass, final Class[] pInterfaces) throws IOException
{
+
+    		final CompilerEnvirons environments = new CompilerEnvirons();
+    		environments.initFromContext(pContext);
+    		final ClassCompiler compiler = new ClassCompiler(environments);
+
+    		if (pSuperClass != null) {
+    			compiler.setTargetExtends(pSuperClass);
+    		}
+
+    		if (pInterfaces != null) {
+    			compiler.setTargetImplements(pInterfaces);
+    		}
+
+    		final byte[] sourceBytes = reader.getBytes(pSourceName);
+
+    		final Object[] classes = compiler.compileToClassFiles(new String(sourceBytes), getName(pSourceName),
1, pClassName);
+
+    		final GeneratedClassLoader loader = pContext.createClassLoader(pContext.getApplicationClassLoader());
+
+    		Class clazz = null;
+
+    		for (int i = 0; i < classes.length; i += 2) {
+
+    			final String clazzName = (String) classes[i];
+    			final byte[] clazzBytes = (byte[]) classes[i+1];
+    			
+    			store.write(clazzName.replace('.', '/') + ".class", clazzBytes);
+    			
+    			Class c = loader.defineClass(clazzName, clazzBytes);
+    			loader.linkClass(c);
+
+    			if (i == 0) {
+    				clazz = c;
+    			}
+
+    		}
+
+    		return clazz;
+    	}
+
+    	private String getName(String s) {
+    		final int i = s.lastIndexOf('/');
+    		if (i < 0) {
+    			return s;
+    		}
+    		
+    		return s.substring(i + 1);
+    	}
+    	
+    	private Scriptable evaluate( final Context pContext, final String pSourceName) throws
JavaScriptException, IOException {
+
+    		if (!reader.isAvailable(pSourceName)) {
+    			throw new FileNotFoundException("File " + pSourceName + " not found");
+    		}
+
+    		final Scriptable target = pContext.newObject(scope);
+
+    		final byte[] sourceBytes = reader.getBytes(pSourceName);
+    		
+    		final Reader reader = new InputStreamReader(new ByteArrayInputStream(sourceBytes));
+
+    		pContext.evaluateReader(target, reader, getName(pSourceName), 1, null);
+
+    		return target;
+    	}
+
+    }
+    
+    
 	public CompilationResult compile( final String[] pResourcePaths, final ResourceReader pReader,
final ResourceStore pStore, final ClassLoader classLoader) {
 
 		final RhinoCompilingClassLoader cl = new RhinoCompilingClassLoader(pReader, pStore, classLoader);
 		
-		
 		for (int i = 0; i < pResourcePaths.length; i++) {
             log.debug("compiling " + pResourcePaths[i]);
             
@@ -44,10 +254,13 @@
             try {
 				cl.loadClass(clazzName);
 			} catch (ClassNotFoundException e) {
-				e.printStackTrace();
 			}
 		}
-		return new CompilationResult(new CompilationProblem[0]);
+		
+        final Collection problems = cl.getProblems();
+        final CompilationProblem[] result = new CompilationProblem[problems.size()];
+        problems.toArray(result);
+        return new CompilationResult(result);
 	}
 	
 }

Modified: jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/test/java/org/apache/commons/jci/compilers/RhinoJavaCompilerTestCase.java
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/test/java/org/apache/commons/jci/compilers/RhinoJavaCompilerTestCase.java?view=diff&rev=513226&r1=513225&r2=513226
==============================================================================
--- jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/test/java/org/apache/commons/jci/compilers/RhinoJavaCompilerTestCase.java
(original)
+++ jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/test/java/org/apache/commons/jci/compilers/RhinoJavaCompilerTestCase.java
Thu Mar  1 00:33:54 2007
@@ -58,6 +58,38 @@
 	}
 
 	public void testUppercasePackageNameCompile() throws Exception {
+		final JavaCompiler compiler = createJavaCompiler(); 
+		
+		final ResourceReader reader = new ResourceReader() {
+			final private Map sources = new HashMap() {{
+				put("Jci/Simple.js", (
+					" var i = 0;\n" +
+					"\n"
+					).getBytes());
+			}};
+			
+			public byte[] getBytes( final String pResourceName ) {
+				return (byte[]) sources.get(pResourceName);
+			}
+
+			public boolean isAvailable( final String pResourceName ) {
+				return sources.containsKey(pResourceName);
+			}
+			
+		};
+		
+		final MemoryResourceStore store = new MemoryResourceStore();
+		final CompilationResult result = compiler.compile(
+				new String[] {
+						"Jci/Simple.js"
+				}, reader, store);
+		
+		assertEquals(toString(result.getErrors()), 0, result.getErrors().length);		
+		assertEquals(toString(result.getWarnings()), 0, result.getWarnings().length);
+		
+		final byte[] clazzBytes = store.read("Jci/Simple.class");		
+		assertNotNull(clazzBytes);
+		assertTrue(clazzBytes.length > 0);
 	}
 	
 	



---------------------------------------------------------------------
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