Debian Codegraph @

src/main/java/com/jcabi/aspects/aj/SingleException.java

raw
1 /**
2 * Copyright (c) 2012-2017, jcabi.com
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met: 1) Redistributions of source code must retain the above
8 * copyright notice, this list of conditions and the following
9 * disclaimer. 2) Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution. 3) Neither the name of the jcabi.com nor
13 * the names of its contributors may be used to endorse or promote
14 * products derived from this software without specific prior written
15 * permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 package com.jcabi.aspects.aj;
31
32 import com.jcabi.aspects.Immutable;
33 import com.jcabi.aspects.UnitedThrow;
34 import java.lang.reflect.Constructor;
35 import java.lang.reflect.Method;
36 import org.aspectj.lang.ProceedingJoinPoint;
37 import org.aspectj.lang.annotation.Around;
38 import org.aspectj.lang.annotation.Aspect;
39 import org.aspectj.lang.reflect.MethodSignature;
40
41 /**
42 * Throw single exception out of method.
43 *
44 * @author Krzysztof Krason ([email protected])
45 * @version $Id: e93dd0c656d9e2dfc3150fd4c81198f4965bca71 $
46 * @since 0.13
47 */
48 @Aspect
49 @Immutable
50 public final class SingleException {
51
52 /**
53 * Catch all exceptions and throw a single selected exception.
54 *
55 * <p>Try NOT to change the signature of this method, in order to keep
56 * it backward compatible.
57 *
58 * @param point Joint point
59 * @return The result of call
60 * @throws Throwable If something goes wrong inside
61 */
62 @Around
63 (
64 // @checkstyle StringLiteralsConcatenation (2 lines)
65 "execution(* * (..))"
66 + " && @annotation(com.jcabi.aspects.UnitedThrow)"
67 )
68 @SuppressWarnings("PMD.AvoidCatchingThrowable")
69 // @checkstyle IllegalThrowsCheck (1 line)
70 public Object wrap(final ProceedingJoinPoint point) throws Throwable {
71 final Method method =
72 MethodSignature.class.cast(point.getSignature()).getMethod();
73 final UnitedThrow annot = method.getAnnotation(UnitedThrow.class);
74 final Class<? extends Throwable> clz = this.clazz(method, annot);
75 try {
76 return point.proceed();
77 // @checkstyle IllegalCatch (1 line)
78 } catch (final Throwable ex) {
79 Throwable throwable = ex;
80 if (!clz.isAssignableFrom(ex.getClass())) {
81 if (this.exists(clz)) {
82 throwable = clz.getConstructor(Throwable.class)
83 .newInstance(ex);
84 } else {
85 throwable = clz.newInstance();
86 }
87 }
88 throw throwable;
89 }
90 }
91
92 /**
93 * Check if there is a constructor with single Throwable argument.
94 * @param clz Class to check.
95 * @return Whether constructor exists.
96 */
97 private boolean exists(final Class<? extends Throwable> clz) {
98 boolean found = false;
99 for (final Constructor<?> ctr : clz.getConstructors()) {
100 if ((ctr.getParameterTypes().length == 1)
101 && (ctr.getParameterTypes()[0] == Throwable.class)) {
102 found = true;
103 break;
104 }
105 }
106 return found;
107 }
108
109 /**
110 * Get required exception class.
111 * @param method Method declaring exception.
112 * @param annot UnitedThrow annotation.
113 * @return Class of exception.
114 */
115 @SuppressWarnings("unchecked")
116 private Class<? extends Throwable> clazz(final Method method,
117 final UnitedThrow annot) {
118 Class<? extends Throwable> clz = annot.value();
119 if (clz == UnitedThrow.None.class) {
120 if (method.getExceptionTypes().length == 0) {
121 clz = IllegalStateException.class;
122 } else {
123 clz = (Class<? extends Throwable>) method
124 .getExceptionTypes()[0];
125 }
126 }
127 return clz;
128 }
129 }