2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.v3po.translate.util;
19 import com.google.common.base.Optional;
20 import java.lang.reflect.Method;
21 import java.util.List;
22 import javax.annotation.Nonnull;
25 * Reflection based utilities
27 public final class ReflectionUtils {
29 private ReflectionUtils() {}
32 * Find a specific method using reflection
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
39 * @return Found method or Optional.absent() if there's no such method
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);
52 return Optional.absent();
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())) {
62 final Class<?>[] parameterTypes = method.getParameterTypes();
63 if (parameterTypes.length != paramTypes.size()) {
67 for (int i = 0; i < parameterTypes.length; i++) {
68 if (!parameterTypes[i].isAssignableFrom(paramTypes.get(i))) {
73 if (!method.getReturnType().equals(retType)) {