Skip to content

Commit

Permalink
Improve class resolver for efficiency
Browse files Browse the repository at this point in the history
  • Loading branch information
sarpsahinalp committed Sep 12, 2024
1 parent a88ad58 commit 2a08e65
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package de.tum.cit.ase.ares.api.architecture.java.archunit.postcompile;

//<editor-fold desc="Imports">
import com.tngtech.archunit.ArchConfiguration;
import com.tngtech.archunit.core.domain.JavaClass;
import com.tngtech.archunit.core.domain.JavaClasses;
import com.tngtech.archunit.core.importer.ClassFileImporter;
import com.tngtech.archunit.core.importer.resolvers.ClassResolverFromClasspath;

import java.net.URL;
import java.util.Optional;
//</editor-fold>

Expand All @@ -13,39 +15,32 @@
*/
public class CustomClassResolver {

//<editor-fold desc="Attributes">
private CustomClassResolver() {
throw new IllegalStateException("Utility class");
}

/**
* Class file importer to import the class files.
* This is used to import the class files from the URL.
*/
private final JavaClasses allClasses;
//</editor-fold>
private static final ClassFileImporter classFileImporter = new ClassFileImporter();

//<editor-fold desc="Constructor">
public CustomClassResolver() {
// We need to import all classes to be able to resolve them later.
// https://www.javadoc.io/doc/com.tngtech.archunit/archunit/0.10.2/com/tngtech/archunit/core/importer/ClassFileImporter.html
allClasses = new ClassFileImporter()
.withImportOption(location -> !location.contains("jrt"))
.importClasspath();
}
//</editor-fold>

//<editor-fold desc="Methods">
/**
* Try to resolve the class by the given type name.
*
* @param typeName The type name of the class to resolve.
* @return The resolved class if it exists.
*/
public Optional<JavaClass> tryResolve(String typeName) {
public static Optional<JavaClass> tryResolve(String typeName) {
ArchConfiguration.get().setClassResolver(ClassResolverFromClasspath.class);
URL url = CustomClassResolver.class.getResource("/" + typeName.replace(".", "/") + ".class");
try {
// Try to resolve the class by the given type name.
return Optional.ofNullable(allClasses.get(typeName));
if (url == null) {
return Optional.empty();
}
return Optional.of(classFileImporter.withImportOption(location -> !location.contains("jrt")).importUrl(url).get(typeName));
} catch (IllegalArgumentException e) {
return Optional.empty();
}
}
//</editor-fold>

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ public class TransitivelyAccessesMethodsCondition extends ArchCondition<JavaClas
* Transitive access path to find the path to the accessed method
*/
private final TransitiveAccessPath transitiveAccessPath = new TransitiveAccessPath();

/**
* Custom class resolver to resolve classes that are outside classpath to be able to analyze them transitively
*/
private final CustomClassResolver customClassResolver;
//</editor-fold>

//<editor-fold desc="Constructor">
Expand All @@ -51,7 +46,6 @@ public class TransitivelyAccessesMethodsCondition extends ArchCondition<JavaClas
public TransitivelyAccessesMethodsCondition(DescribedPredicate<? super JavaAccess<?>> conditionPredicate) {
super("transitively depend on classes that " + conditionPredicate.getDescription());
this.conditionPredicate = checkNotNull(conditionPredicate);
this.customClassResolver = new CustomClassResolver();
}
//</editor-fold>

Expand Down Expand Up @@ -175,7 +169,7 @@ && isExceptionOrError(a.getTargetOwner()))
}

private JavaClass resolveTargetOwner(JavaClass targetOwner) {
Optional<JavaClass> resolvedTarget = customClassResolver.tryResolve(targetOwner.getFullName());
Optional<JavaClass> resolvedTarget = CustomClassResolver.tryResolve(targetOwner.getFullName());
return resolvedTarget.orElse(targetOwner);
}

Expand Down

0 comments on commit 2a08e65

Please sign in to comment.