Update MBoot compiler relative to changes in plexus-compiler.

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@163914 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Emmanuel Venisse 2005-04-12 21:13:24 +00:00
parent ccfdf7af42
commit 57e8d48ef2
5 changed files with 190 additions and 72 deletions

View File

@ -885,6 +885,7 @@ public class MBoot
CompilerConfiguration compilerConfiguration = new CompilerConfiguration(); CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
compilerConfiguration.setOutputLocation( outputDirectory ); compilerConfiguration.setOutputLocation( outputDirectory );
List classpathEntries = classpath( dependencies, extraClasspath, scope, localRepository ); List classpathEntries = classpath( dependencies, extraClasspath, scope, localRepository );
compilerConfiguration.setNoWarn( true );
compilerConfiguration.setClasspathEntries( classpathEntries ); compilerConfiguration.setClasspathEntries( classpathEntries );
compilerConfiguration.setSourceLocations( Arrays.asList( sourceDirectories ) ); compilerConfiguration.setSourceLocations( Arrays.asList( sourceDirectories ) );

View File

@ -4,17 +4,14 @@ import util.DirectoryScanner;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
/** /**
* * @author <a href="mailto:jason@maven.org">Jason van Zyl </a>
* * @author <a href="mailto:michal.maczka@dimatics.com">Michal Maczka </a>
* @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
* @author <a href="mailto:michal.maczka@dimatics.com">Michal Maczka</a>
*
* @version $Id$ * @version $Id$
*/ */
public abstract class AbstractCompiler public abstract class AbstractCompiler
@ -22,12 +19,20 @@ public abstract class AbstractCompiler
{ {
private static String PS = System.getProperty( "path.separator" ); private static String PS = System.getProperty( "path.separator" );
public String getClasspathString( List classpathElements ) /**
* @deprecated Use getPathString(..) instead.
*/
public String getClasspathString( List pathElements ) throws Exception
{
return getPathString( pathElements );
}
public String getPathString( List pathElements )
throws Exception throws Exception
{ {
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
for ( Iterator it = classpathElements.iterator(); it.hasNext(); ) for ( Iterator it = pathElements.iterator(); it.hasNext(); )
{ {
String element = (String) it.next(); String element = (String) it.next();
@ -39,46 +44,70 @@ public abstract class AbstractCompiler
protected String[] getSourceFiles( CompilerConfiguration config ) protected String[] getSourceFiles( CompilerConfiguration config )
{ {
List sources = new ArrayList(); Set sources = new HashSet();
for ( Iterator it = config.getSourceLocations().iterator(); it.hasNext(); ) Set sourceFiles = config.getSourceFiles();
if ( sourceFiles != null && !sourceFiles.isEmpty() )
{ {
String sourceLocation = (String) it.next(); for ( Iterator it = sourceFiles.iterator(); it.hasNext(); )
DirectoryScanner scanner = new DirectoryScanner();
scanner.setBasedir( sourceLocation );
Set includes = config.getIncludes();
if(includes != null && !includes.isEmpty()) {
String[] inclStrs = (String[])includes.toArray(new String[includes.size()]);
scanner.setIncludes( inclStrs );
}
else {
scanner.setIncludes(new String[] {"**/*.java"});
}
Set excludes = config.getIncludes();
if(excludes != null && !excludes.isEmpty()) {
String[] exclStrs = (String[])excludes.toArray(new String[excludes.size()]);
scanner.setIncludes( exclStrs );
}
scanner.scan();
String[] sourceDirectorySources = scanner.getIncludedFiles();
for ( int j = 0; j < sourceDirectorySources.length; j++ )
{ {
File f = new File( sourceLocation, sourceDirectorySources[j] ); File sourceFile = (File) it.next();
sources.add( sourceFile.getAbsolutePath() );
}
}
else
{
for ( Iterator it = config.getSourceLocations().iterator(); it.hasNext(); )
{
String sourceLocation = (String) it.next();
sources.add( f.getPath() ); DirectoryScanner scanner = new DirectoryScanner();
scanner.setBasedir( sourceLocation );
Set includes = config.getIncludes();
if ( includes != null && !includes.isEmpty() )
{
String[] inclStrs = (String[]) includes.toArray( new String[includes.size()] );
scanner.setIncludes( inclStrs );
}
else
{
scanner.setIncludes( new String[] { "**/*.java" } );
}
Set excludes = config.getExcludes();
if ( excludes != null && !excludes.isEmpty() )
{
String[] exclStrs = (String[]) excludes.toArray( new String[excludes.size()] );
scanner.setIncludes( exclStrs );
}
scanner.scan();
String[] sourceDirectorySources = scanner.getIncludedFiles();
for ( int j = 0; j < sourceDirectorySources.length; j++ )
{
File f = new File( sourceLocation, sourceDirectorySources[j] );
sources.add( f.getPath() );
}
} }
} }
String[] sourceArray = new String[sources.size()]; String[] result = null;
return (String[]) sources.toArray( sourceArray ); if ( sources.isEmpty() )
{
result = new String[0];
}
else
{
result = (String[]) sources.toArray( new String[sources.size()] );
}
return result;
} }
protected String makeClassName( String fileName, String sourceDir ) protected String makeClassName( String fileName, String sourceDir )
@ -97,8 +126,7 @@ public abstract class AbstractCompiler
if ( sourceDir != null ) if ( sourceDir != null )
{ {
String prefix = String prefix = new File( sourceDir ).getCanonicalPath().replace( '\\', '/' );
new File( sourceDir ).getCanonicalPath().replace( '\\', '/' );
if ( canonical != null ) if ( canonical != null )
{ {

View File

@ -17,13 +17,32 @@ public class CompilerConfiguration
{ {
private String outputLocation; private String outputLocation;
private List classpathEntries = new LinkedList(); private List classpathEntries = new LinkedList();
private List sourceLocations = new LinkedList(); private List sourceLocations = new LinkedList();
private Set includes = new HashSet(); private Set includes = new HashSet();
private Set excludes = new HashSet(); private Set excludes = new HashSet();
private Map compilerOptions = new TreeMap(); private Map compilerOptions = new TreeMap();
private boolean debug = false; private boolean debug = false;
private Set sourceFiles = new HashSet();
private boolean noWarn;
public void setSourceFiles(Set sourceFiles)
{
this.sourceFiles = sourceFiles;
}
public Set getSourceFiles()
{
return sourceFiles;
}
public void setOutputLocation(String outputLocation) public void setOutputLocation(String outputLocation)
{ {
this.outputLocation = outputLocation; this.outputLocation = outputLocation;
@ -113,4 +132,14 @@ public class CompilerConfiguration
return debug; return debug;
} }
public void setNoWarn( boolean noWarn )
{
this.noWarn = noWarn;
}
public boolean isNoWarn()
{
return noWarn;
}
} }

View File

@ -97,6 +97,18 @@ public class CompilerError
this.message = message; this.message = message;
} }
/**
* The error message constructor.
*
* @param message The actual error text produced by the language processor
* @param error whether it was an error or informational
*/
public CompilerError( String message, boolean error )
{
this.message = message;
this.error = error;
}
/** /**
* Return the filename associated with this compiler error. * Return the filename associated with this compiler error.
* *
@ -173,6 +185,13 @@ public class CompilerError
public String toString() public String toString()
{ {
return file + ":" + "[" + startline + "," + startcolumn + "] " + message; if ( file != null )
{
return file + ":" + "[" + startline + "," + startcolumn + "] " + message;
}
else
{
return message;
}
} }
} }

View File

@ -25,8 +25,6 @@ import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -45,7 +43,8 @@ public class JavacCompiler
{ {
} }
public List compile( CompilerConfiguration config ) throws Exception public List compile( CompilerConfiguration config )
throws Exception
{ {
File destinationDir = new File( config.getOutputLocation() ); File destinationDir = new File( config.getOutputLocation() );
@ -61,8 +60,9 @@ public class JavacCompiler
return Collections.EMPTY_LIST; return Collections.EMPTY_LIST;
} }
System.out.println( "Compiling " + sources.length + " source file" + ( sources.length == 1 ? "" : "s" ) // TODO: use getLogger() - but for some reason it is null when this is used
+ " to " + destinationDir.getAbsolutePath() ); System.out.println( "Compiling " + sources.length + " source file" + ( sources.length == 1 ? "" : "s" ) +
" to " + destinationDir.getAbsolutePath() );
Map compilerOptions = config.getCompilerOptions(); Map compilerOptions = config.getCompilerOptions();
@ -72,25 +72,57 @@ public class JavacCompiler
args.add( destinationDir.getAbsolutePath() ); args.add( destinationDir.getAbsolutePath() );
args.add( "-nowarn" ); if ( config.isNoWarn() )
{
args.add( "-nowarn" );
}
args.add( "-classpath" ); List classpathEntries = config.getClasspathEntries();
if ( classpathEntries != null && !classpathEntries.isEmpty() )
{
args.add( "-classpath" );
args.add( getClasspathString( config.getClasspathEntries() ) ); args.add( getPathString( classpathEntries ) );
}
if ( config.isDebug() ) if ( config.isDebug() )
{ {
args.add( "-g" ); args.add( "-g" );
} }
List sourceLocations = config.getSourceLocations();
if ( sourceLocations != null && !sourceLocations.isEmpty() )
{
args.add( "-sourcepath" );
args.add( getPathString( sourceLocations ) );
}
// TODO: this could be much improved
if ( !compilerOptions.containsKey( "-target" ) )
{
if ( !compilerOptions.containsKey( "-source" ) )
{
// If omitted, later JDKs complain about a 1.1 target
args.add( "-source" );
args.add( "1.3" );
}
// Required, or it defaults to the target of your JDK (eg 1.5)
args.add( "-target" );
args.add( "1.1" );
}
Iterator it = compilerOptions.entrySet().iterator(); Iterator it = compilerOptions.entrySet().iterator();
while ( it.hasNext() ) while ( it.hasNext() )
{ {
Map.Entry entry = (Map.Entry) it.next(); Map.Entry entry = (Map.Entry) it.next();
args.add( entry.getKey() ); args.add( entry.getKey() );
if ( (entry.getValue() != null) ) if ( ( entry.getValue() != null ) )
{
args.add( entry.getValue() ); args.add( entry.getValue() );
}
} }
for ( int i = 0; i < sources.length; i++ ) for ( int i = 0; i < sources.length; i++ )
@ -102,32 +134,34 @@ public class JavacCompiler
File toolsJar = new File( System.getProperty( "java.home" ), "../lib/tools.jar" ); File toolsJar = new File( System.getProperty( "java.home" ), "../lib/tools.jar" );
cl.addURL( toolsJar.toURL() ); if ( toolsJar.exists() )
{
cl.addURL( toolsJar.toURL() );
}
Class c = cl.loadClass( "sun.tools.javac.Main" ); Class c = cl.loadClass( "com.sun.tools.javac.Main" );
Constructor cons = c.getConstructor( new Class[] { OutputStream.class, String.class } );
ByteArrayOutputStream err = new ByteArrayOutputStream(); ByteArrayOutputStream err = new ByteArrayOutputStream();
Object compiler = cons.newInstance( new Object[] { err, "javac" } ); Method compile = c.getMethod( "compile", new Class[]{String[].class} );
Method compile = c.getMethod( "compile", new Class[] { String[].class } ); Integer ok = (Integer) compile.invoke( null, new Object[]{args.toArray( new String[0] )} );
Boolean ok = (Boolean) compile.invoke( compiler, new Object[] { args.toArray( new String[0] ) } ); List messages = parseModernStream(
new BufferedReader( new InputStreamReader( new ByteArrayInputStream( err.toByteArray() ) ) ) );
List messages = parseModernStream( new BufferedReader( new InputStreamReader( new ByteArrayInputStream( err.toByteArray() ) ) ) ); if ( ok.intValue() != 0 && messages.isEmpty() )
if ( !ok.booleanValue() && messages.isEmpty() )
{ {
// TODO: don't throw exception // TODO: exception?
throw new Exception( "Failure executing javac, but could not parse the error:\n\n" + err.toString() ); messages.add( new CompilerError(
"Failure executing javac, but could not parse the error:\n\n" + err.toString(), true ) );
} }
return messages; return messages;
} }
protected List parseModernStream( BufferedReader input ) throws IOException protected List parseModernStream( BufferedReader input )
throws IOException
{ {
List errors = new ArrayList(); List errors = new ArrayList();
@ -143,14 +177,19 @@ public class JavacCompiler
// most errors terminate with the '^' char // most errors terminate with the '^' char
do do
{ {
if ( (line = input.readLine()) == null ) if ( ( line = input.readLine() ) == null )
{ {
return errors; return errors;
} }
// TODO: there should be a better way to parse these
if ( buffer.length() == 0 && line.startsWith( "error: " ) ) if ( buffer.length() == 0 && line.startsWith( "error: " ) )
{ {
errors.add( new CompilerError( line ) ); errors.add( new CompilerError( line, true ) );
}
else if ( buffer.length() == 0 && line.startsWith( "Note: " ) )
{
// skip this one - it is JDK 1.5 telling us that the interface is deprecated.
} }
else else
{ {
@ -200,11 +239,13 @@ public class JavacCompiler
} }
catch ( NoSuchElementException nse ) catch ( NoSuchElementException nse )
{ {
return new CompilerError( "no more tokens - could not parse error message: " + error ); // TODO: exception?
return new CompilerError( "no more tokens - could not parse error message: " + error, true );
} }
catch ( Exception nse ) catch ( Exception nse )
{ {
return new CompilerError( "could not parse error message: " + error ); // TODO: exception?
return new CompilerError( "could not parse error message: " + error, true );
} }
} }