Add option to load class files for verification.

This commit is contained in:
James Roseborough
2010-07-30 16:44:42 +00:00
parent 67a0cac3f4
commit 9f5526202a

View File

@@ -20,6 +20,7 @@
* THE SOFTWARE.
******************************************************************************/
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
@@ -47,9 +48,8 @@ public class luajc {
" -s src source directory\n" +
" -d dir destination directory\n" +
" -p pkg package prefix to apply to all classes\n" +
" -n no debug information (strip debug)\n" +
" -i<n> number format 'n', (n=0,1 or 4, default="+DumpState.NUMBER_FORMAT_DEFAULT+")\n" +
" -r recursively compile all\n" +
" -l load classes to verify generated bytecode\n" +
" -v verbose\n";
private static void usageExit() {
@@ -59,10 +59,9 @@ public class luajc {
private String srcdir = null;
private String destdir = null;
private boolean stripdebug = false;
private int numberformat = DumpState.NUMBER_FORMAT_DEFAULT;
private boolean recurse = false;
private boolean verbose = false;
private boolean loadclasses = false;
private String pkgprefix = null;
private List<InputFile> files = new ArrayList<InputFile>();
@@ -91,13 +90,8 @@ public class luajc {
usageExit();
destdir = args[i];
break;
case 'n':
stripdebug = true;
break;
case 'i':
if ( args[i].length() <= 2 )
usageExit();
numberformat = Integer.parseInt(args[i].substring(2));
case 'l':
loadclasses = true;
break;
case 'p':
if ( ++i >= args.length )
@@ -122,8 +116,6 @@ public class luajc {
System.out.println(version);
System.out.println("srcdir: "+srcdir);
System.out.println("destdir: "+srcdir);
System.out.println("stripdebug: "+stripdebug);
System.out.println("numberformat: "+numberformat);
System.out.println("files: "+seeds);
System.out.println("recurse: "+recurse);
}
@@ -193,8 +185,7 @@ public class luajc {
this.outdir = new File(outdirpath);
}
}
private void processFile( InputFile inf ) {
inf.outdir.mkdirs();
try {
@@ -203,7 +194,7 @@ public class luajc {
// create the chunk
FileInputStream fis = new FileInputStream( inf.infile );
Hashtable t = LuaJC.getInstance().compileAll(fis, inf.luachunkname, inf.srcfilename);
final Hashtable t = LuaJC.getInstance().compileAll(fis, inf.luachunkname, inf.srcfilename);
fis.close();
// write out the chunk
@@ -221,7 +212,33 @@ public class luajc {
fos.write( bytes );
fos.close();
}
// try to load the files
if ( loadclasses ) {
ClassLoader loader = new ClassLoader() {
public Class findClass(String classname) throws ClassNotFoundException {
String key = classname.replace('.', '/');
byte[] bytes = (byte[]) t.get(key);
if ( bytes != null )
return defineClass(classname, bytes, 0, bytes.length);
return super.findClass(classname);
}
};
for ( Enumeration e = t.keys(); e.hasMoreElements(); ) {
String key = (String) e.nextElement();
String classname = key.replace('/', '.');
try {
Class c = loader.loadClass(classname);
if ( verbose )
System.out.println(" loaded "+classname+" as "+c );
} catch ( Throwable th ) {
System.out.flush();
System.err.println(" failed to load "+classname+": "+th );
System.err.flush();
}
}
}
} catch ( Throwable t ) {
t.printStackTrace( System.err );
}