gizmo icon indicating copy to clipboard operation
gizmo copied to clipboard

Loan pattern api for ClassCreator to control life cycle of object from API

Open poldinik opened this issue 10 months ago • 0 comments

I would be nice if ClassCreator provides a static API to produce an instance of ClassCreator with try/catch with resource inside API, instead to delegate to a calling function the responsibility to implement try/catch

public static void withClassCreator(ClassCreator.Builder builder, Consumer<ClassCreator> consumer){
        try(ClassCreator classCreator = builder.build()){
            consumer.accept(classCreator);
        }
    } 

An example of usage

withClassCreator(
                ClassCreator.builder()
                        .classOutput(cl)
                        .className("com.MyTest")
                        .interfaces(MyInterface.class),
                creator -> {
                    MethodCreator method = creator.getMethodCreator("transform", String.class, String.class);
                    ResultHandle existingVal = method.load("complete");
                    TryBlock tb = method.tryBlock();
                    tb.addCatch(IllegalStateException.class).load(34);
                    tb.invokeStaticMethod(MethodDescriptor.ofMethod(ExceptionClass.class.getName(), "throwIllegalState", "V"));
                });

poldinik avatar Mar 30 '24 11:03 poldinik