commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tcu...@apache.org
Subject svn commit: r411565 - in /jakarta/commons/sandbox/jci/trunk: ./ compilers/javac/ compilers/javac/src/ compilers/javac/src/java/ compilers/javac/src/java/org/ compilers/javac/src/java/org/apache/ compilers/javac/src/java/org/apache/commons/ compilers/ja...
Date Sun, 04 Jun 2006 14:37:30 GMT
Author: tcurdt
Date: Sun Jun  4 07:37:29 2006
New Revision: 411565

URL: http://svn.apache.org/viewvc?rev=411565&view=rev
Log:
GSoC: first implementation of javac compiler by Peter Konstantinov


Added:
    jakarta/commons/sandbox/jci/trunk/compilers/javac/
    jakarta/commons/sandbox/jci/trunk/compilers/javac/pom.xml   (with props)
    jakarta/commons/sandbox/jci/trunk/compilers/javac/src/
    jakarta/commons/sandbox/jci/trunk/compilers/javac/src/java/
    jakarta/commons/sandbox/jci/trunk/compilers/javac/src/java/org/
    jakarta/commons/sandbox/jci/trunk/compilers/javac/src/java/org/apache/
    jakarta/commons/sandbox/jci/trunk/compilers/javac/src/java/org/apache/commons/
    jakarta/commons/sandbox/jci/trunk/compilers/javac/src/java/org/apache/commons/jci/
    jakarta/commons/sandbox/jci/trunk/compilers/javac/src/java/org/apache/commons/jci/compilers/
    jakarta/commons/sandbox/jci/trunk/compilers/javac/src/java/org/apache/commons/jci/compilers/FileInputStreamProxy.java
  (with props)
    jakarta/commons/sandbox/jci/trunk/compilers/javac/src/java/org/apache/commons/jci/compilers/JavacClassLoader.java
  (with props)
    jakarta/commons/sandbox/jci/trunk/compilers/javac/src/java/org/apache/commons/jci/compilers/JavacJavaCompiler.java
  (with props)
Modified:
    jakarta/commons/sandbox/jci/trunk/pom.xml

Added: jakarta/commons/sandbox/jci/trunk/compilers/javac/pom.xml
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/jci/trunk/compilers/javac/pom.xml?rev=411565&view=auto
==============================================================================
--- jakarta/commons/sandbox/jci/trunk/compilers/javac/pom.xml (added)
+++ jakarta/commons/sandbox/jci/trunk/compilers/javac/pom.xml Sun Jun  4 07:37:29 2006
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+	<modelVersion>4.0.0</modelVersion>
+	<packaging>jar</packaging>
+
+	<parent>
+		<groupId>org.apache.commons</groupId>
+		<artifactId>commons-jci</artifactId>
+		<version>1.0-SNAPSHOT</version>
+	</parent>
+
+	<groupId>org.apache.commons</groupId>
+	<artifactId>commons-jci-javac</artifactId>
+	<version>1.0-SNAPSHOT</version>
+
+	<name>Compiler Javac</name>
+
+	<dependencies>
+		<dependency>
+			<groupId>org.apache.commons</groupId>
+			<artifactId>commons-jci-core</artifactId>
+			<version>1.0-SNAPSHOT</version>
+		</dependency>
+		<dependency>
+			<groupId>javassist</groupId>
+			<artifactId>javassist</artifactId>
+			<version>3.0</version>
+		</dependency>
+	</dependencies>
+</project>

Propchange: jakarta/commons/sandbox/jci/trunk/compilers/javac/pom.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/sandbox/jci/trunk/compilers/javac/pom.xml
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Added: jakarta/commons/sandbox/jci/trunk/compilers/javac/src/java/org/apache/commons/jci/compilers/FileInputStreamProxy.java
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/jci/trunk/compilers/javac/src/java/org/apache/commons/jci/compilers/FileInputStreamProxy.java?rev=411565&view=auto
==============================================================================
--- jakarta/commons/sandbox/jci/trunk/compilers/javac/src/java/org/apache/commons/jci/compilers/FileInputStreamProxy.java
(added)
+++ jakarta/commons/sandbox/jci/trunk/compilers/javac/src/java/org/apache/commons/jci/compilers/FileInputStreamProxy.java
Sun Jun  4 07:37:29 2006
@@ -0,0 +1,114 @@
+package org.apache.commons.jci.compilers;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileDescriptor;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.channels.FileChannel;
+import org.apache.commons.jci.readers.ResourceReader;
+
+public class FileInputStreamProxy extends InputStream
+{
+	private InputStream inputStream = null;
+
+	ResourceReader reader = null;
+
+	public FileInputStreamProxy(File file) throws FileNotFoundException
+	{
+		if (getReader().isAvailable(file.getName()))
+			inputStream = new ByteArrayInputStream(getReader().getBytes(file.getName()));
+		else
+			inputStream = new FileInputStream(file);
+	}
+
+	public FileInputStreamProxy(FileDescriptor fdObj)
+	{
+		inputStream = new FileInputStream(fdObj);
+	}
+
+	public FileInputStreamProxy(String name) throws FileNotFoundException
+	{
+		if (getReader().isAvailable(name))
+			inputStream = new ByteArrayInputStream(getReader().getBytes(name));
+		else
+		inputStream = new FileInputStream(name);
+	}
+
+	private ResourceReader getReader()
+	{
+		if (reader == null)
+		{
+			JavacClassLoader loader = (JavacClassLoader)Thread.currentThread().getContextClassLoader();
+			reader = loader.getReader();
+		}
+		return reader;
+	}
+
+	public int available() throws IOException
+	{
+		return inputStream.available();
+	}
+
+	public void close() throws IOException
+	{
+		inputStream.close();
+	}
+
+	public boolean equals(Object obj)
+	{
+		return inputStream.equals(obj);
+	}
+
+	public FileChannel getChannel()
+	{
+		throw new RuntimeException(":(");
+	}
+
+	public int hashCode()
+	{
+		return inputStream.hashCode();
+	}
+
+	public void mark(int readlimit)
+	{
+		inputStream.mark(readlimit);
+	}
+
+	public boolean markSupported()
+	{
+		return inputStream.markSupported();
+	}
+
+	public int read() throws IOException
+	{
+		return inputStream.read();
+	}
+
+	public int read(byte[] b, int off, int len) throws IOException
+	{
+		return inputStream.read(b, off, len);
+	}
+
+	public int read(byte[] b) throws IOException
+	{
+		return inputStream.read(b);
+	}
+
+	public void reset() throws IOException
+	{
+		inputStream.reset();
+	}
+
+	public long skip(long n) throws IOException
+	{
+		return inputStream.skip(n);
+	}
+
+	public String toString()
+	{
+		return inputStream.toString();
+	}
+}

Propchange: jakarta/commons/sandbox/jci/trunk/compilers/javac/src/java/org/apache/commons/jci/compilers/FileInputStreamProxy.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/sandbox/jci/trunk/compilers/javac/src/java/org/apache/commons/jci/compilers/FileInputStreamProxy.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Added: jakarta/commons/sandbox/jci/trunk/compilers/javac/src/java/org/apache/commons/jci/compilers/JavacClassLoader.java
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/jci/trunk/compilers/javac/src/java/org/apache/commons/jci/compilers/JavacClassLoader.java?rev=411565&view=auto
==============================================================================
--- jakarta/commons/sandbox/jci/trunk/compilers/javac/src/java/org/apache/commons/jci/compilers/JavacClassLoader.java
(added)
+++ jakarta/commons/sandbox/jci/trunk/compilers/javac/src/java/org/apache/commons/jci/compilers/JavacClassLoader.java
Sun Jun  4 07:37:29 2006
@@ -0,0 +1,65 @@
+package org.apache.commons.jci.compilers;
+
+import java.io.FileInputStream;
+import java.net.URL;
+import java.net.URLClassLoader;
+import javassist.ClassPool;
+import javassist.CtClass;
+import org.apache.commons.jci.readers.ResourceReader;
+import org.apache.commons.jci.stores.ResourceStore;
+
+public class JavacClassLoader extends URLClassLoader
+{
+	private ClassPool classPool;
+
+	private ResourceReader reader;
+
+	private ResourceStore store;
+
+	public JavacClassLoader(ClassPool classPool, ResourceReader reader,
+			ResourceStore store, URL[] urls, ClassLoader parent)
+	{
+		super(urls, parent);
+		this.classPool = classPool;
+		this.reader = reader;
+		this.store = store;
+	}
+
+	protected Class findClass(String name) throws ClassNotFoundException
+	{
+		// System.out.println(name);
+		if (name.equals(JavacClassLoader.class.getName()))
+		{
+			return JavacClassLoader.class;
+		}
+		if (name.equals(ResourceReader.class.getName()))
+		{
+			return ResourceReader.class;
+		}
+		try
+		{
+			// if (!name.equals(FileInputStreamProxy.class.getName()))
+			if (name.equals("com.sun.tools.javac.main.JavaCompiler"))
+			{
+				CtClass jc = classPool.get(name);
+				jc.replaceClassName(FileInputStream.class.getName(),
+						FileInputStreamProxy.class.getName());
+				return jc.toClass();
+			}
+		}
+		catch (Exception e)
+		{
+		}
+		return super.findClass(name);
+	}
+
+	public ResourceReader getReader()
+	{
+		return reader;
+	}
+
+	public ResourceStore getStore()
+	{
+		return store;
+	}
+}
\ No newline at end of file

Propchange: jakarta/commons/sandbox/jci/trunk/compilers/javac/src/java/org/apache/commons/jci/compilers/JavacClassLoader.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/sandbox/jci/trunk/compilers/javac/src/java/org/apache/commons/jci/compilers/JavacClassLoader.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Added: jakarta/commons/sandbox/jci/trunk/compilers/javac/src/java/org/apache/commons/jci/compilers/JavacJavaCompiler.java
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/jci/trunk/compilers/javac/src/java/org/apache/commons/jci/compilers/JavacJavaCompiler.java?rev=411565&view=auto
==============================================================================
--- jakarta/commons/sandbox/jci/trunk/compilers/javac/src/java/org/apache/commons/jci/compilers/JavacJavaCompiler.java
(added)
+++ jakarta/commons/sandbox/jci/trunk/compilers/javac/src/java/org/apache/commons/jci/compilers/JavacJavaCompiler.java
Sun Jun  4 07:37:29 2006
@@ -0,0 +1,96 @@
+package org.apache.commons.jci.compilers;
+
+import java.io.File;
+import java.io.PrintWriter;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLClassLoader;
+import javassist.ClassPool;
+import javassist.NotFoundException;
+import org.apache.commons.jci.readers.ResourceReader;
+import org.apache.commons.jci.stores.ResourceStore;
+import org.apache.commons.lang.ArrayUtils;
+
+public class JavacJavaCompiler extends AbstractJavaCompiler
+{
+	public CompilationResult compile(String[] resourcePaths,
+			ResourceReader reader, ResourceStore store,
+			ClassLoader baseClassLoader)
+	{
+		File toolsJar = new File(System.getProperty("java.home"),
+				"../lib/tools.jar");
+		URL[] urls = null;
+		try
+		{
+			if (baseClassLoader instanceof URLClassLoader)
+			{
+				urls = (URL[]) ArrayUtils.add(
+						((URLClassLoader) baseClassLoader).getURLs(), toolsJar
+								.toURL());
+			}
+			else
+			{
+				urls = new URL[] { new File(".").toURI().toURL(),
+						toolsJar.toURL() };
+			}
+		}
+		catch (MalformedURLException e)
+		{
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+		ClassPool classPool = ClassPool.getDefault();
+		JavacClassLoader javacClassLoader = new JavacClassLoader(classPool,
+				reader, store, urls, baseClassLoader.getParent());
+		ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
+		Thread.currentThread().setContextClassLoader(javacClassLoader);
+		Class javacMain = null;
+		try
+		{
+			javacMain = javacClassLoader.loadClass("com.sun.tools.javac.Main");
+		}
+		catch (ClassNotFoundException e)
+		{
+			e.printStackTrace();
+		}
+		Method compile = null;
+		try
+		{
+			compile = javacMain.getMethod("compile", new Class[] {
+					String[].class, PrintWriter.class });
+		}
+		catch (SecurityException e)
+		{
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+		catch (NoSuchMethodException e)
+		{
+			e.printStackTrace();
+		}
+		try
+		{
+			compile.invoke(null, new Object[] { new String[] { "test.java" },
+					new PrintWriter(System.out) });
+		}
+		catch (IllegalArgumentException e)
+		{
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+		catch (IllegalAccessException e)
+		{
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+		catch (InvocationTargetException e)
+		{
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+		Thread.currentThread().setContextClassLoader(oldLoader);
+		return null;
+	}
+}

Propchange: jakarta/commons/sandbox/jci/trunk/compilers/javac/src/java/org/apache/commons/jci/compilers/JavacJavaCompiler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/sandbox/jci/trunk/compilers/javac/src/java/org/apache/commons/jci/compilers/JavacJavaCompiler.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Modified: jakarta/commons/sandbox/jci/trunk/pom.xml
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/jci/trunk/pom.xml?rev=411565&r1=411564&r2=411565&view=diff
==============================================================================
--- jakarta/commons/sandbox/jci/trunk/pom.xml (original)
+++ jakarta/commons/sandbox/jci/trunk/pom.xml Sun Jun  4 07:37:29 2006
@@ -30,6 +30,7 @@
     <module>compilers/eclipse</module>
     <module>compilers/janino</module>
     <module>compilers/groovy</module>
+    <module>compilers/javac</module>
     <module>tests</module>
     <module>examples</module>
   </modules>
@@ -131,7 +132,7 @@
       <plugin>
         <artifactId>maven-site-plugin</artifactId>
         <groupId>org.apache.maven.plugins</groupId>
-        <version>2.0-SNAPSHOT</version>
+        <version>2.0-beta-5</version>
       </plugin>
     </plugins>
 



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