Skip to content

Commit f2b2714

Browse files
authored
Merge pull request #345 from SentryMan/fix-CheckedProxy
Fix Throwable Aspects
2 parents 82a6504 + 37a7059 commit f2b2714

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

blackbox-test-inject/src/main/java/org/example/myapp/r4j/MyExample.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package org.example.myapp.r4j;
22

3-
import io.avaje.inject.Component;
43
import org.example.myapp.resilience4j.MyRetry;
54

5+
import io.avaje.inject.Component;
6+
67
@Component
78
public class MyExample {
89

910
public int barfCounter;
1011
public int retryWithFallbackCounter;
1112

1213
@MyRetry
13-
public void doingItWithRetry() {
14+
public void doingItWithRetry() throws Throwable, IllegalStateException{
1415
barfCounter++;
1516
throw new IllegalArgumentException("no");
1617
}

inject-generator/src/main/java/io/avaje/inject/generator/AspectMethod.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,22 +181,25 @@ void writeArgs(Append writer) {
181181
writer.append(" return call.finalResult();").eol();
182182
}
183183

184-
writeThrowsCatch(writer);
185184
writer.append(" } catch (RuntimeException ex) {").eol();
186185
writer.append(" ex.addSuppressed(new InvocationException(\"%s proxy threw exception\"));", simpleName).eol();
187186
writer.append(" throw ex;").eol();
188-
writer.append(" } catch (Throwable t) {").eol();
189-
writer.append(" throw new InvocationException(\"%s proxy threw exception\", t);", simpleName).eol();
187+
writeThrowsCatch(writer);
188+
if (thrownTypes.stream().map(Object::toString).noneMatch("java.lang.Throwable"::equals)) {
189+
writer.append(" } catch (Throwable t) {").eol();
190+
writer.append(" throw new InvocationException(\"%s proxy threw exception\", t);", simpleName).eol();
191+
}
190192
writer.append(" }").eol();
191193
}
192194

193195
private void writeThrowsCatch(Append writer) {
194-
195-
if (thrownTypes.isEmpty()) {
196+
final var types = new ArrayList<>(thrownTypes);
197+
types.removeIf(ProcessingContext::isUncheckedException);
198+
if (types.isEmpty()) {
196199
return;
197200
}
198201
writer.append(" } catch (");
199-
thrownTypes.stream()
202+
types.stream()
200203
.map(Object::toString)
201204
.map(Util::shortName)
202205
.collect(collectingAndThen(joining(" | "), writer::append))

inject-generator/src/main/java/io/avaje/inject/generator/ProcessingContext.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,12 @@ static Element asElement(TypeMirror returnType) {
156156
return wrapper == null ? CTX.get().typeUtils.asElement(returnType) : element(wrapper);
157157
}
158158

159+
static boolean isUncheckedException(TypeMirror returnType) {
160+
final var types = CTX.get().typeUtils;
161+
final var runtime = element("java.lang.RuntimeException").asType();
162+
return types.isSubtype(returnType, runtime);
163+
}
164+
159165
static void addModule(String moduleFullName) {
160166
if (moduleFullName != null) {
161167
CTX.get().uniqueModuleNames.add(moduleFullName);

0 commit comments

Comments
 (0)