Java tutorial
//package com.java2s; /* * Copyright (c) 2013. wyouflf (wyouflf@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.lang.reflect.Field; import java.lang.reflect.Method; public class Main { public static Method getColumnGetMethod(Class<?> entityType, Field field) { String fieldName = field.getName(); Method getMethod = null; if (field.getType() == boolean.class) { getMethod = getBooleanColumnGetMethod(entityType, fieldName); } if (getMethod == null) { String methodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); try { getMethod = entityType.getDeclaredMethod(methodName); } catch (NoSuchMethodException e) { //Logger.d(methodName + " not exist"); } } if (getMethod == null && !Object.class.equals(entityType.getSuperclass())) { return getColumnGetMethod(entityType.getSuperclass(), field); } return getMethod; } private static Method getBooleanColumnGetMethod(Class<?> entityType, final String fieldName) { String methodName = "is" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); if (isStartWithIs(fieldName)) { methodName = fieldName; } try { return entityType.getDeclaredMethod(methodName); } catch (NoSuchMethodException e) { //Logger.d(methodName + " not exist"); } return null; } private static boolean isStartWithIs(final String fieldName) { return fieldName != null && fieldName.startsWith("is"); } }