Skip to content

Commit 665c838

Browse files
committed
Fixed bug in ExceptionWrapperInterceptor
Fixed bug in ExceptionWrapperInterceptor that caused to wrap instances of the wrapper into a new wrapper. Fix comment issue with code style
1 parent 2728852 commit 665c838

File tree

1 file changed

+90
-79
lines changed

1 file changed

+90
-79
lines changed

blaze-ee-utils/src/main/java/com/blazebit/cdi/exception/ExceptionWrappingInterceptor.java

+90-79
Original file line numberDiff line numberDiff line change
@@ -23,39 +23,32 @@
2323
* declared in the ExceptionWrap annotation. It ignores exceptions which are
2424
* declared in the throws clause of the method for which this interceptor runs.
2525
*
26+
* <pre>
2627
* <code>
2728
* public class Bean implements Serializable {
2829
*
29-
* @ExceptionWrapping({
30-
* @ExceptionWrap(source=RuntimeExceptionA.class, wrapper=MyExceptionA.class),
31-
* @ExceptionWrap(source=RuntimeExceptionB.class, wrapper=MyExceptionB.class),
32-
* @ExceptionWrap(wrapper=MyExceptionC.class) }) public void example() throws
33-
* MyExceptionA, MyExceptionB,
34-
* MyExceptionC{ // code } } </code>
30+
* @ExceptionWrapping(value = {
31+
* @ExceptionWrap(source = RuntimeExceptionA.class, wrapper = MyExceptionA.class),
32+
* @ExceptionWrap(source = RuntimeExceptionB.class, wrapper = MyExceptionB.class),
33+
* @ExceptionWrap(wrapper = MyExceptionC.class) })
34+
* public void example() throws MyExceptionA, MyExceptionB, MyExceptionC {
35+
* // code
36+
* }
37+
* }
38+
* </code>
39+
* </pre>
3540
*
36-
* RuntimeExceptionA will be wrapped
37-
* into MyExceptionA
38-
* RuntimeExceptionB will be wrapped
39-
* into MyExceptionB Every other
40-
* Exception, that is not instance of
41-
* MyExceptionA, MyExceptionB or
42-
* MyExceptionC, will be wrapped into
43-
* MyExceptionC
41+
* RuntimeExceptionA will be wrapped into MyExceptionA RuntimeExceptionB will be
42+
* wrapped into MyExceptionB Every other Exception, that is not instance of
43+
* MyExceptionA, MyExceptionB or MyExceptionC, will be wrapped into MyExceptionC
4444
*
45-
* The order of ExceptionWrap
46-
* annotations within the
47-
* ExceptionWrapping annotation is
48-
* important and should be declared
49-
* from specific to general.
45+
* The order of ExceptionWrap annotations within the ExceptionWrapping
46+
* annotation is important and should be declared from specific to general.
5047
*
51-
* If you would specify
52-
* @ExceptionWrap(
53-
* wrapper=MyExceptionC.class) as the
54-
* first element of the
55-
* ExceptionWrapping annotation, then
56-
* every exception that is not
57-
* declared in the throws clause will
58-
* be wrapped into a MyExceptionC!
48+
* If you would specify <code>@ExceptionWrap( wrapper=MyExceptionC.class)</code>
49+
* as the first element of the ExceptionWrapping annotation, then every
50+
* exception that is not declared in the throws clause will be wrapped into a
51+
* MyExceptionC!
5952
*
6053
* @since 0.1.2
6154
* @author Christian Beikov
@@ -71,14 +64,16 @@ public class ExceptionWrappingInterceptor implements Serializable {
7164

7265
@AroundInvoke
7366
public Object errorLogging(InvocationContext ic) throws Exception {
74-
// Retrieve the logger for the intercepted class
67+
/* Retrieve the logger for the intercepted class */
7568
Logger log = Logger.getLogger(ic.getTarget().getClass().getName());
7669
Method m = ic.getMethod();
7770
Object targetObject = ic.getTarget();
7871
Class<?> targetClass = targetObject == null ? m.getDeclaringClass()
7972
: targetObject.getClass();
80-
// Retrieve the exceptions declared in the throws clause of the
81-
// intercepted method
73+
/*
74+
* Retrieve the exceptions declared in the throws clause of the
75+
* intercepted method
76+
*/
8277
Class<?>[] declaredExceptions = m.getExceptionTypes();
8378
ExceptionWrapping wrappingAnnotation = AnnotationUtil.findAnnotation(m,
8479
targetClass, ExceptionWrapping.class);
@@ -93,87 +88,103 @@ public Object errorLogging(InvocationContext ic) throws Exception {
9388

9489
wraps = wrappingAnnotation.value();
9590

96-
// Only try to do wrapping when exceptionWrap elements are available
91+
/* Only try to do wrapping when exceptionWrap elements are available */
9792
doWrapping = wraps != null && wraps.length > 0;
9893

9994
try {
10095
ret = ic.proceed();
10196
} catch (Throwable t) {
102-
// Check if the throwable is an instance of
103-
// InvocationTargetException
104-
// and if so, unwrap the cause. OWB did not unwrap exceptions that
105-
// have been thrown in decorators in some versions so we need to do
106-
// this to be able to handle the right exception
97+
/*
98+
* Check if the throwable is an instance of
99+
* InvocationTargetException and if so, unwrap the cause. OWB did
100+
* not unwrap exceptions that have been thrown in decorators in some
101+
* versions so we need to do this to be able to handle the right
102+
* exception
103+
*/
107104
Throwable t1 = ExceptionUtil.unwrapInvocationTargetException(t);
108105

109106
if (doWrapping) {
110-
// Check if the exception that was thrown is declared in the
111-
// throws
112-
// clause and if so, don't apply exception wrapping
107+
/*
108+
* Check if the exception that was thrown is declared in the
109+
* throws clause and if so, don't apply exception wrapping
110+
*/
113111
for (Class<?> declaredException : declaredExceptions) {
114112
if (declaredException.isInstance(t1)) {
115-
// Do this so no exception wrapper is applied
113+
/* Do this so no exception wrapper is applied */
116114
doWrapping = false;
117115
break;
118116
}
119117
}
120118

121119
if (doWrapping) {
122-
// If a wrapping should be applied, iterate through
123-
// exception
124-
// wraps and check if the thrown exception is an instance of
125-
// a declared source class.
120+
/*
121+
* If a wrapping should be applied, iterate through
122+
* exception wraps and check if the thrown exception is an
123+
* instance of a declared source class.
124+
*/
126125
for (ExceptionWrap wrap : wraps) {
127126
Class<? extends Throwable>[] sourceClasses = wrap
128127
.sources();
129128
Class<? extends Exception> wrapperClass = wrap
130129
.wrapper();
131130
Exception e = null;
132131

133-
for (Class<? extends Throwable> source : sourceClasses) {
134-
// When the thrown exception is instance of an
135-
// exception
136-
// wrap source class, create a new exception
137-
// instance
138-
// with the thrown exception as cause
139-
if (source.isInstance(t1)) {
140-
try {
141-
e = wrapperClass.getConstructor(
142-
Throwable.class).newInstance(t1);
143-
} catch (Throwable t2) {
144-
// Declared wrapper exception has no
145-
// constructor
146-
// with a throwable as param
147-
Exception ex1 = null;
148-
149-
// Cast to or wrap the throwable into a new
150-
// exception because we may not throw
151-
// Throwable
152-
// instances within here
153-
if (t2 instanceof Exception) {
154-
ex1 = (Exception) t2;
155-
} else {
156-
ex1 = new Exception(t2);
132+
/*
133+
* Only do wrapping if the exception isn't already a
134+
* instance of the wrapper exceptionF
135+
*/
136+
if (!wrapperClass.isInstance(t1)) {
137+
for (Class<? extends Throwable> source : sourceClasses) {
138+
/*
139+
* When the thrown exception is instance of an
140+
* exception wrap source class, create a new
141+
* exception instance with the thrown exception
142+
* as cause
143+
*/
144+
if (source.isInstance(t1)) {
145+
try {
146+
e = wrapperClass.getConstructor(
147+
Throwable.class)
148+
.newInstance(t1);
149+
} catch (Throwable t2) {
150+
/*
151+
* Declared wrapper exception has no
152+
* constructor with a throwable as param
153+
*/
154+
Exception ex1 = null;
155+
156+
/*
157+
* Cast to or wrap the throwable into a
158+
* new exception because we may not
159+
* throw Throwable instances within here
160+
*/
161+
if (t2 instanceof Exception) {
162+
ex1 = (Exception) t2;
163+
} else {
164+
ex1 = new Exception(t2);
165+
}
166+
167+
log.log(Level.WARNING,
168+
"The applied wrapper exception on the method "
169+
+ m.getName()
170+
+ " has no constructor for type Throwable!",
171+
ex1);
157172
}
158173

159-
log.log(Level.WARNING,
160-
"The applied wrapper exception on the method "
161-
+ m.getName()
162-
+ " has no constructor for type Throwable!",
163-
ex1);
164-
}
165-
166-
if (e != null) {
167-
throw e;
174+
if (e != null) {
175+
throw e;
176+
}
168177
}
169178
}
170179
}
171180
}
172181
}
173182
}
174183

175-
// No exception wrapping was applied, so cast to or wrap the
176-
// throwable into a new exception and throw it
184+
/*
185+
* No exception wrapping was applied, so cast to or wrap the
186+
* throwable into a new exception and throw it
187+
*/
177188
if (t1 instanceof Exception) {
178189
throw (Exception) t1;
179190
}

0 commit comments

Comments
 (0)