集册 Java实例教程 如果给定的方法对象不是getter / setter / is,并且它是public和non,则返回true

如果给定的方法对象不是getter / setter / is,并且它是public和non,则返回true

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

508
如果给定的方法对象不是getter / setter / is,并且是公共的且非静态的,则返回true。

/**

 * A utility class that performs various operations using the Java reflection

 * API.

 * 

 * @author Yanick Duchesne

 *         <dl>

 *         <dt><b>Copyright: </b>

 *         <dd>Copyright &#169; 2002-2003 <a

 *         href="http://www.sapia-oss.org">Sapia Open Source Software </a>. All

 *         Rights Reserved.</dd>

 *         </dt>

 *         <dt><b>License: </b>

 *         <dd>Read the license.txt file of the jar or visit the <a

 *         href="http://www.sapia-oss.org/license.html">license page </a> at the

 *         Sapia OSS web site</dd>

 *         </dt>

 *         </dl>

 */

//package com.nowjava;
/* 
 来自 
*N o w J a v a . c o m - 时  代  Java*/

import java.lang.reflect.Method;

import java.lang.reflect.Modifier;


public class Main {

    /**

     * Returns <code>true</code> if the given method object is not a

     * getter/setter/is, and if it is public and non-static.

     * 

     * @param method

     *          a <code>method</code>.

     * @return <code>true</code> if the given method corresponds to a MBean

     *         operation.

     */

    public static boolean isOperation(Method method) {

        return Modifier.isPublic(method.getModifiers())

                && !Modifier.isStatic(method.getModifiers())

                && !isSetter(method) && !isGetter(method)

                && !isBoolean(method);

    }


    /**

     * Returns <code>true</code> if the given method object corresponds to a

     * setter. The method must start with a "set" prefix and be non-static,

     * public, and take a single parameter.

     * 

     * @param method

     *          a <code>Method</code> object.

     * @return <code>true</code> if the given instance corresponds to a setter.

     */

    public static boolean isSetter(Method method) {
    /** 
     来自 NowJava.com - 时代Java**/

        return method.getName().startsWith("set")

                && Modifier.isPublic(method.getModifiers())

                && !Modifier.isStatic(method.getModifiers())

                && (method.getParameterTypes().length == 1);

    }


    /**

     * Returns <code>true</code> if the given method object corresponds to a

     * getter. The method must start with a "get" prefix and be non-static,

     * public, take no parameter, and have a return type.

     * 

     * @param method

     *          a <code>Method</code> object.

     * @return <code>true</code> if the given instance corresponds to a getter.

     */

    public static boolean isGetter(Method method) {

        return method.getName().startsWith("get")

                && Modifier.isPublic(method.getModifiers())

                && !Modifier.isStatic(method.getModifiers())

                && (method.getParameterTypes().length == 0)

                && (method.getReturnType() != null)

                && !method.getReturnType().equals(void.class);

    }


    
展开阅读全文