Skip to content

Commit

Permalink
Merge pull request #432 from SebastianSchlecht/java-compiler-error-ma…
Browse files Browse the repository at this point in the history
…tching

Java compiler error matching
  • Loading branch information
casid authored Mar 3, 2025
2 parents dbecede + 7cd0a95 commit f0b6fa2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
16 changes: 10 additions & 6 deletions jte/src/main/java/gg/jte/compiler/java/JavaClassCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class JavaClassCompiler implements ClassCompiler {
@Override
Expand Down Expand Up @@ -53,13 +55,15 @@ private void runCompiler(String[] args, Path classDirectory, Map<String, ClassIn
private static String getErrorMessage(String errors, Path classDirectory, Map<String, ClassInfo> templateByClassName) {
try {
String absolutePath = classDirectory.toAbsolutePath().toString();
int classBeginIndex = errors.indexOf(absolutePath) + absolutePath.length() + 1;
int classEndIndex = errors.indexOf(".java:");
String className = errors.substring(classBeginIndex, classEndIndex).replace(File.separatorChar, '.');
//Pattern matches '<absolutePath><separatorChar><relativeTemplatePath>.java:<Line>: error'
Pattern pattern = Pattern.compile("^\\Q%s%s\\E(?<ClassName>.*?)\\.java:(?<LineNumber>\\d+?): error".formatted(absolutePath, File.separatorChar), Pattern.MULTILINE);
Matcher matcher = pattern.matcher(errors);
if (!matcher.find()) {
return "Failed to compile template, error at\n" + errors;
}

int lineStartIndex = classEndIndex + 6;
int lineEndIndex = errors.indexOf(':', lineStartIndex);
int javaLine = Integer.parseInt(errors.substring(lineStartIndex, lineEndIndex));
String className = matcher.group("ClassName").replace(File.separatorChar, '.');
int javaLine = Integer.parseInt(matcher.group("LineNumber"));

ClassInfo templateInfo = templateByClassName.get(className);
int templateLine = templateInfo.lineInfo[javaLine - 1] + 1;
Expand Down
23 changes: 23 additions & 0 deletions jte/src/test/java/gg/jte/TemplateEngineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ void helloWorld() {
thenOutputIs("Hello World");
}

@Test
void helloWorldWithDeprecatedMethodCall() {
givenRawTemplate("@param gg.jte.TemplateEngineTest.Model model\n${model.getAnotherWorld()} ${model.deprecatedMethod()}");
thenOutputIs("Another World true");
}


@Test
void templateWithoutParameters() {
givenRawTemplate("Hello World!");
Expand Down Expand Up @@ -1471,6 +1478,17 @@ void compileError6() {
.hasMessage("Failed to compile template, error at test/template.jte:3. No parameter with name param2 is defined in test.jte");
}

@Test
void compileErrorWithWarning() {
givenTemplate("test.jte", "@param gg.jte.TemplateEngineTest.Model model\n${model.deprecatedMethod()}\nThis will not compile!\n${model.helloUnknown}\n!!");
givenTemplate("@template.test(model)");
thenRenderingFailsWithException()
.hasMessageStartingWith("Failed to compile template, error at test.jte:4\n")
.hasMessageContaining("cannot find symbol")
.hasMessageContaining("model.helloUnknown")
.hasMessageContaining("has been deprecated");
}

@Test
void calledWithWrongParam1() {
givenRawTemplate("@param String hello\n${hello}");
Expand Down Expand Up @@ -1569,6 +1587,8 @@ private void thenOutputIs(String expected) {
return assertThat(throwable).isInstanceOf(TemplateException.class);
}



@SuppressWarnings("SameParameterValue")
private void thenRenderingFailsWithExceptionCausedBy(Class<? extends Throwable> clazz) {
thenRenderingFailsWithException().hasCauseInstanceOf(clazz);
Expand Down Expand Up @@ -1624,5 +1644,8 @@ public void setHello(String hello) {
public String getThatThrows() {
throw new NullPointerException("Oops");
}

@Deprecated(forRemoval = true)
public boolean deprecatedMethod() { return true; }
}
}

0 comments on commit f0b6fa2

Please # to comment.