Android examples for java.lang.reflect:Method Get
get Field Set Method
//package com.java2s; import java.lang.reflect.Field; import java.lang.reflect.Method; public class Main { public static Method getFieldSetMethod(Class<?> clazz, Field f) { String fn = f.getName();/*from w w w. j av a 2s. c om*/ String mn = "set" + fn.substring(0, 1).toUpperCase() + fn.substring(1); try { return clazz.getDeclaredMethod(mn, f.getType()); } catch (NoSuchMethodException e) { if (f.getType() == boolean.class) { return getBooleanFieldSetMethod(clazz, f); } } return null; } public static Method getBooleanFieldSetMethod(Class<?> clazz, Field f) { String fn = f.getName(); String mn = "set" + fn.substring(0, 1).toUpperCase() + fn.substring(1); if (isISStart(f.getName())) { mn = "set" + fn.substring(2, 3).toUpperCase() + fn.substring(3); } try { return clazz.getDeclaredMethod(mn, f.getType()); } catch (NoSuchMethodException e) { e.printStackTrace(); return null; } } private static boolean isISStart(String fieldName) { if (fieldName == null || fieldName.trim().length() == 0) return false; return fieldName.startsWith("is") && !Character.isLowerCase(fieldName.charAt(2)); } }