集册 Java实例教程 如果方法与访问器的命名约定匹配,则返回true。

如果方法与访问器的命名约定匹配,则返回true。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

367
如果方法与访问器的命名约定匹配,则返回true。


//package com.nowjava;

import java.lang.reflect.Method;

import java.lang.reflect.Type;// from 时 代 J     a    v  a - nowjava.com


public class Main {

    /** getClass() method name. */

    private static final String GET_CLASS_METHOD_NAME = "getClass";

    /** 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";


    /**

     * 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();// 来 自 时代Java公众号

        if (Void.TYPE == type || method.getParameterTypes().length != 0) {

            return false;

        }

        final String name = method.getName();

        if (name.startsWith(GET_ACCESSOR_PREFIX) && name.length() > 3

           
展开阅读全文