Skip to content

Commit b44478d

Browse files
committed
make rules a little more ideomatic and reduce redundant message strings by leveraging automatic message generation
1 parent 919fc34 commit b44478d

File tree

1 file changed

+20
-29
lines changed

1 file changed

+20
-29
lines changed

archrules-gradle-plugin-development/src/archRules/java/com/netflix/nebula/archrules/gradleplugins/GradleTaskActionRule.java

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@
55
import com.tngtech.archunit.core.domain.JavaAccess;
66
import com.tngtech.archunit.core.domain.JavaFieldAccess;
77
import com.tngtech.archunit.core.domain.JavaMethod;
8-
import com.tngtech.archunit.lang.ArchCondition;
9-
import com.tngtech.archunit.lang.ArchRule;
10-
import com.tngtech.archunit.lang.ConditionEvents;
11-
import com.tngtech.archunit.lang.Priority;
12-
import com.tngtech.archunit.lang.SimpleConditionEvent;
8+
import com.tngtech.archunit.lang.*;
139
import com.tngtech.archunit.lang.syntax.ArchRuleDefinition;
1410
import org.jspecify.annotations.NullMarked;
1511

@@ -29,37 +25,32 @@ public class GradleTaskActionRule implements ArchRulesService {
2925
* cause runtime errors in Gradle 10+. Move Project access to configuration time
3026
* (constructor/initializer) and use task properties instead.
3127
*/
32-
public static final ArchRule taskActionShouldNotAccessProject = createTaskActionRule(
33-
notAccessProject(),
34-
"access the Project object",
35-
"Accessing Project in @TaskAction methods breaks configuration cache and will be removed in Gradle 10. " +
36-
"Move Project access to task configuration time and use task inputs/properties instead.",
37-
"task_project"
38-
);
28+
public static final ArchRule taskActionShouldNotAccessProject = ArchRuleDefinition.priority(Priority.MEDIUM)
29+
.methods()
30+
.that(areAnnotatedWithTaskAction())
31+
.should(notAccessProject())
32+
.allowEmptyShould(true)
33+
.because(
34+
"Accessing Project in @TaskAction methods breaks configuration cache and will be removed in Gradle 10. " +
35+
"Move Project access to task configuration time and use task inputs/properties instead. " +
36+
"See https://docs.gradle.org/9.2.0/userguide/upgrading_version_7.html#task_project"
37+
);
3938

4039
/**
4140
* Prevents {@code @TaskAction} methods from calling {@code getTaskDependencies()}.
4241
* <p>
4342
* Calling {@code getTaskDependencies()} in task actions breaks configuration cache and will
4443
* cause runtime errors in Gradle 10+. Task dependencies should be declared at configuration time.
4544
*/
46-
public static final ArchRule taskActionShouldNotCallGetTaskDependencies = createTaskActionRule(
47-
notCallGetTaskDependencies(),
48-
"call getTaskDependencies()",
49-
"Calling getTaskDependencies() in @TaskAction methods breaks configuration cache and will be removed in Gradle 10. " +
50-
"Declare task dependencies at configuration time instead.",
51-
"task_dependencies"
52-
);
53-
54-
private static ArchRule createTaskActionRule(ArchCondition<JavaMethod> condition, String actionDescription, String reason, String docAnchor) {
55-
return ArchRuleDefinition.priority(Priority.MEDIUM)
56-
.methods()
57-
.that(areAnnotatedWithTaskAction())
58-
.should(condition)
59-
.allowEmptyShould(true)
60-
.as("Methods annotated with @TaskAction should not " + actionDescription)
61-
.because(reason + " See https://docs.gradle.org/9.2.0/userguide/upgrading_version_7.html#" + docAnchor);
62-
}
45+
public static final ArchRule taskActionShouldNotCallGetTaskDependencies = ArchRuleDefinition.priority(Priority.MEDIUM)
46+
.methods()
47+
.that(areAnnotatedWithTaskAction())
48+
.should(notCallGetTaskDependencies())
49+
.because(
50+
"Calling getTaskDependencies() in @TaskAction methods breaks configuration cache and will be removed in Gradle 10. " +
51+
"Declare task dependencies at configuration time instead. " +
52+
"See https://docs.gradle.org/9.2.0/userguide/upgrading_version_7.html#task_dependencies"
53+
);
6354

6455
private static DescribedPredicate<JavaMethod> areAnnotatedWithTaskAction() {
6556
return new DescribedPredicate<JavaMethod>("are annotated with @TaskAction") {

0 commit comments

Comments
 (0)