Skip to content

Commit 5d531cd

Browse files
committed
allow defining any rules in DependencyRuler
1 parent 0ab5f45 commit 5d531cd

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public class FindBugsTest {
141141
.because("It's checked and OK like this",
142142
In.classes(DependencyRules.class, PmdRuleset.class).ignore("DP_DO_INSIDE_DO_PRIVILEGED"),
143143
In.classes("*Test", "Rulesets")
144-
.and(In.classes("ClassFileParser").withMethods("parse"))
144+
.and(In.classes("ClassFileParser").withMethods("doParse"))
145145
.ignore("URF_UNREAD_FIELD"));
146146

147147
FindBugsResult result = new FindBugsAnalyzer(config, collector).analyze();

code-assert/src/main/java/guru/nidi/codeassert/dependency/DependencyRuler.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,26 @@ public DependencyRule rule() {
9191
return new DependencyRule("*", true);
9292
}
9393

94+
/**
95+
* Create a rule for a package allowing everything.
96+
* Shortcut for DependencyRule.allowAll(name);
97+
*
98+
* @return the rule.
99+
*/
100+
public DependencyRule allowRule(String name) {
101+
return new DependencyRule(name, true);
102+
}
103+
104+
/**
105+
* Create a rule for a package denying everything.
106+
* Shortcut for DependencyRule.denyAll(name);
107+
*
108+
* @return the rule.
109+
*/
110+
public DependencyRule denyRule(String name) {
111+
return new DependencyRule(name, false);
112+
}
113+
94114
public void defineRules() {
95115
}
96116
}

code-assert/src/main/java/guru/nidi/codeassert/dependency/DependencyRules.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ private List<DependencyRule> initFields(String basePackage, DependencyRuler rule
186186

187187
private DependencyRule initField(String basePackage, DependencyRuler ruler, Field f) throws IllegalAccessException {
188188
final String name = f.getName();
189+
final CodeElement value = (CodeElement) f.get(ruler);
190+
if (value instanceof DependencyRule && !"*".equals(value.pattern.getPattern())) {
191+
return (DependencyRule) value;
192+
}
189193
deprecationWarnings(name);
190194
final String pack = addPackages(basePackage, "$self".equals(name) ? "" : camelCaseToDotCase(name));
191195
final DependencyRule rule = rule(pack);

0 commit comments

Comments
 (0)