Skip to content

Commit

Permalink
fix: exception during processing of classes with unknown type
Browse files Browse the repository at this point in the history
Signed-off-by: Joke de Buhr <joke@xckk.de>
  • Loading branch information
joke committed May 1, 2022
1 parent 7994899 commit f9c5d64
Showing 1 changed file with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github.joke.spockmockable.internal;

import java.util.Optional;
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.agent.ByteBuddyAgent;
import net.bytebuddy.agent.builder.AgentBuilder;
Expand Down Expand Up @@ -91,9 +90,13 @@ private static Set<String> readPropertyFromProperties(final InputStream stream,
final Properties properties = new Properties();
properties.load(stream);

return Stream.of(properties.getProperty(key, "")
.split(","))
.collect(toSet());
final String property = properties.getProperty(key);
if ("".equals(property)) {
return emptySet();
}

return Stream.of(property.split(","))
.collect(toSet());
}

private static void buildAndInstallTransformer(final Set<String> classes, Set<String> packages) {
Expand All @@ -104,11 +107,23 @@ private static void buildAndInstallTransformer(final Set<String> classes, Set<St
.with(InitializationStrategy.NoOp.INSTANCE)
.with(RETRANSFORMATION)
.with(REDEFINE)
.type(typeDescription -> isNotATest(typeDescription) && (isInClasses(classes, typeDescription) || isInPackages(packages, typeDescription)))
.type(typeDescription -> isTransformable(classes, packages, typeDescription))
.transform(MockableExtension::transform)
.installOn(instrumentation);
}

private static boolean isTransformable(final Set<String> classes, final Set<String> packages, final TypeDescription typeDescription) {
return isInClasses(classes, typeDescription) || isTransformableFromPackage(packages, typeDescription);
}

private static boolean isTransformableFromPackage(final Set<String> packages, final TypeDescription typeDescription) {
return isInPackages(packages, typeDescription) && isNotATest(typeDescription) && isNotASpockMock(typeDescription);
}

private static boolean isNotASpockMock(final TypeDescription typeDescription) {
return !typeDescription.getName().contains("$SpockMock$");
}

private static boolean isNotATest(TypeDescription typeDescription) {
return !typeDescription.isAssignableTo(Specification.class);
}
Expand All @@ -120,9 +135,7 @@ private static boolean isInClasses(Set<String> classes, TypeDescription typeDesc
private static boolean isInPackages(Set<String> packages, TypeDescription typeDescription) {
return packages
.stream()
.anyMatch(packageName -> Optional.ofNullable(typeDescription.getCanonicalName())
.map(canonicalName -> canonicalName.startsWith(packageName + "."))
.orElse(false));
.anyMatch(packageName -> typeDescription.getName().startsWith(packageName + "."));
}

private static DynamicType.Builder<?> transform(final DynamicType.Builder<?> builder, final TypeDescription typeDescription, final ClassLoader classLoader, final JavaModule module) {
Expand Down

0 comments on commit f9c5d64

Please sign in to comment.