diff --git a/jadx-core/src/main/java/jadx/core/dex/visitors/MethodThrowsVisitor.java b/jadx-core/src/main/java/jadx/core/dex/visitors/MethodThrowsVisitor.java index 72b8cb4ac..908776227 100644 --- a/jadx-core/src/main/java/jadx/core/dex/visitors/MethodThrowsVisitor.java +++ b/jadx-core/src/main/java/jadx/core/dex/visitors/MethodThrowsVisitor.java @@ -153,12 +153,12 @@ public class MethodThrowsVisitor extends AbstractVisitor { return; } } - visitThrows(mth, exceptionType); + visitThrows(mth, exceptionType, excludedExceptions); } else { if (throwArg instanceof InsnWrapArg) { InsnWrapArg insnWrapArg = (InsnWrapArg) throwArg; ArgType exceptionType = insnWrapArg.getType(); - visitThrows(mth, exceptionType); + visitThrows(mth, exceptionType, excludedExceptions); } } return; @@ -189,9 +189,7 @@ public class MethodThrowsVisitor extends AbstractVisitor { if (cMth != null && cMth.getThrows() != null && !cMth.getThrows().isEmpty()) { MethodThrowsAttr attr = mth.get(AType.METHOD_THROWS); if (attr != null) { - for (ArgType ex : cMth.getThrows()) { - attr.getList().add(ex.getObject()); - } + attr.getList().addAll(filterExceptions(cMth.getThrows(), excludedExceptions)); } } } @@ -199,8 +197,14 @@ public class MethodThrowsVisitor extends AbstractVisitor { } } - private void visitThrows(MethodNode mth, ArgType excType) { + private void visitThrows(MethodNode mth, ArgType excType, Set excludedExceptions) { if (excType.isTypeKnown() && isThrowsRequired(mth, excType)) { + for (String excludedException : excludedExceptions) { + if (isBaseException(excType.getObject(), excludedException)) { + return; + } + } + mth.get(AType.METHOD_THROWS).getList().add(excType.getObject()); } } @@ -231,7 +235,7 @@ public class MethodThrowsVisitor extends AbstractVisitor { } /** - * @return is 'possibleParent' a exception class of 'exception' + * @return is 'possibleParent' an exception class of 'exception' */ private boolean isBaseException(String exception, String possibleParent) { if (exception.equals(possibleParent)) { @@ -252,7 +256,25 @@ public class MethodThrowsVisitor extends AbstractVisitor { for (String exception : exceptions) { boolean filtered = false; for (String excluded : excludedExceptions) { - filtered = exception.equals(excluded) || isBaseException(exception, excluded); + filtered = isBaseException(exception, excluded); + if (filtered) { + break; + } + } + if (!filtered) { + filteredExceptions.add(exception); + } + } + return filteredExceptions; + } + + private Collection filterExceptions(Collection exceptionArgTypes, Set excludedExceptions) { + Set filteredExceptions = new HashSet<>(); + for (ArgType exceptionArgType : exceptionArgTypes) { + boolean filtered = false; + String exception = exceptionArgType.getObject(); + for (String excluded : excludedExceptions) { + filtered = isBaseException(exception, excluded); if (filtered) { break; } diff --git a/jadx-core/src/test/java/jadx/tests/integration/others/TestThrows.java b/jadx-core/src/test/java/jadx/tests/integration/others/TestThrows.java index 4d3fdccd9..9bd3d157f 100644 --- a/jadx-core/src/test/java/jadx/tests/integration/others/TestThrows.java +++ b/jadx-core/src/test/java/jadx/tests/integration/others/TestThrows.java @@ -2,6 +2,7 @@ package jadx.tests.integration.others; import java.io.FileNotFoundException; import java.io.IOException; +import java.io.InputStream; import org.junit.jupiter.api.Test; @@ -72,6 +73,20 @@ public class TestThrows extends IntegrationTest { doSomething1(i); } } + + public void noThrownExceptions1(InputStream i1) { + try { + i1.close(); + } catch (IOException ignore) { + } + } + + public void noThrownExceptions2() { + try { + throw new FileNotFoundException(""); + } catch (IOException ignore) { + } + } } @Test @@ -87,6 +102,9 @@ public class TestThrows extends IntegrationTest { .containsOne("throwThrowable() throws Throwable {") .containsOne("exceptionSource() throws FileNotFoundException {") .containsOne("mergeThrownExceptions() throws IOException {") - .containsOne("rethrowThrowable() {"); + .containsOne("rethrowThrowable() {") + .containsOne("noThrownExceptions1(InputStream i1) {") + .containsOne("noThrownExceptions2() {"); + } }