Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add filter for methods annotated with @groovy.transform.Generated (#610)
- Loading branch information
Showing
6 changed files
with
205 additions
and
20 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
...oco.core.test/src/org/jacoco/core/internal/analysis/filter/GroovyGeneratedFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2009, 2017 Mountainminds GmbH & Co. KG and Contributors | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Marc R. Hoffmann - initial API and implementation | ||
* | ||
*******************************************************************************/ | ||
package org.jacoco.core.internal.analysis.filter; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNull; | ||
import static org.junit.Assert.fail; | ||
|
||
import org.jacoco.core.internal.instr.InstrSupport; | ||
import org.junit.Test; | ||
import org.objectweb.asm.Opcodes; | ||
import org.objectweb.asm.tree.AbstractInsnNode; | ||
import org.objectweb.asm.tree.MethodNode; | ||
|
||
public class GroovyGeneratedFilterTest implements IFilterOutput { | ||
|
||
private final IFilter filter = new GroovyGeneratedFilter(); | ||
|
||
private AbstractInsnNode fromInclusive; | ||
private AbstractInsnNode toInclusive; | ||
|
||
@Test | ||
public void testNoAnnotations() { | ||
final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, | ||
"hashCode", "()I", null, null); | ||
|
||
m.visitInsn(Opcodes.ICONST_0); | ||
m.visitInsn(Opcodes.IRETURN); | ||
|
||
filter.filter("Foo", "java/lang/Object", m, this); | ||
|
||
assertNull(fromInclusive); | ||
assertNull(toInclusive); | ||
} | ||
|
||
@Test | ||
public void testOtherAnnotation() { | ||
final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, | ||
"hashCode", "()I", null, null); | ||
m.visitAnnotation("Lother/Annotation;", true); | ||
|
||
m.visitInsn(Opcodes.ICONST_0); | ||
m.visitInsn(Opcodes.IRETURN); | ||
|
||
filter.filter("Foo", "java/lang/Object", m, this); | ||
|
||
assertNull(fromInclusive); | ||
assertNull(toInclusive); | ||
} | ||
|
||
@Test | ||
public void testGroovyGeneratedAnnotation() { | ||
final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, | ||
"hashCode", "()I", null, null); | ||
m.visitAnnotation("Lgroovy/transform/Generated;", true); | ||
|
||
m.visitInsn(Opcodes.ICONST_0); | ||
m.visitInsn(Opcodes.IRETURN); | ||
|
||
filter.filter("Foo", "java/lang/Object", m, this); | ||
|
||
assertEquals(m.instructions.getFirst(), fromInclusive); | ||
assertEquals(m.instructions.getLast(), toInclusive); | ||
} | ||
|
||
public void ignore(final AbstractInsnNode fromInclusive, | ||
final AbstractInsnNode toInclusive) { | ||
assertNull(this.fromInclusive); | ||
this.fromInclusive = fromInclusive; | ||
this.toInclusive = toInclusive; | ||
} | ||
|
||
public void merge(final AbstractInsnNode i1, final AbstractInsnNode i2) { | ||
fail(); | ||
} | ||
|
||
} |
66 changes: 66 additions & 0 deletions
66
...coco.core/src/org/jacoco/core/internal/analysis/filter/AbstractAnnotatedMethodFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2009, 2017 Mountainminds GmbH & Co. KG and Contributors | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Marc R. Hoffmann - initial API and implementation | ||
* | ||
*******************************************************************************/ | ||
package org.jacoco.core.internal.analysis.filter; | ||
|
||
import java.util.List; | ||
|
||
import org.objectweb.asm.tree.AnnotationNode; | ||
import org.objectweb.asm.tree.MethodNode; | ||
|
||
/** | ||
* Filters annotated methods. | ||
*/ | ||
abstract class AbstractAnnotatedMethodFilter implements IFilter { | ||
private final String descType; | ||
|
||
/** | ||
* Configures a new filter instance. | ||
* | ||
* @param annotationType | ||
* VM type of the annotation | ||
*/ | ||
protected AbstractAnnotatedMethodFilter(final String annotationType) { | ||
this.descType = "L" + annotationType + ";"; | ||
} | ||
|
||
public void filter(final String className, final String superClassName, | ||
final MethodNode methodNode, final IFilterOutput output) { | ||
if (hasAnnotation(methodNode)) { | ||
output.ignore(methodNode.instructions.getFirst(), | ||
methodNode.instructions.getLast()); | ||
} | ||
} | ||
|
||
private boolean hasAnnotation(final MethodNode methodNode) { | ||
final List<AnnotationNode> annotations = getAnnotations(methodNode); | ||
if (annotations != null) { | ||
for (final AnnotationNode annotation : annotations) { | ||
if (descType.equals(annotation.desc)) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Retrieves the annotations to search from a method. Depending on the | ||
* retention of the annotation this is either | ||
* <code>visibleAnnotations</code> or <code>invisibleAnnotations</code>. | ||
* | ||
* @param methodNode | ||
* method to retrieve annotations from | ||
* @return list of annotations | ||
*/ | ||
abstract List<AnnotationNode> getAnnotations(final MethodNode methodNode); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
org.jacoco.core/src/org/jacoco/core/internal/analysis/filter/GroovyGeneratedFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2009, 2017 Mountainminds GmbH & Co. KG and Contributors | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Marc R. Hoffmann - initial API and implementation | ||
* | ||
*******************************************************************************/ | ||
package org.jacoco.core.internal.analysis.filter; | ||
|
||
import java.util.List; | ||
|
||
import org.objectweb.asm.tree.AnnotationNode; | ||
import org.objectweb.asm.tree.MethodNode; | ||
|
||
/** | ||
* Filters methods annotated with <code>@groovy.transform.Generated</code>. | ||
*/ | ||
public final class GroovyGeneratedFilter extends AbstractAnnotatedMethodFilter { | ||
|
||
/** | ||
* New filter. | ||
*/ | ||
public GroovyGeneratedFilter() { | ||
super("groovy/transform/Generated"); | ||
} | ||
|
||
@Override | ||
List<AnnotationNode> getAnnotations(final MethodNode methodNode) { | ||
return methodNode.visibleAnnotations; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters