[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
Re: [aspectj-users] Jar Verifier Tool?
|
On 8:00:01 am 10/05/05 "Ron Bodkin" <rbodkin@xxxxxxxxxxxxxx> wrote:
> Has anyone run across a useful tool that verifies the bytecode in all
> the class files embedded in a jar (e.g., runs java -verify on each
> contained class file)? I am seeing a problem with some AspectJ
> compiled code and would like to verify the contents of a large EAR
> we've woven into.
>
>
> Thanks,
>
> Ron
>
>
>
> Ron Bodkin
>
> Chief Technology Officer
>
> New Aspects of Software
>
> w: (415) 824-4690
>
here's quick and dirty program to do it.. it grabs jars from the command
line and class path and prints whether java -verify returns normally. I'm
assuming that process will return a non-zero value if it fails... if this
assumption is wrong this won't work.
/////////////////////////////////////////////////////////
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.jar.*;
import java.lang.reflect.*;
public class Verify {
public static void main(String[] args) throws Exception {new
Verify().realMain(args);}
public void realMain(String[] args) throws Exception {
List jars = new ArrayList();
for (int i=0; i<args.length; i++) maybeAdd(args[i],jars);
String classpath = System.getProperty("java.class.path");
if (classpath != null) {
for (StringTokenizer st = new StringTokenizer(classpath,
File.pathSeparator, false);
st.hasMoreTokens();) {
maybeAdd(st.nextToken(), jars);
}
}
for (Iterator it = jars.iterator(); it.hasNext();) {
File jar = (File)it.next();
ClassLoader cl = new URLClassLoader(new URL[]{jar.toURL()});
JarFile jf = new JarFile(jar);
entryLoop: for (Enumeration en = jf.entries(); en.hasMoreElements();) {
JarEntry e = (JarEntry)en.nextElement();
String name = e.getName();
if (!name.endsWith(".class")) continue entryLoop;
String className = name.replace('/','.').substring(0, name.length()-6);
try {
Class cls = cl.loadClass(className);
Method main = cls.getMethod("main", new Class[]{String[].class});
if (main != null) {
System.out.print("Testing: " + cls.getName() + "... ");
System.out.flush();
Process p = null;
try {
p = Runtime.getRuntime().exec
(new String[]{"java", "-classpath", jar.getAbsolutePath(),
"-verify", cls.getName()});
p.waitFor();
int val = p.exitValue();
System.out.println((val == 0 ? "OK." : "failed!"));
} catch (Exception ee) {
ee.printStackTrace();
} finally {
if (p != null) p.destroy();
}
}
} catch (Exception _) {}
}
}
}
/**
* String, Collection -> Boolean
* @return true if we added a file named <code>name</code> to
<code>jars</code>
* @sideeffect add a file named <code>name</code> if that file's a jar file
*/
private boolean maybeAdd(String name, Collection jars) {
if (!name.endsWith(".jar")) return false;
File f = new File(name);
if (!f.exists()) return false;
if (f.isDirectory()) return false;
return jars.add(f);
}
}
////////////////////////////////////////////////////////////
Jeff