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 @@ private void compile( Collection dependencies, String sourceDirectory, String ou
CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
compilerConfiguration.setOutputLocation( outputDirectory );
List classpathEntries = classpath( dependencies, extraClasspath, scope, localRepository );
compilerConfiguration.setNoWarn( true );
compilerConfiguration.setClasspathEntries( classpathEntries );
compilerConfiguration.setSourceLocations( Arrays.asList( sourceDirectories ) );

View File

@ -4,33 +4,38 @@
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
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$
*/
public abstract class AbstractCompiler
implements Compiler
{
private static String PS = System.getProperty( "path.separator" );
/**
* @deprecated Use getPathString(..) instead.
*/
public String getClasspathString( List pathElements ) throws Exception
{
return getPathString( pathElements );
}
public String getClasspathString( List classpathElements )
public String getPathString( List pathElements )
throws Exception
{
StringBuffer sb = new StringBuffer();
for ( Iterator it = classpathElements.iterator(); it.hasNext(); )
for ( Iterator it = pathElements.iterator(); it.hasNext(); )
{
String element = (String) it.next();
sb.append( element ).append( PS );
}
@ -39,46 +44,70 @@ public String getClasspathString( List classpathElements )
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();
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++ )
for ( Iterator it = sourceFiles.iterator(); it.hasNext(); )
{
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 )
@ -97,8 +126,7 @@ protected String makeClassName( String fileName, String sourceDir )
if ( sourceDir != null )
{
String prefix =
new File( sourceDir ).getCanonicalPath().replace( '\\', '/' );
String prefix = new File( sourceDir ).getCanonicalPath().replace( '\\', '/' );
if ( canonical != null )
{
@ -147,4 +175,4 @@ protected String[] toStringArray( List arguments )
return args;
}
}
}

View File

@ -17,13 +17,32 @@ public class CompilerConfiguration
{
private String outputLocation;
private List classpathEntries = new LinkedList();
private List sourceLocations = new LinkedList();
private Set includes = new HashSet();
private Set excludes = new HashSet();
private Map compilerOptions = new TreeMap();
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)
{
this.outputLocation = outputLocation;
@ -112,5 +131,15 @@ public boolean isDebug()
{
return debug;
}
public void setNoWarn( boolean noWarn )
{
this.noWarn = noWarn;
}
public boolean isNoWarn()
{
return noWarn;
}
}

View File

@ -97,6 +97,18 @@ public CompilerError( String 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.
*
@ -173,6 +185,13 @@ public String getMessage()
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.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
@ -45,7 +43,8 @@ public JavacCompiler()
{
}
public List compile( CompilerConfiguration config ) throws Exception
public List compile( CompilerConfiguration config )
throws Exception
{
File destinationDir = new File( config.getOutputLocation() );
@ -61,8 +60,9 @@ public List compile( CompilerConfiguration config ) throws Exception
return Collections.EMPTY_LIST;
}
System.out.println( "Compiling " + sources.length + " source file" + ( sources.length == 1 ? "" : "s" )
+ " to " + destinationDir.getAbsolutePath() );
// TODO: use getLogger() - but for some reason it is null when this is used
System.out.println( "Compiling " + sources.length + " source file" + ( sources.length == 1 ? "" : "s" ) +
" to " + destinationDir.getAbsolutePath() );
Map compilerOptions = config.getCompilerOptions();
@ -72,25 +72,57 @@ public List compile( CompilerConfiguration config ) throws Exception
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() )
{
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();
while ( it.hasNext() )
{
Map.Entry entry = (Map.Entry) it.next();
args.add( entry.getKey() );
if ( (entry.getValue() != null) )
if ( ( entry.getValue() != null ) )
{
args.add( entry.getValue() );
}
}
for ( int i = 0; i < sources.length; i++ )
@ -102,32 +134,34 @@ public List compile( CompilerConfiguration config ) throws Exception
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" );
Constructor cons = c.getConstructor( new Class[] { OutputStream.class, String.class } );
Class c = cl.loadClass( "com.sun.tools.javac.Main" );
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.booleanValue() && messages.isEmpty() )
if ( ok.intValue() != 0 && messages.isEmpty() )
{
// TODO: don't throw exception
throw new Exception( "Failure executing javac, but could not parse the error:\n\n" + err.toString() );
// TODO: exception?
messages.add( new CompilerError(
"Failure executing javac, but could not parse the error:\n\n" + err.toString(), true ) );
}
return messages;
}
protected List parseModernStream( BufferedReader input ) throws IOException
protected List parseModernStream( BufferedReader input )
throws IOException
{
List errors = new ArrayList();
@ -143,14 +177,19 @@ protected List parseModernStream( BufferedReader input ) throws IOException
// most errors terminate with the '^' char
do
{
if ( (line = input.readLine()) == null )
if ( ( line = input.readLine() ) == null )
{
return errors;
}
// TODO: there should be a better way to parse these
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
{
@ -200,11 +239,13 @@ private CompilerError parseModernError( String error )
}
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 )
{
return new CompilerError( "could not parse error message: " + error );
// TODO: exception?
return new CompilerError( "could not parse error message: " + error, true );
}
}
@ -212,4 +253,4 @@ public String toString()
{
return "Sun Javac Compiler";
}
}
}