HONEYCOMB-106 - Support for generic cache management
[honeycomb.git] / infra / translate-utils / src / main / java / io / fd / honeycomb / v3po / translate / util / ReflectionUtils.java
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
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 io.fd.honeycomb.v3po.translate.util;
18
19 import com.google.common.base.Optional;
20 import java.lang.reflect.Method;
21 import java.util.List;
22 import javax.annotation.Nonnull;
23
24 /**
25  * Reflection based utilities
26  */
27 public final class ReflectionUtils {
28
29     private ReflectionUtils() {}
30
31     /**
32      * Find a specific method using reflection
33      *
34      * @param managedType Class object to find method in
35      * @param prefix Method name prefix used when finding the method. Case does not matter.
36      * @param paramTypes List of input argument types
37      * @param retType Return type
38      *
39      * @return Found method or Optional.absent() if there's no such method
40      */
41     @Nonnull
42     public static Optional<Method> findMethodReflex(@Nonnull final Class<?> managedType,
43                                                     @Nonnull final String prefix,
44                                                     @Nonnull final List<Class<?>> paramTypes,
45                                                     @Nonnull final Class<?> retType) {
46         for (Method method : managedType.getMethods()) {
47             if (isMethodMatch(prefix, paramTypes, retType, method)) {
48                 return Optional.of(method);
49             }
50         }
51
52         return Optional.absent();
53     }
54
55     private static boolean isMethodMatch(final @Nonnull String prefix,
56                                          final @Nonnull List<Class<?>> paramTypes,
57                                          final @Nonnull Class<?> retType, final Method method) {
58         if (!method.getName().toLowerCase().startsWith(prefix.toLowerCase())) {
59             return false;
60         }
61
62         final Class<?>[] parameterTypes = method.getParameterTypes();
63         if (parameterTypes.length != paramTypes.size()) {
64             return false;
65         }
66
67         for (int i = 0; i < parameterTypes.length; i++) {
68             if (!parameterTypes[i].isAssignableFrom(paramTypes.get(i))) {
69                 return false;
70             }
71         }
72
73         if (!method.getReturnType().equals(retType)) {
74             return false;
75         }
76
77         return true;
78     }
79 }