Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude methods annotated with @lombok.Generated #513

Merged
merged 1 commit into from
Apr 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*******************************************************************************
* 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 java.util.Arrays;

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.AnnotationNode;
import org.objectweb.asm.tree.MethodNode;

public class LombokGeneratedFilterTest implements IFilterOutput {

private final IFilter filter = new LombokGeneratedFilter();

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(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;", false);

m.visitInsn(Opcodes.ICONST_0);
m.visitInsn(Opcodes.IRETURN);

filter.filter(m, this);

assertNull(fromInclusive);
assertNull(toInclusive);
}

@Test
public void testLombokGeneratedAnnotation() {
final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0,
"hashCode", "()I", null, null);
m.visitAnnotation("Llombok/Generated;", false);

m.visitInsn(Opcodes.ICONST_0);
m.visitInsn(Opcodes.IRETURN);

filter.filter(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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.jacoco.core.analysis.ISourceNode;
import org.jacoco.core.internal.analysis.filter.IFilter;
import org.jacoco.core.internal.analysis.filter.IFilterOutput;
import org.jacoco.core.internal.analysis.filter.LombokGeneratedFilter;
import org.jacoco.core.internal.analysis.filter.SynchronizedFilter;
import org.jacoco.core.internal.analysis.filter.SyntheticFilter;
import org.jacoco.core.internal.flow.IFrame;
Expand All @@ -42,7 +43,8 @@ public class MethodAnalyzer extends MethodProbesVisitor
implements IFilterOutput {

private static final IFilter[] FILTERS = new IFilter[] {
new SyntheticFilter(), new SynchronizedFilter() };
new SyntheticFilter(), new SynchronizedFilter(),
new LombokGeneratedFilter() };

private final boolean[] probes;

Expand Down Expand Up @@ -328,8 +330,9 @@ public void visitEnd() {
final int covered = i.getCoveredBranches();
final ICounter instrCounter = covered == 0 ? CounterImpl.COUNTER_1_0
: CounterImpl.COUNTER_0_1;
final ICounter branchCounter = total > 1 ? CounterImpl.getInstance(
total - covered, covered) : CounterImpl.COUNTER_0_0;
final ICounter branchCounter = total > 1
? CounterImpl.getInstance(total - covered, covered)
: CounterImpl.COUNTER_0_0;
coverage.increment(instrCounter, branchCounter, i.getLine());
}
coverage.incrementMethodCounter();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*******************************************************************************
* 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>@lombok.Generated</code>.
*/
public class LombokGeneratedFilter implements IFilter {

public void filter(final MethodNode methodNode,
final IFilterOutput output) {
if (hasLombokGeneratedAnnotation(methodNode)) {
output.ignore(methodNode.instructions.getFirst(),
methodNode.instructions.getLast());
}
}

private boolean hasLombokGeneratedAnnotation(final MethodNode methodNode) {
final List<AnnotationNode> runtimeInvisibleAnnotations = methodNode.invisibleAnnotations;
if (runtimeInvisibleAnnotations != null) {
for (final AnnotationNode annotation : runtimeInvisibleAnnotations) {
if ("Llombok/Generated;".equals(annotation.desc)) {
return true;
}
}
}
return false;
}

}
3 changes: 3 additions & 0 deletions org.jacoco.doc/docroot/doc/changes.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ <h3>New Features</h3>
<li>Exclude from a report a part of bytecode that compiler generates for a
synchronized statement
(GitHub <a href="https://github.com/jacoco/jacoco/issues/501">#501</a>).</li>
<li>Exclude from a report methods which are annotated with <code>@lombok.Generated</code>.
Initial analysis and contribution by Rüdiger zu Dohna.
(GitHub <a href="https://github.com/jacoco/jacoco/issues/513">#513</a>).</li>
<li>Maven aggregated reports will now also include modules of runtime dependencies
(GitHub <a href="https://github.com/jacoco/jacoco/issues/498">#498</a>).</li>
</ul>
Expand Down