📄 mappedpropertydescriptor.java
字号:
findMappedPropertyType();
}
// -------------------------------------------------------- Public Methods
/**
* Gets the Class object for the property values.
*
* @return The Java type info for the property values. Note that
* the "Class" object may describe a built-in Java type such as "int".
* The result may be "null" if this is a mapped property that
* does not support non-keyed access.
* <p>
* This is the type that will be returned by the mappedReadMethod.
*/
public Class getMappedPropertyType() {
return mappedPropertyType;
}
/**
* Gets the method that should be used to read one of the property value.
*
* @return The method that should be used to read the property value.
* May return null if the property can't be read.
*/
public Method getMappedReadMethod() {
return mappedReadMethod;
}
/**
* Sets the method that should be used to read one of the property value.
*
* @param mappedGetter The mapped getter method.
* @throws IntrospectionException If an error occurs finding the
* mapped property
*/
public void setMappedReadMethod(Method mappedGetter)
throws IntrospectionException {
mappedReadMethod = mappedGetter;
findMappedPropertyType();
}
/**
* Gets the method that should be used to write one of the property value.
*
* @return The method that should be used to write one of the property value.
* May return null if the property can't be written.
*/
public Method getMappedWriteMethod() {
return mappedWriteMethod;
}
/**
* Sets the method that should be used to write the property value.
*
* @param mappedSetter The mapped setter method.
* @throws IntrospectionException If an error occurs finding the
* mapped property
*/
public void setMappedWriteMethod(Method mappedSetter)
throws IntrospectionException {
mappedWriteMethod = mappedSetter;
findMappedPropertyType();
}
// ------------------------------------------------------- Private Methods
/**
* Introspect our bean class to identify the corresponding getter
* and setter methods.
*/
private void findMappedPropertyType() throws IntrospectionException {
try {
mappedPropertyType = null;
if (mappedReadMethod != null) {
if (mappedReadMethod.getParameterTypes().length != 1) {
throw new IntrospectionException
("bad mapped read method arg count");
}
mappedPropertyType = mappedReadMethod.getReturnType();
if (mappedPropertyType == Void.TYPE) {
throw new IntrospectionException
("mapped read method " +
mappedReadMethod.getName() + " returns void");
}
}
if (mappedWriteMethod != null) {
Class[] params = mappedWriteMethod.getParameterTypes();
if (params.length != 2) {
throw new IntrospectionException
("bad mapped write method arg count");
}
if (mappedPropertyType != null &&
mappedPropertyType != params[1]) {
throw new IntrospectionException
("type mismatch between mapped read and write methods");
}
mappedPropertyType = params[1];
}
} catch (IntrospectionException ex) {
throw ex;
}
}
/**
* Return a capitalized version of the specified property name.
*
* @param s The property name
*/
private static String capitalizePropertyName(String s) {
if (s.length() == 0) {
return s;
}
char[] chars = s.toCharArray();
chars[0] = Character.toUpperCase(chars[0]);
return new String(chars);
}
/**
* Find a method on a class with a specified number of parameters.
*/
private static Method internalGetMethod(Class initial, String methodName,
int parameterCount) {
// For overridden methods we need to find the most derived version.
// So we start with the given class and walk up the superclass chain.
for (Class clazz = initial; clazz != null; clazz = clazz.getSuperclass()) {
Method[] methods = clazz.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
Method method = methods[i];
if (method == null) {
continue;
}
// skip static methods.
int mods = method.getModifiers();
if (!Modifier.isPublic(mods) ||
Modifier.isStatic(mods)) {
continue;
}
if (method.getName().equals(methodName) &&
method.getParameterTypes().length == parameterCount) {
return method;
}
}
}
// Now check any inherited interfaces. This is necessary both when
// the argument class is itself an interface, and when the argument
// class is an abstract class.
Class[] interfaces = initial.getInterfaces();
for (int i = 0; i < interfaces.length; i++) {
Method method = internalGetMethod(interfaces[i], methodName, parameterCount);
if (method != null) {
return method;
}
}
return null;
}
/**
* Find a method on a class with a specified number of parameters.
*/
private static Method getMethod(Class clazz, String methodName, int parameterCount)
throws IntrospectionException {
if (methodName == null) {
return null;
}
Method method = internalGetMethod(clazz, methodName, parameterCount);
if (method != null) {
return method;
}
// No Method found
throw new IntrospectionException("No method \"" + methodName +
"\" with " + parameterCount + " parameter(s)");
}
/**
* Find a method on a class with a specified parameter list.
*/
private static Method getMethod(Class clazz, String methodName, Class[] parameterTypes)
throws IntrospectionException {
if (methodName == null) {
return null;
}
Method method = MethodUtils.getMatchingAccessibleMethod(clazz, methodName, parameterTypes);
if (method != null) {
return method;
}
int parameterCount = (parameterTypes == null) ? 0 : parameterTypes.length;
// No Method found
throw new IntrospectionException("No method \"" + methodName +
"\" with " + parameterCount + " parameter(s) of matching types.");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -