⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 classutil.java

📁 Tree taglib,生成树的标签库
💻 JAVA
字号:
/*
    Copyright 2004 Jenkov Development

    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.jenkov.prizetags.util;

import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;

/**
 * @author Jakob Jenkov - Copyright 2005 Jenkov Development
 */
public class ClassUtil {

    public static Object getBean(Object owner, String propertyName)
    throws IllegalAccessException, InvocationTargetException {
        if(owner == null){
            throw new NullPointerException("Object (owner) to find property in was null");
        }
        Method property = null;
        property = getProperty(owner, propertyName);

        //if first property name guess failed, try again with "get".
        if(property == null){
            property = getProperty(owner, toGetterName(propertyName, "get")) ;
        }

        //if first two property guesses failed, try again with "is"
        if(property == null){
            property = getProperty(owner, toGetterName(propertyName, "is"));
        }

        if(property == null){
            throw new IllegalArgumentException("Object of class " + owner.getClass() +
                    " does not have a property called " + propertyName);
        }

        return property.invoke(owner, null);
    }

    private static Method getProperty(Object owner, String propertyName) {
        try {
            return owner.getClass().getMethod(propertyName, null);
        } catch (NoSuchMethodException e) {
            //ignore, we will try to add "get" in front of the property and try again
        }
        return null;
    }

    private static String toGetterName(String propertyName, String prefix) {
        return prefix + propertyName.substring(0,1).toUpperCase() + propertyName.substring(1);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -