Java examples for Reflection:Field
Convert method name to field name
//package com.java2s; import java.util.Locale; public class Main { public static void main(String[] argv) throws Exception { String methodName = "java2s.com"; System.out.println(m2f(methodName)); }/*from w w w. j a v a 2 s .c o m*/ public static final int SET_START = "set".length(); public static final int SET_END = SET_START + 1; public static final int IS_START = "is".length(); public static final int IS_END = IS_START + 1; public static String m2f(String methodName) { if (methodName.startsWith("set") || methodName.startsWith("get")) { return methodName.substring(SET_START, SET_END).toLowerCase( Locale.CHINA) + methodName.substring(SET_END); } else if (methodName.startsWith("is")) { return methodName.substring(IS_START, IS_END).toLowerCase( Locale.CHINA) + methodName.substring(IS_END); } else { throw new IllegalArgumentException( "method not start with get or is."); } } }