This is an automated email from the ASF dual-hosted git repository.
rmannibucau pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/openwebbeans.git
The following commit(s) were added to refs/heads/master by this push:
new 1a2ecea OWB-1303 ensure user exception in postconstruct are preserved
1a2ecea is described below
commit 1a2eceafaf3b102010a6c0597c81085aca8c927e
Author: Romain Manni-Bucau <[email protected]>
AuthorDate: Wed Dec 4 14:14:14 2019 +0100
OWB-1303 ensure user exception in postconstruct are preserved
---
.../apache/webbeans/component/AbstractOwbBean.java | 40 ++++++----
.../test/lifecycle/ExceptionOnCallbackTest.java | 90 ++++++++++++++++++++++
2 files changed, 114 insertions(+), 16 deletions(-)
diff --git
a/webbeans-impl/src/main/java/org/apache/webbeans/component/AbstractOwbBean.java
b/webbeans-impl/src/main/java/org/apache/webbeans/component/AbstractOwbBean.java
index a4a0778..7b487f6 100644
---
a/webbeans-impl/src/main/java/org/apache/webbeans/component/AbstractOwbBean.java
+++
b/webbeans-impl/src/main/java/org/apache/webbeans/component/AbstractOwbBean.java
@@ -23,6 +23,7 @@ import org.apache.webbeans.config.OWBLogConst;
import org.apache.webbeans.config.WebBeansContext;
import org.apache.webbeans.container.SerializableBean;
import org.apache.webbeans.context.creational.CreationalContextImpl;
+import org.apache.webbeans.exception.WebBeansException;
import org.apache.webbeans.logger.WebBeansLoggerFacade;
import javax.enterprise.context.Dependent;
@@ -34,8 +35,10 @@ import javax.enterprise.inject.spi.PassivationCapable;
import javax.enterprise.inject.spi.Producer;
import java.io.Serializable;
import java.lang.annotation.Annotation;
+import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
+import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -134,25 +137,30 @@ public abstract class AbstractOwbBean<T>
}
catch (Exception re)
{
- Throwable throwable = getRootException(re);
-
- if(!(throwable instanceof RuntimeException))
+ Throwable current = re;
+ Set<Throwable> visited = new HashSet<>();
+ while (current instanceof InvocationTargetException || current
instanceof WebBeansException)
{
- throw new CreationException(throwable);
+ Throwable cause = current.getCause();
+ if (visited.add(cause))
+ {
+ current = cause;
+ }
+ else
+ {
+ break;
+ }
}
- throw (RuntimeException) throwable;
- }
-
- }
-
- private Throwable getRootException(Throwable throwable)
- {
- Throwable current = throwable;
- while (current.getCause() != null && current.getCause() != current)
- {
- current = current.getCause();
+ if(current != null && !(current instanceof RuntimeException))
+ {
+ throw new CreationException(current);
+ }
+ if (current == null) // just a guard but highly unlikely
+ {
+ throw new CreationException(re);
+ }
+ throw (RuntimeException) current;
}
- return current;
}
/*
diff --git
a/webbeans-impl/src/test/java/org/apache/webbeans/test/lifecycle/ExceptionOnCallbackTest.java
b/webbeans-impl/src/test/java/org/apache/webbeans/test/lifecycle/ExceptionOnCallbackTest.java
new file mode 100644
index 0000000..61ee2dc
--- /dev/null
+++
b/webbeans-impl/src/test/java/org/apache/webbeans/test/lifecycle/ExceptionOnCallbackTest.java
@@ -0,0 +1,90 @@
+/*
+ * 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
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.webbeans.test.lifecycle;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+import javax.annotation.PostConstruct;
+import javax.enterprise.context.ApplicationScoped;
+import javax.inject.Inject;
+
+import org.apache.webbeans.test.AbstractUnitTest;
+import org.junit.Test;
+
+public class ExceptionOnCallbackTest extends AbstractUnitTest {
+ @Test
+ public void exceptionOnPostConstruct() {
+ startContainer(MainRunnable.class, ClassUsingThrowingRepository.class,
ThrowingRepository.class);
+ final MainRunnable instance = getInstance(MainRunnable.class);
+ instance.run();
+ assertEquals("Wrapping cause.", instance.getError().getMessage());
+ }
+
+ @ApplicationScoped
+ public static class ClassUsingThrowingRepository {
+ @Inject
+ ThrowingRepository repo;
+
+ public Object get() {
+ return repo.get();
+ }
+ }
+
+ @ApplicationScoped
+ public static class ThrowingRepository {
+ private Object fancyObjectWhichNeverSeesTheLightOfDay;
+
+ @PostConstruct
+ void init() {
+ fancyObjectWhichNeverSeesTheLightOfDay = getFancyObject();
+ }
+
+ private Object getFancyObject() {
+ throw new RuntimeException(
+ "Wrapping cause.",
+ new NullPointerException("Null pointer exception in
initilization of throwing repo."));
+ }
+
+ public Object get() {
+ return fancyObjectWhichNeverSeesTheLightOfDay;
+ }
+ }
+
+ @ApplicationScoped
+ public static class MainRunnable implements Runnable {
+ @Inject
+ ClassUsingThrowingRepository repoUsingClass;
+
+ private RuntimeException error;
+
+ @Override
+ public void run() {
+ try {
+ repoUsingClass.get();
+ } catch (final RuntimeException exc) {
+ error = exc;
+ }
+ }
+
+ public RuntimeException getError() {
+ return error;
+ }
+ }
+}