Skip to content

Commit

Permalink
#159: Remove cactoos lib due to useless dependency (#160)
Browse files Browse the repository at this point in the history
Fixed #159
  • Loading branch information
dgroup authored Jun 13, 2023
1 parent f76ec25 commit 2ca31b5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 24 deletions.
11 changes: 1 addition & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@
<version>0.0.0</version>
<packaging>jar</packaging>
<name>${project.artifactId}</name>
<description>Library with pmd rules that bring new regulations related to known problems in REST
API, logging, monitoring, etc., including reconfigured default/out of box pmd rules considering
well-known frameworks like Spring, Quarkus, etc.
</description>
<description>Linting rules for Java frameworks like Spring, Quarkus, Jackson, SLF4J, etc., to avoid known problems in REST API, logging, observability, performance, and general best practices.</description>
<url>https://github.com/dgroup/arch4u-pmd</url>
<licenses>
<license>
Expand Down Expand Up @@ -96,7 +93,6 @@
<commons-logging.version>1.2</commons-logging.version>
<!-- General dependencies -->
<pmd.rules.version>6.46.0</pmd.rules.version>
<cactoos.version>0.50.1</cactoos.version>
<!-- Testing -->
<hamcrest.version>2.2</hamcrest.version>
<cactoos-matchers.version>0.25</cactoos-matchers.version>
Expand All @@ -120,11 +116,6 @@
</sonar.coverage.jacoco.xmlReportPaths>
</properties>
<dependencies>
<dependency>
<groupId>org.cactoos</groupId>
<artifactId>cactoos</artifactId>
<version>${cactoos.version}</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
Expand Down
25 changes: 11 additions & 14 deletions src/main/java/io/github/dgroup/arch4u/pmd/UseExistingConstant.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,20 @@
package io.github.dgroup.arch4u.pmd;

import java.util.regex.Pattern;
import net.sourceforge.pmd.RuleContext;
import net.sourceforge.pmd.lang.java.ast.ASTLiteral;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
import net.sourceforge.pmd.lang.java.rule.regex.RegexHelper;
import net.sourceforge.pmd.properties.PropertyDescriptor;
import net.sourceforge.pmd.properties.PropertyFactory;
import org.apache.commons.lang3.StringUtils;
import org.cactoos.scalar.Sticky;
import org.cactoos.scalar.Unchecked;

/**
* Rule to avoid creating string constants for {@code MediaType} values.
* Example: {@code "application/json'}
* Use existing classes.
*
* @see <a href="https://github.com/dgroup/arch4u-pmd/discussions/43">https://github.com/dgroup/arch4u-pmd/discussions/43</a>
* @since 0.1.0
* @see
* <a href="https://github.com/dgroup/arch4u-pmd/discussions/43">https://github.com/dgroup/arch4u-pmd/discussions/43</a>
*/
@SuppressWarnings("PMD.StaticAccessToStaticFields")
public final class UseExistingConstant extends AbstractJavaRule {
Expand All @@ -56,9 +53,9 @@ public final class UseExistingConstant extends AbstractJavaRule {
.build();

/**
* Cached pattern supplier.
* Pattern to match string text/literal that should be replaced by constant.
*/
private final Unchecked<Pattern> pattern;
private Pattern pattern;

/**
* Constructor for defining property descriptor.
Expand All @@ -67,23 +64,23 @@ public final class UseExistingConstant extends AbstractJavaRule {
public UseExistingConstant() {
this.definePropertyDescriptor(REGEX_PROPERTY);
this.addRuleChainVisit(ASTLiteral.class);
this.pattern = new Unchecked<>(
new Sticky<>(
() -> Pattern.compile(this.getProperty(REGEX_PROPERTY))
)
);
}

@Override
public void start(final RuleContext ctx) {
super.start(ctx);
this.pattern = Pattern.compile(this.getProperty(REGEX_PROPERTY));
}

@Override
public Object visit(final ASTLiteral node, final Object data) {
if (node.isStringLiteral()) {
String image = node.getTextBlockContent();
image = image.substring(1, image.length() - 1);
if (image.length() > 0 && RegexHelper.isMatch(this.pattern.value(), image)) {
if (image.length() > 0 && this.pattern.matcher(image).find()) {
this.addViolation(data, node);
}
}
return data;
}

}

0 comments on commit 2ca31b5

Please sign in to comment.