From bdbce01b9a3c45cc7233411519d7d6e7076e6b81 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Mon, 6 Feb 2023 11:14:49 +0100 Subject: [PATCH] metaprogramming: fix crashing instead of fair error reporting --- .../impl/CompositeMethodGenerator.java | 2 +- .../impl/MetaprogrammingDependencyListener.java | 2 +- .../impl/MetaprogrammingImpl.java | 17 +++++++++++++++++ .../metaprogramming/impl/UsageGenerator.java | 6 ++++-- 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/metaprogramming/impl/src/main/java/org/teavm/metaprogramming/impl/CompositeMethodGenerator.java b/metaprogramming/impl/src/main/java/org/teavm/metaprogramming/impl/CompositeMethodGenerator.java index d91c788a9..ae9c10c4d 100644 --- a/metaprogramming/impl/src/main/java/org/teavm/metaprogramming/impl/CompositeMethodGenerator.java +++ b/metaprogramming/impl/src/main/java/org/teavm/metaprogramming/impl/CompositeMethodGenerator.java @@ -119,7 +119,7 @@ public class CompositeMethodGenerator { } CompositeMethodGenerator(VariableContext varContext, Program program) { - this.diagnostics = MetaprogrammingImpl.agent.getDiagnostics(); + this.diagnostics = MetaprogrammingImpl.createDiagnostics(); this.program = program; this.varContext = varContext; } diff --git a/metaprogramming/impl/src/main/java/org/teavm/metaprogramming/impl/MetaprogrammingDependencyListener.java b/metaprogramming/impl/src/main/java/org/teavm/metaprogramming/impl/MetaprogrammingDependencyListener.java index f878493e3..6d58b9d14 100644 --- a/metaprogramming/impl/src/main/java/org/teavm/metaprogramming/impl/MetaprogrammingDependencyListener.java +++ b/metaprogramming/impl/src/main/java/org/teavm/metaprogramming/impl/MetaprogrammingDependencyListener.java @@ -42,7 +42,7 @@ public class MetaprogrammingDependencyListener extends AbstractDependencyListene @Override public void started(DependencyAgent agent) { proxyClassLoader = new MetaprogrammingClassLoader(agent.getClassLoader()); - describer = new MethodDescriber(agent.getDiagnostics(), agent.getClassSource()); + describer = new MethodDescriber(MetaprogrammingImpl.createDiagnostics(), agent.getClassSource()); MetaprogrammingImpl.classLoader = proxyClassLoader; MetaprogrammingImpl.classSource = agent.getClassSource(); diff --git a/metaprogramming/impl/src/main/java/org/teavm/metaprogramming/impl/MetaprogrammingImpl.java b/metaprogramming/impl/src/main/java/org/teavm/metaprogramming/impl/MetaprogrammingImpl.java index a9b25e558..131909b13 100644 --- a/metaprogramming/impl/src/main/java/org/teavm/metaprogramming/impl/MetaprogrammingImpl.java +++ b/metaprogramming/impl/src/main/java/org/teavm/metaprogramming/impl/MetaprogrammingImpl.java @@ -73,6 +73,7 @@ public final class MetaprogrammingImpl { static CompositeMethodGenerator generator; static ValueType returnType; static boolean unsupportedCase; + static boolean error; private MetaprogrammingImpl() { } @@ -405,6 +406,7 @@ public final class MetaprogrammingImpl { public void error(SourceLocation location, String error, Object... params) { convertParams(params); agent.getDiagnostics().error(convertLocation(location), error, params); + MetaprogrammingImpl.error = true; } @Override @@ -438,4 +440,19 @@ public final class MetaprogrammingImpl { : new CallLocation(method.getReference()); } }; + + public static org.teavm.diagnostics.Diagnostics createDiagnostics() { + return new org.teavm.diagnostics.Diagnostics() { + @Override + public void error(CallLocation location, String error, Object... params) { + MetaprogrammingImpl.error = true; + agent.getDiagnostics().error(location, error, params); + } + + @Override + public void warning(CallLocation location, String error, Object... params) { + agent.getDiagnostics().warning(location, error, params); + } + }; + } } diff --git a/metaprogramming/impl/src/main/java/org/teavm/metaprogramming/impl/UsageGenerator.java b/metaprogramming/impl/src/main/java/org/teavm/metaprogramming/impl/UsageGenerator.java index d0f2def74..8abe5f9f3 100644 --- a/metaprogramming/impl/src/main/java/org/teavm/metaprogramming/impl/UsageGenerator.java +++ b/metaprogramming/impl/src/main/java/org/teavm/metaprogramming/impl/UsageGenerator.java @@ -131,7 +131,7 @@ class UsageGenerator { String suffix = getSuffix(); implRef = buildMethodReference(suffix); MetaprogrammingImpl.templateMethod = model.getMetaMethod(); - VariableContext varContext = new TopLevelVariableContext(diagnostics); + VariableContext varContext = new TopLevelVariableContext(MetaprogrammingImpl.createDiagnostics()); MetaprogrammingImpl.generator = new CompositeMethodGenerator(varContext); MetaprogrammingImpl.varContext = varContext; MetaprogrammingImpl.returnType = model.getMethod().getReturnType(); @@ -154,6 +154,7 @@ class UsageGenerator { } MetaprogrammingImpl.unsupportedCase = false; + MetaprogrammingImpl.error = false; try { proxyMethod.invoke(null, proxyArgs); @@ -162,10 +163,11 @@ class UsageGenerator { e.printStackTrace(new PrintWriter(writer)); diagnostics.error(location, "Error calling proxy method {{m0}}: " + writer.toString(), model.getMetaMethod()); + MetaprogrammingImpl.error = true; } MetaprogrammingImpl.close(); - if (MetaprogrammingImpl.unsupportedCase) { + if (MetaprogrammingImpl.unsupportedCase || MetaprogrammingImpl.error) { return; }