java.lang.reflect.Modifier类的研究

来源:互联网 发布:小蜜软件怎么样 编辑:程序博客网 时间:2024/05/22 10:45
  1. Modifier类简介
    Modifier类是位于java.lang.reflect(see)反射包下的一个类,它提供了有关类或成员变量(字段、方法、构造等)的访问修饰符的信息。
    各成员变量都会有一个getModifiers()方法,getModifiers()方法返回一个包含标志的int值,标志描述了为数组元素应用了哪个修饰符(private、public、protected等)。
    关于该int标志,我翻看了一下Modifier源码,
    /* * %W% %E% * * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package java.lang.reflect;import java.security.AccessController;import sun.reflect.LangReflectAccess;import sun.reflect.ReflectionFactory;/** * The Modifier class provides <code>static</code> methods and * constants to decode class and member access modifiers.  The sets of * modifiers are represented as integers with distinct bit positions * representing different modifiers.  The values for the constants * representing the modifiers are taken from <a * href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/VMSpecTOC.doc.html"><i>The * Java</i><sup><small>TM</small></sup> <i>Virtual Machine Specification, Second * edition</i></a> tables  * <a href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#75734">4.1</a>, * <a href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#88358">4.4</a>, * <a href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#75568">4.5</a>, and  * <a href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#88478">4.7</a>. * * @see Class#getModifiers() * @see Member#getModifiers() * * @author Nakul Saraiya * @author Kenneth Russell */publicclass Modifier {    /*      * Bootstrapping protocol between java.lang and java.lang.reflect     *  packages      */    static {        sun.reflect.ReflectionFactory factory =            (sun.reflect.ReflectionFactory) AccessController.doPrivileged(                new ReflectionFactory.GetReflectionFactoryAction()            );        factory.setLangReflectAccess(new java.lang.reflect.ReflectAccess());    }    /**     * Return <tt>true</tt> if the integer argument includes the     * <tt>public</tt> modifier, <tt>false</tt> otherwise.     *     * @param mod a set of modifiers     * @return <tt>true</tt> if <code>mod</code> includes the     * <tt>public</tt> modifier; <tt>false</tt> otherwise.     */    public static boolean isPublic(int mod) {return (mod & PUBLIC) != 0;    }    /**     * Return <tt>true</tt> if the integer argument includes the     * <tt>private</tt> modifier, <tt>false</tt> otherwise.     *     * @param mod a set of modifiers     * @return <tt>true</tt> if <code>mod</code> includes the     * <tt>private</tt> modifier; <tt>false</tt> otherwise.     */    public static boolean isPrivate(int mod) {return (mod & PRIVATE) != 0;    }    /**     * Return <tt>true</tt> if the integer argument includes the     * <tt>protected</tt> modifier, <tt>false</tt> otherwise.     *     * @param mod a set of modifiers     * @return <tt>true</tt> if <code>mod</code> includes the     * <tt>protected</tt> modifier; <tt>false</tt> otherwise.     */    public static boolean isProtected(int mod) {return (mod & PROTECTED) != 0;    }    /**     * Return <tt>true</tt> if the integer argument includes the     * <tt>static</tt> modifier, <tt>false</tt> otherwise.     *     * @param mod a set of modifiers     * @return <tt>true</tt> if <code>mod</code> includes the     * <tt>static</tt> modifier; <tt>false</tt> otherwise.     */    public static boolean isStatic(int mod) {return (mod & STATIC) != 0;    }    /**     * Return <tt>true</tt> if the integer argument includes the     * <tt>final</tt> modifier, <tt>false</tt> otherwise.     *     * @param mod a set of modifiers     * @return <tt>true</tt> if <code>mod</code> includes the     * <tt>final</tt> modifier; <tt>false</tt> otherwise.     */    public static boolean isFinal(int mod) {return (mod & FINAL) != 0;    }    /**     * Return <tt>true</tt> if the integer argument includes the     * <tt>synchronized</tt> modifier, <tt>false</tt> otherwise.     *     * @param mod a set of modifiers     * @return <tt>true</tt> if <code>mod</code> includes the     * <tt>synchronized</tt> modifier; <tt>false</tt> otherwise.     */    public static boolean isSynchronized(int mod) {return (mod & SYNCHRONIZED) != 0;    }    /**     * Return <tt>true</tt> if the integer argument includes the     * <tt>volatile</tt> modifier, <tt>false</tt> otherwise.     *     * @param mod a set of modifiers     * @return <tt>true</tt> if <code>mod</code> includes the     * <tt>volatile</tt> modifier; <tt>false</tt> otherwise.     */    public static boolean isVolatile(int mod) {return (mod & VOLATILE) != 0;    }    /**     * Return <tt>true</tt> if the integer argument includes the     * <tt>transient</tt> modifier, <tt>false</tt> otherwise.     *     * @param mod a set of modifiers     * @return <tt>true</tt> if <code>mod</code> includes the     * <tt>transient</tt> modifier; <tt>false</tt> otherwise.     */    public static boolean isTransient(int mod) {return (mod & TRANSIENT) != 0;    }    /**     * Return <tt>true</tt> if the integer argument includes the     * <tt>native</tt> modifier, <tt>false</tt> otherwise.     *     * @param mod a set of modifiers     * @return <tt>true</tt> if <code>mod</code> includes the     * <tt>native</tt> modifier; <tt>false</tt> otherwise.     */    public static boolean isNative(int mod) {return (mod & NATIVE) != 0;    }    /**     * Return <tt>true</tt> if the integer argument includes the     * <tt>interface</tt> modifier, <tt>false</tt> otherwise.     *     * @param mod a set of modifiers     * @return <tt>true</tt> if <code>mod</code> includes the     * <tt>interface</tt> modifier; <tt>false</tt> otherwise.     */    public static boolean isInterface(int mod) {return (mod & INTERFACE) != 0;    }    /**     * Return <tt>true</tt> if the integer argument includes the     * <tt>abstract</tt> modifier, <tt>false</tt> otherwise.     *     * @param mod a set of modifiers     * @return <tt>true</tt> if <code>mod</code> includes the     * <tt>abstract</tt> modifier; <tt>false</tt> otherwise.     */    public static boolean isAbstract(int mod) {return (mod & ABSTRACT) != 0;    }    /**     * Return <tt>true</tt> if the integer argument includes the     * <tt>strictfp</tt> modifier, <tt>false</tt> otherwise.     *     * @param mod a set of modifiers     * @return <tt>true</tt> if <code>mod</code> includes the     * <tt>strictfp</tt> modifier; <tt>false</tt> otherwise.     */    public static boolean isStrict(int mod) {return (mod & STRICT) != 0;    }    /**     * Return a string describing the access modifier flags in     * the specified modifier. For example:     * <blockquote><pre>     *    public final synchronized strictfp     * </pre></blockquote>     * The modifier names are returned in an order consistent with the     * suggested modifier orderings given in <a     * href="http://java.sun.com/docs/books/jls/second_edition/html/j.title.doc.html"><em>The     * Java Language Specification, Second Edition</em></a> sections     * <a href="http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#21613">§8.1.1</a>,      * <a href="http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#78091">§8.3.1</a>,      * <a href="http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#78188">§8.4.3</a>,      * <a href="http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#42018">§8.8.3</a>, and     * <a href="http://java.sun.com/docs/books/jls/second_edition/html/interfaces.doc.html#235947">§9.1.1</a>.       * The full modifier ordering used by this method is:     * <blockquote> <code>      * public protected private abstract static final transient     * volatile synchronized native strictfp     * interface </code> </blockquote>      * The <code>interface</code> modifier discussed in this class is     * not a true modifier in the Java language and it appears after     * all other modifiers listed by this method.  This method may     * return a string of modifiers that are not valid modifiers of a     * Java entity; in other words, no checking is done on the     * possible validity of the combination of modifiers represented     * by the input.     *     * @parammod a set of modifiers     * @returna string representation of the set of modifiers     * represented by <code>mod</code>     */    public static String toString(int mod) {StringBuffer sb = new StringBuffer();int len;if ((mod & PUBLIC) != 0)sb.append("public ");if ((mod & PROTECTED) != 0)sb.append("protected ");if ((mod & PRIVATE) != 0)sb.append("private ");/* Canonical order */if ((mod & ABSTRACT) != 0)sb.append("abstract ");if ((mod & STATIC) != 0)sb.append("static ");if ((mod & FINAL) != 0)sb.append("final ");if ((mod & TRANSIENT) != 0)sb.append("transient ");if ((mod & VOLATILE) != 0)sb.append("volatile ");if ((mod & SYNCHRONIZED) != 0)sb.append("synchronized ");if ((mod & NATIVE) != 0)sb.append("native ");if ((mod & STRICT) != 0)sb.append("strictfp ");if ((mod & INTERFACE) != 0)sb.append("interface ");if ((len = sb.length()) > 0)/* trim trailing space */    return sb.toString().substring(0, len-1);return "";    }    /*     * Access modifier flag constants from <em>The Java Virtual     * Machine Specification, Second Edition</em>, tables 4.1, 4.4,     * 4.5, and 4.7.     */    /**     * The <code>int</code> value representing the <code>public</code>      * modifier.     */        public static final int PUBLIC           = 0x00000001;    /**     * The <code>int</code> value representing the <code>private</code>      * modifier.     */        public static final int PRIVATE          = 0x00000002;    /**     * The <code>int</code> value representing the <code>protected</code>      * modifier.     */        public static final int PROTECTED        = 0x00000004;    /**     * The <code>int</code> value representing the <code>static</code>      * modifier.     */        public static final int STATIC           = 0x00000008;    /**     * The <code>int</code> value representing the <code>final</code>      * modifier.     */        public static final int FINAL            = 0x00000010;    /**     * The <code>int</code> value representing the <code>synchronized</code>      * modifier.     */        public static final int SYNCHRONIZED     = 0x00000020;    /**     * The <code>int</code> value representing the <code>volatile</code>      * modifier.     */        public static final int VOLATILE         = 0x00000040;    /**     * The <code>int</code> value representing the <code>transient</code>      * modifier.     */        public static final int TRANSIENT        = 0x00000080;    /**     * The <code>int</code> value representing the <code>native</code>      * modifier.     */        public static final int NATIVE           = 0x00000100;    /**     * The <code>int</code> value representing the <code>interface</code>      * modifier.     */        public static final int INTERFACE        = 0x00000200;    /**     * The <code>int</code> value representing the <code>abstract</code>      * modifier.     */        public static final int ABSTRACT         = 0x00000400;    /**     * The <code>int</code> value representing the <code>strictfp</code>      * modifier.     */        public static final int STRICT           = 0x00000800;    // Bits not (yet) exposed in the public API either because they    // have different meanings for fields and methods and there is no    // way to distinguish between the two in this class, or because    // they are not Java programming language keywords    static final int BRIDGE    = 0x00000040;    static final int VARARGS   = 0x00000080;    static final int SYNTHETIC = 0x00001000;    static final int ANNOTATION= 0x00002000;    static final int ENUM      = 0x00004000;    static boolean isSynthetic(int mod) {      return (mod & SYNTHETIC) != 0;    }}


    通过观察源码其实不难发现Modifier这个类非常之简单,Modifier类提供了一系列的"isXXX"方法,如下MethodSummary。可以使用这一系列的方法来分析int值。例如,如果参数包含"public"修饰符,静态方法isPublic()就返回true;否则返回false。
  2. Modifier类的常用方法


    Method Summarystatic booleanisAbstract(int mod) 
              Return true if the integer argument includes the abstract modifier, false otherwise.static booleanisFinal(int mod) 
              Return true if the integer argument includes the final modifier, false otherwise.static booleanisInterface(int mod) 
              Return true if the integer argument includes the interface modifier, false otherwise.static booleanisNative(int mod) 
              Return true if the integer argument includes the native modifier, false otherwise.static booleanisPrivate(int mod) 
              Return true if the integer argument includes the private modifier, false otherwise.static booleanisProtected(int mod) 
              Return true if the integer argument includes the protected modifier, false otherwise.static booleanisPublic(int mod) 
              Return true if the integer argument includes the public modifier, false otherwise.static booleanisStatic(int mod) 
              Return true if the integer argument includes the static modifier, false otherwise.static booleanisStrict(int mod) 
              Return true if the integer argument includes the strictfp modifier, false otherwise.static booleanisSynchronized(int mod) 
              Return true if the integer argument includes the synchronized modifier, false otherwise.static booleanisTransient(int mod) 
              Return true if the integer argument includes the transient modifier, false otherwise.static booleanisVolatile(int mod) 
              Return true if the integer argument includes the volatile modifier, false otherwise.static StringtoString(int mod) 
              Return a string describing the access modifier flags in the specified modifier.
  3. Demo示例
    package com.davidchou.test.reflection;import java.lang.reflect.Method;import java.lang.reflect.Modifier;import junit.framework.TestCase;/** * java.lang.reflect.Modifier类研究 * @author DavidChou * @since 2013/03/24 19:13 */public class ModifierTest extends TestCase{class A {public void method1(){}public void method2(){}private void method3(){}protected void method4(){}}public void test(){A a = new A();Class<?> clazz = a.getClass();Method[] methods = clazz.getDeclaredMethods();for (Method method : methods) {int modifiers = method.getModifiers();if(Modifier.isPublic(modifiers)){System.out.println("public method:" + method);}else if(Modifier.isPrivate(modifiers)){System.out.println("private method:" + method);}}}}

    执行单元测试,运行结果:
    public method:public void com.davidchou.test.reflection.ModifierTest$A.method1()public method:public void com.davidchou.test.reflection.ModifierTest$A.method2()private method:private void com.davidchou.test.reflection.ModifierTest$A.method3()
    PS:
    从JDK7开始,Modifier类还提供了一套静态方法,这套方法返回一些访问修饰符的类型,这些访问修饰符能够应用于特定类型的程序元素。这套方法是:

    static int classModifiers();static int constructorModifiers();static int fieldModifiers();static int methodModifiers();


  4. 参考文档
    http://docs.oracle.com/javase/6/docs/api/
    http://docs.oracle.com/javase/7/docs/api/
原创粉丝点击