get Write Method - Java Reflection

Java examples for Reflection:Method

Description

get Write Method

Demo Code


//package com.java2s;
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 {/*from  w w w . ja va 2  s  .co  m*/
            writeMethod = clazz.getMethod("set" + base, params);
        } catch (Exception e) {
            // no write method
        }

        return writeMethod;
    }

    private static String capitalize(String s) {
        if (s.length() == 0) {
            return s;
        } else {
            char chars[] = s.toCharArray();
            chars[0] = Character.toUpperCase(chars[0]);
            return String.valueOf(chars);
        }
    }
}

Related Tutorials