INRIA / INRIA/spoon

[Bug]: Refactoring via changeTypeName does not change all references

Open
#5,084 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Java
Stars
2k
Forks
393
Avg merge
11h 24m
Merged PRs (30d)
36

Description

### Describe the bug

Calling `Refactoring.changeTypeName( clazz, newName );` does not change all references to a class. In particular, instantiations by invoking `new` appear to be ignored.

### Source code you are trying to analyze/transform

```Java
cd /tmp
git clone https://github.com/DaveJarvis/nts
```

### Source code for your Spoon processing

``` Java
import spoon.Launcher;
import spoon.refactoring.Refactoring;
import spoon.support.sniper.SniperJavaPrettyPrinter;

import java.io.File;
import java.util.regex.Pattern;

public class App {
public static void main( final String[] args ) {
final var outputDir = new File("/tmp/nts-new");
final var spoon = new Launcher();

final var env = spoon.getEnvironment();
env.setPrettyPrinterCreator(
() -> new SniperJavaPrettyPrinter( env )
);

spoon.setSourceOutputDirectory( outputDir );

spoon.addInputResource( "/tmp/nts/app/src/main/java/nts/app" );
spoon.addInputResource( "/tmp/nts/dvi/src/main/java/nts/dvi" );
spoon.addInputResource( "/tmp/nts/png/src/main/java/nts/png" );
spoon.addInputResource( "/tmp/nts/svg/src/main/java/nts/svg" );
spoon.addInputResource( "/tmp/nts/tex/src/main/java/nts/align" );
spoon.addInputResource( "/tmp/nts/tex/src/main/java/nts/base" );
spoon.addInputResource( "/tmp/nts/tex/src/main/java/nts/builder" );
spoon.addInputResource( "/tmp/nts/tex/src/main/java/nts/command" );
spoon.addInputResource( "/tmp/nts/tex/src/main/java/nts/hyph" );
spoon.addInputResource( "/tmp/nts/tex/src/main/java/nts/io" );
spoon.addInputResource( "/tmp/nts/tex/src/main/java/nts/math" );
spoon.addInputResource( "/tmp/nts/tex/src/main/java/nts/noad" );
spoon.addInputResource( "/tmp/nts/tex/src/main/java/nts/node" );
spoon.addInputResource( "/tmp/nts/tex/src/main/java/nts/render" );
spoon.addInputResource( "/tmp/nts/tex/src/main/java/nts/tex" );
spoon.addInputResource( "/tmp/nts/tex/src/main/java/nts/tfm" );
spoon.addInputResource( "/tmp/nts/tex/src/main/java/nts/typo" );
spoon.addInputResource( "/tmp/nts/tex/src/test/java/nts/io" );

final var model = spoon.buildModel();
final var p = Pattern.compile( "^(?!Kt).*" );

for( final var clazz : model.getAllTypes() ) {
final var name = clazz.getSimpleName();
final var m = p.matcher( name );

if( m.matches() ) {
final var newName = "Kt" + name;

System.out.println( "rename " + name + " to " + newName );
Refactoring.changeTypeName( clazz, newName );
}
}

if( outputDir.exists() || outputDir.mkdirs() ) {
spoon.prettyprint();
}
}
}
```

### Actual output

The class `KtAccentNoad` contains the following line:

```Java
KtNoad noad = new OrdNoad(nucleus);
```

### Expected output

The class `KtAccentNoad` contains the following line:

```Java
KtNoad noad = new KtOrdNoad(nucleus);
```

### Spoon Version

Latest

### JVM Version

18

### What operating system are you using?

Linux

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at Refactoring.changeTypeName and reproduce the reported case with the provided App processing code and nts source tree. Trace how type references are updated, paying particular attention to constructor invocations, and verify that the generated KtAccentNoad code changes new OrdNoad to new KtOrdNoad.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
devtools, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.