InstanceMethodInvocationException.java
01 /*
02  * Copyright 2008-2015 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             b.append(type.getName());
61         }
62         b.append(")");
63 
64         return b.toString();
65     }
66 
67     @Nonnull
68     protected static String formatArguments(@Nonnull Object instance, @Nonnull Method method) {
69         checkNonNull(instance, "instance");
70         checkNonNull(method, "method");
71         StringBuilder b = new StringBuilder("An error occurred while invoking instance method ")
72             .append(instance.getClass().getName())
73             .append(".").append(method.getName()).append("(");
74 
75         boolean first = true;
76         for (Class<?> type : method.getParameterTypes()) {
77             if (first) {
78                 first = false;
79             else {
80                 b.append(",");
81             }
82             b.append(type.getName());
83         }
84         b.append(")");
85 
86         return b.toString();
87     }
88 }