Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@Cleanup support #287

Open
maxvader opened this issue Aug 31, 2016 · 2 comments
Open

@Cleanup support #287

maxvader opened this issue Aug 31, 2016 · 2 comments

Comments

@maxvader
Copy link

Hello, I see that this annotation is not in the supported list.
Do you plan to support it?

At the moment it can generate fake errors in the IDE for code like this:
try { @Cleanup InputStream profileIs = Thread.currentThread().getContextClassLoader().getResourceAsStream(PROFILE_CONTEXT); } catch (IOException e) { throw new CustomException(e); }

This code is correct and Eclipse accepts it, Intellij says that the IOException is never thrown but is not correct, is thrown by InputStream.close().
(I know that in this specific case AutoClosable should be used, but it's not always possible).

@sirgl
Copy link
Contributor

sirgl commented Jan 26, 2018

Hello! We added extension point(com.intellij.codeInspection.resources.ImplicitResourceCloser) to support such scenario in Intellij Idea. It will be in 2018.1.

mplushnikov added a commit that referenced this issue Oct 24, 2018
* added example for issue #512

* fix removing of field annotations during delombok

* cleanup code

* added to changelog #530

* added support for ImplicitResourceCloser extension point for @cleanup #287

* master is now for: up from 2018.1 IntelliJ builds
@mplushnikov
Copy link
Owner

Hello @sirgl ,
thank you for providing of new extension point! Just implemented it in plugin.

Unfortunately it will not fix current issue, because it's something else. Here I need some extension point to give IntelliJ a hint, that some line of code will actually produce an exception.
It's because @cleanup will generate try/finally block internally and call resource.close() in it. Which one will produce not catched IOException.

Code example:

import lombok.Cleanup;

import java.io.IOException;
import java.io.InputStream;

public class Main {

    public static void main(String[] args) {
        try {
            @Cleanup InputStream profile = Thread.currentThread().getContextClassLoader().getResourceAsStream("/someFile");
            System.out.println(profile);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

Code after lombok processing:

import java.io.IOException;
import java.io.InputStream;

public class Main {

    public static void main(String[] args) {
        try {
            InputStream profile = Thread.currentThread().getContextClassLoader().getResourceAsStream("/someFile");
            try {
                profile.getClass();
            } finally {
                if (java.util.Collections.singletonList(profile).get(0) != null) {
                    profile.close();
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

Do you have some suggestions, how to make it working? Or can you provide some additional extension points?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants