Skip to content

Commit

Permalink
refactor: use record classes
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospereira committed May 7, 2024
1 parent da958b4 commit ee7bda8
Show file tree
Hide file tree
Showing 8 changed files with 10 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,13 @@ private void writeBinaryTextPartsConstants(StringBuilder fields) {
@Override
public void onError( String message ) {
DebugInfo debugInfo = getCurrentDebugInfo();
throw new TemplateException("Failed to compile " + debugInfo.name + ", error at line " + debugInfo.line + ": " + message);
throw new TemplateException("Failed to compile " + debugInfo.name() + ", error at line " + debugInfo.line() + ": " + message);
}

@Override
public void onError(String message, int templateLine) {
DebugInfo debugInfo = getDebugInfo(templateLine);
throw new TemplateException("Failed to compile " + debugInfo.name + ", error at line " + debugInfo.line + ": " + message);
throw new TemplateException("Failed to compile " + debugInfo.name() + ", error at line " + debugInfo.line() + ": " + message);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private TemplateException handleException(Exception e) {
DebugInfo debugInfo = loader.resolveDebugInfo(null, stackTrace);
String message = "Failed to render " + loader.name;
if (debugInfo != null) {
message += ", error at " + debugInfo.name + ":" + debugInfo.line;
message += ", error at " + debugInfo.name() + ":" + debugInfo.line();

loader.rewriteStackTrace(e, null, stackTrace);
}
Expand Down
2 changes: 1 addition & 1 deletion jte-runtime/src/main/java/gg/jte/TemplateEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ private TemplateException handleRenderException(String name, Template template,
DebugInfo debugInfo = templateLoader.resolveDebugInfo(classLoader, stackTrace);
String message = "Failed to render " + name;
if (debugInfo != null) {
message += ", error at " + debugInfo.name + ":" + debugInfo.line;
message += ", error at " + debugInfo.name() + ":" + debugInfo.line();

templateLoader.rewriteStackTrace(e, classLoader, stackTrace);
}
Expand Down
10 changes: 1 addition & 9 deletions jte-runtime/src/main/java/gg/jte/runtime/DebugInfo.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
package gg.jte.runtime;

public class DebugInfo {
public final String name;
public final int line;

public DebugInfo(String name, int line) {
this.name = name;
this.line = line;
}
}
public record DebugInfo(String name, int line) {}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class DirectoryWatcher {
private io.methvin.watcher.DirectoryWatcher watcher;

public DirectoryWatcher(TemplateEngine templateEngine, DirectoryCodeResolver codeResolver) {
this(templateEngine, codeResolver.getRoot());
this(templateEngine, codeResolver.root());
}

public DirectoryWatcher(TemplateEngine templateEngine, Path root) {
Expand Down
10 changes: 1 addition & 9 deletions jte/src/main/java/gg/jte/compiler/TemplateParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -1109,15 +1109,7 @@ public int getTemplateLine() {
}
}

private static class Indent {
public final Mode mode;
public final int amount;

public Indent(Mode mode, int amount) {
this.mode = mode;
this.amount = amount;
}
}
private record Indent(Mode mode, int amount) {}

public static class HtmlTag implements gg.jte.html.HtmlTag {

Expand Down
4 changes: 2 additions & 2 deletions jte/src/main/java/gg/jte/compiler/java/JavaCodeGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,13 @@ private void writeBinaryTextPartsConstants(StringBuilder fields) {
@Override
public void onError( String message ) {
DebugInfo debugInfo = getCurrentDebugInfo();
throw new TemplateException("Failed to compile " + debugInfo.name + ", error at line " + debugInfo.line + ": " + message);
throw new TemplateException("Failed to compile " + debugInfo.name() + ", error at line " + debugInfo.line() + ": " + message);
}

@Override
public void onError(String message, int templateLine) {
DebugInfo debugInfo = getDebugInfo(templateLine);
throw new TemplateException("Failed to compile " + debugInfo.name + ", error at line " + debugInfo.line + ": " + message);
throw new TemplateException("Failed to compile " + debugInfo.name() + ", error at line " + debugInfo.line() + ": " + message);
}

@Override
Expand Down
11 changes: 1 addition & 10 deletions jte/src/main/java/gg/jte/resolve/DirectoryCodeResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@
/**
* Resolves template code within a given root directory.
*/
public class DirectoryCodeResolver implements CodeResolver {
private final Path root;

public DirectoryCodeResolver(Path root) {
this.root = root;
}
public record DirectoryCodeResolver(Path root) implements CodeResolver {

@Override
public String resolve(String name) {
Expand Down Expand Up @@ -70,8 +65,4 @@ public List<String> resolveAllTemplateNames() {
throw new UncheckedIOException("Failed to resolve all templates in " + root, e);
}
}

public Path getRoot() {
return root;
}
}

0 comments on commit ee7bda8

Please sign in to comment.