com.baidu.jprotobuf.pbrpc.utils.ReflectionUtils Maven / Gradle / Ivy
Show all versions of jprotobuf-rpc-core Show documentation
/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.baidu.jprotobuf.pbrpc.utils;
import java.lang.reflect.Method;
/**
* Simple utility class for working with the reflection API and handling
* reflection exceptions.
*
*
* @author xiemalin
* @since 1.0
*/
public class ReflectionUtils {
/**
* Perform the given callback operation on all matching methods of the
* given class and superclasses.
* The same named method occurring on subclass and superclass will
* appear twice, unless excluded by a {@link MethodFilter}.
*
* @param targetClass class to start looking at
* @param mc the callback to invoke for each method
* @throws IllegalArgumentException the illegal argument exception
* @see #doWithMethods(Class, MethodCallback, MethodFilter)
*/
public static void doWithMethods(Class> targetClass, MethodCallback mc) throws IllegalArgumentException {
doWithMethods(targetClass, mc, null);
}
/**
* Perform the given callback operation on all matching methods of the
* given class and superclasses.
*
The same named method occurring on subclass and superclass will
* appear twice, unless excluded by the specified {@link MethodFilter}.
*
* @param targetClass class to start looking at
* @param mc the callback to invoke for each method
* @param mf the filter that determines the methods to apply the callback to
* @throws IllegalArgumentException the illegal argument exception
*/
public static void doWithMethods(Class> targetClass, MethodCallback mc, MethodFilter mf)
throws IllegalArgumentException {
// Keep backing up the inheritance hierarchy.
do {
Method[] methods = targetClass.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
if (mf != null && !mf.matches(methods[i])) {
continue;
}
try {
mc.doWith(methods[i]);
}
catch (IllegalAccessException ex) {
throw new IllegalStateException(
"Shouldn't be illegal to access method '" + methods[i].getName() + "': " + ex);
}
}
targetClass = targetClass.getSuperclass();
}
while (targetClass != null);
}
/**
* Checks if is void.
*
* @param cls the cls
* @return true, if is void
*/
public static boolean isVoid(Class> cls) {
if (cls == Void.class || cls == void.class) {
return true;
}
return false;
}
/**
* Action to take on each method.
*/
public static interface MethodCallback {
/**
* Perform an operation using the given method.
*
* @param method the method to operate on
* @throws IllegalArgumentException the illegal argument exception
* @throws IllegalAccessException the illegal access exception
*/
void doWith(Method method) throws IllegalArgumentException, IllegalAccessException;
}
/**
* Callback optionally used to method fields to be operated on by a method callback.
*/
public static interface MethodFilter {
/**
* Determine whether the given method matches.
*
* @param method the method to check
* @return true, if successful
*/
boolean matches(Method method);
}
}