java.io.IOException: Error while instrumenting

300 views
Skip to first unread message

Prabitha Susil

unread,
Aug 24, 2022, 8:31:15 AM8/24/22
to JaCoCo and EclEmma Users
Hi,

I am using org.jacoco.core-0.8.6.202001130357.jar, asm-8.0.1.jar with Java 8. While trying to read a .class files, i am getting error - java.io.IOException: Error while instrumenting.

Any pointers would be of great help.

Thanks,
Prabitha

Marc Hoffmann

unread,
Aug 24, 2022, 9:18:51 AM8/24/22
to JaCoCo and EclEmma Users
Hi Prabitha,

please provide the full stack trace, this will probably show the root cause.

Regards,
-marc

--
You received this message because you are subscribed to the Google Groups "JaCoCo and EclEmma Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jacoco+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jacoco/3ea8a508-ba9d-4bd3-b03b-54af55c2ae19n%40googlegroups.com.

Prabitha Susil

unread,
Aug 25, 2022, 12:19:52 AM8/25/22
to jac...@googlegroups.com
Hi Marc,

Please find the full stack.
ava.io.IOException: Error while instrumenting oracle/apps/scm/productModel/items/ui/bean/ItemOverviewBean.
at org.jacoco.core.instr.Instrumenter.instrumentError(Instrumenter.java:159)
at org.jacoco.core.instr.Instrumenter.instrument(Instrumenter.java:109)
at org.jacoco.core.instr.Instrumenter.instrument(Instrumenter.java:134)
at ree.PerTestCoverageCollector$2.visitClassExecution(PerTestCoverageCollector.java:572)
at org.jacoco.core.data.ExecutionDataReader.readExecutionData(ExecutionDataReader.java:151)
at org.jacoco.core.data.ExecutionDataReader.readBlock(ExecutionDataReader.java:116)
at org.jacoco.core.data.ExecutionDataReader.read(ExecutionDataReader.java:93)
at ree.PerTestCoverageCollector.dump(PerTestCoverageCollector.java:692)
at ree.PerTestCoverageCollector.executeTest(PerTestCoverageCollector.java:211)
at ree.PerTestCoverageManager.main(PerTestCoverageManager.java:97)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 6
at org.jacoco.core.internal.instr.InstrSupport.getMajorVersion(InstrSupport.java:176)
at org.jacoco.core.internal.instr.InstrSupport.classReaderFor(InstrSupport.java:275)
at org.jacoco.core.instr.Instrumenter.instrument(Instrumenter.java:75)
at org.jacoco.core.instr.Instrumenter.instrument(Instrumenter.java:107)
... 8 more

The Code I was trying out is as below
public void dump(InputStream in) throws Exception {
File output_file = new File((configData.get("OUTPUT_FILE_DIR") + TESTCASE_NAME + "#"+configData.get("TEST_NUM")+(".txt")));
System.out.println("TestCase: "+TESTCASE_NAME);
System.out.println("OUTPUT_FILE_DIR: "+configData.get("OUTPUT_FILE_DIR"));
System.out.println(configData.get("OUTPUT_FILE_DIR") + TESTCASE_NAME + ".txt");
output_file.createNewFile();
final PrintStream out = new PrintStream(new FileOutputStream(output_file, false));

out.println("CLASS ID         HITS/PROBES   CLASS NAME");
System.out.println("INDATE in"+in);
//String results = new BufferedReader (new InputStreamReader(in)).lines().collect(Collectors.joining("\n"));
//System.out.println("INDATE"+results);
final ExecutionDataReader reader = new ExecutionDataReader(in);
reader.setSessionInfoVisitor(new ISessionInfoVisitor() {
public void visitSessionInfo(final SessionInfo info) {
out.printf("Session \"%s\": %s - %s%n", info.getId(), new Date(info.getStartTimeStamp()),
new Date(info.getDumpTimeStamp()));
}
});

reader.setExecutionDataVisitor(new IExecutionDataVisitor() {
public void visitClassExecution(final ExecutionData data) {
CLASS_NAME = data.getName();//"oracle/apps/scm/productModel/items/ui/bean/ItemOverviewBean";//
long CLASS_ID = data.getId();
Date todayDate = new Date();
final DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
System.out.println("Today DAte is :" + sdf.format(todayDate));
System.out.println("CLASS_NAME:"+CLASS_NAME);
System.out.println("CLASS_ID:"+CLASS_ID);
//Test method level coverage
/*************************************************************************************/

// For instrumentation and runtime we need a IRuntime instance
       // to collect execution data:
try{
       final IRuntime runtime = new LoggerRuntime();

       // The Instrumenter creates a modified version of our test target class
       // that contains additional probes for execution data recording:
       final Instrumenter instr = new Instrumenter(runtime);
       InputStream original = getTargetClass();
       final byte[] instrumented = instr.instrument(original, CLASS_NAME);
       original.close();

       // Now we're ready to run our instrumented class and need to startup the
       // runtime first:
       final RuntimeData dataRunTime = new RuntimeData();
       runtime.startup(dataRunTime);

       // In this tutorial we use a special class loader to directly load the
       // instrumented class definition from a byte[] instances.
       final MemoryClassLoader memoryClassLoader = new MemoryClassLoader();
       memoryClassLoader.addDefinition(CLASS_NAME, instrumented);
       final Class<?> targetClass = memoryClassLoader.loadClass(CLASS_NAME);

       // Here we execute our test target class through its Runnable interface:
       final Runnable targetInstance = (Runnable) targetClass.newInstance();
       targetInstance.run();

       // At the end of test execution we collect execution data and shutdown
       // the runtime:
       final ExecutionDataStore executionData = new ExecutionDataStore();
       final SessionInfoStore sessionInfos = new SessionInfoStore();
       dataRunTime.collect(executionData, sessionInfos, false);
       runtime.shutdown();

       // Together with the original class definition we can calculate coverage
       // information:
       final CoverageBuilder coverageBuilder = new CoverageBuilder();
       final Analyzer analyzer = new Analyzer(executionData, coverageBuilder);
       original = getTargetClass();
       analyzer.analyzeClass(original, CLASS_NAME);
       original.close();

       // Let's dump some metrics and line coverage information:
       for (final IClassCoverage cc : coverageBuilder.getClasses()) {
           out.printf("Coverage of class %s%n", cc.getName());

           printCounter("instructions", cc.getInstructionCounter());
           printCounter("branches", cc.getBranchCounter());
           printCounter("lines", cc.getLineCounter());
           printCounter("methods", cc.getMethodCounter());
           printCounter("complexity", cc.getComplexityCounter());

           for (int i = cc.getFirstLine(); i <= cc.getLastLine(); i++) {
               out.printf("Line %s: %s%n", Integer.valueOf(i),
                       getColor(cc.getLine(i).getStatus()));
           }
       }
}catch(Exception e){
e.printStackTrace();

}
/***************************************************************************************/

}
//}
});
reader.read();
in.close();
System.out.println();
}
/** Currently hardcoded the file details to validate **/
public InputStream getTargetClass() throws Exception{
InputStream fs = new FileInputStream(CLASSPATH);
System.out.println(fs.toString());
String results = new BufferedReader (new InputStreamReader(fs)).lines().collect(Collectors.joining("\n"));
System.out.println("INDATE"+results);
return fs;
}

Thanks,
Prabitha


Marc Hoffmann

unread,
Aug 25, 2022, 3:09:16 AM8/25/22
to JaCoCo and EclEmma Users
Hallo Prabita,

the exception shows, that the class file ist too short, basically does not even have a version header. The reason is the implementation of your getTargetClass() method:

/** Currently hardcoded the file details to validate **/
public InputStream getTargetClass() throws Exception{
InputStream fs = new FileInputStream(CLASSPATH);
System.out.println(fs.toString());
String results = new BufferedReader (new InputStreamReader(fs)).lines().collect(Collectors.joining("\n"));
System.out.println("INDATE"+results);
return fs;
}

The method consumes all content from the InputStream and returns the same stream instance. Therefore there is nothing left to read.

Regards,
-marc


On 25. Aug 2022, at 06:19, Prabitha Susil <prabit...@gmail.com> wrote:

Hi Marc,

Please find the full stack.
ava.io.IOException: Error while instrumenting oracle/apps/scm/productModel/items/ui/bean/ItemOverviewBean.
at org.jacoco.core.instr.Instrumenter.instrumentError(Instrumenterita.java:159)

Prabitha Susil

unread,
Aug 26, 2022, 7:31:32 AM8/26/22
to jac...@googlegroups.com
Thanks Marc that helped.. Is there a way to get the Method names from the class, that missed coverage?

Marc Hoffmann

unread,
Aug 28, 2022, 1:01:44 PM8/28/22
to JaCoCo and EclEmma Users
Hi Prabitha,

sure, you can use the API for this:

IClassCoverage cc = ...

for (final IMethodCoverage mc : cc.getMethods()) {
if (mc.getInstructionCounter().getMissedCount() > 0) {
System.out.println("Missed Coverage in " + mc.getName());
}
}

Regards,
-marc


Prabitha Susil

unread,
Aug 29, 2022, 8:21:14 AM8/29/22
to jac...@googlegroups.com
thanks marc..  mc.getInstructionCounter().getMissedCount()  was not giving correct data, but tried  mc.getMethodCounter().getMissedCount(), that works.
Thanks a lot for the help

Reply all
Reply to author
Forward
0 new messages