23
23
* declared in the ExceptionWrap annotation. It ignores exceptions which are
24
24
* declared in the throws clause of the method for which this interceptor runs.
25
25
*
26
+ * <pre>
26
27
* <code>
27
28
* public class Bean implements Serializable {
28
29
*
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>
35
40
*
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
44
44
*
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.
50
47
*
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!
59
52
*
60
53
* @since 0.1.2
61
54
* @author Christian Beikov
@@ -71,14 +64,16 @@ public class ExceptionWrappingInterceptor implements Serializable {
71
64
72
65
@ AroundInvoke
73
66
public Object errorLogging (InvocationContext ic ) throws Exception {
74
- // Retrieve the logger for the intercepted class
67
+ /* Retrieve the logger for the intercepted class */
75
68
Logger log = Logger .getLogger (ic .getTarget ().getClass ().getName ());
76
69
Method m = ic .getMethod ();
77
70
Object targetObject = ic .getTarget ();
78
71
Class <?> targetClass = targetObject == null ? m .getDeclaringClass ()
79
72
: 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
+ */
82
77
Class <?>[] declaredExceptions = m .getExceptionTypes ();
83
78
ExceptionWrapping wrappingAnnotation = AnnotationUtil .findAnnotation (m ,
84
79
targetClass , ExceptionWrapping .class );
@@ -93,87 +88,103 @@ public Object errorLogging(InvocationContext ic) throws Exception {
93
88
94
89
wraps = wrappingAnnotation .value ();
95
90
96
- // Only try to do wrapping when exceptionWrap elements are available
91
+ /* Only try to do wrapping when exceptionWrap elements are available */
97
92
doWrapping = wraps != null && wraps .length > 0 ;
98
93
99
94
try {
100
95
ret = ic .proceed ();
101
96
} 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
+ */
107
104
Throwable t1 = ExceptionUtil .unwrapInvocationTargetException (t );
108
105
109
106
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
+ */
113
111
for (Class <?> declaredException : declaredExceptions ) {
114
112
if (declaredException .isInstance (t1 )) {
115
- // Do this so no exception wrapper is applied
113
+ /* Do this so no exception wrapper is applied */
116
114
doWrapping = false ;
117
115
break ;
118
116
}
119
117
}
120
118
121
119
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
+ */
126
125
for (ExceptionWrap wrap : wraps ) {
127
126
Class <? extends Throwable >[] sourceClasses = wrap
128
127
.sources ();
129
128
Class <? extends Exception > wrapperClass = wrap
130
129
.wrapper ();
131
130
Exception e = null ;
132
131
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 );
157
172
}
158
173
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
+ }
168
177
}
169
178
}
170
179
}
171
180
}
172
181
}
173
182
}
174
183
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
+ */
177
188
if (t1 instanceof Exception ) {
178
189
throw (Exception ) t1 ;
179
190
}
0 commit comments