Skip to content

Conversation

@matthiasblaesing
Copy link
Contributor

@matthiasblaesing matthiasblaesing commented Dec 5, 2025

Example:

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

public class Test {

    public static byte[] test(InputStream input) throws Exception {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        int len;

        byte[] buffer = new byte[1024];

        while ((len = input.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }
        input.close();

        return outStream.toByteArray();
    }
}

Rewrite result before fix:

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

public class Test {

    public static byte[] test(InputStream input) throws Exception {
        try (input) {
            outStream = new ByteArrayOutputStream();
            int len;
            byte[] buffer = new byte[1024];
            while ((len = input.read(buffer)) != -1) {
                outStream.write(buffer, 0, len);
            }
        }

        return outStream.toByteArray();
    }
}

Problem is here, that the result variable is used outside the new try block, but the declaration is not written.

Closes: #9046

@matthiasblaesing matthiasblaesing added this to the NB29 milestone Dec 5, 2025
@matthiasblaesing matthiasblaesing added Java [ci] enable extra Java tests (java.completion, java.source.base, java.hints, refactoring.java, form) ci:dev-build [ci] produce a dev-build zip artifact (7 days expiration, see link on workflow summary page) labels Dec 5, 2025
@mbien mbien added the hints label Dec 6, 2025
Comment on lines 1562 to 1563
.input("package test;\n"
+ "import java.io.ByteArrayOutputStream;\n"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: new test code could use multi line Strings.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Copy link
Contributor

@lahodaj lahodaj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me, thanks!

gen.copyComments(tt.getFinallyBlock(), nt, false);
}
}
if(! preVarDeclsWritten) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit - could be if(! preVarDeclsWritten -> if (!preVarDeclsWritten (space moved after if), but it is not critical.

Comment on lines 1562 to 1563
.input("package test;\n"
+ "import java.io.ByteArrayOutputStream;\n"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

…aration) is guarded and other variables are used outside the new block

Example:

============== Source ==============

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

public class Test {

    public static byte[] test(InputStream input) throws Exception {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        int len;

        byte[] buffer = new byte[1024];

        while ((len = input.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }
        input.close();

        return outStream.toByteArray();
    }
}

============== Result ==============

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

public class Test {

    public static byte[] test(InputStream input) throws Exception {
        try (input) {
            outStream = new ByteArrayOutputStream();
            int len;
            byte[] buffer = new byte[1024];
            while ((len = input.read(buffer)) != -1) {
                outStream.write(buffer, 0, len);
            }
        }

        return outStream.toByteArray();
    }
}

====================================

Problem is here, that the result variable is used outside the new try
block, but the declaration is not written.


Closes: apache#9046
@matthiasblaesing
Copy link
Contributor Author

Thanks for review. Given the small size of the PR I updated in place.

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

Labels

ci:dev-build [ci] produce a dev-build zip artifact (7 days expiration, see link on workflow summary page) hints Java [ci] enable extra Java tests (java.completion, java.source.base, java.hints, refactoring.java, form)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Code conversion error

3 participants