Java tutorial
//package com.java2s; /** * A collection of small utilities for component management and reflection handling. * <p/> * <hr/> Copyright 2006-2012 Torsten Heup * <p/> * 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 * <p/> * http://www.apache.org/licenses/LICENSE-2.0 * <p/> * 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.Method; public class Main { /** * Returns a getter method for the given property, or null if there is none. * * @param target Target exposing the getter. * @param propertyName The getter's property. * @return a getter method for the given property, or null if there is none. */ public static Method getGetter(final Object target, final String propertyName) { if (target == null) throw new IllegalArgumentException("Parameter 'target' must not be null!"); if (propertyName == null) throw new IllegalArgumentException("Parameter 'propertyName' must not be null!"); return getGetter(target.getClass(), propertyName); } /** * Returns a suitable getter for the given property from the specified class. This method will try the different * bean naming conventions getProperty, isProperty and hasProperty, and will return the first method it finds. * * @param clazz Class to obtain getter from. * @param propertyName Name of the property returned by the getter. * @return a suitable getter for the given property from the specified class */ public static Method getGetter(final Class clazz, final String propertyName) { if (clazz == null) throw new IllegalArgumentException("Parameter 'clazz' must not be null!"); if (propertyName == null) throw new IllegalArgumentException("Parameter 'propertyName' must not be null!"); Method m; m = getGetterWithPrefix(clazz, propertyName, "get"); if (m != null) return m; m = getGetterWithPrefix(clazz, propertyName, "is"); if (m != null) return m; m = getGetterWithPrefix(clazz, propertyName, "has"); return m; } /** * Internal method used by the getGetter method to try the different possible prefixes 'get', 'has' and 'is'. * * @param target Target object exposing the getter. * @param property Property being searched. * @param prefix Prefix for the supposed getter. * @return The getter method if it exists, null otherwise. */ protected static Method getGetterWithPrefix(final Class target, final String property, final String prefix) { String name = prefix + Character.toUpperCase(property.charAt(0)); if (property.length() > 1) name = name + property.substring(1); return getMethod(target, name); } /** * Returns a the specified method of the target object, or null if the method does not exist. This is merely a * conveniance method to avoid reflection's exceptions. * * @param target Target object to check * @param methodName Name of the method being queried * @param argumentTypes Types of the method's arguments * @return Method instance if the method exists, false otherwise. */ public static synchronized Method getMethod(final Object target, final String methodName, final Class... argumentTypes) { return getMethod(target.getClass(), methodName, argumentTypes); } /** * Attempts to find a method with the given name and argument types on the specified class. Other than Class's * getMethod(String, Class...) method, this method will also return protected and private methods. * * @param clazz Class supposed to offer the method being searched. * @param methodName Name of the method. * @param argumentTypes The arguments found in the method signature, if any. * @return a method with the given name and argument types on the specified class, if any. */ public static synchronized Method getMethod(final Class clazz, final String methodName, final Class... argumentTypes) { try { return clazz.getMethod(methodName, argumentTypes); } catch (NoSuchMethodException e) { /* Ok, so there's no public method... */ } Class runner = clazz; while (runner != null) { try { return runner.getDeclaredMethod(methodName, argumentTypes); } catch (NoSuchMethodException e) { /* No luck here either */ } runner = runner.getSuperclass(); } /* Still no luck, means there is no suitable method */ return null; } }