field To Setter method - Java Reflection

Java examples for Reflection:Field Set

Description

field To Setter method

Demo Code


//package com.java2s;
import java.beans.IntrospectionException;

import java.beans.PropertyDescriptor;

public class Main {
    public static void main(String[] argv) throws Exception {
        Class clazz = String.class;
        String fieldName = "java2s.com";
        System.out.println(fieldToSetter(clazz, fieldName));
    }//  w w w . jav a 2  s. co m

    public static <T> String fieldToSetter(Class<T> clazz, String fieldName) {
        try {
            PropertyDescriptor propertyDescriptor = new PropertyDescriptor(
                    fieldName, clazz);
            return propertyDescriptor.getWriteMethod().getName();
        } catch (IntrospectionException e) {
            e.printStackTrace();
            return null;
        }
        // return "set" + fieldName.substring(0, 1).toUpperCase() +
        // fieldName.substring(1);
    }
}

Related Tutorials