how to skip instrument a certain class with <excludes>?

5,484 views
Skip to first unread message

zw1506...@163.com

unread,
Jan 2, 2017, 9:34:02 PM1/2/17
to JaCoCo and EclEmma Users
hi,

I want to skip instrument a class named BlockBuilderStatus.class ,but it didn't work , here is my configuration ,is there anything wrong?

<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.7.201606060606</version>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
<configuration>
<excludes>
<exclude>
*.jar
</exclude>
<exclude>
**/BlockBuilderStatus.class
</exclude>
</excludes>
</configuration>
</plugin>

Method deepInstanceSize in BlockBuilderStatus.java will be invoked when the class is loaded , and it throws an Exception when use jacoco , it will go into 'if (clazz.isArray())' this branch. so I want to skip instrument the class .

private static int deepInstanceSize(Class<?> clazz)
{
if (clazz.isArray()) {
throw new IllegalArgumentException(String.format("Cannot determine size of %s because it contains an array", clazz.getSimpleName()));
}
if (clazz.isInterface()) {
throw new IllegalArgumentException(String.format("%s is an interface", clazz.getSimpleName()));
}
if (Modifier.isAbstract(clazz.getModifiers())) {
throw new IllegalArgumentException(String.format("%s is abstract", clazz.getSimpleName()));
}
if (!clazz.getSuperclass().equals(Object.class)) {
throw new IllegalArgumentException(String.format("Cannot determine size of a subclass. %s extends from %s", clazz.getSimpleName(), clazz.getSuperclass().getSimpleName()));
}

int size = ClassLayout.parseClass(clazz).instanceSize();
for (Field field : clazz.getDeclaredFields()) {
if (!field.getType().isPrimitive()) {
size += deepInstanceSize(field.getType());
}
}
return size;
}

Evgeny Mandrikov

unread,
Jan 3, 2017, 4:15:33 PM1/3/17
to JaCoCo and EclEmma Users
Hi,

First of all quoting JaCoCo FAQ item "My code uses reflection. Why does it fail when I execute it with JaCoCo?" ( see http://www.jacoco.org/jacoco/trunk/doc/faq.html ) :
To collect execution data JaCoCo instruments the classes under test which adds two members to the classes: A private static field $jacocoData and a private static method $jacocoInit(). Both members are marked as synthetic.
Please change your code to ignore synthetic members. This is a good practice anyways as also the Java compiler creates synthetic members in certain situation.

And answering your question about exclusions: there is known inconsistency between exclusion patterns for instrumentation and report generation ( see https://github.com/jacoco/jacoco/issues/34 ): first one accepts Java or VM class name, while second one accepts file name. So if you want to exclude class from instrumentation - specify it as " **/BlockBuilderStatus" for "instrument" goal. Or as a workaround - you can specify "**/BlockBuilderStatus*", which will also work for "report" goal.

zw1506...@163.com

unread,
Jan 3, 2017, 10:57:23 PM1/3/17
to JaCoCo and EclEmma Users
Hi, Thank you so much for answering. I have tried to exclude class from instrumentation 
in "instrument" goal.
            I want use BlockBuilderStatus.class without  instrumentation  
<execution>
    <id>default-instrument</id>
    <goals>
        <goal>instrument</goal>
    </goals>
    <configuration>
        <excludes>
            <exclude>
                 *.jar
            </exclude>
            <exclude>
                **/BlockBuilderStatus
             </exclude>
        </excludes>
    </configuration>
</execution>
  Its a multi-module project ,so I  use an maven-antrun-plugin. and it failes with error message: "Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.6:run (default) on project common-api: An Ant BuildException has occured: Error while creating report: Error while analyzing  xxx.class. Class xxx  is already instrumented. "   

  "To collect execution data JaCoCo instruments the classes under test which adds two members to the classes: A private static field $jacocoData and a private static method $jacocoInit(). Both members are marked as synthetic.
Please change your code to ignore synthetic members. This is a good practice anyways as also the Java compiler creates synthetic members in certain situation."   where can I find a sample?  actually,if BlockBuilderStatus.class is instrumented ,it will go into this branch  '  if (clazz.isArray()) {
            throw new IllegalArgumentException(String.format("Cannot determine size of %s because it contains an array", clazz.getSimpleName()));
        }'   ,and this BlockBuilderStatus.class is  referenced by many classes
 



 

在 2017年1月4日星期三 UTC+8上午5:15:33,Evgeny Mandrikov写道:

Evgeny Mandrikov

unread,
Jan 6, 2017, 4:46:29 PM1/6/17
to JaCoCo and EclEmma Users
 I want use BlockBuilderStatus.class without  instrumentation

Your initial question had no usage of http://www.jacoco.org/jacoco/trunk/doc/instrument-mojo.html , so I told you about exclusions for "prepare-agent" and "report".
And "instrument" accepts file name for exclusions.
 
An Ant BuildException has occured: Error while creating report: Error while analyzing  xxx.class. Class xxx  is already instrumented. "
 
This happens when you try to mix online instrumentation using Agent with offline pre-instrumentation. Don't use both for the same classes.

Please change your code to ignore synthetic members.

zw1506...@163.com

unread,
Jan 9, 2017, 5:08:31 AM1/9/17
to JaCoCo and EclEmma Users
Hi,
Thank you so much . I think I took  a mistake like what you said  
'This happens when you try to mix online instrumentation using Agent with offline pre-instrumentation. Don't use both for the same classes.'  
I am very new in  jacoco ,ant and  maven , and I've tried  to   use  <exclude> * </exclude>    for "prepare-agent" and "report",but it doesn't work. Maybe I shoud do it in org.jacoco.ant , but I still have no idea how to exclude it.  Here is my profile for jacoco, and it is defined in parent pom.xml . "sub_moduleA"  is one of the sub-modules .
Could you give me an sample to exclude a file and just modify the profile ,Thank you again !
<profile>
    <id>jacoco</id>
        <build>
            <plugins>
                <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
                <includes>
                       <include>**/facebook/presto/**/Test*.java</include>
</includes>              
</configuration>
   </plugin> 
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>properties-maven-plugin</artifactId>
                    <version>1.0.0</version>
                    <inherited>false</inherited>
                    <executions>
                        <execution>
                            <goals>
                                <goal>set-system-properties</goal>
                            </goals>
                            <configuration>
                                <properties>
                                    <property>
                                        <name>parent.basedir</name>
                                        <value>${basedir}</value>
                                    </property>
                                </properties>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <executions>
                        <!-- Copy the ant tasks jar. Needed for ts.jacoco.report-ant . -->
                        <execution>
                            <id>jacoco-dependency-ant</id>
                            <goals>
                                <goal>copy</goal>
                            </goals>
                            <phase>process-test-resources</phase>
                            <inherited>false</inherited>
                            <configuration>
                                <artifactItems>
                                    <artifactItem>
                                        <groupId>org.jacoco</groupId>
                                        <artifactId>org.jacoco.ant</artifactId>
                                        <version>0.7.7.201606060606</version>
                                    </artifactItem>
                                </artifactItems>
                                <stripVersion>true</stripVersion>
                                <outputDirectory>${basedir}/target/jacoco-jars</outputDirectory>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>   

                <plugin>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.6</version>
                    <executions>
                        <execution>
                            <phase>post-integration-test</phase>
                            <goals>
                                <goal>run</goal>
                            </goals>
                            <configuration>
                                <target>
                                    <echo message="Generating JaCoCo Reports" />
                                    <taskdef classname="org.jacoco.ant.ReportTask" name="report">
                                        <classpath path="${parent.basedir}/target/jacoco-jars/org.jacoco.ant.jar" />
                                    </taskdef>
                                    <mkdir dir="${parent.basedir}/target/site/jacoco" />
                                    <report>
                                        <executiondata>
                                            <fileset dir="${parent.basedir}/sub_moduleA/target">
                                                <include name="jacoco.exec" />
                                            </fileset>
                                            <fileset dir="${parent.basedir}/sub_moduleB/target">
                                                <include name="jacoco.exec" />
                                            </fileset>
                                                ...
                                        </executiondata>
                                        <structure name="jacoco-multi Coverage Project">
                                            <group name="jacoco-multi">
                                                <sourcefiles encoding="UTF-8">
                                                    <fileset dir="${parent.basedir}/sub_moduleA/src/main/java" />
                                                    <fileset dir="${parent.basedir}/sub_moduleB/src/main/java" />
                    ...
                                                </sourcefiles>
                                                <classfiles>
                                                    <fileset dir="${parent.basedir}/sub_moduleA/target/classes" >
                                                        <exclude name="*.jar"/>
                                                    </fileset>
                                                    <fileset dir="${parent.basedir}sub_moduleB/target/classes" >
                                                        <exclude name="*.jar"/>
                                                    </fileset>
                                                    ...
                                                </classfiles>
                                            </group>
                                        </structure>
                                        <csv destfile="${parent.basedir}/target/site/jacoco/coverage-report.csv" />
                                        <xml destfile="${parent.basedir}/target/site/jacoco/coverage-report.xml" />
                                        <html destdir="${parent.basedir}/target/site/jacoco" />
                                    </report>
                                </target>
                            </configuration>
                        </execution>
                    </executions>
                    <dependencies>
                        <dependency>
                            <groupId>org.jacoco</groupId>
                            <artifactId>org.jacoco.ant</artifactId>
                            <version>0.7.6.201602180812</version>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </build>
    </profile>



在 2017年1月7日星期六 UTC+8上午5:46:29,Evgeny Mandrikov写道:
Reply all
Reply to author
Forward
This conversation is locked
You cannot reply and perform actions on locked conversations.
0 new messages