集册 Java实例教程 获取写入方法

获取写入方法

欢马劈雪     最近更新时间:2020-01-02 10:19:05

357
获取写入方法


//package com.nowjava;/*nowjava.com*/

import java.lang.reflect.Method;


public class Main {



    @SuppressWarnings("unchecked")

    public static Method getWriteMethod(

            @SuppressWarnings("rawtypes") Class clazz, String propertyName,

            @SuppressWarnings("rawtypes") Class propertyType) {

        Method writeMethod = null;

        String base = capitalize(propertyName);


        @SuppressWarnings("rawtypes")

        Class params[] = { propertyType };

        try {

            writeMethod = clazz.getMethod("set" + base, params);/**来 自 时 代 J a v a 公 众 号 - nowjava.com**/

        } catch (Exception e) {

            // no write method

        }


        return writeMethod;

    }


    private static String capitalize(String s) {

        if (s.length() == 0) {

            return s;

        } 
展开阅读全文