简体   繁体   English

isAbstract() 修饰符返回不正确的结果 - 为什么?

[英]isAbstract() Modifier returning Incorrect result - Why?

To my understanding the following code should print False as output根据我的理解,以下代码应该打印False作为输出

However, when I ran this code it is printing True as output.但是,当我运行此代码时,它将True作为输出打印。

From Java docs:来自 Java 文档:

Return true if the integer argument includes the abstract modifier, false otherwise.如果整数参数包含抽象修饰符,则返回 true,否则返回 false。

public class Test{
    public static void main(String[] args) {
        System.out.println(Modifier.isAbstract(byte[].class.getModifiers())); 
    }
}

Can some one help me understand this behavior ?有人可以帮助我理解这种行为吗?

The Javadoc of int java.lang.Class.getModifiers() specifies what should be returned for some of the modifiers for array types (for example, the final modifier is required to be true and the interface modifier is required to be false ). int java.lang.Class.getModifiers()Javadoc指定了对于数组类型的某些修饰符应该返回什么(例如, final修饰符必须为true ,而interface修饰符必须为false )。 On the other hand, it doesn't specify what the abstract or static modifiers should be for array types, which means the decision to return true or false is not documented in the JDK.另一方面,它没有指定数组类型的abstractstatic修饰符应该是什么,这意味着返回truefalse的决定没有记录在 JDK 中。 Therefore any implementation can choose to return either true or false .因此,任何实现都可以选择返回truefalse

int java.lang.Class.getModifiers() int java.lang.Class.getModifiers()

Returns the Java language modifiers for this class or interface, encoded in an integer.返回此类或接口的 Java 语言修饰符,以整数编码。 The modifiers consist of the Java Virtual Machine's constants for public, protected, private, final, static, abstract and interface;修饰符由 Java 虚拟机的 public、protected、private、final、static、abstract 和 interface 常量组成; they should be decoded using the methods of class Modifier.它们应该使用类 Modifier 的方法进行解码。

If the underlying class is an array class, then its public, private and protected modifiers are the same as those of its component type .如果底层类是数组类,则其 public、private 和 protected 修饰符与其组件类型的修饰符相同 If this Class represents a primitive type or void, its public modifier is always true, and its protected and private modifiers are always false.如果此类表示原始类型或 void,则其 public 修饰符始终为 true,其 protected 和 private 修饰符始终为 false。 If this object represents an array class , a primitive type or void, then its final modifier is always true and its interface modifier is always false .如果此对象表示数组类、原始类型或 void,则其最终修饰符始终为 true,其接口修饰符始终为 false The values of its other modifiers are not determined by this specification.它的其他修饰符的值不是由本规范确定的。

The modifier encodings are defined in The Java Virtual Machine Specification, table 4.1.修饰符编码在 Java 虚拟机规范表 4.1 中定义。

A hint to this behavior may be found in the JLS, 10.8.可以在 JLS 10.8 中找到对此行为的提示 Class Objects for Arrays : 数组的类对象

Every array has an associated Class object, shared with all other arrays with the same component type.每个数组都有一个关联的 Class 对象,与具有相同组件类型的所有其他数组共享。

Although an array type is not a class, the Class object of every array acts as if: [snipped]虽然数组类型不是类,但每个数组的 Class 对象都表现为:[snipped]

Under this reasoning, an array isn't a "real" class, so it definitely isn't a concrete class.根据这种推理,数组不是“真正的”类,因此它绝对不是具体的类。 The same logic would apply to int.class being considered abstract.相同的逻辑适用于被视为抽象的int.class

The definition of abstract says: 抽象的定义说:

An abstract class is a class that is incomplete, or to be considered incomplete.抽象类是不完整或被认为不完整的类。

If there were a pure array like [] then it would be indeed incomplete since no component type is provided.如果有一个像[]这样的纯数组,那么它确实是不完整的,因为没有提供组件类型。

This would violate the specification of 15.10.1.这将违反15.10.1的规范 Array Creation Expressions : 数组创建表达式

It is a compile-time error if the ClassOrInterfaceType does not denote a reifiable type.如果 ClassOrInterfaceType 不表示可具体化的类型,则会出现编译时错误。

It does not just denote a reifiable type but no type at all.它不仅表示可具体化的类型,而且根本没有类型。 Thus it would be inpossible to create instances of [] - just like for abstract classes.因此,创建[]实例是不可能的——就像抽象类一样。

Since there is no pure array [] this is only kind of speculation.由于没有纯数组[]这只是一种推测。 Moreover the modifiers were returned for byte[] .此外,还为byte[]返回了修饰符。 It remains the specification shown by Eran .它仍然是Eran显示的规范。

My explantation would be, that arrays are considered abstract because the are instantiated by the JVM itself.我的解释是,数组被认为是abstract因为它们是由 JVM 本身实例化的。

There just exists no concrete class for any array type.任何数组类型都不存在具体的类。

An array has a contract defined by the JLS :一个数组有一个 由 JLS 定义契约

  1. Index Accessability索引可访问性
  2. Implementation for Cloneable and SerializableCloneable和可Serializable

But nobody except the Language itself can fulfill those, because we can't really declare an implementation ourself.但是除了语言本身之外,没有人可以实现这些,因为我们自己无法真正声明实现。

From My understanding the java language specification for getModifier() is:根据我的理解, getModifier()的 Java 语言规范是:

If the underlying class is an array class, then its public, private and protected modifiers are the same as those of its component type.如果底层类是一个数组类,那么它的 public、private 和 protected 修饰符与其组件类型的修饰符相同。 If this Class represents a primitive type or void, its public modifier is always true, and its protected and private modifiers are always false如果此类表示原始类型或 void,则其 public 修饰符始终为 true,其 protected 和 private 修饰符始终为 false

Now, The values of its other modifiers are not determined by this specification eg ABSTRACT .现在,它的其他修饰符的值不是由这个规范决定的,例如ABSTRACT

From JVMS Table 4.1-A:来自 JVMS 表 4.1-A:

ACC_ABSTRACT 0x0400 Declared abstract; ACC_ABSTRACT 0x0400 声明的抽象; must not be instantiated.不得实例化。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM