[tcljava-dev] Patch: java::import -classpath -resolve
Brought to you by:
mdejong
From: Tom P. <tpo...@ny...> - 2003-04-09 04:08:44
|
Here's a patch to java::import that adds -classpath and -resolve options. The -classpath option specifies a specific classpath and/or jar files from which to load the classes being imported. The -resolve passes the 'resolve' flag to the class loader (see docs for java.lang.ClassLoader.loadClass() ). Updated docs included. I'm using these options in my Hyde package that I hope to finally get released RSN. -- Tom Poindexter tpo...@ny... http://www.nyx.net/~tpoindex/ diff -u -r tcljava.cvs/docs/TclJava/ClassLoading.html tcljava.tp/docs/TclJava/ClassLoading.html --- tcljava.cvs/docs/TclJava/ClassLoading.html 2002-10-08 22:38:44.000000000 -0600 +++ tcljava.tp/docs/TclJava/ClassLoading.html 2003-04-08 21:21:04.000000000 -0600 @@ -44,7 +44,8 @@ files specified by <I>pathList</I>. (Available only for the <B>java::load</B> command.) <P><DT>[4]<DD> Search <I>pathList</I> again, inspecting all jar files found in -each directory. (Available only for the <B>java::load</B> command.) +each directory. (Available for the <B>java::load</B> and +<B>java::import</B>commands.) <P><DT>[5]<DD> Search the <B>env(TCL_CLASSPATH)</B> list, looking only in directories or jar files specified by <B>env(TCL_CLASSPATH)</B>. @@ -103,4 +104,4 @@ </BODY> -</HTML> \ No newline at end of file +</HTML> diff -u -r tcljava.cvs/docs/TclJava/JavaImportCmd.html tcljava.tp/docs/TclJava/JavaImportCmd.html --- tcljava.cvs/docs/TclJava/JavaImportCmd.html 2003-03-12 20:15:00.000000000 -0700 +++ tcljava.tp/docs/TclJava/JavaImportCmd.html 2003-04-08 21:23:05.000000000 -0600 @@ -18,7 +18,7 @@ Usage: </H3> -<DD><B>java::import</B> ?<B>-forget</B>? ?<B>-package</B> <I>pkg</I>? ?<I>class ...</I>? +<DD><B>java::import</B> ?<B>-forget</B>? ?<B>-resolve</B>? ?<B>-classpath</B> <I>pathList</I>? ?<B>-package</B> <I>pkg</I>? ?<I>class ...</I>? <P> @@ -36,6 +36,13 @@ specifier, meaning the '.' character must not appear in a <I>class</I> name. <P> +The <B>-classpath</B> <I>pathList</I> argument specifies a Tcl list of +directories and/or Jar files to search during class loading. If +<B>-classpath</B> <I>pathList</I> is omitted, the class loader will +search for the class on using <B>CLASSPATH</B> and +<B>env(TCL_CLASSPATH)</B>. Refer to +<A HREF="ClassLoading.html">Class Loading</A> for additional information. +<P> If the <B>java::import</B> command is invoked with no arguments it will return a list of all the currently imported classes. If the <B>java::import</B> @@ -51,6 +58,11 @@ <P> +The <B>-resolve</B> argument will cause the class loader to perform +resolution and linking steps for the classes being imported. + +<P> + </DL> <DL> diff -u -r tcljava.cvs/src/tcljava/tcl/lang/JavaImportCmd.java tcljava.tp/src/tcljava/tcl/lang/JavaImportCmd.java --- tcljava.cvs/src/tcljava/tcl/lang/JavaImportCmd.java 2003-03-12 20:15:27.000000000 -0700 +++ tcljava.tp/src/tcljava/tcl/lang/JavaImportCmd.java 2003-04-08 21:16:28.000000000 -0600 @@ -22,9 +22,20 @@ import java.util.*; - public class JavaImportCmd implements Command { +static final private String validOpts[] = { + "-resolve", + "-forget", + "-package", + "-classpath" +}; + +static final private int RESOLVE = 0; +static final private int FORGET = 1; +static final private int PACKAGE = 2; +static final private int CLASSPATH = 3; + /* *---------------------------------------------------------------------- * @@ -50,7 +61,7 @@ throws TclException // A standard Tcl exception. { - final String usage = "java::import ?-forget? ?-package pkg? ?class ...?"; + final String usage = "java::import ?-resolve? ?-forget? ?-package pkg? ?-classpath pathList? ?class ...?"; Hashtable classTable = interp.importTable[0]; Hashtable packageTable = interp.importTable[1]; @@ -61,6 +72,9 @@ Enumeration search, search2; String elem, elem2; int startIdx, i; + boolean resolve = false; + TclObject classPath = null; + int opt; // If there are no args simply return all the imported classes if (objv.length == 1) { @@ -75,15 +89,61 @@ interp.setResult(import_list); return; } - - // See if there is a -forget argument + + // process args startIdx = 1; elem = objv[startIdx].toString(); - if (elem.equals("-forget")) { - forget = true; + while (elem.charAt(0) == '-' && startIdx < objv.length) { + try { + opt = TclIndex.get(interp, objv[startIdx], validOpts, "option", 0); + } catch (TclException e) { + throw new TclException(interp, "bad option \"" + objv[startIdx] + + "\": " + usage); + } + switch (opt) { + case RESOLVE: + resolve = true; + break; + case FORGET: + forget = true; + break; + case PACKAGE: + if (startIdx + 1 < objv.length) { + pkg = objv[startIdx + 1].toString(); + if (pkg.length() == 0) { + throw new TclException(interp, + "-package \"pkg\" not specified: " + usage); + } + startIdx++; + } else { + throw new TclException(interp, + "-package \"pkg\" not specified: " + usage); + } + break; + case CLASSPATH: + if (startIdx + 1 < objv.length) { + classPath = objv[startIdx + 1]; + startIdx++; + } else { + throw new TclException(interp, + " -classpath \"arg\" not specified: " + usage); + } + break; + } + startIdx++; + if (startIdx < objv.length) { + elem = objv[startIdx].toString(); + } } + // check for mutally exclusive -resolve -forget + if (forget && resolve) { + throw new TclException(interp, + "-forget and -resolve are mutually exclusive: " + usage); + } + + // When -forget is given with no arguments, we simply // return. This is needed to support the following usage. // @@ -97,36 +157,13 @@ } - // Figure out if the "-package pkg" arguments are given - elem = objv[startIdx].toString(); - if (elem.equals("-package")) { - startIdx++; - - // "java::import -package" is not a valid usage - // "java::import -forget -package" is not a valid usage - if (startIdx >= objv.length) { - throw new TclException(interp, usage); - } - - pkg = objv[startIdx].toString(); - if (pkg.length() == 0) { - throw new TclException(interp, usage); - } - startIdx++; - } - - - // No additional arguments means we have hit one of - // two conditions. - // - // "java::import -forget -package pkg" - // "java::import -package pkg" + // No additional options, process class arguments if (startIdx >= objv.length) { if (forget) { // We must have "java::import -forget -package pkg" - // Ceck that it is not "java::import -forget" which is invalid! + // Check that it is not "java::import -forget" which is invalid! if (pkg == null) { throw new TclException(interp, usage); } @@ -225,7 +262,7 @@ operation = "forget"; } - TclClassLoader tclClassLoader = new TclClassLoader(interp, null); + TclClassLoader tclClassLoader = new TclClassLoader(interp, classPath); // Start processing class arguments begining at startIdx. @@ -275,6 +312,7 @@ fullyqualified = pkg + "." + elem; } + // split the fullyqualified name into a package and class // by looking for the last '.' character in the string. // If there is no '.' in the string the class is in the @@ -307,7 +345,7 @@ boolean inGlobal = true; try { - tclClassLoader.loadClass(class_name); + tclClassLoader.loadClass(class_name, resolve); } catch (ClassNotFoundException e) { inGlobal = false; tclClassLoader.removeCache(class_name); @@ -328,7 +366,7 @@ "\", it does not exist"); try { - Class c = tclClassLoader.loadClass(fullyqualified); + Class c = tclClassLoader.loadClass(fullyqualified, resolve); if (!PkgInvoker.isAccessible(c)) { throw new TclException(interp, "Class \"" + c.getName() + "\" is not accessible"); |