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