What sets an "interface name" apart from a "class name"?

WorldSEnder :

In paragraph 4.4.2 of the java 8 specification (similarly in a few other places around there) it is mentioned

class_index

The value of the class_index item must be a valid index into the constant_pool table. The constant_pool entry at that index must be a CONSTANT_Class_info structure (§4.4.1) representing a class or interface type that has the field or method as a member.

The class_index item of a CONSTANT_Methodref_info structure must be a class type, not an interface type.

I've looked around but as far as I can tell, it is impossible from the bytecode alone to tell apart interface types from class types i.e. to actually check this requirement before resolving the named class/interface at runtime against a class loader.

Am I correct in that assumption or did I miss a way to check this requirement from for a single CONSTANT_Class_info in a class file in isolation?

Holger :

You’re right, the CONSTANT_Class_info won’t tell you whether the referenced type is an interface and besides actually resolving the type, you can only collect hints within your current class file about the expected nature of the type and perhaps detect, when it has been used inconsistently.

Besides CONSTANT_Methodref_info expecting a declared type not to be an interface and CONSTANT_InterfaceMethodref_info expecting it to be an interface, the presence of a type reference in the super_class field implies a non-interface type whereas any type present in the interfaces array implies an interface.

In case of nested types, you may check the InnerClasses Attribute, which lists all nested types with their access flags, including ACC_INTERFACE.

Further, each type used as an annotation is technically an interface, though the annotation attributes do not use CONSTANT_Class_info pool items but rather refer to type names and signature strings (CONSTANT_Utf8_info) directly.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=161270&siteId=1