[Bug]: SniperPrettyPrinter - After replace ref types to another type, but print old import type
- Dominant language
- Java
- Stars
- 2k
- Forks
- 392
- Avg merge
- 11h 24m
- Merged PRs (30d)
- 36
Description
### Describe the bug
I want to replace import statements from one classpath to another, so I search all CtTypeReference and use replace() to another one.
Then I use SniperPrettyPrinter to output my code, but the import statements still prints the original type.
### Source code you are trying to analyze/transform
```Java
CtType typeB = ctModel.getAllTypes().stream().filter(t -> t.getQualifiedName().equals("com.B")).toList().get(0);
ctModel.getElements(new TypeFilter<>(CtTypeReference.class) {
@Override
public boolean matches(CtTypeReference element) {
return element.getQualifiedName().equals("com.A") && !element.isImplicit();
}
}).forEach(ref -> {
ref.replace(typeB.getReference());
});
```
### Source code for your Spoon processing
```Java
launcher.getEnvironment().setNoClasspath(true);
launcher.getEnvironment().setIgnoreDuplicateDeclarations(true);
launcher.getEnvironment().setPrettyPrinterCreator(() -> {
SniperJavaPrettyPrinter printer = new SniperJavaPrettyPrinter(launcher.getEnvironment());
List> preprocessors = List.of(
//try to import as much types as possible
new ForceImportProcessor(),
//remove unused imports first. Do not add new imports at time when conflicts are not resolved
new ImportCleaner().setCanAddImports(false),
//solve conflicts, the current imports are relevant too
new ImportConflictDetector(),
//compute final imports
new ImportCleaner().setImportComparator(new DefaultImportComparator())
);
printer.setIgnoreImplicit(false);
printer.setPreprocessors(preprocessors);
return printer;
});
```
### Actual output
```Java
package com;
import com.A;
import com.B;
class Foo{}
```
### Expected output
```Java
package com;
import com.B;
class Foo{}
```
### Spoon Version
11.2.1
### JVM Version
17
### What operating system are you using?
mac 15.4.1
Contributor guide
Assessment
This issue has not been assessed yet.