01 /*
02 * Copyright 2008-2017 the original author or authors.
03 *
04 * Licensed under the Apache License, Version 2.0 (the "License");
05 * you may not use this file except in compliance with the License.
06 * You may obtain a copy of the License at
07 *
08 * http://www.apache.org/licenses/LICENSE-2.0
09 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package griffon.exceptions;
17
18 import javax.annotation.Nonnull;
19 import javax.annotation.Nullable;
20 import java.lang.reflect.Method;
21
22 /**
23 * @author Andres Almiray
24 * @since 2.0.0
25 */
26 public class InstanceMethodInvocationException extends MethodInvocationException {
27 private static final long serialVersionUID = -2325571968606780435L;
28 private static final String INSTANCE = "instance";
29
30 public InstanceMethodInvocationException(@Nonnull Object instance, @Nonnull String methodName, @Nullable Object[] args) {
31 super(formatArguments(instance, methodName, args));
32 }
33
34 public InstanceMethodInvocationException(@Nonnull Object instance, @Nonnull String methodName, @Nullable Object[] args, @Nonnull Throwable cause) {
35 super(formatArguments(instance, methodName, args), cause);
36 }
37
38 public InstanceMethodInvocationException(@Nonnull Object instance, @Nonnull Method method) {
39 super(formatArguments(instance, method));
40 }
41
42 public InstanceMethodInvocationException(@Nonnull Object instance, @Nonnull Method method, @Nonnull Throwable cause) {
43 super(formatArguments(instance, method), cause);
44 }
45
46 @Nonnull
47 private static String formatArguments(@Nonnull Object instance, @Nonnull String methodName, @Nullable Object[] args) {
48 checkNonNull(instance, INSTANCE);
49 checkNonBlank(methodName, "methodName");
50 StringBuilder b = new StringBuilder("An error occurred while invoking instance method ")
51 .append(instance.getClass().getName())
52 .append(".").append(methodName).append("(");
53
54 boolean first = true;
55 for (Class<?> type : convertToTypeArray(args)) {
56 if (first) {
57 first = false;
58 } else {
59 b.append(",");
60 }
61 if (type == null) {
62 // we don't know the type as the argument is null,
63 // let's fallback to plain old Object
64 b.append(Object.class.getName());
65 } else {
66 b.append(type.getName());
67 }
68 }
69 b.append(")");
70
71 return b.toString();
72 }
73
74 @Nonnull
75 protected static String formatArguments(@Nonnull Object instance, @Nonnull Method method) {
76 checkNonNull(instance, INSTANCE);
77 checkNonNull(method, "method");
78 StringBuilder b = new StringBuilder("An error occurred while invoking instance method ")
79 .append(instance.getClass().getName())
80 .append(".").append(method.getName()).append("(");
81
82 boolean first = true;
83 for (Class<?> type : method.getParameterTypes()) {
84 if (first) {
85 first = false;
86 } else {
87 b.append(",");
88 }
89 b.append(type.getName());
90 }
91 b.append(")");
92
93 return b.toString();
94 }
95 }
|