-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix UseConstantAsMetricName rule - from XPath to Java
- Loading branch information
Showing
4 changed files
with
86 additions
and
23 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
src/main/java/io/github/dgroup/arch4u/pmd/UseConstantAsMetricName.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,42 @@ | ||
package io.github.dgroup.arch4u.pmd; | ||
|
||
import net.sourceforge.pmd.lang.java.ast.ASTAnnotation; | ||
import net.sourceforge.pmd.lang.java.ast.ASTMemberValuePair; | ||
import net.sourceforge.pmd.lang.java.ast.ASTStringLiteral; | ||
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule; | ||
import net.sourceforge.pmd.lang.java.types.TypeTestUtil; | ||
import net.sourceforge.pmd.properties.PropertyDescriptor; | ||
import net.sourceforge.pmd.properties.PropertyFactory; | ||
|
||
import java.util.List; | ||
|
||
public class UseConstantAsMetricName extends AbstractJavaRule { | ||
|
||
private static final PropertyDescriptor<List<String>> METRIC_ANNOTATIONS_DESCRIPTOR = | ||
PropertyFactory.stringListProperty("metricAnnotations") | ||
.desc("List of the metric annotations.") | ||
.defaultValues("io.micrometer.core.annotation.Timed") | ||
.build(); | ||
|
||
private final List<String> metricAnnotations; | ||
|
||
public UseConstantAsMetricName() { | ||
definePropertyDescriptor(METRIC_ANNOTATIONS_DESCRIPTOR); | ||
metricAnnotations = getProperty(METRIC_ANNOTATIONS_DESCRIPTOR); | ||
} | ||
|
||
@Override | ||
public Object visit(ASTAnnotation annotation, Object data) { | ||
if (isMetricAnnotation(annotation)) { | ||
annotation.getMembers() | ||
.map(ASTMemberValuePair::getValue) | ||
.filter(val -> val instanceof ASTStringLiteral) | ||
.forEach(val -> asCtx(data).addViolation(val)); | ||
} | ||
return super.visit(annotation, data); | ||
} | ||
|
||
private boolean isMetricAnnotation(ASTAnnotation node) { | ||
return metricAnnotations.stream().anyMatch(annot -> TypeTestUtil.isExactlyA(annot, node)); | ||
} | ||
} |
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
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