简体   繁体   English

为什么java.lang.reflect.Modifier具有公共构造函数?

[英]Why does java.lang.reflect.Modifier have a public constructor?

I'm learning java reflection and I'm curious to know what use java.lang.reflect.Modifier has for a public constructor. 我正在学习Java反射,并且很好奇要知道java.lang.reflect.Modifier对公共构造函数有什么用。 The java documentation doesn't give any description or use for it and the class has only static methods and constants. Java文档没有给出任何描述或使用,并且该类只有静态方法和常量。

EDIT: Added some test code. 编辑:添加了一些测试代码。

import java.lang.reflect.*;

  public class TestModifier {
        Modifier modifier = new Modifier(); // Compiles fine.
       // Math math = new Math(); // Won't compile.  Math() has private access.
}

The constructor exists and it's present in the javadoc although no info is present. 尽管不存在任何信息,但构造函数存在并且存在于javadoc中 I found some information about it in the a source code comment in the GNU classpath , a project that is and I quote: 我在GNU classpath的源代码注释中找到了有关它的一些信息,并且引用了:

(GNU Classpath, Essential Libraries for Java,) is a GNU project to create free core class libraries for use with virtual machines and compilers for the java programming language. (GNU Classpath,Java的基本库)是一个GNU项目,用于创建免费的核心类库,以供Java编程语言的虚拟机和编译器使用。

In their source code for the Modifier class you can find the following non-javadoc comment for the constructor: 在其针对Modifier类源代码中,您可以找到以下针对构造函数的非Javadoc注释:

This constructor really shouldn't be here ... there are no instance methods or variables of this class, so instantiation is worthless. 这个构造函数实际上不应该在这里...没有此类的实例方法或变量,因此实例化毫无价值。 However, this function is in the 1.1 spec, so it is added for completeness. 但是,此功能在1.1规范中,因此为了完整性起见添加了此功能。

I don't know if this is totally accurate since I could only access Java 1.3.1 spec and in the reflection model part of it what you can read is (bold's mine): 我不知道这是否完全准确,因为我只能访问Java 1.3.1规范,在反射模型部分中,您可以读到的是(粗体的)

The Modifier class is an uninstantiable class that exports class methods to decode Java language modifiers for classes and members. Modifier类是无法实例化的类,该类导出类方法以解码类和成员的Java语言修饰符。 The language modifiers are encoded in an integer, and use the encoding constants defined by The Java Virtual Machine Specification. 语言修饰符以整数编码,并使用The Java Virtual Machine Specification定义的编码常数。

I do wanna believe in the comment from the source code, but I can't really point you at the specification where such thing is said.. Even stranger to me is that the previous quote even being from spec 1.3 already states that it's uninstantiable. 我确实想相信源代码中的注释,但是我无法真正指出要说这种话的规范。对我来说,甚至更奇怪的是,即使来自规范1.3的先前引用也已表明它是不可实例化的。

This is definitely not an error in javadoc. 这绝对不是javadoc中的错误。 You can't find such a constructor means this is a default constructor (no arg constructor) which is by default public because the java.lang.reflect.Modifier is a public class. 您找不到这样的构造函数意味着这是默认的构造函数(无arg构造函数),默认情况下它是公共的,因为java.lang.reflect.Modifier是公共类。

如果与default options一起使用,则它可能是javadoc的属性,如果未为该类定义无参数构造函数,并且没有其他带参数的default constructor ,则它将在文档中提供default constructor函数。

Good find, this is an oversight of the API designer. 很好的发现,这是API设计人员的疏忽。

But, it also proves that it is not a big deal at all to instantiate a utility class; 但是,这也证明了实例化一个实用程序类根本不是什么大问题。 new Modifier() has never caused anybody any problem. new Modifier()从未引起任何问题。 Neither would new Math() . new Math()也不会。

Don't listen to chicken little. 不要少听鸡。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Mocking java.lang.reflect.Modifier 方法与 mockito - Mocking java.lang.reflect.Modifier methods with mockito 为什么Enum构造函数不能具有protected或public访问修饰符 - Why Enum constructor can't have protected or public access modifier 为什么 Boolean 对象在 Java 中有一个公共构造函数? - Why does the Boolean object have a public constructor in Java? 为什么Calendar类没有公共构造函数? - Why does the Calendar class not have a public constructor? 什么时候以及为什么发生未实现(java.lang.reflect.InvocationTargetException)? - When and why does Not implemented (java.lang.reflect.InvocationTargetException) occur? 为什么Java类构造函数必须是公共的或受保护的,以便将类扩展到其包之​​外? - Why does a Java constructor have to be public or protected for a class to be extended outside its package? JAVA java.lang.reflect.Constructor无法调用Date构造函数 - JAVA java.lang.reflect.Constructor cannot call Date constructor Java 9接口:为什么默认修改器转换为公共修改器 - Java 9 Interface : Why default Modifier Converted into public Modifier 为什么显示“ java.lang.reflect.InvocationTargetException” - Why “java.lang.reflect.InvocationTargetException” showing Java:为什么构造函数具有访问修饰符? - Java: why a constructor has access modifier?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM