Java examples for Reflection:Method Return
Return true if method is either an accessor or a mutator.
//package com.java2s; import java.lang.reflect.Method; import java.lang.reflect.Type; public class Main { /** getClass() method name. */ private static final String GET_CLASS_METHOD_NAME = "getClass"; /** Prefix for all mutator methods. */ private static final String MUTATOR_PREFIX = "set"; /** Prefix for all isser accessor methods. */ private static final String IS_ACCESSOR_PREFIX = "is"; /** Prefix for all getter accessor methods. */ private static final String GET_ACCESSOR_PREFIX = "get"; /**/* w ww . j av a 2 s . c om*/ * Return true if method is either an accessor or a mutator. * * @param method the method. * @return true if method is either an accessor or a mutator. */ public static boolean isAttributeMethod(final Method method) { return isAccessor(method) || isMutator(method); } /** * Return true if method matches naming convention for accessor. * * @param method the method. * @return true if method matches naming convention for accessor. */ public static boolean isAccessor(final Method method) { final Type type = method.getGenericReturnType(); if (Void.TYPE == type || method.getParameterTypes().length != 0) { return false; } final String name = method.getName(); if (name.startsWith(GET_ACCESSOR_PREFIX) && name.length() > 3 && !GET_CLASS_METHOD_NAME.equals(name)) { return true; } else { return name.startsWith(IS_ACCESSOR_PREFIX) && name.length() > 2 && Boolean.TYPE == type; } } /** * Return true if method matches naming convention for mutator. * * @param method the method. * @return true if method matches naming convention for mutator. */ public static boolean isMutator(final Method method) { final String name = method.getName(); return name.startsWith(MUTATOR_PREFIX) && name.length() > 3 && Void.TYPE == method.getGenericReturnType() && method.getParameterTypes().length == 1; } }