You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by te...@apache.org on 2007/01/03 13:17:15 UTC
svn commit: r492113 - in
/harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/javac:
Compiler.java Main.java
Author: tellison
Date: Wed Jan 3 04:17:14 2007
New Revision: 492113
URL: http://svn.apache.org/viewvc?view=rev&rev=492113
Log:
Add search for ECJ on java.home relative paths.
Allow compiler to return success code.
Modified:
harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/javac/Compiler.java
harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/javac/Main.java
Modified: harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/javac/Compiler.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/javac/Compiler.java?view=diff&rev=492113&r1=492112&r2=492113
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/javac/Compiler.java (original)
+++ harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/javac/Compiler.java Wed Jan 3 04:17:14 2007
@@ -34,26 +34,33 @@
class Compiler {
/* A default encoding for console messages */
- static String CONSOLE_ENCODING = System.getProperty("console.encoding",
- "ISO8859_1");
+ static String CONSOLE_ENCODING = System.getProperty("console.encoding", //$NON-NLS-1$
+ "ISO8859_1"); //$NON-NLS-1$
/* FIXME: Hard-coded for now, the name of the ECJ JAR file */
- static final String ECJ_JAR_FILE = "ecj_3.2.jar";
+ static final String ECJ_JAR_FILE = "ecj_3.2.jar"; //$NON-NLS-1$
+
+ static final String TOOLS_JAR_FILE = "tools.jar"; //$NON-NLS-1$
/* The name of the ECJ compiler class */
- static final String MAIN_CLASS_NAME = "org.eclipse.jdt.internal.compiler.batch.Main";
+ static final String MAIN_CLASS_NAME = "org.eclipse.jdt.internal.compiler.batch.Main"; //$NON-NLS-1$
/*
* Invokes the compiler with the given command-line arguments. The supported
* arguments can be determined form the usage message.
+ *
+ * Answers the result of the compilation from ECJ; i.e. true if the compile
+ * succeeded, and false otherwise.
*/
- public static void main(String[] args) {
+ public static boolean main(String[] args) {
Compiler myself = new Compiler();
myself.initialize();
// If there is a problem invoking the method, simply dump the trace for
// now
try {
- myself.staticMainMth.invoke(myself.mainInst, new Object[] { args });
+ Object result = myself.staticMainMth.invoke(myself.mainInst,
+ new Object[] { args });
+ return (Boolean) result;
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
@@ -61,6 +68,7 @@
} catch (InvocationTargetException e) {
e.printStackTrace();
}
+ return false;
}
// Reference to ECJ 'Main' compiler class.
@@ -135,36 +143,77 @@
IllegalArgumentException, InstantiationException,
IllegalAccessException, InvocationTargetException {
- URLClassLoader loader;
+ // Find the ECJ JAR file, prefer those found near loaders
+ URL ecjURL = searchLoaders();
+ if (ecjURL == null) {
+ ecjURL = searchPaths();
+ }
+ if (ecjURL == null) {
+ throw new RuntimeException("Cannot find file " + ECJ_JAR_FILE);
+ }
+
+ // Load the ECJ main class
+ URLClassLoader loader = new URLClassLoader(new URL[] { ecjURL });
+ ecjCompilerClass = loader.loadClass(MAIN_CLASS_NAME);
+ }
+
+ /*
+ * Looks for the ECJ JAR file in the current working directory, and in the
+ * jdk/lib of the current runtime. Answers with a URL of the JAR file if
+ * found, or null if not found.
+ */
+ private URL searchPaths() throws MalformedURLException {
+ // Search in current working directory
+ File cwdFile = new File(ECJ_JAR_FILE);
+ if (cwdFile.exists()) {
+ return cwdFile.toURL();
+ }
+
+ // Look for it via the java.home
+ File javaHomeFile = new File(System.getProperty("java.home")); //$NON-NLS-1$
+ String pathFromJDK = "lib" + File.separator + ECJ_JAR_FILE; //$NON-NLS-1$
+
+ // Is java.home pointing at a JDK?
+ File jdkBasedFile = new File(javaHomeFile, pathFromJDK);
+ if (jdkBasedFile.exists()) {
+ return jdkBasedFile.toURL();
+ }
+ // Maybe it is pointing at a JRE.
+ File jdkHomeFile = javaHomeFile.getParentFile();
+ if (jdkHomeFile == null) {
+ return null;
+ }
+ File jreBasedFile = new File(jdkHomeFile, pathFromJDK);
+ if (jreBasedFile.exists()) {
+ return jreBasedFile.toURL();
+ }
- // Find the ECJ JAR file
- if (new File(ECJ_JAR_FILE).exists()) {
- // It is in the working directory
- URL ecjURL = new URL("file:" + ECJ_JAR_FILE);
- loader = new URLClassLoader(new URL[] { ecjURL });
- } else {
- // Assume it is next to the tools.jar
- URLClassLoader bogusLoader = new URLClassLoader(new URL[] {});
- URLClassLoader parentLoader = (URLClassLoader) bogusLoader
- .getParent();
- URL[] uls = parentLoader.getURLs();
- URL ecjURL = null;
+ // We didn't find it
+ return null;
+ }
+
+ /*
+ * Find the ECJ jar by searching for the tools.jar location and figuring
+ * that it is alongside that.
+ */
+ private URL searchLoaders() throws MalformedURLException {
+ URLClassLoader bogusLoader = new URLClassLoader(new URL[] {});
+ ClassLoader parentLoader = bogusLoader.getParent();
+ while (parentLoader instanceof URLClassLoader) {
+ URLClassLoader parentURLLoader = (URLClassLoader) parentLoader;
+ URL[] uls = parentURLLoader.getURLs();
for (int i = 0; i < uls.length; i++) {
URL l = uls[i];
String filename = new File(l.getFile()).getName();
- if (filename.equals("tools.jar")) {
- ecjURL = new URL(l, ECJ_JAR_FILE);
- break;
+ if (filename.equals(TOOLS_JAR_FILE)) {
+ return new URL(l, ECJ_JAR_FILE);
}
}
- if (ecjURL == null) {
- throw new RuntimeException("Cannot find file " + ECJ_JAR_FILE);
- }
- loader = new URLClassLoader(new URL[] { ecjURL });
+ // Not found here, move up a level
+ parentLoader = parentLoader.getParent();
}
-
- // Load the ECJ main class
- ecjCompilerClass = loader.loadClass(MAIN_CLASS_NAME);
+ // We didn't find it
+ return null;
}
/*
@@ -173,10 +222,10 @@
*/
protected void initializeMethods() throws SecurityException,
NoSuchMethodException {
- staticMainMth = ecjCompilerClass.getMethod("main",
+ staticMainMth = ecjCompilerClass.getMethod("main", //$NON-NLS-1$
new Class[] { String[].class });
printUsageMth = ecjCompilerClass
- .getMethod("printUsage", (Class[]) null);
+ .getMethod("printUsage", (Class[]) null); //$NON-NLS-1$
}
/**
Modified: harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/javac/Main.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/javac/Main.java?view=diff&rev=492113&r1=492112&r2=492113
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/javac/Main.java (original)
+++ harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/javac/Main.java Wed Jan 3 04:17:14 2007
@@ -17,44 +17,57 @@
package org.apache.harmony.tools.javac;
-
/**
* This is the entry point for the javac tool.
*/
public final class Main {
+ /*
+ * Command-line tool invokes this method.
+ */
public static void main(String[] args) {
+ new Main().compile(args);
+ }
+
+ /**
+ * Default constructor.
+ */
+ public Main() {
+ super();
+ }
+
+ /**
+ * Invokes the ECJ compiler with the given arguments.
+ *
+ * @param args
+ * the arguments passed through to the compiler
+ * @return true on compilation success, false on failure
+ */
+ public boolean compile(String[] args) {
/* Give me something to do */
- if (args.length == 0) {
+ if (args == null || args.length == 0) {
new Compiler().printUsage();
- return;
+ return false;
}
/* Add in the base class library code to compile against */
String[] newArgs = addBootclasspath(args);
/* Invoke the compiler */
- Compiler.main(newArgs);
- }
-
- /**
- * Default constructor.
- */
- public Main() {
- super();
+ return Compiler.main(newArgs);
}
/*
* Set up the compiler option to compile against the running JRE class
* libraries.
*/
- public static String[] addBootclasspath(String[] args) {
+ private String[] addBootclasspath(String[] args) {
String[] result = new String[args.length + 2];
System.arraycopy(args, 0, result, 0, args.length);
- result[args.length] = "-classpath";
+ result[args.length] = "-classpath"; //$NON-NLS-1$
result[args.length + 1] = System.getProperty(
- "org.apache.harmony.boot.class.path", ".");
+ "org.apache.harmony.boot.class.path", "."); //$NON-NLS-1$ //$NON-NLS-2$
return result;
}
}