今天在查看公司原生框架的时候,发现一个Modifier类的用法,简单看了一下发现这是个工具类,位于java.lang.reflect下。
1、功能
该类是修饰符工具类,用于判断和获取某个类、变量、方法的修饰符。
2、简单使用方法
范例:获取某个类的修饰符
package com.classloader.entity; public class User { private int id; private String username; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } }
package com.classloader.reflect; import java.lang.reflect.Modifier; import com.classloader.entity.User; public class ReflectTest { public static void main(String[] args) { Class clazz = User.class; // 获取User类的修饰符 int modifier = clazz.getModifiers(); // 判断是否是abstract修饰符 System.out.println(Modifier.isAbstract(modifier)); // 判断是否是接口 System.out.println(Modifier.isInterface(modifier)); // 获取其修饰符 System.out.println(clazz.getModifiers()); } }
3、访问修饰符列表(源码)
/** * 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;
都是16进制的int类型。
4、方法列表
5、一般使用场合
既然是位于java.lang.reflect下,一般会在动态加载过程中、使用java反射对某些类进行过滤时,会用到,就现在公司的框架,是在扫描类时,用于对部分类过滤使用到的。一般开发并不是很常用,但是对于写框架,个人还是觉得可以用到的。
本文出自 “java程序冥” 博客,请务必保留此出处http://793404905.blog.51cto.com/6179428/1552832
原文地址:http://793404905.blog.51cto.com/6179428/1552832