evenisse 2005/04/12 14:13:24
Modified: maven-mboot2/src/main/java MBoot.java maven-mboot2/src/main/java/compile AbstractCompiler.java CompilerConfiguration.java CompilerError.java JavacCompiler.java Log: Update MBoot compiler relative to changes in plexus-compiler. Revision Changes Path 1.95 +1 -0 maven-components/maven-mboot2/src/main/java/MBoot.java Index: MBoot.java =================================================================== RCS file: /home/cvs/maven-components/maven-mboot2/src/main/java/MBoot.java,v retrieving revision 1.94 retrieving revision 1.95 diff -u -r1.94 -r1.95 --- MBoot.java 5 Apr 2005 04:11:02 -0000 1.94 +++ MBoot.java 12 Apr 2005 21:13:24 -0000 1.95 @@ -885,6 +885,7 @@ 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 ) ); 1.3 +68 -40 maven-components/maven-mboot2/src/main/java/compile/AbstractCompiler.java Index: AbstractCompiler.java =================================================================== RCS file: /home/cvs/maven-components/maven-mboot2/src/main/java/compile/AbstractCompiler.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- AbstractCompiler.java 9 Jan 2005 22:38:43 -0000 1.2 +++ AbstractCompiler.java 12 Apr 2005 21:13:24 -0000 1.3 @@ -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:[EMAIL PROTECTED]">Jason van Zyl</a> - * @author <a href="mailto:[EMAIL PROTECTED]">Michal Maczka</a> - * + * @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl </a> + * @author <a href="mailto:[EMAIL PROTECTED]">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 @@ 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() ) + { + for ( Iterator it = sourceFiles.iterator(); it.hasNext(); ) + { + File sourceFile = (File) it.next(); + sources.add( sourceFile.getAbsolutePath() ); + } + } + else { - String sourceLocation = (String) it.next(); - - DirectoryScanner scanner = new DirectoryScanner(); + for ( Iterator it = config.getSourceLocations().iterator(); it.hasNext(); ) + { + String sourceLocation = (String) it.next(); - scanner.setBasedir( sourceLocation ); + DirectoryScanner scanner = new DirectoryScanner(); - 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"}); - } + scanner.setBasedir( sourceLocation ); - Set excludes = config.getIncludes(); - if(excludes != null && !excludes.isEmpty()) { - String[] exclStrs = (String[])excludes.toArray(new String[excludes.size()]); - scanner.setIncludes( exclStrs ); - } + 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" } ); + } - scanner.scan(); + Set excludes = config.getExcludes(); + if ( excludes != null && !excludes.isEmpty() ) + { + String[] exclStrs = (String[]) excludes.toArray( new String[excludes.size()] ); + scanner.setIncludes( exclStrs ); + } - String[] sourceDirectorySources = scanner.getIncludedFiles(); + scanner.scan(); - for ( int j = 0; j < sourceDirectorySources.length; j++ ) - { - File f = new File( sourceLocation, sourceDirectorySources[j] ); + String[] sourceDirectorySources = scanner.getIncludedFiles(); + + for ( int j = 0; j < sourceDirectorySources.length; j++ ) + { + File f = new File( sourceLocation, sourceDirectorySources[j] ); - sources.add( f.getPath() ); + sources.add( f.getPath() ); + } } } - String[] sourceArray = new String[sources.size()]; + String[] result = null; + + if ( sources.isEmpty() ) + { + result = new String[0]; + } + else + { + result = (String[]) sources.toArray( new String[sources.size()] ); + } - return (String[]) sources.toArray( sourceArray ); + return result; } protected String makeClassName( String fileName, String sourceDir ) @@ -97,8 +126,7 @@ if ( sourceDir != null ) { - String prefix = - new File( sourceDir ).getCanonicalPath().replace( '\\', '/' ); + String prefix = new File( sourceDir ).getCanonicalPath().replace( '\\', '/' ); if ( canonical != null ) { @@ -147,4 +175,4 @@ return args; } -} +} \ No newline at end of file 1.2 +29 -0 maven-components/maven-mboot2/src/main/java/compile/CompilerConfiguration.java Index: CompilerConfiguration.java =================================================================== RCS file: /home/cvs/maven-components/maven-mboot2/src/main/java/compile/CompilerConfiguration.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- CompilerConfiguration.java 9 Jan 2005 22:38:43 -0000 1.1 +++ CompilerConfiguration.java 12 Apr 2005 21:13:24 -0000 1.2 @@ -17,13 +17,32 @@ { 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 @@ { return debug; } + + public void setNoWarn( boolean noWarn ) + { + this.noWarn = noWarn; + } + + public boolean isNoWarn() + { + return noWarn; + } } 1.3 +21 -2 maven-components/maven-mboot2/src/main/java/compile/CompilerError.java Index: CompilerError.java =================================================================== RCS file: /home/cvs/maven-components/maven-mboot2/src/main/java/compile/CompilerError.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- CompilerError.java 9 Jan 2005 22:38:43 -0000 1.2 +++ CompilerError.java 12 Apr 2005 21:13:24 -0000 1.3 @@ -98,6 +98,18 @@ } /** + * 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 String toString() { - return file + ":" + "[" + startline + "," + startcolumn + "] " + message; + if ( file != null ) + { + return file + ":" + "[" + startline + "," + startcolumn + "] " + message; + } + else + { + return message; + } } } 1.7 +68 -27 maven-components/maven-mboot2/src/main/java/compile/JavacCompiler.java Index: JavacCompiler.java =================================================================== RCS file: /home/cvs/maven-components/maven-mboot2/src/main/java/compile/JavacCompiler.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- JavacCompiler.java 21 Mar 2005 06:06:42 -0000 1.6 +++ JavacCompiler.java 12 Apr 2005 21:13:24 -0000 1.7 @@ -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 List compile( CompilerConfiguration config ) throws Exception + public List compile( CompilerConfiguration config ) + throws Exception { File destinationDir = new File( config.getOutputLocation() ); @@ -61,8 +60,9 @@ 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 @@ 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 @@ File toolsJar = new File( System.getProperty( "java.home" ), "../lib/tools.jar" ); - cl.addURL( toolsJar.toURL() ); - - Class c = cl.loadClass( "sun.tools.javac.Main" ); + if ( toolsJar.exists() ) + { + cl.addURL( toolsJar.toURL() ); + } - 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} ); - Boolean ok = (Boolean) compile.invoke( compiler, new Object[] { args.toArray( new String[0] ) } ); + Integer ok = (Integer) compile.invoke( null, 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 @@ // 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 @@ } 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 @@ { return "Sun Javac Compiler"; } -} \ No newline at end of file +}