Hi,
 

> I'm wondering if this is somehow related to if a nested class gets 
> instrumented or not.


It is unrelated.

JVM throws IllegalAccessError in many various different cases of access 
that is not permitted. And one of them is when these classes defined by 
different class loaders - in the example below class "Outer" will be 
defined by "MyClassLoader", while class "Inner" defined by another class 
loader and 

java.lang.IllegalAccessError: tried to access class Outer$Nested from class 
Outer

will be thrown:

import java.io.ByteArrayOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 

class Outer { 
   class Nested { 
   } 

   public static void main(String[] args) throws Exception { 
       System.out.println(Nested.class); 
       class MyClassLoader extends ClassLoader { 
           Class<?> define(String name, byte[] bytes) { 
               return defineClass(name, bytes, 0, bytes.length); 
           } 
       } 
       MyClassLoader classLoader = new MyClassLoader(); 
       //classLoader.define("Outer$Nested", 
       // 
     read(Outer.class.getResourceAsStream("Outer$Nested.class"))); 
       Class c = classLoader.define("Outer", 
               read(Outer.class.getResourceAsStream("Outer.class"))); 
       c.getDeclaredClasses(); 
   } 

   private static byte[] read(InputStream input) throws IOException { 
       byte[] buffer = new byte[1024]; 
       ByteArrayOutputStream output = new ByteArrayOutputStream(); 
       int bytes; 
       while ((bytes = input.read(buffer)) != -1) { 
           output.write(buffer, 0, bytes); 
       } 
       output.close(); 
       input.close();
       return output.toByteArray(); 
   } 
}

Same happens in your case. Uncomment two lines to define "Inner" via 
"MyClassLoader" and "IllegalAccessError" will go away.

Class loading on itself is a complex subject, so advice - make sure that 
you understand class loading and that it works in your case prior to 
addition of instrumentation.

Now back to the question

When I instrument a class with Jacoco, do nested classes also get 
> instrumented as well?


As stated in Javadoc of method that you use - 
http://www.jacoco.org/jacoco/trunk/doc/api/org/jacoco/core/instr/Instrumenter.html#instrument(java.io.InputStream,
 
java.lang.String) :

Creates a instrumented version of the *given class* if possible.

If necessary to rephrase, then: only bytes of *one and only one class* are 
instrumented - the one that is passed as argument to this method.


Regards,
Evgeny

On Friday, March 30, 2018 at 6:50:44 PM UTC+2, [email protected] wrote:
>
> My above stack trace is not quite right. I had made modifications to 
> attempt to catch and ignore the errors which resulted in the above trace.
>
> Throwable thrown while handling command: java.lang.IllegalAccessError: 
> tried to access class 
> org.apache.commons.collections4.multiset.SynchronizedMultiSet$SynchronizedSet 
> from class org.apache.commons.collections4.multiset.SynchronizedMultiSet
> java.lang.IllegalAccessError: tried to access class 
> org.apache.commons.collections4.multiset.SynchronizedMultiSet$SynchronizedSet 
> from class org.apache.commons.collections4.multiset.SynchronizedMultiSet
>         at java.lang.Class.getDeclaredClasses0(Native Method)
>         at java.lang.Class.getDeclaredClasses(Class.java:1867)
>         at 
> randoop.reflection.ClassUtil.getDeclaredClasses(ClassUtil.java:60)
>         at 
> randoop.reflection.ReflectionManager.apply(ReflectionManager.java:156)
>         at 
> randoop.reflection.ReflectionManager.apply(ReflectionManager.java:83)
>         at 
> randoop.reflection.OperationModel.addClassTypes(OperationModel.java:513)
>         at 
> randoop.reflection.OperationModel.createModel(OperationModel.java:153)
>         at randoop.main.GenTests.handle(GenTests.java:275)
>         at randoop.main.Main.nonStaticMain(Main.java:65)
>         at randoop.main.Main.main(Main.java:29)
>
> This trace shows the behavior that I described in the above post. (Without 
> any additional error handling that I haven't described). The outer class 
> accessing the inner class causes the IllegalAccessError.
>
>

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jacoco/5f023a96-23a0-4dbd-ae07-083907dfbc58%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to