Android examples for java.lang.reflect:Method Get
get Field Get Method
//package com.java2s; import java.lang.reflect.Field; import java.lang.reflect.Method; public class Main { public static Method getFieldGetMethod(Class<?> clazz, Field f) { String fn = f.getName();//from ww w .j a v a 2s . c o m Method m = null; if (f.getType() == boolean.class) { m = getBooleanFieldGetMethod(clazz, fn); } if (m == null) { m = getFieldGetMethod(clazz, fn); } return m; } public static Method getFieldGetMethod(Class<?> clazz, String fieldName) { String mn = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); try { return clazz.getDeclaredMethod(mn); } catch (NoSuchMethodException e) { e.printStackTrace(); return null; } } public static Method getBooleanFieldGetMethod(Class<?> clazz, String fieldName) { String mn = "is" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); if (isISStart(fieldName)) { mn = fieldName; } try { return clazz.getDeclaredMethod(mn); } 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)); } }