-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More expressive return type from generator
Instead of returning just a String with the Java source file (compilation unit), instead include all information needed to correctly write the source to a file.
- Loading branch information
1 parent
5156e0b
commit c80ae40
Showing
5 changed files
with
45 additions
and
14 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
generator/src/main/java/no/rune/record/matcher/JavaCompilationUnit.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package no.rune.record.matcher; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.NotDirectoryException; | ||
import java.nio.file.Path; | ||
import java.util.Optional; | ||
|
||
import static java.nio.file.Files.isDirectory; | ||
|
||
public record JavaCompilationUnit(String content, String name, Optional<String> packageName) { | ||
|
||
public JavaCompilationUnit(String content, String name, Package location) { | ||
this(content, name, Optional.of(location).map(Package::getName)); | ||
} | ||
|
||
Path writeToBaseDirectory(Path baseDirectory) throws NotDirectoryException, IOException { | ||
if (!isDirectory(baseDirectory)) { | ||
throw new NotDirectoryException(baseDirectory.toString()); | ||
} | ||
var targetDirectory = packageName.map(p -> baseDirectory.resolve(p.replace('.', '/'))).orElse(baseDirectory); | ||
var targetFile = targetDirectory.resolve(name + ".java"); | ||
try { | ||
if (!baseDirectory.equals(targetDirectory)) { | ||
Files.createDirectories(targetDirectory); | ||
} | ||
return Files.writeString(targetFile, content); | ||
} catch (IOException e) { | ||
throw new IOException("Unable to write to " + targetFile + ", " + | ||
"because " + e.getClass().getSimpleName() + ": " + e.getMessage(), e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
...rator/src/test/java/no/rune/record/matcher/DefaultRecordMatcherClassNameResolverTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters