diff options
author | Evgeny Mandrikov <Godin@users.noreply.github.com> | 2018-08-14 22:47:58 +0200 |
---|---|---|
committer | Marc R. Hoffmann <hoffmann@mountainminds.com> | 2018-08-14 22:47:58 +0200 |
commit | 1283826e5c230f833036266c24b7921ae5f51142 (patch) | |
tree | 450132785a8523a41d09de702c1e7d9999a38d05 /org.jacoco.core/src/org/jacoco/core/internal/analysis | |
parent | 29a770dd29cd5ed4a7ad7896c33eda670b2750f2 (diff) | |
download | jacoco-1283826e5c230f833036266c24b7921ae5f51142.tar.gz |
Add filter for classes and methods with annotation `Generated` (#731)
Diffstat (limited to 'org.jacoco.core/src/org/jacoco/core/internal/analysis')
5 files changed, 66 insertions, 142 deletions
diff --git a/org.jacoco.core/src/org/jacoco/core/internal/analysis/filter/AbstractAnnotatedMethodFilter.java b/org.jacoco.core/src/org/jacoco/core/internal/analysis/filter/AbstractAnnotatedMethodFilter.java deleted file mode 100644 index 7981bc6a..00000000 --- a/org.jacoco.core/src/org/jacoco/core/internal/analysis/filter/AbstractAnnotatedMethodFilter.java +++ /dev/null @@ -1,66 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2009, 2018 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 MethodNode methodNode, - final IFilterContext context, 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); - -} diff --git a/org.jacoco.core/src/org/jacoco/core/internal/analysis/filter/AnnotationGeneratedFilter.java b/org.jacoco.core/src/org/jacoco/core/internal/analysis/filter/AnnotationGeneratedFilter.java new file mode 100644 index 00000000..aae93f69 --- /dev/null +++ b/org.jacoco.core/src/org/jacoco/core/internal/analysis/filter/AnnotationGeneratedFilter.java @@ -0,0 +1,63 @@ +/******************************************************************************* + * Copyright (c) 2009, 2018 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: + * Evgeny Mandrikov - 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 classes and methods annotated with + * {@link java.lang.annotation.RetentionPolicy#RUNTIME runtime visible} and + * {@link java.lang.annotation.RetentionPolicy#CLASS invisible} annotation whose + * simple name is <code>Generated</code>. + */ +public final class AnnotationGeneratedFilter implements IFilter { + + public void filter(final MethodNode methodNode, + final IFilterContext context, final IFilterOutput output) { + + for (String annotation : context.getClassAnnotations()) { + if (matches(annotation)) { + output.ignore(methodNode.instructions.getFirst(), + methodNode.instructions.getLast()); + return; + } + } + + if (presentIn(methodNode.invisibleAnnotations) + || presentIn(methodNode.visibleAnnotations)) { + output.ignore(methodNode.instructions.getFirst(), + methodNode.instructions.getLast()); + } + + } + + private static boolean matches(final String annotation) { + return "Generated;".equals( + annotation.substring(Math.max(annotation.lastIndexOf('/'), + annotation.lastIndexOf('$')) + 1)); + } + + private static boolean presentIn(final List<AnnotationNode> annotations) { + if (annotations != null) { + for (AnnotationNode annotation : annotations) { + if (matches(annotation.desc)) { + return true; + } + } + } + return false; + } + +} diff --git a/org.jacoco.core/src/org/jacoco/core/internal/analysis/filter/Filters.java b/org.jacoco.core/src/org/jacoco/core/internal/analysis/filter/Filters.java index 2e719221..b235cfb8 100644 --- a/org.jacoco.core/src/org/jacoco/core/internal/analysis/filter/Filters.java +++ b/org.jacoco.core/src/org/jacoco/core/internal/analysis/filter/Filters.java @@ -31,10 +31,9 @@ public final class Filters implements IFilter { new TryWithResourcesJavac11Filter(), new TryWithResourcesJavacFilter(), new TryWithResourcesEcjFilter(), new FinallyFilter(), new PrivateEmptyNoArgConstructorFilter(), - new StringSwitchJavacFilter(), new LombokGeneratedFilter(), - new GroovyGeneratedFilter(), new EnumEmptyConstructorFilter(), - new KotlinGeneratedFilter(), new KotlinLateinitFilter(), - new KotlinWhenSealedFilter()); + new StringSwitchJavacFilter(), new EnumEmptyConstructorFilter(), + new AnnotationGeneratedFilter(), new KotlinGeneratedFilter(), + new KotlinLateinitFilter(), new KotlinWhenSealedFilter()); private final IFilter[] filters; diff --git a/org.jacoco.core/src/org/jacoco/core/internal/analysis/filter/GroovyGeneratedFilter.java b/org.jacoco.core/src/org/jacoco/core/internal/analysis/filter/GroovyGeneratedFilter.java deleted file mode 100644 index 816cc4cd..00000000 --- a/org.jacoco.core/src/org/jacoco/core/internal/analysis/filter/GroovyGeneratedFilter.java +++ /dev/null @@ -1,36 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2009, 2018 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; - } - -} diff --git a/org.jacoco.core/src/org/jacoco/core/internal/analysis/filter/LombokGeneratedFilter.java b/org.jacoco.core/src/org/jacoco/core/internal/analysis/filter/LombokGeneratedFilter.java deleted file mode 100644 index b35e741f..00000000 --- a/org.jacoco.core/src/org/jacoco/core/internal/analysis/filter/LombokGeneratedFilter.java +++ /dev/null @@ -1,36 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2009, 2018 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 final class LombokGeneratedFilter extends AbstractAnnotatedMethodFilter { - - /** - * New filter. - */ - public LombokGeneratedFilter() { - super("lombok/Generated"); - } - - @Override - List<AnnotationNode> getAnnotations(final MethodNode methodNode) { - return methodNode.invisibleAnnotations; - } - -} |