【Java 反射】对于获取修饰符需要知道的事

反射

修饰符返回值

虚拟机规范表4.1、4.4、4.5和4.7中的访问修饰符标志常量

修饰符类型修饰符名称标志值(十进制)标志值(十六进制)
PUBLIC公共10x00000001
PRIVATE私有20x00000002
PROTECTED保护40x00000004
STATIC静态80x00000008
FINAL不可变160x00000010
SYNCHRONIZED同步320x00000020
VOLATILE可变640x00000040
TRANSIENT非序列化1280x00000080
NATIVE本地2560x00000100
INTERFACE接口5120x00000200
ABSTRACT抽象10240x00000400
STRICT严格浮点运算20480x00000800

源码出处:package java.lang.reflect.Modifier;

虚拟机规范表4.1、4.4、4.5和4.7中的访问修饰符标志常量

/*
 * Access modifier flag constants from tables 4.1, 4.4, 4.5, and 4.7 of
 * <cite>The Java&trade; Virtual Machine Specification</cite>
 */

/**
 * The {@code int} value representing the {@code public}
 * modifier.
 */
public static final int PUBLIC           = 0x00000001;

/**
 * The {@code int} value representing the {@code private}
 * modifier.
 */
public static final int PRIVATE          = 0x00000002;

/**
 * The {@code int} value representing the {@code protected}
 * modifier.
 */
public static final int PROTECTED        = 0x00000004;

/**
 * The {@code int} value representing the {@code static}
 * modifier.
 */
public static final int STATIC           = 0x00000008;

/**
 * The {@code int} value representing the {@code final}
 * modifier.
 */
public static final int FINAL            = 0x00000010;

/**
 * The {@code int} value representing the {@code synchronized}
 * modifier.
 */
public static final int SYNCHRONIZED     = 0x00000020;

/**
 * The {@code int} value representing the {@code volatile}
 * modifier.
 */
public static final int VOLATILE         = 0x00000040;

/**
 * The {@code int} value representing the {@code transient}
 * modifier.
 */
public static final int TRANSIENT        = 0x00000080;

/**
 * The {@code int} value representing the {@code native}
 * modifier.
 */
public static final int NATIVE           = 0x00000100;

/**
 * The {@code int} value representing the {@code interface}
 * modifier.
 */
public static final int INTERFACE        = 0x00000200;

/**
 * The {@code int} value representing the {@code abstract}
 * modifier.
 */
public static final int ABSTRACT         = 0x00000400;

/**
 * The {@code int} value representing the {@code strictfp}
 * modifier.
 */
public static final int STRICT           = 0x00000800;

推荐写法

注意:当有多个不同种类的修饰符时,实际返回值为多个修饰符的值相加的和

比如方法:

//简单示例方法
public synchronized String getName() {
    return name;
}
//获取方法修饰符(打印的主要代码)
System.out.println(method.getModifiers());

这个方法的修饰符返回值就是 33,即 public + synchronized : 1 + 32 = 33

直接获取修饰符返回值不直观,我们更推荐这样获取修饰符:

//推荐获取方法修饰符的写法
System.out.println(Modifier.toString(method.getModifiers()));

//控制台输出
public synchronized

方法源码出处:package java.lang.reflect.Modifier;

public static String toString(int mod) {
    StringBuilder sb = new StringBuilder();
    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 "";
}

补充:源码中定义的其他常量信息

未(尚未)在公共API中公开的位,因为它们对字段和方法有不同的含义,并且没有区分这类中两者的方法,或者因为它们不是Java编程语言关键字

// 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 final int MANDATED  = 0x00008000;

VARARGS

在Java中,varargs是一种修饰符,它允许方法接受数量可变的参数。这是通过在参数列表中最后一个参数类型后面加上省略号(...)实现的。

VARARGS修饰符在Java中并不存在,这可能是你误解了varargs的概念。在Java中,varargs修饰符是通过在方法参数列表中的最后一个参数类型后面加上...来实现的。

以下是一个使用varargsJava方法的例子:

public class VarargsExample {
 public static void printVarargs(String... varargs) {
     for (String str : varargs) {
         System.out.println(str);
     }
 }

 public static void main(String[] args) {
     printVarargs("Hello", "World", "Java");
 }
}

在上述代码中,printVarargs方法接受一个可变参数,即一个字符串数组。在main方法中,我们调用printVarargs方法并传递了三个字符串作为参数。

注意:varargs只能应用于最后一个参数,而且方法中只能有一个varargs参数。

SYNTHETIC

SYNTHETICJava字节码中的一个修饰符,它表示该字段或方法并非由用户代码显式声明,而是由编译器自动生成的。例如,内部类的引用到外部类的实例会被标记为SYNTHETIC字段。

如果你在查看Java类文件时看到带有SYNTHETIC标记的字段或方法,通常意味着它是由编译器自动生成的,并不需要用户手动编写。

解决方法:

  1. 如果你在分析或者调试代码时遇到了带有SYNTHETIC标记的成员,可以理解这些成员是由编译器添加的,通常不需要用户干预。
  2. 如果你正在使用反射API来访问这些成员,并且想要确保只访问用户定义的字段和方法,可以通过检查成员的isSynthetic()方法来过滤这些自动生成的成员。
  3. 如果你是Java字节码的修改者或生成者,确保不要错误地生成了SYNTHETIC标记,除非你有特殊的需求需要让这部分代码对编译器可见。

MANDATED

MANDATEDJava中用于指示JVM如何使用特定的Java类或接口的修饰符。这个修饰符通常与JNI(Java Native Interface)相关的代码或者Java平台的内部类和接口有关。它是一个由JVM内部使用的标志,并不是开发者直接可控制或使用的。

如果你在代码中看到了 MANDATED 关键字,很可能是因为你正在查看的是Java的内部类或接口,或者是由JNI代码生成的类。

如果你遇到了包含 MANDATED 关键字的错误,通常这意味着你正在尝试访问或操作一个不应该直接被用户代码访问或操作的Java类。解决这类问题通常需要你避免直接操作这些内部类或接口。

如果你是在阅读Java的源码或者JVM的实现时遇到了 MANDATED 关键字,你可以理解为这个类或接口是被JVM的某些特定部分或是JNI调用的,而不是普通的用户代码。

如果你需要进一步的帮助来理解如何处理特定的 MANDATED 修饰符的问题,你需要提供更多的上下文信息,例如,你正在运行的具体代码、错误信息的完整内容,或者你正在使用的Java版本和JVM实现。

  • 20
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值