[Bug] NullPointerException when renaming a method using setSimpleName on a VirtualFile
- Dominant language
- Java
- Stars
- 2k
- Forks
- 392
- Avg merge
- 11h 24m
- Merged PRs (30d)
- 36
Description
Hi everyone,
I am unsure whether #3744 introduced a regression when calling `setSimpleName` on a model built from a `VirtualFile`. Is it valid to combine a `VirtualFile` and the `SniperJavaPrettyPrinter`?
Here is a minimal example that will throw a `NullPointerException`:
```java
@Test
public void reproduction() {
var resource = new VirtualFile("public class SomeClass { void SomeMethod() {}}");
Launcher launcher = new Launcher();
launcher.addInputResource(resource);
launcher.getEnvironment().setPrettyPrinterCreator(
() -> new spoon.support.sniper.SniperJavaPrettyPrinter(launcher.getEnvironment())
);
launcher.buildModel();
var model = launcher.getModel();
var executables = model.getElements(new TypeFilter<>(CtExecutable.class));
var element = executables.get(1);
element.setSimpleName("SomeOtherMethod");
}
```
Exception location:
https://github.com/INRIA/spoon/pull/3744/files#diff-befbcc64ff160d86d9fa914487968edee1031311321a89dafbae1d0f69e4ee3bR254
`spoon.support.sniper.internal.ElementSourceFragment.fromSameFile` as the `SourcePosition.getFile()` call returns null.
Contributor guide
Assessment
This issue has not been assessed yet.