apache / apache/netbeans

Introducing method on "instanceof" caused compile error

Open
#7,428 0 comments 0 reactions 0 assignees View on GitHub
Java kind:bug
Dominant language
Java
Stars
3.1k
Forks
935
Avg merge
2d 3h
Merged PRs (30d)
17

Description

### Apache NetBeans version

Apache NetBeans 21

### What happened

Using "introduce method" on "instanceof" caused compile error in output program.

### Language / Project Type / NetBeans Component

Java Maven Project Java Application

### How to reproduce

Input program:
```
class X {
private void foo(Object o) {
if (o instanceof X x) { // select the entire o instanceof X x
System.out.println(x.toString());
x.foo(null);
}

}
}
```

Steps:
Select “o instanceof X x”
Refactor -> Introduce -> Method

Actual output:
```
class X {
private void foo(Object o) {
if (extract1(o)) { // select the entire o instanceof X x
System.out.println(x.toString());
x.foo(null);
}

}

private boolean extract1(Object o) {
return o instanceof X x;
}
}
```

Expected result:
Prevent from introducing method on "instanceof"

### Did this work correctly in an earlier version?

No / Don't know

### Operating System

Windows 11

### JDK

22.0.1

### Apache NetBeans packaging

Apache NetBeans provided installer

### Anything else

_No response_

### Are you willing to submit a pull request?

No

Contributor guide

Open the contributing guide

Research direction

Reproduce the issue in a Java Maven project by selecting `o instanceof X x` and choosing Refactor → Introduce → Method. Start by locating the Introduce Method refactoring entry point and its handling of `instanceof` selections. Done means the refactoring prevents this selection and does not generate uncompilable output; no source file or regression test is named in the report.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.