eclipse-jdt / eclipse-jdt/eclipse.jdt.ui

Inline local variable fails with non-static class attributes

Open
#1,034 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
59
Forks
127
Avg merge
23h 30m
Merged PRs (30d)
35

Description

In the latest version (In version 4.27.0) the refactor for inlining local variable fails when the variable points to a class attribute.

In the following example, if I try to inline the variable _name_, the refactor converts the variable in a static reference, and then fails.

```
public class InlineAttributeBug {

static class Attributes {
String name;
String age;
}

String foo() {
Attributes attributes = new Attributes();
attributes.name = "fooobar";

// inlining variable name fails
String name = attributes.name;
return String.format("%s", name);
}
}
```

What the refactor proposes is the line `String.format("%s", InlineAttributeBug.Attributes.name)`, which fails because the attribute is not static.

P.S. I know that having public, non static, attributes is a bad practice, but sometimes you have to work with legacy code or auto-generated code that you cannot modify.

Contributor guide

Open the contributing guide

Research direction

Start with the Java reproducer in the issue and the inline-local-variable refactoring entry point. Reproduce the failure when the local variable refers to a non-static class attribute, then trace how the replacement reference is generated. Done means the refactoring preserves the instance receiver and the regression is covered by a test.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.