Author: tcurdt Date: Thu Feb 22 13:01:57 2007 New Revision: 510650 URL: http://svn.apache.org/viewvc?view=rev&rev=510650 Log: move test into the compilers / core, still problems with the javac
Added: jakarta/commons/sandbox/jci/trunk/compilers/groovy/src/test/ jakarta/commons/sandbox/jci/trunk/compilers/groovy/src/test/java/ jakarta/commons/sandbox/jci/trunk/compilers/groovy/src/test/java/org/ jakarta/commons/sandbox/jci/trunk/compilers/groovy/src/test/java/org/apache/ jakarta/commons/sandbox/jci/trunk/compilers/groovy/src/test/java/org/apache/commons/ jakarta/commons/sandbox/jci/trunk/compilers/groovy/src/test/java/org/apache/commons/jci/ jakarta/commons/sandbox/jci/trunk/compilers/groovy/src/test/java/org/apache/commons/jci/compilers/ jakarta/commons/sandbox/jci/trunk/compilers/groovy/src/test/java/org/apache/commons/jci/compilers/GroovyJavaCompilerTestCase.java (with props) jakarta/commons/sandbox/jci/trunk/compilers/javac/src/test/ jakarta/commons/sandbox/jci/trunk/compilers/javac/src/test/java/ jakarta/commons/sandbox/jci/trunk/compilers/javac/src/test/java/org/ jakarta/commons/sandbox/jci/trunk/compilers/javac/src/test/java/org/apache/ jakarta/commons/sandbox/jci/trunk/compilers/javac/src/test/java/org/apache/commons/ jakarta/commons/sandbox/jci/trunk/compilers/javac/src/test/java/org/apache/commons/jci/ jakarta/commons/sandbox/jci/trunk/compilers/javac/src/test/java/org/apache/commons/jci/compilers/ jakarta/commons/sandbox/jci/trunk/compilers/javac/src/test/java/org/apache/commons/jci/compilers/JavacJavaCompilerTestCase.java (with props) Removed: jakarta/commons/sandbox/jci/trunk/tests/src/test/java/org/apache/commons/jci/compilers/AbstractCompilerTestCase.java jakarta/commons/sandbox/jci/trunk/tests/src/test/java/org/apache/commons/jci/compilers/GroovySources.java jakarta/commons/sandbox/jci/trunk/tests/src/test/java/org/apache/commons/jci/compilers/JavaCompilerFactoryTestCase.java jakarta/commons/sandbox/jci/trunk/tests/src/test/java/org/apache/commons/jci/compilers/JavaSources.java Modified: jakarta/commons/sandbox/jci/trunk/compilers/eclipse/src/test/java/org/apache/commons/jci/compilers/EclipseJavaCompilerTestCase.java jakarta/commons/sandbox/jci/trunk/compilers/groovy/src/main/java/org/apache/commons/jci/compilers/GroovyJavaCompiler.java jakarta/commons/sandbox/jci/trunk/compilers/janino/src/test/java/org/apache/commons/jci/compilers/JaninoJavaCompilerTestCase.java jakarta/commons/sandbox/jci/trunk/compilers/javac/src/main/java/org/apache/commons/jci/compilers/FileInputStreamProxy.java jakarta/commons/sandbox/jci/trunk/compilers/javac/src/main/java/org/apache/commons/jci/compilers/FileOutputStreamProxy.java jakarta/commons/sandbox/jci/trunk/compilers/javac/src/main/java/org/apache/commons/jci/compilers/JavacClassLoader.java jakarta/commons/sandbox/jci/trunk/compilers/javac/src/main/java/org/apache/commons/jci/compilers/JavacJavaCompiler.java jakarta/commons/sandbox/jci/trunk/core/src/test/java/org/apache/commons/jci/AbstractTestCase.java jakarta/commons/sandbox/jci/trunk/core/src/test/java/org/apache/commons/jci/compilers/AbstractCompilerTestCase.java Modified: jakarta/commons/sandbox/jci/trunk/compilers/eclipse/src/test/java/org/apache/commons/jci/compilers/EclipseJavaCompilerTestCase.java URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/jci/trunk/compilers/eclipse/src/test/java/org/apache/commons/jci/compilers/EclipseJavaCompilerTestCase.java?view=diff&rev=510650&r1=510649&r2=510650 ============================================================================== --- jakarta/commons/sandbox/jci/trunk/compilers/eclipse/src/test/java/org/apache/commons/jci/compilers/EclipseJavaCompilerTestCase.java (original) +++ jakarta/commons/sandbox/jci/trunk/compilers/eclipse/src/test/java/org/apache/commons/jci/compilers/EclipseJavaCompilerTestCase.java Thu Feb 22 13:01:57 2007 @@ -2,6 +2,10 @@ public final class EclipseJavaCompilerTestCase extends AbstractCompilerTestCase { + public String getCompilerName() { + return "eclipse"; + } + public JavaCompiler createJavaCompiler() { return new EclipseJavaCompiler(); } Modified: jakarta/commons/sandbox/jci/trunk/compilers/groovy/src/main/java/org/apache/commons/jci/compilers/GroovyJavaCompiler.java URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/jci/trunk/compilers/groovy/src/main/java/org/apache/commons/jci/compilers/GroovyJavaCompiler.java?view=diff&rev=510650&r1=510649&r2=510650 ============================================================================== --- jakarta/commons/sandbox/jci/trunk/compilers/groovy/src/main/java/org/apache/commons/jci/compilers/GroovyJavaCompiler.java (original) +++ jakarta/commons/sandbox/jci/trunk/compilers/groovy/src/main/java/org/apache/commons/jci/compilers/GroovyJavaCompiler.java Thu Feb 22 13:01:57 2007 @@ -26,7 +26,7 @@ public final class GroovyJavaCompiler extends AbstractJavaCompiler { - private final static Log log = LogFactory.getLog(GroovyJavaCompiler.class); + private final Log log = LogFactory.getLog(GroovyJavaCompiler.class); public CompilationResult compile( final String[] pResourceNames, @@ -42,7 +42,7 @@ for (int i = 0; i < source.length; i++) { final String resourceName = pResourceNames[i]; source[i] = new SourceUnit( - ClassUtils.convertResourceNameToClassName(resourceName), + ClassUtils.convertResourceToClassName(resourceName), new String(pReader.getBytes(resourceName)), // FIXME delay the read configuration, groovyClassLoader, @@ -61,7 +61,7 @@ for (final Iterator it = classes.iterator(); it.hasNext();) { final GroovyClass clazz = (GroovyClass) it.next(); final byte[] bytes = clazz.getBytes(); - pStore.write(clazz.getName(), bytes); + pStore.write(ClassUtils.convertClassToResourcePath(clazz.getName()), bytes); } } catch (final MultipleCompilationErrorsException e) { final ErrorCollector col = e.getErrorCollector(); Added: jakarta/commons/sandbox/jci/trunk/compilers/groovy/src/test/java/org/apache/commons/jci/compilers/GroovyJavaCompilerTestCase.java URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/jci/trunk/compilers/groovy/src/test/java/org/apache/commons/jci/compilers/GroovyJavaCompilerTestCase.java?view=auto&rev=510650 ============================================================================== --- jakarta/commons/sandbox/jci/trunk/compilers/groovy/src/test/java/org/apache/commons/jci/compilers/GroovyJavaCompilerTestCase.java (added) +++ jakarta/commons/sandbox/jci/trunk/compilers/groovy/src/test/java/org/apache/commons/jci/compilers/GroovyJavaCompilerTestCase.java Thu Feb 22 13:01:57 2007 @@ -0,0 +1,19 @@ +package org.apache.commons.jci.compilers; + + +public final class GroovyJavaCompilerTestCase extends AbstractCompilerTestCase { + + public String getCompilerName() { + return "groovy"; + } + + public JavaCompiler createJavaCompiler() { + return new GroovyJavaCompiler(); + } + + public void testInternalClassCompile() throws Exception { + // FIXME: inner classes not supported in groovy? + } + + +} Propchange: jakarta/commons/sandbox/jci/trunk/compilers/groovy/src/test/java/org/apache/commons/jci/compilers/GroovyJavaCompilerTestCase.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: jakarta/commons/sandbox/jci/trunk/compilers/groovy/src/test/java/org/apache/commons/jci/compilers/GroovyJavaCompilerTestCase.java ------------------------------------------------------------------------------ svn:keywords = Date Revision Author HeadURL Id Propchange: jakarta/commons/sandbox/jci/trunk/compilers/groovy/src/test/java/org/apache/commons/jci/compilers/GroovyJavaCompilerTestCase.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: jakarta/commons/sandbox/jci/trunk/compilers/janino/src/test/java/org/apache/commons/jci/compilers/JaninoJavaCompilerTestCase.java URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/jci/trunk/compilers/janino/src/test/java/org/apache/commons/jci/compilers/JaninoJavaCompilerTestCase.java?view=diff&rev=510650&r1=510649&r2=510650 ============================================================================== --- jakarta/commons/sandbox/jci/trunk/compilers/janino/src/test/java/org/apache/commons/jci/compilers/JaninoJavaCompilerTestCase.java (original) +++ jakarta/commons/sandbox/jci/trunk/compilers/janino/src/test/java/org/apache/commons/jci/compilers/JaninoJavaCompilerTestCase.java Thu Feb 22 13:01:57 2007 @@ -2,6 +2,10 @@ public final class JaninoJavaCompilerTestCase extends AbstractCompilerTestCase { + public String getCompilerName() { + return "janino"; + } + public JavaCompiler createJavaCompiler() { return new JaninoJavaCompiler(); } Modified: jakarta/commons/sandbox/jci/trunk/compilers/javac/src/main/java/org/apache/commons/jci/compilers/FileInputStreamProxy.java URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/jci/trunk/compilers/javac/src/main/java/org/apache/commons/jci/compilers/FileInputStreamProxy.java?view=diff&rev=510650&r1=510649&r2=510650 ============================================================================== --- jakarta/commons/sandbox/jci/trunk/compilers/javac/src/main/java/org/apache/commons/jci/compilers/FileInputStreamProxy.java (original) +++ jakarta/commons/sandbox/jci/trunk/compilers/javac/src/main/java/org/apache/commons/jci/compilers/FileInputStreamProxy.java Thu Feb 22 13:01:57 2007 @@ -3,113 +3,89 @@ 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(); +public class FileInputStreamProxy extends InputStream { + + private final static ThreadLocal readerThreadLocal = new ThreadLocal() { + public void set(Object o) { + System.out.println("writing " + o + " for " + Thread.currentThread().hashCode()); + super.set(o); + System.out.println("reading1 " + super.get() + " for " + Thread.currentThread().hashCode()); + } + + public Object get() { + final Object o = super.get(); + System.out.println("reading2 " + o + " for " + Thread.currentThread().hashCode()); + return o; } - return reader; + }; + + private final InputStream in; + + private final String name; + + public static void setResourceReader( final ResourceReader pReader ) { + readerThreadLocal.set(pReader); } - - public int available() throws IOException - { - return inputStream.available(); + + public FileInputStreamProxy(File pFile) throws FileNotFoundException { + this("" + pFile); } - public void close() throws IOException - { - inputStream.close(); + public FileInputStreamProxy(FileDescriptor fdObj) { + throw new RuntimeException(); } - public boolean equals(Object obj) - { - return inputStream.equals(obj); - } + public FileInputStreamProxy(String pName) throws FileNotFoundException { + name = pName; - public FileChannel getChannel() - { - // TODO - throw new RuntimeException(":("); - } + final ResourceReader reader = (ResourceReader) readerThreadLocal.get(); - public int hashCode() - { - return inputStream.hashCode(); + if (reader == null) { + throw new RuntimeException("forgot to set the ResourceReader for this thread?"); + } + + in = new ByteArrayInputStream(reader.getBytes(name)); + } + + public int read() throws IOException { + return in.read(); } - public void mark(int readlimit) - { - inputStream.mark(readlimit); + public int available() throws IOException { + return in.available(); } - public boolean markSupported() - { - return inputStream.markSupported(); + public void close() throws IOException { + in.close(); } - public int read() throws IOException - { - return inputStream.read(); + public synchronized void mark(int readlimit) { + in.mark(readlimit); } - public int read(byte[] b, int off, int len) throws IOException - { - return inputStream.read(b, off, len); + public boolean markSupported() { + return in.markSupported(); } - public int read(byte[] b) throws IOException - { - return inputStream.read(b); + public int read(byte[] b, int off, int len) throws IOException { + return in.read(b, off, len); } - public void reset() throws IOException - { - inputStream.reset(); + public int read(byte[] b) throws IOException { + return in.read(b); } - public long skip(long n) throws IOException - { - return inputStream.skip(n); + public synchronized void reset() throws IOException { + in.reset(); } - public String toString() - { - return inputStream.toString(); + public long skip(long n) throws IOException { + return in.skip(n); } } Modified: jakarta/commons/sandbox/jci/trunk/compilers/javac/src/main/java/org/apache/commons/jci/compilers/FileOutputStreamProxy.java URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/jci/trunk/compilers/javac/src/main/java/org/apache/commons/jci/compilers/FileOutputStreamProxy.java?view=diff&rev=510650&r1=510649&r2=510650 ============================================================================== --- jakarta/commons/sandbox/jci/trunk/compilers/javac/src/main/java/org/apache/commons/jci/compilers/FileOutputStreamProxy.java (original) +++ jakarta/commons/sandbox/jci/trunk/compilers/javac/src/main/java/org/apache/commons/jci/compilers/FileOutputStreamProxy.java Thu Feb 22 13:01:57 2007 @@ -6,111 +6,52 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.io.OutputStream; -import org.apache.commons.jci.stores.ResourceStore; -public class FileOutputStreamProxy extends OutputStream -{ - private ByteArrayOutputStream out = null; - - private ResourceStore store = null; +public class FileOutputStreamProxy extends OutputStream { - private String fileName; - public FileOutputStreamProxy(File file, boolean append) - throws FileNotFoundException - { - fileName = file.getName(); - out = new ByteArrayOutputStream(); - if(append) - try - { - out.write(getStore().read(fileName)); - } - catch (IOException e) - { - } - } - - public FileOutputStreamProxy(File file) throws FileNotFoundException - { - fileName = file.getName(); - out = new ByteArrayOutputStream(); - } - - public FileOutputStreamProxy(FileDescriptor fdObj) - { - throw new RuntimeException(":("); - } - - public FileOutputStreamProxy(String name, boolean append) - throws FileNotFoundException - { - fileName = name; - out = new ByteArrayOutputStream(); - if(append) - try - { - out.write(getStore().read(fileName)); - } - catch (IOException e) - { - } - } - - public FileOutputStreamProxy(String name) throws FileNotFoundException - { - fileName = name; - out = new ByteArrayOutputStream(); - } + private final ByteArrayOutputStream out = new ByteArrayOutputStream(); + private final String name; - private ResourceStore getStore() - { - if (store == null) - { - JavacClassLoader loader = (JavacClassLoader)Thread.currentThread().getContextClassLoader(); - store = loader.getStore(); - } - return store; + public FileOutputStreamProxy(File pFile, boolean append) throws FileNotFoundException { + name = pFile.getName(); } - public void close() throws IOException - { - getStore().write(fileName, out.toByteArray()); - out.close(); + public FileOutputStreamProxy(File pFile) throws FileNotFoundException { + System.out.println("Writing to file " + pFile); + name = pFile.getName(); } - public boolean equals(Object obj) - { - return out.equals(obj); + public FileOutputStreamProxy(FileDescriptor fdObj) { + throw new RuntimeException(); } - public void flush() throws IOException - { - out.flush(); + public FileOutputStreamProxy(String pName, boolean append) throws FileNotFoundException { + name = pName; } - public int hashCode() - { - return out.hashCode(); + public FileOutputStreamProxy(String pName) throws FileNotFoundException { + name = pName; + } + + public void write(int value) throws IOException { + out.write(value); } - public String toString() - { - return out.toString(); + public void close() throws IOException { + System.out.println("Wrote " + out.size() + " bytes"); + out.close(); } - public void write(byte[] b, int off, int len) throws IOException - { - out.write(b, off, len); + public void flush() throws IOException { + out.flush(); } - public void write(byte[] b) throws IOException - { - out.write(b); + public void write(byte[] b, int off, int len) throws IOException { + out.write(b, off, len); } - public void write(int b) throws IOException - { + public void write(byte[] b) throws IOException { out.write(b); } } Modified: jakarta/commons/sandbox/jci/trunk/compilers/javac/src/main/java/org/apache/commons/jci/compilers/JavacClassLoader.java URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/jci/trunk/compilers/javac/src/main/java/org/apache/commons/jci/compilers/JavacClassLoader.java?view=diff&rev=510650&r1=510649&r2=510650 ============================================================================== --- jakarta/commons/sandbox/jci/trunk/compilers/javac/src/main/java/org/apache/commons/jci/compilers/JavacClassLoader.java (original) +++ jakarta/commons/sandbox/jci/trunk/compilers/javac/src/main/java/org/apache/commons/jci/compilers/JavacClassLoader.java Thu Feb 22 13:01:57 2007 @@ -2,65 +2,81 @@ import java.io.FileInputStream; import java.io.FileOutputStream; -import javassist.ClassMap; -import javassist.ClassPool; -import javassist.CtClass; -import org.apache.commons.jci.readers.ResourceReader; -import org.apache.commons.jci.stores.ResourceStore; - -public class JavacClassLoader extends ClassLoader -{ - private ClassPool classPool; - - private ResourceReader reader; - - private ResourceStore store; - - public JavacClassLoader(ClassPool classPool, ResourceReader reader, - ResourceStore store, ClassLoader parent) - { - super(parent); - this.classPool = classPool; - this.reader = reader; - this.store = store; +import java.io.IOException; +import java.io.InputStream; + +import org.apache.commons.io.IOUtils; +import org.objectweb.asm.ClassReader; +import org.objectweb.asm.ClassWriter; +import org.objectweb.asm.util.CheckClassAdapter; +import org.vafer.dependency.asm.RenamingVisitor; +import org.vafer.dependency.utils.ResourceRenamer; + +public final class JavacClassLoader extends ClassLoader { + + public JavacClassLoader( final ClassLoader pParent ) { + super(pParent); } + + protected Class findClass( final String name ) throws ClassNotFoundException { - protected Class findClass(String name) throws ClassNotFoundException - { - try - { - CtClass jc = classPool.get(name); - if (jc != null) - { - if (name.startsWith("com.sun.tools.javac")) - { - ClassMap classMap = new ClassMap(); - classMap.put(FileOutputStream.class.getName(), - FileOutputStreamProxy.class.getName()); - classMap.put(FileInputStream.class.getName(), - FileInputStreamProxy.class.getName()); - jc.replaceClassName(classMap); - } - return jc.toClass(); - } - else - { - return getParent().loadClass(name); - } + //System.out.println("findClass " + name); + + if (name.startsWith("java.")) { + return super.findClass(name); } - catch (Exception e) - { + + final InputStream classStream = getResourceAsStream(name.replace('.', '/') + ".class"); + + try { + + final byte[] classBytes; + + if (name.startsWith("")) { + final ClassWriter renamedCw = new ClassWriter(true, false); + new ClassReader(classStream).accept(new RenamingVisitor(new CheckClassAdapter(renamedCw), new ResourceRenamer() { + public String getNewNameFor(final String pOldName) { + if (pOldName.startsWith(FileOutputStream.class.getName())) { + //System.out.println("rewriting FOS" + name); + return FileOutputStreamProxy.class.getName(); + } + if (pOldName.startsWith(FileInputStream.class.getName())) { + //System.out.println("rewriting FIS" + name); + return FileInputStreamProxy.class.getName(); + } + return pOldName; + } + }), false); + + classBytes = renamedCw.toByteArray(); + + } else { + classBytes = IOUtils.toByteArray(classStream); + } + + return defineClass(name, classBytes, 0, classBytes.length); + } catch (IOException e) { + throw new ClassNotFoundException("", e); } - return super.findClass(name); } - public ResourceReader getReader() - { - return reader; - } + protected synchronized Class loadClass( final String classname, final boolean resolve ) throws ClassNotFoundException { + + Class theClass = findLoadedClass(classname); + if (theClass != null) { + return theClass; + } + + try { + theClass = findClass(classname); + } catch (ClassNotFoundException cnfe) { + theClass = getParent().loadClass(classname); + } + + if (resolve) { + resolveClass(theClass); + } - public ResourceStore getStore() - { - return store; + return theClass; } } Modified: jakarta/commons/sandbox/jci/trunk/compilers/javac/src/main/java/org/apache/commons/jci/compilers/JavacJavaCompiler.java URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/jci/trunk/compilers/javac/src/main/java/org/apache/commons/jci/compilers/JavacJavaCompiler.java?view=diff&rev=510650&r1=510649&r2=510650 ============================================================================== --- jakarta/commons/sandbox/jci/trunk/compilers/javac/src/main/java/org/apache/commons/jci/compilers/JavacJavaCompiler.java (original) +++ jakarta/commons/sandbox/jci/trunk/compilers/javac/src/main/java/org/apache/commons/jci/compilers/JavacJavaCompiler.java Thu Feb 22 13:01:57 2007 @@ -10,258 +10,234 @@ import java.util.List; import java.util.NoSuchElementException; import java.util.StringTokenizer; -import javassist.ClassPool; -import javassist.NotFoundException; + import org.apache.commons.jci.problems.CompilationProblem; import org.apache.commons.jci.readers.ResourceReader; import org.apache.commons.jci.stores.ResourceStore; -public class JavacJavaCompiler extends AbstractJavaCompiler -{ - protected static final String EOL = System.getProperty("line.separator");; +public final class JavacJavaCompiler extends AbstractJavaCompiler { + private static final String EOL = System.getProperty("line.separator");; private static final String WARNING_PREFIX = "warning: "; - private static final String NOTE_PREFIX = "Note: "; - private static final String ERROR_PREFIX = "error: "; private final JavacJavaCompilerSettings settings; - public JavacJavaCompiler(final JavacJavaCompilerSettings settings) - { - super(); - this.settings = settings; + public JavacJavaCompiler() { + settings = null; + } + + + public JavacJavaCompiler( final JavacJavaCompilerSettings pSettings ) { + settings = pSettings; } - public CompilationResult compile(String[] resourcePaths, - ResourceReader reader, ResourceStore store, - ClassLoader baseClassLoader) - { - ClassPool classPool = ClassPool.getDefault(); - try - { - classPool.appendClassPath(System.getProperty("java.home") - + "/../lib/tools.jar"); - } - catch (NotFoundException e) - { - return new CompilationResult( - new CompilationProblem[] { new JavacCompilationProblem( - "tools.jar not fount", true) }); - } - JavacClassLoader javacClassLoader = new JavacClassLoader(classPool, - reader, store, baseClassLoader); - ClassLoader oldLoader = Thread.currentThread().getContextClassLoader(); - Thread.currentThread().setContextClassLoader(javacClassLoader); - try - { - Class javacMain = javacClassLoader - .loadClass("com.sun.tools.javac.Main"); - Method compile = javacMain.getMethod("compile", new Class[] { - String[].class, PrintWriter.class }); - StringWriter out = new StringWriter(); - Integer ok = (Integer) compile.invoke(null, - new Object[] { buildCompilerArguments(resourcePaths), - new PrintWriter(out) }); - CompilationResult result = parseModernStream(new BufferedReader( - new StringReader(out.toString()))); - if (result.getErrors().length == 0 && ok.intValue() != 0) - return new CompilationResult( - new CompilationProblem[] { new JavacCompilationProblem( - "Failure executing javac, but could not parse the error: " - + out.toString(), true) }); + public CompilationResult compile( final String[] pSourcePaths, final ResourceReader pReader, ResourceStore pStore, final ClassLoader pClasspathClassLoader) { + + try { + final ClassLoader cl = new JavacClassLoader(pClasspathClassLoader); + final Class renamedClass = cl.loadClass("com.sun.tools.javac.Main"); + + FileInputStreamProxy.setResourceReader(pReader); + + final Method compile = renamedClass.getMethod("compile", new Class[] { String[].class, PrintWriter.class }); + final StringWriter out = new StringWriter(); + final Integer ok = (Integer) compile.invoke(null, new Object[] { pSourcePaths, new PrintWriter(out) }); + + final CompilationResult result = parseModernStream(new BufferedReader(new StringReader(out.toString()))); + + if (result.getErrors().length == 0 && ok.intValue() != 0) { + return new CompilationResult(new CompilationProblem[] { + new JavacCompilationProblem("Failure executing javac, but could not parse the error: " + out.toString(), true) }); + } + return result; - } - catch (Exception e) - { - return new CompilationResult( - new CompilationProblem[] { new JavacCompilationProblem( - "Error while executing the compiler: " - + e.toString(), true) }); - } - finally - { - Thread.currentThread().setContextClassLoader(oldLoader); - } + + } catch(Exception e) { + return new CompilationResult(new CompilationProblem[] { + new JavacCompilationProblem("Error while executing the compiler: " + e.toString(), true) }); + } + + + + +// JavacClassLoader javacClassLoader = new JavacClassLoader(classPool, +// reader, store, baseClassLoader); +// ClassLoader oldLoader = Thread.currentThread().getContextClassLoader(); +// Thread.currentThread().setContextClassLoader(javacClassLoader); +// try +// { +// } +// catch (Exception e) +// { +// } +// finally +// { +// Thread.currentThread().setContextClassLoader(oldLoader); +// } } - private CompilationResult parseModernStream(BufferedReader input) - throws IOException - { - List problems = new ArrayList(); + private CompilationResult parseModernStream( final BufferedReader pReader ) throws IOException { + final List problems = new ArrayList(); String line; - StringBuffer buffer; - while (true) - { + + while (true) { // cleanup the buffer - buffer = new StringBuffer(); // this is quicker than clearing it + final StringBuffer buffer = new StringBuffer(); + // most errors terminate with the '^' char - do - { - line = input.readLine(); - if (line == null) - { - return new CompilationResult( - (CompilationProblem[]) problems - .toArray(new CompilationProblem[problems - .size()])); + do { + line = pReader.readLine(); + if (line == null) { + return new CompilationResult((CompilationProblem[]) problems.toArray(new CompilationProblem[problems.size()])); } + // TODO: there should be a better way to parse these - if (buffer.length() == 0 && line.startsWith(ERROR_PREFIX)) - { + if (buffer.length() == 0 && line.startsWith(ERROR_PREFIX)) { problems.add(new JavacCompilationProblem(line, true)); } - else - if (buffer.length() == 0 && line.startsWith(NOTE_PREFIX)) - { - // skip this one - it is JDK 1.5 telling us that the - // interface is deprecated. - } - else - { - buffer.append(line); - buffer.append(EOL); - } - } - while (!line.endsWith("^")); - // add the error bean + else if (buffer.length() == 0 && line.startsWith(NOTE_PREFIX)) { + // skip this one - it is JDK 1.5 telling us that the + // interface is deprecated. + } else { + buffer.append(line); + buffer.append(EOL); + } + } while (!line.endsWith("^")); + + // add the error problems.add(parseModernError(buffer.toString())); } } - private CompilationProblem parseModernError(String error) - { - StringTokenizer tokens = new StringTokenizer(error, ":"); + private CompilationProblem parseModernError( final String pError ) { + final StringTokenizer tokens = new StringTokenizer(pError, ":"); boolean isError = true; - StringBuffer msgBuffer; - try - { + try { String file = tokens.nextToken(); // When will this happen? - if (file.length() == 1) - { + if (file.length() == 1) { file = new StringBuffer(file).append(":").append( tokens.nextToken()).toString(); } - int line = Integer.parseInt(tokens.nextToken()); - msgBuffer = new StringBuffer(); + final int line = Integer.parseInt(tokens.nextToken()); + final StringBuffer msgBuffer = new StringBuffer(); + String msg = tokens.nextToken(EOL).substring(2); isError = !msg.startsWith(WARNING_PREFIX); + // Remove the 'warning: ' prefix - if (!isError) - { + if (!isError) { msg = msg.substring(WARNING_PREFIX.length()); } msgBuffer.append(msg); + String context = tokens.nextToken(EOL); String pointer = tokens.nextToken(EOL); - if (tokens.hasMoreTokens()) - { + + if (tokens.hasMoreTokens()) { msgBuffer.append(EOL); msgBuffer.append(context); // 'symbol' line msgBuffer.append(EOL); msgBuffer.append(pointer); // 'location' line msgBuffer.append(EOL); + context = tokens.nextToken(EOL); - try - { + + try { pointer = tokens.nextToken(EOL); - } - catch (NoSuchElementException e) - { + } catch (NoSuchElementException e) { pointer = context; context = null; } } - String message = msgBuffer.toString(); + final String message = msgBuffer.toString(); int startcolumn = pointer.indexOf("^"); - int endcolumn = context == null ? startcolumn : context.indexOf( - " ", startcolumn); - if (endcolumn == -1) - { + int endcolumn = context == null ? startcolumn : context.indexOf(" ", startcolumn); + if (endcolumn == -1) { endcolumn = context.length(); } - return new JavacCompilationProblem(file, isError, line, - startcolumn, line, endcolumn, message); + return new JavacCompilationProblem(file, isError, line, startcolumn, line, endcolumn, message); } - catch (NoSuchElementException e) - { - return new JavacCompilationProblem( - "no more tokens - could not parse error message: " + error, - isError); - } - catch (NumberFormatException e) - { - return new JavacCompilationProblem( - "could not parse error message: " + error, isError); - } - catch (Exception e) - { - return new JavacCompilationProblem( - "could not parse error message: " + error, isError); + catch (NoSuchElementException e) { + return new JavacCompilationProblem("no more tokens - could not parse error message: " + pError, isError); + } + catch (NumberFormatException e) { + return new JavacCompilationProblem("could not parse error message: " + pError, isError); + } + catch (Exception e) { + return new JavacCompilationProblem("could not parse error message: " + pError, isError); } } - private String[] buildCompilerArguments(String[] resourcePaths) + private String[] buildCompilerArguments( final String[] resourcePaths ) { - List args = new ArrayList(); - for (int i = 0; i < resourcePaths.length; i++) - { + final List args = new ArrayList(); + for (int i = 0; i < resourcePaths.length; i++) { args.add(resourcePaths[i]); } - if (settings != null) - { - if (settings.isOptimize()) + + if (settings != null) { + if (settings.isOptimize()) { args.add("-O"); - if (settings.isDebug()) + } + + if (settings.isDebug()) { args.add("-g"); - if (settings.isVerbose()) + } + + if (settings.isVerbose()) { args.add("-verbose"); - if (settings.isShowDeprecation()) - { + } + + if (settings.isShowDeprecation()) { args.add("-deprecation"); // This is required to actually display the deprecation messages settings.setShowWarnings(true); } - if (settings.getMaxmem() != null) + + if (settings.getMaxmem() != null) { args.add("-J-Xmx" + settings.getMaxmem()); - if (settings.getMeminitial() != null) + } + + if (settings.getMeminitial() != null) { args.add("-J-Xms" + settings.getMeminitial()); - if (!settings.isShowWarnings()) + } + + if (!settings.isShowWarnings()) { args.add("-nowarn"); + } + // TODO: this could be much improved - if (settings.getTargetVersion() != null) - { + if (settings.getTargetVersion() != null) { // Required, or it defaults to the target of your JDK (eg 1.5) args.add("-target"); args.add("1.1"); - } - else - { + } else { args.add("-target"); args.add(settings.getTargetVersion()); } + // TODO suppressSource - if (settings.getSourceVersion() != null) - { + if (settings.getSourceVersion() != null) { // If omitted, later JDKs complain about a 1.1 target args.add("-source"); args.add("1.3"); - } - else - { + } else { args.add("-source"); args.add(settings.getSourceVersion()); } + // TODO suppressEncoding - if (settings.getSourceEncoding() != null) - { + if (settings.getSourceEncoding() != null) { args.add("-encoding"); args.add(settings.getSourceEncoding()); } + // TODO CustomCompilerArguments } + return (String[]) args.toArray(new String[args.size()]); } } Added: jakarta/commons/sandbox/jci/trunk/compilers/javac/src/test/java/org/apache/commons/jci/compilers/JavacJavaCompilerTestCase.java URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/jci/trunk/compilers/javac/src/test/java/org/apache/commons/jci/compilers/JavacJavaCompilerTestCase.java?view=auto&rev=510650 ============================================================================== --- jakarta/commons/sandbox/jci/trunk/compilers/javac/src/test/java/org/apache/commons/jci/compilers/JavacJavaCompilerTestCase.java (added) +++ jakarta/commons/sandbox/jci/trunk/compilers/javac/src/test/java/org/apache/commons/jci/compilers/JavacJavaCompilerTestCase.java Thu Feb 22 13:01:57 2007 @@ -0,0 +1,13 @@ +package org.apache.commons.jci.compilers; + +public class JavacJavaCompilerTestCase extends AbstractCompilerTestCase { + + public JavaCompiler createJavaCompiler() { + return new JavacJavaCompiler(); + } + + public String getCompilerName() { + return "javac"; + } + +} Propchange: jakarta/commons/sandbox/jci/trunk/compilers/javac/src/test/java/org/apache/commons/jci/compilers/JavacJavaCompilerTestCase.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: jakarta/commons/sandbox/jci/trunk/compilers/javac/src/test/java/org/apache/commons/jci/compilers/JavacJavaCompilerTestCase.java ------------------------------------------------------------------------------ svn:keywords = Date Revision Author HeadURL Id Propchange: jakarta/commons/sandbox/jci/trunk/compilers/javac/src/test/java/org/apache/commons/jci/compilers/JavacJavaCompilerTestCase.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: jakarta/commons/sandbox/jci/trunk/core/src/test/java/org/apache/commons/jci/AbstractTestCase.java URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/jci/trunk/core/src/test/java/org/apache/commons/jci/AbstractTestCase.java?view=diff&rev=510650&r1=510649&r2=510650 ============================================================================== --- jakarta/commons/sandbox/jci/trunk/core/src/test/java/org/apache/commons/jci/AbstractTestCase.java (original) +++ jakarta/commons/sandbox/jci/trunk/core/src/test/java/org/apache/commons/jci/AbstractTestCase.java Thu Feb 22 13:01:57 2007 @@ -30,21 +30,8 @@ private final Log log = LogFactory.getLog(AbstractTestCase.class); - protected String extension = "java"; - protected File directory; - /* - public void runBare() throws Throwable { - try { - setUp(); - runTest(); - } finally { - tearDown(); - } - } - */ - protected void setUp() throws Exception { System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog"); Modified: jakarta/commons/sandbox/jci/trunk/core/src/test/java/org/apache/commons/jci/compilers/AbstractCompilerTestCase.java URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/jci/trunk/core/src/test/java/org/apache/commons/jci/compilers/AbstractCompilerTestCase.java?view=diff&rev=510650&r1=510649&r2=510650 ============================================================================== --- jakarta/commons/sandbox/jci/trunk/core/src/test/java/org/apache/commons/jci/compilers/AbstractCompilerTestCase.java (original) +++ jakarta/commons/sandbox/jci/trunk/core/src/test/java/org/apache/commons/jci/compilers/AbstractCompilerTestCase.java Thu Feb 22 13:01:57 2007 @@ -3,15 +3,26 @@ import java.util.HashMap; import java.util.Map; +import junit.framework.TestCase; + +import org.apache.commons.jci.problems.CompilationProblem; import org.apache.commons.jci.readers.ResourceReader; import org.apache.commons.jci.stores.MemoryResourceStore; -import junit.framework.TestCase; - public abstract class AbstractCompilerTestCase extends TestCase { public abstract JavaCompiler createJavaCompiler(); + public abstract String getCompilerName(); + + public void testFactoryCreation() { + final JavaCompiler factoryCompiler = new JavaCompilerFactory().createCompiler(getCompilerName()); + assertNotNull(factoryCompiler); + + final JavaCompiler compiler = createJavaCompiler(); + assertEquals(factoryCompiler.getClass().getName(), compiler.getClass().getName()); + } + public void testSimpleCompile() throws Exception { final JavaCompiler compiler = createJavaCompiler(); @@ -42,8 +53,8 @@ "jci/Simple.java" }, reader, store); - assertEquals(0, result.getErrors().length); - assertEquals(0, result.getWarnings().length); + 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); @@ -84,12 +95,12 @@ final MemoryResourceStore store = new MemoryResourceStore(); final CompilationResult result = compiler.compile( new String[] { - "jci/Simple.java", - "jci/Extended.java" + "jci/Extended.java", + "jci/Simple.java" }, reader, store); - assertEquals(0, result.getErrors().length); - assertEquals(0, result.getWarnings().length); + assertEquals(toString(result.getErrors()), 0, result.getErrors().length); + assertEquals(toString(result.getWarnings()), 0, result.getWarnings().length); final byte[] clazzBytesSimple = store.read("jci/Simple.class"); assertNotNull(clazzBytesSimple); @@ -100,4 +111,101 @@ assertTrue(clazzBytesExtended.length > 0); } + public void testInternalClassCompile() throws Exception { + final JavaCompiler compiler = createJavaCompiler(); + + final ResourceReader reader = new ResourceReader() { + final private Map sources = new HashMap() {{ + put("jci/Simple.java", ( + "package jci;\n" + + "public class Simple {\n" + + " private class Sub {\n" + + " }\n" + + " public String toString() {\n" + + " new Sub();\n" + + " return \"Simple\";\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.java" + }, 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); + + final byte[] subClazzBytes = store.read("jci/Simple$Sub.class"); + assertNotNull(subClazzBytes); + assertTrue(subClazzBytes.length > 0); + + } + + public void testUppercasePackageNameCompile() throws Exception { + final JavaCompiler compiler = createJavaCompiler(); + + final ResourceReader reader = new ResourceReader() { + final private Map sources = new HashMap() {{ + put("Jci/Simple.java", ( + "package Jci;\n" + + "public class Simple {\n" + + " public String toString() {\n" + + " return \"Simple\";\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.java" + }, 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); + } + + + + private final String toString( final CompilationProblem[] pProblems ) { + final StringBuffer sb = new StringBuffer(); + + for (int i = 0; i < pProblems.length; i++) { + final CompilationProblem problem = pProblems[i]; + sb.append(problem.getMessage()).append(", "); + } + + return sb.toString(); + } + } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]