lishunli / projectlombok

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

delombok can generate invalid code with @SneakyThrows at ctor #443

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
This class...

public class SneakyThrowIssue {
  @SneakyThrows
  public SneakyThrowIssue() throws IOException {
    this("bla");
  }

  public SneakyThrowIssue(final String string) throws IOException {
    throw new IOException();
  }
}

... is delomboked to the following code (which contains a syntax error):

public class SneakyThrowIssue {

    public SneakyThrowIssue() throws IOException {
        this("bla");
        try  catch (final java.lang.Throwable $ex) {
            throw lombok.Lombok.sneakyThrow($ex);
        }
    }

    public SneakyThrowIssue(final String string) throws IOException {

        throw new IOException();
    }
}

What is the expected output? 
A valid try / catch-block (or better fully omitted as the try does not contain 
anything?)

What do you see instead?
see above 

What version of the product are you using? On what operating system?
0.11.6

Please provide any additional information below.
-

Original issue reported on code.google.com by brinkman...@gmail.com on 19 Jan 2013 at 11:12

GoogleCodeExporter commented 9 years ago
There's a rule for @SneakyThrows that super and this calls are excluded, 
because they HAVE to be the first line in your constructor if you have one, 
thus, we can't put a try around it.

The bug here is that putting @SneakyThrows on any method (or constructor) that 
has no statements whatsoever to put a try/catch block around (so, an entirely 
empty method OR a constructor that consists solely of a this or super call) 
results in broken delombok output. Either the @SneakyThrows annotation isn't 
removed (in the case of empty methods), or you get this wonky brace-free 'try  
catch (....' construction.

What you 'want' for this issue (that you can @SneakyThrows away exceptions 
thrown in your this/super call) cannot be fixed, but, we will address the issue 
of empty methods/constructors, probably by just generating a warning, removing 
the annotation (in javac), and NOT generating a try/catch block as there'd be 
no point.

We've got this fixed (basically, we generate a warning if there's no code to 
wrap in a try/catch block and leave it at that) locally and that fix will show 
up in the next release.

Original comment by reini...@gmail.com on 12 Mar 2013 at 12:27