Skip to content
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

Do not try to create proxies for instances of Throwable #30

Merged
merged 3 commits into from
Apr 17, 2024
Merged
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 @@ -117,6 +117,8 @@ publishing {
}

signing {
isRequired = providers.environmentVariable("CI").isPresent

sign(publishing.publications["mavenJava"])
useInMemoryPgpKeys(System.getenv("PGP_SIGNING_KEY"), System.getenv("PGP_SIGNING_KEY_PASSPHRASE"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public Object invoke(Object proxy, Method method, Object[] args) {
targetMethod.setAccessible(true);

Object result = targetMethod.invoke(target, targetArgs);
if (result == null || isJdkType(result.getClass())) {
if (result == null || isJdkTypeOrThrowable(result.getClass())) {
return result;
}
return createProxy(result, method.getReturnType());
Expand All @@ -62,7 +62,7 @@ private static Object[] toTargetArgs(Object[] args) {
if (args.length == 1 && args[0] instanceof Function) {
return new Object[]{adaptFunctionArg((Function<?, ?>) args[0])};
}
if (Arrays.stream(args).allMatch(it -> isJdkType(it.getClass()))) {
if (Arrays.stream(args).allMatch(it -> isJdkTypeOrThrowable(it.getClass()))) {
return args;
}
throw new RuntimeException("Unsupported argument types in " + Arrays.toString(args));
Expand All @@ -84,7 +84,7 @@ private static Function<Object, Object> adaptFunctionArg(Function func) {
}

private static Object createLocalProxy(Object target) {
if (isJdkType(target.getClass())) {
if (isJdkTypeOrThrowable(target.getClass())) {
return target;
}

Expand Down Expand Up @@ -118,7 +118,7 @@ private static Class<?>[] convertTypes(Class<?>[] parameterTypes, ClassLoader cl
}
return Arrays.stream(parameterTypes)
.map(type -> {
if (isJdkType(type)) {
if (isJdkTypeOrThrowable(type)) {
return type;
}

Expand All @@ -131,9 +131,11 @@ private static Class<?>[] convertTypes(Class<?>[] parameterTypes, ClassLoader cl
.toArray(Class<?>[]::new);
}

private static boolean isJdkType(Class<?> type) {
private static boolean isJdkTypeOrThrowable(Class<?> type) {
ClassLoader typeClassLoader = type.getClassLoader();
return typeClassLoader == null || typeClassLoader.equals(Object.class.getClassLoader());
// JDK types are present across classloaders, so there is no need to create proxies for those
// we can't create proxies for instances of Throwable as there is no shared interface
return typeClassLoader == null || typeClassLoader.equals(Object.class.getClassLoader()) || Throwable.class.isAssignableFrom(type);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,35 @@ public List<Throwable> getFailures() {
assertEquals(Collections.singletonList(failure), capturedNewBuildResult.getValue().getFailures());
}

@Test
@DisplayName("can run the build finished action using the proxy when the build result has custom failures")
@SuppressWarnings("Convert2Lambda")
void testBuildFinishedActionWithCustomException() {
// given
class CustomException extends Throwable {
public CustomException(String message) {
super(message);
}
}

// and
Throwable failure = new CustomException("Boom!");
BuildResult buildResult = new BuildResult() {
@Override
public List<Throwable> getFailures() {
return Collections.singletonList(failure);
}
};
doExecuteActionWith(buildResult).when(configuration).buildFinished(any());

// when
ArgCapturingAction<BuildResultAdapter> capturer = new ArgCapturingAction<>();
adapter.buildFinished(capturer);

// then
assertEquals(Collections.singletonList(failure), capturer.getValue().getFailures());
}

@Test
@DisplayName("build scan published action can be configured via an adapter using the new scan model")
void testBuildScanPublishedAction() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,35 @@ public Throwable getFailure() {
assertEquals(Collections.singletonList(failure), capturer.getValue().getFailures());
}

@Test
@DisplayName("can run the build finished action using the proxy when the build result has custom failures")
@SuppressWarnings("Convert2Lambda")
void testBuildFinishedActionWithCustomException() {
// given
class CustomException extends Throwable {
public CustomException(String message) {
super(message);
}
}

// and
Throwable failure = new CustomException("Boom!");
BuildResult buildResult = new BuildResult() {
@Override
public Throwable getFailure() {
return failure;
}
};
doExecuteActionWith(buildResult).when(extension).buildFinished(any());

// when
ArgCapturingAction<BuildResultAdapter> capturer = new ArgCapturingAction<>();
adapter.buildFinished(capturer);

// then
assertEquals(Collections.singletonList(failure), capturer.getValue().getFailures());
}

@Test
@DisplayName("can run the build scan published action using the proxy")
void testBuildScanPublishedAction() {
Expand Down
Loading