Skip to content

Possible Incoherent behavior of handleBracedCode in LineBreaksPreparator.java #5239

Description

@inuyasha82

I stumbled into this strange behavior while wokring on the pr #5184

In LineBreaksPreparator.java during ASTree visto of AnnotationDeclaration,
On annotation handling, the function handleBracedCode is called, that is basically splitting the annotations of the first braced code in the annotations declaration (and only the first).

    public class TestExample {

    public record TestRecord(@Serial @Deprecated @SuppressWarnings(value = {"" }) String name, @Deprecated @Serial @SuppressWarnings(value = { "" }) String age,
			@Deprecated String test) {
	}
}

For something like that inside a record, it will become:

    public record TestRecord(@Serial @Deprecated @SuppressWarnings(value = {
	    "" }) String name, @Deprecated @Serial @SuppressWarnings(value = { "" }) String age,
	    @Deprecated String test) {
    }

What looks incoherent to me is the fact that it split correctly the first SuppressWarning on the first variable declaration, but it doesn't do the same for the second one.
And I think this is happening because the call is done inside the visit for the RecordDeclaration:

@Override
	public boolean visit(RecordDeclaration node) {
		handleAnnotations(node.modifiers(), this.options.insert_new_line_after_annotation_on_type);
		handleBracedCode(node, node.getName(), this.options.brace_position_for_record_declaration,
				this.options.indent_body_declarations_compare_to_record_header);
		handleBodyDeclarations(node.bodyDeclarations());
		return true;
	}

That is containing the whole record declaration, not the variable/annotations declarations, or the modifiers. So the handleBracedCode in line preparator is actually searching for the first braced code to update and that's it.
Then the annotation split happens sometime later in the code, while this visit method is called:

	@Override
	public boolean visit(SingleVariableDeclaration node) {
		handleAnnotations(node.modifiers(),
				node.getParent() instanceof EnhancedForStatement
						? this.options.insert_new_line_after_annotation_on_local_variable
						: this.options.insert_new_line_after_annotation_on_parameter);

		return true;
	}

that as you can see doesn't handle braced code.
So what will basically happen is that only the first braces are handled.

I think this behavior is kind of inconsistent when dealing with annotation, even because this is apparently not happening with method parameters, even if there are annotations:

public class TestExample {

    private void testMethod(@Deprecated @SuppressWarnings( value= {""}) String serial, @Deprecated @SuppressWarnings(value = {""}) String name) {

    }
}

become:

public class TestExample {

    private void testMethod(@Deprecated @SuppressWarnings(value = { "" }) String serial,
	    @Deprecated @SuppressWarnings(value = { "" }) String name) {

    }
}

I'm not sure if there is a rationale behind that, this is why I created this issue, to understand if it's a bug, or not.
I haven't went deep into the analysis, but from what I understood is only handleBracedCode involved.

Is that a bug that should be fixed or not?

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions