You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by cl...@apache.org on 2022/05/12 15:14:04 UTC
[activemq-artemis] branch new-logging updated: Adding support for exception on LogProcessor
This is an automated email from the ASF dual-hosted git repository.
clebertsuconic pushed a commit to branch new-logging
in repository https://gitbox.apache.org/repos/asf/activemq-artemis.git
The following commit(s) were added to refs/heads/new-logging by this push:
new 8998c16d86 Adding support for exception on LogProcessor
8998c16d86 is described below
commit 8998c16d86c1abbb53e16bc3a609a5b6b88905ab
Author: Clebert Suconic <cl...@apache.org>
AuthorDate: Thu May 12 11:13:39 2022 -0400
Adding support for exception on LogProcessor
---
.../artemis/logprocessor/LogProcessor.java | 37 +++++++++++++++++++---
.../activemq/artemis/logprocessor/MyException.java | 31 ++++++++++++++++++
.../artemis/logprocessor/SimpleBundle.java | 3 ++
.../artemis/logprocessor/SimpleBundleTest.java | 10 ++++++
4 files changed, 76 insertions(+), 5 deletions(-)
diff --git a/artemis-log-processor/src/main/java/org/apache/activemq/artemis/logprocessor/LogProcessor.java b/artemis-log-processor/src/main/java/org/apache/activemq/artemis/logprocessor/LogProcessor.java
index ca31b03177..986ea09cc4 100644
--- a/artemis-log-processor/src/main/java/org/apache/activemq/artemis/logprocessor/LogProcessor.java
+++ b/artemis-log-processor/src/main/java/org/apache/activemq/artemis/logprocessor/LogProcessor.java
@@ -27,13 +27,17 @@ import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
+import javax.lang.model.type.DeclaredType;
+import javax.lang.model.type.TypeMirror;
import javax.tools.Diagnostic;
import javax.tools.JavaFileObject;
import java.io.PrintWriter;
+import java.lang.reflect.Parameter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
+import com.sun.jdi.ClassType;
import org.apache.activemq.artemis.logprocessor.annotation.LogBundle;
import org.apache.activemq.artemis.logprocessor.annotation.GetLogger;
import org.apache.activemq.artemis.logprocessor.annotation.LogMessage;
@@ -174,6 +178,8 @@ public class LogProcessor extends AbstractProcessor {
boolean hasParameters = false;
+
+ VariableElement exceptionParameter = null;
// the one that will be used on the call
StringBuffer callList = new StringBuffer();
while (parameters.hasNext()) {
@@ -185,6 +191,13 @@ public class LogProcessor extends AbstractProcessor {
writerOutput.write(", ");
callList.append(",");
}
+
+ if (isException(parameter.asType())) {
+ if (exceptionParameter != null) {
+ throw new IllegalStateException("messageAnnotation " + messageAnnotation.value() + " has two exceptions defined on the method");
+ }
+ exceptionParameter = parameter;
+ }
}
// the real implementation
@@ -202,17 +215,31 @@ public class LogProcessor extends AbstractProcessor {
if (executableMember.getReturnType().toString().equals(String.class.getName())) {
writerOutput.println(" return returnString;");
} else {
- // TODO: where the return type is actually an exception type, and a given arg was originally a Cause,
- // it used to be set as the created exception cause, but now is being used in the formatter above incorrectly.
-
- // TODO: where the return is an exception, the stacktrace will be different than before unless adjusted (as existing bits did).
- writerOutput.println(" return new " + executableMember.getReturnType().toString() + "(returnString);");
+ if (exceptionParameter != null) {
+ writerOutput.println(" return new " + executableMember.getReturnType().toString() + "(returnString, " + exceptionParameter.getSimpleName() + ");");
+ } else {
+ writerOutput.println(" return new " + executableMember.getReturnType().toString() + "(returnString);");
+ }
}
writerOutput.println(" }");
writerOutput.println();
}
+ boolean isException(TypeMirror parameter) {
+ if (parameter.toString().equals("java.lang.Throwable")) {
+ return true;
+ }
+ if (parameter instanceof DeclaredType) {
+ DeclaredType declaredType = (DeclaredType) parameter;
+ if (declaredType.asElement() instanceof TypeElement) {
+ TypeElement theElement = (TypeElement) declaredType.asElement();
+ return isException(theElement.getSuperclass());
+ }
+ }
+ return false;
+ }
+
private String encodeSpecialChars(String input) {
return input.replaceAll("\n", "\\\\n").replaceAll("\"", "\\\\\"");
}
diff --git a/artemis-log-processor/src/test/java/org/apache/activemq/artemis/logprocessor/MyException.java b/artemis-log-processor/src/test/java/org/apache/activemq/artemis/logprocessor/MyException.java
new file mode 100644
index 0000000000..d9551c3d85
--- /dev/null
+++ b/artemis-log-processor/src/test/java/org/apache/activemq/artemis/logprocessor/MyException.java
@@ -0,0 +1,31 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.activemq.artemis.logprocessor;
+
+public class MyException extends Exception {
+
+ public MyException(String error) {
+ super(error);
+ }
+
+ public MyException(String error, Exception cause) {
+ super(error, cause);
+ }
+
+
+}
diff --git a/artemis-log-processor/src/test/java/org/apache/activemq/artemis/logprocessor/SimpleBundle.java b/artemis-log-processor/src/test/java/org/apache/activemq/artemis/logprocessor/SimpleBundle.java
index 152a71567b..255df469dd 100644
--- a/artemis-log-processor/src/test/java/org/apache/activemq/artemis/logprocessor/SimpleBundle.java
+++ b/artemis-log-processor/src/test/java/org/apache/activemq/artemis/logprocessor/SimpleBundle.java
@@ -46,4 +46,7 @@ public interface SimpleBundle {
@LogMessage(id = 7, value = "multi\nLine\nMessage", level = LogMessage.Level.WARN)
void multiLines();
+
+ @Message(id = 8, value = "EX{}")
+ MyException someExceptionWithCause(String message, Exception myCause);
}
diff --git a/artemis-log-processor/src/test/java/org/apache/activemq/artemis/logprocessor/SimpleBundleTest.java b/artemis-log-processor/src/test/java/org/apache/activemq/artemis/logprocessor/SimpleBundleTest.java
index 063d9a48d6..f38bc61175 100644
--- a/artemis-log-processor/src/test/java/org/apache/activemq/artemis/logprocessor/SimpleBundleTest.java
+++ b/artemis-log-processor/src/test/java/org/apache/activemq/artemis/logprocessor/SimpleBundleTest.java
@@ -59,4 +59,14 @@ public class SimpleBundleTest {
public void testMultiLines() {
SimpleBundle.MESSAGES.multiLines();
}
+
+
+ @Test
+ public void testWithException() {
+ Exception myCause = new Exception("this is myCause");
+ MyException myException = SimpleBundle.MESSAGES.someExceptionWithCause("1", myCause);
+ Assert.assertEquals("TST8 EX1", myException.getMessage());
+ Assert.assertSame(myCause, myException.getCause());
+
+ }
}