eclipse-jdt / eclipse-jdt/eclipse.jdt.core

Possible Incoherent behavior of `handleBracedCode` in LineBreaksPreparator.java

Open
#5,239 1 comment 0 reactions 1 assignee Claimed by @inuyasha82 View on GitHub
bug
Dominant language
Java
Stars
237
Forks
195
Avg merge
1d 12h
Merged PRs (30d)
47

Description

I stumbled into this strange behavior while wokring on the pr https://github.com/eclipse-jdt/eclipse.jdt.core/pull/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).
```java
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:
```java
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`:
```java
@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:
```java
@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:

```java
public class TestExample {

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

}
}
```

become:

```java
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?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.