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

Fix Throwable Aspects #345

Merged
merged 1 commit into from
May 5, 2023
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
@@ -1,16 +1,17 @@
package org.example.myapp.r4j;

import io.avaje.inject.Component;
import org.example.myapp.resilience4j.MyRetry;

import io.avaje.inject.Component;

@Component
public class MyExample {

public int barfCounter;
public int retryWithFallbackCounter;

@MyRetry
public void doingItWithRetry() {
public void doingItWithRetry() throws Throwable, IllegalStateException{
barfCounter++;
throw new IllegalArgumentException("no");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,22 +181,25 @@ void writeArgs(Append writer) {
writer.append(" return call.finalResult();").eol();
}

writeThrowsCatch(writer);
writer.append(" } catch (RuntimeException ex) {").eol();
writer.append(" ex.addSuppressed(new InvocationException(\"%s proxy threw exception\"));", simpleName).eol();
writer.append(" throw ex;").eol();
writer.append(" } catch (Throwable t) {").eol();
writer.append(" throw new InvocationException(\"%s proxy threw exception\", t);", simpleName).eol();
writeThrowsCatch(writer);
if (thrownTypes.stream().map(Object::toString).noneMatch("java.lang.Throwable"::equals)) {
writer.append(" } catch (Throwable t) {").eol();
writer.append(" throw new InvocationException(\"%s proxy threw exception\", t);", simpleName).eol();
}
writer.append(" }").eol();
}

private void writeThrowsCatch(Append writer) {

if (thrownTypes.isEmpty()) {
final var types = new ArrayList<>(thrownTypes);
types.removeIf(ProcessingContext::isUncheckedException);
if (types.isEmpty()) {
return;
}
writer.append(" } catch (");
thrownTypes.stream()
types.stream()
.map(Object::toString)
.map(Util::shortName)
.collect(collectingAndThen(joining(" | "), writer::append))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ static Element asElement(TypeMirror returnType) {
return wrapper == null ? CTX.get().typeUtils.asElement(returnType) : element(wrapper);
}

static boolean isUncheckedException(TypeMirror returnType) {
final var types = CTX.get().typeUtils;
final var runtime = element("java.lang.RuntimeException").asType();
return types.isSubtype(returnType, runtime);
}

static void addModule(String moduleFullName) {
if (moduleFullName != null) {
CTX.get().uniqueModuleNames.add(moduleFullName);
Expand Down