lishunli / projectlombok

Automatically exported from code.google.com/p/projectlombok
0 stars 0 forks source link

@Delegate feature with Exception forwarding #550

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I would like to use the @Delegate feature, but I need Exception catching and 
throwing.
Scenario: the exceptions thrown by the called methods are different from the 
ones that are allowed for the calling method. 

Current code:

class someClass{

private Object delegatedObject;

public Object someMethod(String someParam) throws BusinessException {
        try {
            return delegatedObject.someMethod(someParam);
        } 
        catch (PersistenceException e) {
            throw new BusinessException(e);
        }
        catch (OtherException e) {
            throw new BusinessException(e);
        }
    }
}

What is the expected feature?

class someClass{

@Delegate(RedirectExceptions={"PersistenceException","otherException"}, 
"BusinessException")
private Object delegatedObject;

}

Original issue reported on code.google.com by alexis.i...@gmail.com on 24 Jul 2013 at 1:55

GoogleCodeExporter commented 9 years ago
Sorry, we don't see this happen within the next two years, closing the issue.

- Should it do this for all methods? 
- Should we add 'throws' clauses to all methods?
- If the code in one of the methods doesn't actually declare to throw an 
exception, how do we handle that? Generate a compiler error?

All in all, too much is unclear, it is hard to make it easy to understand, and 
it doesn't add quite a lot. I personally only encountered this when 
implementing RMI, but that's something from the past, isn't it?

Original comment by r.spilker on 5 Aug 2013 at 7:47

GoogleCodeExporter commented 9 years ago
- Should it do this for all methods? 
Yes. I mean all methods should be delegated, and those who throw exceptions 
should be surrounded with the ty-catch clause.

- Should we add 'throws' clauses to all methods?
No, only those which throw the given exceptions

- If the code in one of the methods doesn't actually declare to throw an 
exception, how do we handle that? Generate a compiler error?
No, just use the standard delgation mechanism, without adding try-catch 
encapsulation

Scenario: here is the code of the delegate object

Class DelegateObject
{
    public Object someMethod(String someParam) throws PersistenceException, OtherException 
    {
        //stuff ...
    }

    public Object someMethod2(String someParam) 
    {
        //stuff ...
    }

    public Object someMethod3(String someParam) throws MyException
    {
        //stuff ...
    }
}

The generated code would be:

class someClass{

    private DelegateObject delegateObject;

    public Object someMethod(String someParam) throws BusinessException {
        try {
            return delegateObject.someMethod(someParam);
        } 
        catch (PersistenceException e) {
            throw new BusinessException(e);
        }
        catch (OtherException e) {
            throw new BusinessException(e);
        }
    }
    public Object someMethod2(String someParam){

        return delegateObject.someMethod2(someParam);
    }

    public Object someMethod3(String someParam) throws MyException{

        return delegateObject.someMethod3(someParam);
    }
}

Original comment by alexis.i...@gmail.com on 6 Aug 2013 at 2:14