/**
* JLibs: Common Utilities for Java
* Copyright (C) 2009 Santhosh Kumar T <santhosh.tekuri@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
//package com.nowjava;
/*
来自
*N o w J a v a . c o m - 时代Java*/
import java.lang.reflect.Method;
import java.util.Locale;
public class Main {
public static void main(String[] argv) throws Exception {
Class beanClass = String.class;
String property = "nowjava.com";
System.out.println(getPropertyType(beanClass, property));
}
/** prefix used by non-boolean getter methods */
public static final String GET = "get";
/**
* n o w j a v a . c o m - 时 代 Java 提 供
**/
/** prefix used by boolean getter methods */
public static final String IS = "is";
/**
* Returns the type of <code>property</code> in given <code>beanClass</code>
*
* @param beanClass bean class
* @param property name of the property
*
* @return null if the property is not found. otherwise returns the type of the <code>property</code>
*/
public static Class getPropertyType(Class<?> beanClass, String property) {
Method getter = getGetterMethod(beanClass, property);
if (getter == null)
return null;
else
return getter.getReturnType();
}
/**
* Returns getter method for <code>property</code> in specified <code>beanClass</code>
*
* @param beanClass bean class
* @param property name of the property
*
* @return getter method. null if <code>property</code> is not found
*
* @see #getGetterMethod(Class, String, Class)
*/
public static Method getGetterMethod(Class<?> beanClass, String property) {
try {
return beanClass.getMethod(GET + getMethodSuffix(property));
} catch (NoSuchMethodException ex1) {
try {
return beanClass.getMethod(IS + getMethodSuffix(property));
} catch (NoSuchMethodException ex2) {
return null;
}
}
}
/**
* Returns getter method for <code>property</code> in specified <code>beanClass</code>
*
* @param beanClass bean class
* @param property name of the property
* @param propertyType type of the property. This is used to compute getter method name.
*
* @return getter method. null if <code>property</code> is not found
*
* @see #getGetterMethod(Class, String)
*/
public static Method getGetterMethod(Class<?> beanClass,
String property, Class propertyType) {
try {
try {
if (propertyType == boolean.class)
return beanClass.getMethod(IS
+ getMethodSuffix(property));
} catch (NoSuchMethodException ignore) {
// ignore
}
return beanClass.getMethod(GET + getMethodSuffix(property));
} catch (NoSuchMethodException ex) {
return null;
}
}
public static String getMethodSuffix(String property) {
switch (property.length()) {
case 0:
throw new IllegalArgumentException("invalid property name: "
+ property);
case 1:
return property.toUpperCase(Locale.ENGLISH);
default:
/**代码未完, 请加载全部代码(NowJava.com).**/
本文系作者在时代Java发表,未经许可,不得转载。如有侵权,请联系nowjava@qq.com删除。