getter method name to Setter method name - Java Reflection

Java examples for Reflection:Method Name

Description

getter method name to Setter method name

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        String methodName = "java2s.com";
        System.out.println(getter2Setter(methodName));
    }//from  w ww.  ja va 2  s .  co  m

    public static String getter2Setter(String methodName) {
        if (methodName.startsWith("get")) {
            return "s" + methodName.substring(1);
        } else if (methodName.startsWith("is")) {
            return "set" + methodName.substring(2);
        } else {
            throw new IllegalArgumentException(
                    "method not start with get or is.");
        }
    }
}

Related Tutorials