Debian Codegraph @

src/main/java/com/jcabi/aspects/aj/Mnemos.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.log.Logger;
34 import java.lang.reflect.Method;
35 import java.util.Arrays;
36 import org.aspectj.lang.ProceedingJoinPoint;
37 import org.aspectj.lang.reflect.MethodSignature;
38
39 /**
40 * Utility class with text functions for making mnemos.
41 *
42 * @author Yegor Bugayenko ([email protected])
43 * @version $Id: 2dd60bfada5dc6199bf4e994079e56238e091209 $
44 */
45 @Immutable
46 @SuppressWarnings({ "PMD.TooManyMethods", "PMD.AvoidUsingShortType" })
47 final class Mnemos {
48
49 /**
50 * Comma between elements.
51 */
52 private static final String COMMA = ", ";
53
54 /**
55 * Dots that skip.
56 */
57 private static final String DOTS = "...";
58
59 /**
60 * Private ctor, it's a utility class.
61 */
62 private Mnemos() {
63 // intentionally empty
64 }
65
66 /**
67 * Make a string out of point.
68 * @param point The point
69 * @param trim Shall we trim long texts?
70 * @param skip Shall we skip details and output just dots?
71 * @param logthis Shall we add toString result to log?
72 * @return Text representation of it
73 * @since 0.8.1
74 * @checkstyle ParameterNumber (3 lines)
75 */
76 public static String toText(final ProceedingJoinPoint point,
77 final boolean trim, final boolean skip, final boolean logthis) {
78 final String additional;
79 if (logthis && (point.getThis() != null)) {
80 additional = point.getThis().toString();
81 } else {
82 additional = "";
83 }
84 return Mnemos.toText(
85 MethodSignature.class.cast(point.getSignature()).getMethod(),
86 point.getArgs(), additional,
87 trim, skip
88 );
89 }
90 /**
91 * Make a string out of point.
92 * @param point The point
93 * @param trim Shall we trim long texts?
94 * @param skip Shall we skip details and output just dots?
95 * @return Text representation of it
96 * @since 0.7.19
97 */
98 public static String toText(final ProceedingJoinPoint point,
99 final boolean trim, final boolean skip) {
100 return Mnemos.toText(
101 MethodSignature.class.cast(point.getSignature()).getMethod(),
102 point.getArgs(),
103 trim, skip
104 );
105 }
106
107 /**
108 * Make a string out of point.
109 * @param point The point
110 * @param trim Shall we trim long texts?
111 * @return Text representation of it
112 * @deprecated Use toText(Method,Object,boolean,boolean) instead
113 */
114 @Deprecated
115 public static String toText(final ProceedingJoinPoint point,
116 final boolean trim) {
117 return Mnemos.toText(point, trim, false);
118 }
119
120 /**
121 * Make a string out of point.
122 * @param point The point
123 * @param trim Shall we trim long texts?
124 * @return Text representation of it
125 * @deprecated Use toText() instead
126 */
127 @Deprecated
128 public static String toString(final ProceedingJoinPoint point,
129 final boolean trim) {
130 return Mnemos.toText(point, trim, false);
131 }
132
133 /**
134 * Make a string out of method.
135 * @param method The method
136 * @param args Actual arguments of the method
137 * @param additional Additional text to add before log line
138 * @param trim Shall we trim long texts?
139 * @param skip Shall we skip details and output just dots?
140 * @return Text representation of it
141 * @since 0.8.1
142 * @checkstyle ParameterNumber (4 lines)
143 */
144 public static String toText(final Method method, final Object[] args,
145 final String additional, final boolean trim, final boolean skip) {
146 final StringBuilder log = new StringBuilder();
147 if (additional != null) {
148 log.append(additional);
149 }
150 log.append('#').append(method.getName()).append('(');
151 if (skip) {
152 log.append(Mnemos.DOTS);
153 } else {
154 for (int pos = 0; pos < args.length; ++pos) {
155 if (pos > 0) {
156 log.append(Mnemos.COMMA);
157 }
158 log.append(Mnemos.toText(args[pos], trim, false));
159 }
160 }
161 log.append(')');
162 return log.toString();
163 }
164
165 /**
166 * Make a string out of method.
167 * @param method The method
168 * @param args Actual arguments of the method
169 * @param trim Shall we trim long texts?
170 * @param skip Shall we skip details and output just dots?
171 * @return Text representation of it
172 * @since 0.7.19
173 * @checkstyle ParameterNumber (4 lines)
174 */
175 public static String toText(final Method method, final Object[] args,
176 final boolean trim, final boolean skip) {
177 return Mnemos.toText(method, args, "", trim, skip);
178 }
179
180 /**
181 * Make a string out of method.
182 * @param method The method
183 * @param args Actual arguments of the method
184 * @param trim Shall we trim long texts?
185 * @return Text representation of it
186 * @deprecated Use toText(Method,Object,boolean,boolean) instead
187 */
188 @Deprecated
189 public static String toText(final Method method, final Object[] args,
190 final boolean trim) {
191 return Mnemos.toText(method, args, trim, false);
192 }
193
194 /**
195 * Make a string out of method.
196 * @param method The method
197 * @param args Actual arguments of the method
198 * @param trim Shall we trim long texts?
199 * @return Text representation of it
200 * @deprecated Use toText() instead
201 */
202 @Deprecated
203 public static String toString(final Method method, final Object[] args,
204 final boolean trim) {
205 return Mnemos.toText(method, args, trim, false);
206 }
207
208 /**
209 * Make a string out of an exception.
210 * @param exp The exception
211 * @return Text representation of it
212 */
213 public static String toText(final Throwable exp) {
214 final StringBuilder text = new StringBuilder();
215 text.append(exp.getClass().getName());
216 final String msg = exp.getMessage();
217 if (msg != null) {
218 text.append('(').append(msg).append(')');
219 }
220 return text.toString();
221 }
222
223 /**
224 * Make a string out of an object.
225 * @param arg The argument
226 * @param trim Shall we trim long texts?
227 * @param skip Shall we skip it with dots?
228 * @return Text representation of it
229 * @since 0.7.19
230 */
231 @SuppressWarnings("PMD.AvoidCatchingThrowable")
232 public static String toText(final Object arg, final boolean trim,
233 final boolean skip) {
234 final StringBuilder text = new StringBuilder();
235 if (arg == null) {
236 text.append("NULL");
237 } else if (skip) {
238 text.append(Mnemos.DOTS);
239 } else {
240 try {
241 final String mnemo = Mnemos.toText(arg);
242 if (trim) {
243 text.append(Logger.format("%[text]s", mnemo));
244 } else {
245 text.append(mnemo);
246 }
247 // @checkstyle IllegalCatch (1 line)
248 } catch (final Throwable ex) {
249 text.append(
250 String.format(
251 "[%s thrown %s]",
252 arg.getClass().getName(),
253 Mnemos.toText(ex)
254 )
255 );
256 }
257 }
258 return text.toString();
259 }
260
261 /**
262 * Make a string out of an object.
263 * @param arg The argument
264 * @param trim Shall we trim long texts?
265 * @return Text representation of it
266 * @deprecated Use toText(Object,boolean,boolean) instead
267 */
268 @Deprecated
269 public static String toText(final Object arg, final boolean trim) {
270 return Mnemos.toText(arg, trim, false);
271 }
272
273 /**
274 * Make a string out of an object.
275 * @param arg The argument
276 * @param trim Shall we trim long texts?
277 * @return Text representation of it
278 * @deprecated Use toText() instead
279 */
280 @Deprecated
281 public static String toString(final Object arg, final boolean trim) {
282 return Mnemos.toText(arg, trim, false);
283 }
284
285 /**
286 * Make a string out of an object.
287 * @param arg The argument
288 * @return Text representation of it
289 */
290 private static String toText(final Object arg) {
291 String text;
292 if (arg.getClass().isArray()) {
293 if (arg instanceof Object[]) {
294 text = Mnemos.objectArrays((Object[]) arg);
295 } else {
296 text = Mnemos.primitiveArrays(arg);
297 }
298 } else {
299 final String origin = arg.toString();
300 if (arg instanceof String || origin.contains(" ")
301 || origin.isEmpty()) {
302 text = String.format("'%s'", origin);
303 } else {
304 text = origin;
305 }
306 }
307 return text;
308 }
309
310 /**
311 * Text representation of object arrays.
312 * @param arg Array to change into String.
313 * @return Array in String.
314 */
315 private static String objectArrays(final Object... arg) {
316 final StringBuilder bldr = new StringBuilder();
317 bldr.append('[');
318 for (final Object item : arg) {
319 if (bldr.length() > 1) {
320 bldr.append(Mnemos.COMMA);
321 }
322 bldr.append(Mnemos.toText(item, false, false));
323 }
324 return bldr.append(']').toString();
325 }
326
327 /**
328 * Text representation of primitive arrays.
329 * @param arg Array to change into String.
330 * @return Array in String.
331 */
332 private static String primitiveArrays(final Object arg) {
333 final String text;
334 if (arg instanceof char[]) {
335 text = Arrays.toString((char[]) arg);
336 } else if (arg instanceof byte[]) {
337 text = Arrays.toString((byte[]) arg);
338 } else if (arg instanceof short[]) {
339 text = Arrays.toString((short[]) arg);
340 } else if (arg instanceof int[]) {
341 text = Arrays.toString((int[]) arg);
342 } else if (arg instanceof long[]) {
343 text = Arrays.toString((long[]) arg);
344 } else if (arg instanceof float[]) {
345 text = Arrays.toString((float[]) arg);
346 } else if (arg instanceof double[]) {
347 text = Arrays.toString((double[]) arg);
348 } else if (arg instanceof boolean[]) {
349 text = Arrays.toString((boolean[]) arg);
350 } else {
351 text = "[unknown]";
352 }
353 return text;
354 }
355 }