Replace class in file with new implementation
- Dominant language
- Java
- Stars
- 2k
- Forks
- 392
- Avg merge
- 11h 24m
- Merged PRs (30d)
- 36
Description
I'm currently trying to implement a Class-to-Record Transformation POC using spoon.
It is almost done and works pretty well, the only issue I have is with bringing the modified code back as a prittyprinted string.
```kotlin
SniperJavaPrettyPrinter(env).printCompilationUnit(cu)
```
works in general but has weird spaces at the beginning (which can be removed with `trim()`) and messes up the newlines for imports and the class.
```java
import lombok.Getter;public record ....
```
instead of:
```java
import lombok.Getter;
public record ....
```
Using `prettyprint()` directly, messes up the order and style of surrounding comments.
```java
/* .before */
// .after
record Person(String firstName, String lastName) {
public String name() {
return (firstName + " ") + lastName;
}
}
```
instead of:
```java
// .before
record Person(String firstName, String lastName) {
public String name() {
return firstName + " " + lastName;
}
}
// .after
```
I replace the class by calling `class.replace(record)` and copying the `position` and `parent` direct from the class to the record.
Do I miss something or have I misunderstood the replace function or the way one does refactroings with spoon?
Any help is appreciated.
Contributor guide
Assessment
This issue has not been assessed yet.