Skip to content

BeanPostProcessor architecture rules do not work with external classes #45202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.function.Supplier;
import java.util.stream.Stream;

Expand All @@ -33,11 +37,13 @@
import org.gradle.api.DefaultTask;
import org.gradle.api.Task;
import org.gradle.api.Transformer;
import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.file.FileCollection;
import org.gradle.api.file.FileTree;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Classpath;
import org.gradle.api.tasks.IgnoreEmptyDirectories;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputFiles;
Expand All @@ -58,6 +64,7 @@
* @author Scott Frederick
* @author Ivan Malutin
* @author Phillip Webb
* @author Dmytro Nosan
*/
public abstract class ArchitectureCheck extends DefaultTask {

Expand All @@ -80,14 +87,17 @@ private List<String> asDescriptions(List<ArchRule> rules) {
}

@TaskAction
void checkArchitecture() throws IOException {
JavaClasses javaClasses = new ClassFileImporter().importPaths(classFilesPaths());
List<EvaluationResult> violations = evaluate(javaClasses).filter(EvaluationResult::hasViolation).toList();
File outputFile = getOutputDirectory().file("failure-report.txt").get().getAsFile();
writeViolationReport(violations, outputFile);
if (!violations.isEmpty()) {
throw new VerificationException("Architecture check failed. See '" + outputFile + "' for details.");
}
void checkArchitecture() throws Exception {
withCompileClasspath(() -> {
JavaClasses javaClasses = new ClassFileImporter().importPaths(classFilesPaths());
List<EvaluationResult> violations = evaluate(javaClasses).filter(EvaluationResult::hasViolation).toList();
File outputFile = getOutputDirectory().file("failure-report.txt").get().getAsFile();
writeViolationReport(violations, outputFile);
if (!violations.isEmpty()) {
throw new VerificationException("Architecture check failed. See '" + outputFile + "' for details.");
}
return null;
});
}

private List<Path> classFilesPaths() {
Expand All @@ -98,8 +108,24 @@ private Stream<EvaluationResult> evaluate(JavaClasses javaClasses) {
return getRules().get().stream().map((rule) -> rule.evaluate(javaClasses));
}

private void withCompileClasspath(Callable<?> callable) throws Exception {
ClassLoader previous = Thread.currentThread().getContextClassLoader();
try {
List<URL> urls = new ArrayList<>();
for (File file : getCompileClasspath().getFiles()) {
urls.add(file.toURI().toURL());
}
ClassLoader classLoader = new URLClassLoader(urls.toArray(new URL[0]), getClass().getClassLoader());
Thread.currentThread().setContextClassLoader(classLoader);
callable.call();
}
finally {
Thread.currentThread().setContextClassLoader(previous);
}
}

private void writeViolationReport(List<EvaluationResult> violations, File outputFile) throws IOException {
outputFile.getParentFile().mkdirs();
Files.createDirectories(outputFile.getParentFile().toPath());
StringBuilder report = new StringBuilder();
for (EvaluationResult violation : violations) {
report.append(violation.getFailureReport());
Expand All @@ -126,6 +152,10 @@ final FileTree getInputClasses() {
return this.classes.getAsFileTree();
}

@InputFiles
@Classpath
public abstract ConfigurableFileCollection getCompileClasspath();

@Optional
@InputFiles
@PathSensitive(PathSensitivity.RELATIVE)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -49,6 +49,7 @@ private void registerTasks(Project project) {
TaskProvider<ArchitectureCheck> checkPackageTangles = project.getTasks()
.register("checkArchitecture" + StringUtils.capitalize(sourceSet.getName()), ArchitectureCheck.class,
(task) -> {
task.getCompileClasspath().from(sourceSet.getCompileClasspath());
task.setClasses(sourceSet.getOutput().getClassesDirs());
task.getResourcesDirectory().set(sourceSet.getOutput().getResourcesDir());
task.dependsOn(sourceSet.getProcessResourcesTaskName());
Expand Down
Loading