1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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 
17 package com.android.layoutlib.bridge.util;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 
22 import java.lang.reflect.InvocationTargetException;
23 import java.lang.reflect.Method;
24 
25 /**
26  * Utility to convert checked Reflection exceptions to unchecked exceptions.
27  */
28 public class ReflectionUtils {
29 
30     @NonNull
getMethod(@onNull Class<?> clazz, @NonNull String name, @Nullable Class<?>... params)31     public static Method getMethod(@NonNull Class<?> clazz, @NonNull String name,
32             @Nullable Class<?>... params) throws ReflectionException {
33         try {
34             return clazz.getMethod(name, params);
35         } catch (NoSuchMethodException e) {
36             throw new ReflectionException(e);
37         }
38     }
39 
40     @Nullable
invoke(@onNull Method method, @Nullable Object object, @Nullable Object... args)41     public static Object invoke(@NonNull Method method, @Nullable Object object,
42             @Nullable Object... args) throws ReflectionException {
43         Exception ex;
44         try {
45             return method.invoke(object, args);
46         } catch (IllegalAccessException e) {
47             ex = e;
48         } catch (InvocationTargetException e) {
49             ex = e;
50         }
51         throw new ReflectionException(ex);
52     }
53 
54     /**
55      * Check if the object is an instance of a class named {@code className}. This doesn't work
56      * for interfaces.
57      */
isInstanceOf(Object object, String className)58     public static boolean isInstanceOf(Object object, String className) {
59         Class superClass = object.getClass();
60         while (superClass != null) {
61             String name = superClass.getName();
62             if (name.equals(className)) {
63                 return true;
64             }
65             superClass = superClass.getSuperclass();
66         }
67         return false;
68     }
69 
70     @NonNull
getCause(@onNull Throwable throwable)71     public static Throwable getCause(@NonNull Throwable throwable) {
72         Throwable cause = throwable.getCause();
73         return cause == null ? throwable : cause;
74     }
75 
76     /**
77      * Wraps all reflection related exceptions. Created since ReflectiveOperationException was
78      * introduced in 1.7 and we are still on 1.6
79      */
80     public static class ReflectionException extends Exception {
ReflectionException()81         public ReflectionException() {
82             super();
83         }
84 
ReflectionException(String message)85         public ReflectionException(String message) {
86             super(message);
87         }
88 
ReflectionException(String message, Throwable cause)89         public ReflectionException(String message, Throwable cause) {
90             super(message, cause);
91         }
92 
ReflectionException(Throwable cause)93         public ReflectionException(Throwable cause) {
94             super(cause);
95         }
96     }
97 }
98