InstanceMethodInvocationException.java
01 /*
02  * Copyright 2008-2016 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 
29     public InstanceMethodInvocationException(@Nonnull Object instance, @Nonnull String methodName, @Nullable Object[] args) {
30         super(formatArguments(instance, methodName, args));
31     }
32 
33     public InstanceMethodInvocationException(@Nonnull Object instance, @Nonnull String methodName, @Nullable Object[] args, @Nonnull Throwable cause) {
34         super(formatArguments(instance, methodName, args), cause);
35     }
36 
37     public InstanceMethodInvocationException(@Nonnull Object instance, @Nonnull Method method) {
38         super(formatArguments(instance, method));
39     }
40 
41     public InstanceMethodInvocationException(@Nonnull Object instance, @Nonnull Method method, @Nonnull Throwable cause) {
42         super(formatArguments(instance, method), cause);
43     }
44 
45     @Nonnull
46     private static String formatArguments(@Nonnull Object instance, @Nonnull String methodName, @Nullable Object[] args) {
47         checkNonNull(instance, "instance");
48         checkNonBlank(methodName, "methodName");
49         StringBuilder b = new StringBuilder("An error occurred while invoking instance method ")
50             .append(instance.getClass().getName())
51             .append(".").append(methodName).append("(");
52 
53         boolean first = true;
54         for (Class<?> type : convertToTypeArray(args)) {
55             if (first) {
56                 first = false;
57             else {
58                 b.append(",");
59             }
60             if (type == null) {
61                 // we don't know the type as the argument is null,
62                 // let's fallback to plain old Object
63                 b.append(Object.class.getName());
64             else {
65                 b.append(type.getName());
66             }
67         }
68         b.append(")");
69 
70         return b.toString();
71     }
72 
73     @Nonnull
74     protected static String formatArguments(@Nonnull Object instance, @Nonnull Method method) {
75         checkNonNull(instance, "instance");
76         checkNonNull(method, "method");
77         StringBuilder b = new StringBuilder("An error occurred while invoking instance method ")
78             .append(instance.getClass().getName())
79             .append(".").append(method.getName()).append("(");
80 
81         boolean first = true;
82         for (Class<?> type : method.getParameterTypes()) {
83             if (first) {
84                 first = false;
85             else {
86                 b.append(",");
87             }
88             b.append(type.getName());
89         }
90         b.append(")");
91 
92         return b.toString();
93     }
94 }