搜索吸气剂方法
/* Copyright 1996-2008 Ariba, Inc. 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. $Id: //ariba/platform/util/core/ariba/util/fieldvalue/FieldValueAccessorUtil.java#5 $ *//* 来 自 时代Java公众号*/ import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Member; import java.lang.reflect.Method; import java.lang.reflect.Modifier; public class Main{ /** Same as lookupSetterMethod except uses matchForGetter rather than matchForSetter. */ protected static Method lookupGetterMethod(Class targetClass, String targetFieldName, int argCount) { Method methodArray[] = getDeclaredMethods(targetClass); for (int index = methodArray.length - 1; index > -1; index--) { Method currentPrimitiveMethod = methodArray[index]; if ((currentPrimitiveMethod.getParameterTypes().length == argCount) && matchForGetter(targetFieldName, currentPrimitiveMethod.getName())) { return currentPrimitiveMethod; }/* 来 自 NowJava.com - 时 代 Java*/ } Method getterMethod = null; Class targetSuperclass = targetClass.getSuperclass(); if (targetSuperclass != null) { getterMethod = lookupGetterMethod(targetSuperclass, targetFieldName, argCount); } return getterMethod; } /** A convenicence method which allows for lookup of single argument getter method without specifying the number of arguments (assumes argCount = 1). */ protected static Method lookupGetterMethod(Class targetClass, String targetFieldName) { return lookupGetterMethod(targetClass, targetFieldName, 0); } /** A cover which hides the try/catch block. @param targetClass the class on which to perform getDeclaredMethods @return an array of Methods */ protected static Method[] getDeclaredMethods(Class targetClass) { try { return targetClass.getDeclaredMethods(); } catch (SecurityException securityException) { throw new FieldValueException(securityException); } } /** Determines if the methodName can be used given the targetFieldName. First, an excat match is attempted, then the 'get' prefix is applied and the first letter of targetFieldName is capitalized. @param targetFieldName the desired field name @param methodName the actual method name @return wheter or not the two match according to the rules for matching getters */ public static boolean matchForGetter(String targetFieldName, String methodName) { return targetFieldName.equals(methodName) || FieldValueAccessorUtil.matchWithPrefix(targetFieldName, methodName, "get"); } /** Determines if firstString equals (secondStringPrefix + secondStringSuffix) without actually concatanting the strings. @param firstString any String @param secondStringPrefix any String @param secondStringSuffix any String @return a boolean indicating a match */ protected static boolean equals(String firstString, String secondStringPrefix, String secondStringSuffix) { int secondStringPrefixLength = secondStringPrefix.length(); int secondStringSuffixLength = secondStringSuffix.length(); int secondStringLength = secondStringPrefixLength + secondStringSuffixLength; return (firstString.length() == secondStringLength) && firstString.regionMatches(0, secondStringPrefix, 0, secondStringPrefixLength) && firstString.regionMatches(secondStringPrefixLength, secondStringSuffix, 0, secondStringSuffixLength); }