Skip to content

Commit

Permalink
Add detection of Scala classes (bazelbuild#922)
Browse files Browse the repository at this point in the history
  • Loading branch information
Godin authored and marchof committed Aug 19, 2019
1 parent 6babdb5 commit 9a91884
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
package org.jacoco.core.internal.analysis;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.jacoco.core.internal.flow.MethodProbesVisitor;
import org.jacoco.core.internal.instr.InstrSupport;
import org.junit.Before;
import org.junit.Test;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.Opcodes;

/**
Expand Down Expand Up @@ -57,4 +59,12 @@ public void testMethodFilter_Empty() {
assertEquals(0, coverage.getMethods().size());
}

@Test
public void should_collect_attributes() {
assertTrue(analyzer.getClassAttributes().isEmpty());
analyzer.visitAttribute(new Attribute("foo") {
});
assertTrue(analyzer.getClassAttributes().contains("foo"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class FilterContextMock implements IFilterContext {
public String className = "Foo";
public String superClassName = "java/lang/Object";
public Set<String> classAnnotations = new HashSet<String>();
public Set<String> classAttributes = new HashSet<String>();
public String sourceFileName = "Foo.java";
public String sourceDebugExtension;

Expand All @@ -37,6 +38,10 @@ public Set<String> getClassAnnotations() {
return classAnnotations;
}

public Set<String> getClassAttributes() {
return classAttributes;
}

public String getSourceFileName() {
return sourceFileName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,34 @@ public void testLambda() {
}

@Test
public void should_not_filter_Scala_anonymous_functions() {
public void should_filter_synthetic_method_with_prefix_anonfun_in_non_Scala_classes() {
final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION,
Opcodes.ACC_SYNTHETIC, "$anonfun$main$1", "()V", null, null);
m.visitInsn(Opcodes.RETURN);

filter.filter(m, context, output);
assertMethodIgnored(m);
}

@Test
public void should_not_filter_synthetic_method_with_prefix_anonfun_in_Scala_classes() {
final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION,
Opcodes.ACC_SYNTHETIC, "$anonfun$main$1", "()V", null, null);
m.visitInsn(Opcodes.RETURN);

context.classAttributes.add("ScalaSig");
filter.filter(m, context, output);
assertIgnored();
}

@Test
public void should_not_filter_synthetic_method_with_prefix_anonfun_in_Scala_inner_classes() {
final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION,
Opcodes.ACC_SYNTHETIC, "$anonfun$main$1", "()V", null, null);
m.visitInsn(Opcodes.RETURN);

context.classAttributes.add("Scala");
filter.filter(m, context, output);
assertIgnored();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.jacoco.core.internal.flow.MethodProbesVisitor;
import org.jacoco.core.internal.instr.InstrSupport;
import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.tree.MethodNode;
Expand All @@ -37,6 +38,8 @@ public class ClassAnalyzer extends ClassProbesVisitor

private final Set<String> classAnnotations = new HashSet<String>();

private final Set<String> classAttributes = new HashSet<String>();

private String sourceDebugExtension;

private final IFilter filter;
Expand Down Expand Up @@ -75,6 +78,11 @@ public AnnotationVisitor visitAnnotation(final String desc,
return super.visitAnnotation(desc, visible);
}

@Override
public void visitAttribute(final Attribute attribute) {
classAttributes.add(attribute.type);
}

@Override
public void visitSource(final String source, final String debug) {
coverage.setSourceFileName(stringPool.get(source));
Expand Down Expand Up @@ -146,6 +154,10 @@ public Set<String> getClassAnnotations() {
return classAnnotations;
}

public Set<String> getClassAttributes() {
return classAttributes;
}

public String getSourceFileName() {
return coverage.getSourceFileName();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public interface IFilterContext {
*/
Set<String> getClassAnnotations();

/**
* @return names of the class attributes
*/
Set<String> getClassAttributes();

/**
* @return file name of the corresponding source file or <code>null</code>
* if not available
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
*/
public final class SyntheticFilter implements IFilter {

private static boolean isScalaClass(final IFilterContext context) {
return context.getClassAttributes().contains("ScalaSig")
|| context.getClassAttributes().contains("Scala");
}

public void filter(final MethodNode methodNode,
final IFilterContext context, final IFilterOutput output) {
if ((methodNode.access & Opcodes.ACC_SYNTHETIC) == 0) {
Expand All @@ -29,8 +34,10 @@ public void filter(final MethodNode methodNode,
return;
}

if (methodNode.name.startsWith("$anonfun$")) {
return;
if (isScalaClass(context)) {
if (methodNode.name.startsWith("$anonfun$")) {
return;
}
}

if (KotlinGeneratedFilter.isKotlinClass(context)) {
Expand Down

0 comments on commit 9a91884

Please sign in to comment.