Skip to content

TryCatchBlock in lambda will not be collected #1069

Closed
@demonbug

Description

the second assertion will fail

    @Test
    public void test_trycatch_contract() {
        JavaClasses classes = new ClassFileImporter().importClasses(ClassWithTryCatchInLambda.class);

        List<JavaMethodCall> calls = classes.stream()
            .flatMap(cls -> cls.getMethodCallsFromSelf().stream())
            .filter(call -> "parseDouble".equals(call.getName()))
            .collect(Collectors.toList());
        Assertions.assertFalse(calls.isEmpty());

        List<TryCatchBlock> tryBlocks =
            calls.stream().flatMap(call -> call.getContainingTryBlocks().stream()).collect(Collectors.toList());
        Assertions.assertFalse(tryBlocks.isEmpty());
    }

    private class ClassWithTryCatchInLambda {
        public void testCall() {
            testFunctionInterface(text -> {
                try {
                    Double.parseDouble(text);
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            });
        }

        private void testFunctionInterface(Consumer<String> func) {
            func.accept("123456");
        }
    }

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions