From 07dbc9cafa601a6b37da61fd7f53842bfee6020a Mon Sep 17 00:00:00 2001 From: Sam Ruby Date: Tue, 23 May 2000 12:08:17 +0000 Subject: [PATCH] Support JDK 1.3 (a.k.a. "modern") compiler. Submitted by: Robin Green git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@267667 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/tools/ant/taskdefs/Javac.java | 92 ++++++++++++++++++- 1 file changed, 88 insertions(+), 4 deletions(-) diff --git a/src/main/org/apache/tools/ant/taskdefs/Javac.java b/src/main/org/apache/tools/ant/taskdefs/Javac.java index 72abe3b2d..5d41801ff 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Javac.java +++ b/src/main/org/apache/tools/ant/taskdefs/Javac.java @@ -56,6 +56,7 @@ package org.apache.tools.ant.taskdefs; import org.apache.tools.ant.*; +import java.lang.reflect.Method; import java.io.*; import java.util.*; @@ -82,10 +83,17 @@ import java.util.*; * located properly in the classpath. * * @author James Davidson duncan@x180.com + * @author Robin Green greenrd@hotmail.com */ public class Javac extends MatchingTask { + /** + * Integer returned by the "Modern" jdk1.3 compiler to indicate success. + */ + private static final int + MODERN_COMPILER_SUCCESS = 0; + private File srcDir; private File destDir; private String compileClasspath; @@ -431,10 +439,86 @@ public class Javac extends MatchingTask { */ private void doModernCompile() throws BuildException { - project.log("Performing a Modern Compile"); - project.log("WARNING: Modern Compiler usage not implemented!"); - project.log("Switching modes to Classic"); - doClassicCompile(); + project.log("Using modern compiler", project.MSG_VERBOSE); + String classpath = getCompileClasspath(); + Vector argList = new Vector(); + + if (deprecation == true) + argList.addElement("-deprecation"); + + argList.addElement("-d"); + argList.addElement(destDir.getAbsolutePath()); + argList.addElement("-classpath"); + argList.addElement(classpath); + argList.addElement("-sourcepath"); + argList.addElement(srcDir.getAbsolutePath()); + if (target != null) { + argList.addElement("-target"); + argList.addElement(target); + } + if (debug) { + argList.addElement("-g"); + } + if (optimize) { + argList.addElement("-O"); + } + if (bootclasspath != null) { + argList.addElement("-bootclasspath"); + argList.addElement(bootclasspath); + } + if (extdirs != null) { + argList.addElement("-extdirs"); + argList.addElement(extdirs); + } + + project.log("Compilation args: " + argList.toString(), + project.MSG_VERBOSE); + + String[] args = new String[argList.size() + compileList.size()]; + int counter = 0; + + for (int i = 0; i < argList.size(); i++) { + args[i] = (String)argList.elementAt(i); + counter++; + } + + // XXX + // should be using system independent line feed! + + StringBuffer niceSourceList = new StringBuffer("Files to be compiled:" + + "\r\n"); + + Enumeration enum = compileList.elements(); + while (enum.hasMoreElements()) { + args[counter] = (String)enum.nextElement(); + niceSourceList.append(" " + args[counter] + "\r\n"); + counter++; + } + + project.log(niceSourceList.toString(), project.MSG_VERBOSE); + + // This won't build under JDK1.2.2 because the new compiler + // doesn't exist there. + //com.sun.tools.javac.Main compiler = new com.sun.tools.javac.Main(); + //if (compiler.compile(args) != 0) { + + // Use reflection to be able to build on all JDKs >= 1.1: + try { + Class c = Class.forName ("com.sun.tools.javac.Main"); + Object compiler = c.newInstance (); + Method compile = c.getMethod ("compile", + new Class [] {(new String [] {}).getClass ()}); + int result = ((Integer) compile.invoke + (compiler, new Object [] {args})) .intValue (); + if (result != MODERN_COMPILER_SUCCESS) { + String msg = + "Compile failed, messages should have been provided."; + throw new BuildException(msg); + } + } catch (Exception ex) { + throw new BuildException (ex); + } + } /**