-
Bug
-
Resolution: Fixed
-
P3
-
jcov_1.1
-
1.1
-
generic
-
generic
-
Verified
Name: kbR10066 Date: 12/20/2002
Suppose we have an app which loads one of its classes via its own class loader.
Then if we statically instrument the app and run the instrumented image, a situation
can happen when no coverage data is generated for the class loaded via the custom
class loader.
To reproduce, compile the sources a.java and b.java below with -Xjcov:
% javac -Xjcov a.java b.java
Instrument the classes:
% java -cp jcov.jar com.sun.tdk.jcov.InstrMain -overwrite -saveatend=a.main *class
Run the app:
% java -cp .:jcov.jar a
LOADING b
ClassLoader1@8d107f
b@cf2c80
LOADING b
ClassLoader2@a8c4e7
b@729854
Resulting java.jcov does not contain data for class "b". JDK version used was 1.4.0.
--- a.java ---
import java.io.*;
import java.security.*;
public class a {
public static void main(String[] args) {
try {
Class c1 = new ClassLoader1().loadClass("b");
Class c2 = new ClassLoader2().loadClass("b");
System.out.println(c1.newInstance());
System.out.println(c2.newInstance());
} catch (Throwable e) {
e.printStackTrace();
}
}
}
class CL extends ClassLoader {
final static int READ_CHUNK_SIZE = 1024;
public static byte[] read(String name) {
name = name.replace('.', '/') + ".class";
byte[] arr = null;
try {
InputStream is = ClassLoader.getSystemResourceAsStream(name);
int len = is.available();
int n = 0;
int res = 0;
arr = new byte[len];
do {
res += n;
int i = len - res;
if (i > READ_CHUNK_SIZE) {
i = READ_CHUNK_SIZE;
}
n = is.read(arr, res, i);
} while (n > 0);
is.close();
} catch (Throwable e) {
e.printStackTrace();
return null;
}
return arr;
}
}
class ClassLoader1 extends CL {
protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
Class res = null;
try {
byte[] arr = read(name);
res = defineClass(name, arr, 0, arr.length);
} catch (Throwable e) {
res = findSystemClass(name);
}
if (resolve) {
resolveClass(res);
}
return res;
}
}
class ClassLoader2 extends CL {
protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
Class res = null;
try {
byte[] arr = read(name);
res = defineClass(name, arr, 0, arr.length);
} catch (Throwable e) {
res = findSystemClass(name);
}
if (resolve) {
resolveClass(res);
}
return res;
}
}
--- end of a.java ---
--- b.java ---
public class b {
static {
System.out.println("LOADING b");
}
public b () {
System.out.println(getClass().getClassLoader());
}
}
--- end of b.java ---
======================================================================