PageRenderTime 26ms CodeModel.GetById 17ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/com/google/ie/common/util/ClassUtility.java

http://thoughtsite.googlecode.com/
Java | 111 lines | 44 code | 15 blank | 52 comment | 10 complexity | 82710dd1e20c06365a4364dd39156ecd MD5 | raw file
  1/* Copyright 2010 Google Inc.
  2 * 
  3 * Licensed under the Apache License, Version 2.0 (the "License");
  4 * you may not use this file except in compliance with the License.
  5 * You may obtain a copy of the License at
  6 * 
  7 *      http://www.apache.org/licenses/LICENSE-2.0
  8 * 
  9 * Unless required by applicable law or agreed to in writing, software
 10 * distributed under the License is distributed on an "AS IS" BASIS.
 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12 * See the License for the specific language governing permissions and
 13 * limitations under the License
 14 */
 15
 16package com.google.ie.common.util;
 17
 18import java.lang.reflect.InvocationTargetException;
 19import java.lang.reflect.Method;
 20
 21/**
 22 * Utility class for accessing value for object by using reflection.
 23 * 
 24 * @author gmaurya
 25 * 
 26 */
 27public class ClassUtility {
 28
 29    /**
 30     * Get the getter method names for attributes.
 31     * 
 32     * @param fieldName the name of the field
 33     * @param fieldType the type of the field
 34     * @return String the name of the getter method for the field
 35     */
 36    private static <T> String getGetterMethodName(String fieldName, java.lang.Class<T> fieldType) {
 37
 38        String firstStringChar = fieldName.substring(0, 1).toUpperCase();
 39        String remaingString = fieldName.substring(1);
 40
 41        String methodName = null;
 42        /* In case the the field is a boolean */
 43        if (fieldType != null && fieldType == boolean.class
 44                        || fieldType == Boolean.class) {
 45
 46            methodName = "is" + firstStringChar + remaingString;
 47        } else {
 48            methodName = "get" + firstStringChar + remaingString;
 49        }
 50        return methodName;
 51    }
 52
 53    /**
 54     * This method invoke the method and return the return value.
 55     * 
 56     * @param object the object
 57     * @param fileName String
 58     * @return the result of dispatching the method represented by this object
 59     *         on object
 60     * 
 61     * @throws SecurityException
 62     * @throws NoSuchMethodException
 63     * @throws IllegalArgumentException
 64     * @throws IllegalAccessException
 65     * @throws InvocationTargetException
 66     */
 67    @SuppressWarnings("unchecked")
 68    public static Object getObject(Object object, String fileName)
 69                    throws SecurityException, NoSuchMethodException,
 70                    IllegalArgumentException, IllegalAccessException,
 71                    InvocationTargetException {
 72        String methodName = getGetterMethodName(fileName, null);
 73        Method method = null;
 74        Class inputClass = object.getClass();
 75        method = getMethod(inputClass, methodName, null);
 76
 77        return method.invoke(object, (Object[]) null);
 78
 79    }
 80
 81    /**
 82     * This method return the list of method of any class.
 83     * 
 84     * @param clazz Class
 85     * @param methodName String
 86     * @param parameterTypes Class[]
 87     * @return Method
 88     * @throws SecurityException
 89     * @throws NoSuchMethodException
 90     */
 91    private static <T, Y> Method getMethod(Class<T> clazz, String methodName,
 92                    Class<Y>[] parameterTypes) throws SecurityException,
 93                    NoSuchMethodException {
 94        Method method = null;
 95        try {
 96            method = clazz.getMethod(methodName, parameterTypes);
 97        } catch (NoSuchMethodException e) {
 98            if (methodName.startsWith("get")) {
 99                methodName = methodName.replaceFirst("get", "is");
100            } else if (methodName.startsWith("is")) {
101                methodName = methodName.replaceFirst("get", "is");
102            }
103
104            method = clazz.getMethod(methodName, parameterTypes);
105        }
106
107        return method;
108    }
109
110}
111