- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
如果你上课scala.runtime.AbstractPartialFunction
来自 Scala 2.10.2(我没有检查其他版本)并比较 AbstractPartialFunction.class.getInterfaces()
的输出和 AbstractPartialFunction.class.getGenericInterfaces()
您可能会注意到,结果不匹配。通用接口(interface)是 scala.Function1<T1, R>
和 scala.PartialFunction<T1, R>
,而 getInterfaces() 只返回 scala.PartialFunction
.从 scala 文档中我可以看到通用信息是正确的,因为 PartialFunction 是一个 Function1。
getInterfaces 的 javadoc 说:
If this object represents a class, the return value is an array containing objects representing all interfaces implemented by the class. The order of the interface objects in the array corresponds to the order of the interface names in the implements clause of the declaration of the class represented by this object.
和 getGenericInterfaces 具有完全相同的文本。
据此(以及其他文本,包括 stackoverflow 信息)我得出结论,数组的顺序和长度是相等的。只是这里不是这样。为什么?
到目前为止,我能够用几个 java7 和 java8 重现它,没有尝试 java6 甚至 java5。
编辑:
AbstractPartialFunction 的 javap 输出(当然只有标题)是:
Compiled from "AbstractPartialFunction.scala"
public abstract class scala.runtime.AbstractPartialFunction<T1, R> implements scala.Function1<T1, R>, scala.PartialFunction<T1, R>`
在那里使用 asm 库和 Textifier 我可以看到这个头信息:
// class version 50.0 (50)
// access flags 0x421
// signature <T1:Ljava/lang/Object;R:Ljava/lang/Object;>Ljava/lang/Object;Lscala/Function1<TT1;TR;>;Lscala/PartialFunction<TT1;TR;>;
// declaration: scala/runtime/AbstractPartialFunction<T1, R> implements scala.Function1<T1, R>, scala.PartialFunction<T1, R>
public abstract class scala/runtime/AbstractPartialFunction implements scala/PartialFunction
加上一个 ScalaSigAttribute。当然这两种情况我都没有展示方法
最佳答案
// Compiled from AbstractPartialFunction.scala (version 1.6 : 50.0, super bit)
// Signature: <T1:Ljava/lang/Object;R:Ljava/lang/Object;>Ljava/lang/Object;Lscala/Function1<TT1;TR;>;Lscala/PartialFunction<TT1;TR;>;
@scala.reflect.ScalaSignature(bytes="some bytes...")
public abstract class scala.runtime.AbstractPartialFunction implements scala.PartialFunction {
类型的签名大致等同于这个 Java 声明:
class AbstractPartialFunction implements PartialFunction, Function1 {}
PartialFunction 扩展 Function1:
// Compiled from PartialFunction.scala (version 1.6 : 50.0, no super bit)
// Signature: <A:Ljava/lang/Object;B:Ljava/lang/Object;>Ljava/lang/Object;Lscala/Function1<TA;TB;>;
@scala.reflect.ScalaSignature(bytes="some bytes...")
public abstract interface scala.PartialFunction extends scala.Function1 {
这里是 the structure of a Java class file :
ClassFile {
u4 magic;
u2 minor_version;
u2 major_version;
u2 constant_pool_count;
cp_info constant_pool[constant_pool_count-1];
u2 access_flags;
u2 this_class;
u2 super_class;
u2 interfaces_count;
u2 interfaces[interfaces_count];
u2 fields_count;
field_info fields[fields_count];
u2 methods_count;
method_info methods[methods_count];
u2 attributes_count;
attribute_info attributes[attributes_count];
}
类型实现的接口(interface)存储在常量池中,并通过引用记录在interfaces数组中:
class signature is stored as an attribute在属性数组中:
The
Signature
attribute records generic signature information for any class, interface, constructor or member whose generic signature in the Java programming language would include references to type variables or parameterized types.
所以这是我对正在发生的事情的有根据的猜测:
Class.getInterfaces()
返回 interfaces 数组的值Class.getGenericInterfaces()
使用类型签名构建其返回值此行为不同于 Oracle JDK 从等效 Java 源生成的字节码,但它显然通过了 JVM 在加载类时所做的所有检查。
关于java - Class#getInterfaces() 和 Class#getGenericInterfaces() 返回不同长度的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22250619/
我刚刚发现 Type.GetInterface 和嵌套类型有一个非常奇怪的行为。 以下示例代码将显示问题,我正在使用接口(interface)的 Type.FullName 来检查给定类型是否派生自该
我创建了一个使用 System.Reflection 命名空间的项目。好吧,当我尝试使用 GetInterfaces 方法时,我发现它在 Windows 应用商店中不存在。 我尝试创建一个扩展方法,并
我尝试编写一个dll并由控制台程序调用,部分编码如下,但不知何故,我不知道为什么 getinterface 总是返回 null,然后我无法调用 dll 中的函数。 MyDLL.dll namespac
我的问题是,我从历史记录中得到的结果与我们较旧的 DLL 不同,当时变化很小,而且我不知道这些变化与显示的错误有何关联。 这是我们的 SecurityPluginServices.dll 模块中方法的
使用Hibernate ,我得到一个List ( BlablaPO 实现 Blabla )。当我做这样的事情时: for(Blabla blabla : list) { Class[] int
我会尽量简洁: 考虑界面: package com.stackoverflow; public interface Printable { void print(); } 我创建了一个实现此接口(
在 .Net 中使用反射,有什么区别: if (foo.IsAssignableFrom(typeof(IBar))) 和 if (foo.GetInterface(typeof(IBar
首先,有很多这样的问题,也许有些 OP 甚至问过这个完全相同的问题。问题是这些问题的答案(接受与否)实际上没有回答这个问题,至少我找不到。 我如何确定一个类直接声明的接口(interface),而不是
我们继承了一个设计不佳的 WCF 服务,我们希望对其进行改进。它的一个问题是它有一百多个方法(在两个不同的接口(interface)上),我们怀疑其中的大部分都没有被使用。我们决定对每个方法进行一些记
本文整理了Java中org.batfish.datamodel.Zone.getInterfaces()方法的一些代码示例,展示了Zone.getInterfaces()的具体用法。这些代码示例主要来
我最近不得不在 .NET 中进行一些反射(reflection),并且在比较泛型类型定义时偶然发现了一个奇怪的行为。 我必须决定一个类型是否是 IDictionary通过提取 Type来自 LINQ
我创建了这样一个类型: TypeBuilder tb = moduleBuilder.DefineType(myname, TypeAttributes.Class | TypeAttri
本文整理了Java中org.hippoecm.repository.api.WorkflowDescriptor.getInterfaces()方法的一些代码示例,展示了WorkflowDescrip
如果你上课scala.runtime.AbstractPartialFunction来自 Scala 2.10.2(我没有检查其他版本)并比较 AbstractPartialFunction.clas
谁能向我解释为什么下面代码中的 GetInterfaces() 会返回一个 FullName = null 的接口(interface)类型? public class Program { s
我需要从 Java 代码调用(使用 Nashorn)一个在 JavaScript 中定义的函数并向其传递一些参数。我没有使用 Invocable.invokeFunction("Foo", arg1,
我有以下代码在 Windows 7 中运行: MbnInterfaceManager mbnInfMgr = new MbnInterfaceManager();
我正在尝试检测一个实现java.sql.PreparedStatement接口(interface)的类。我已经在转换方法中使用以下代码进行了尝试,但它甚至没有经过这样的类。但我正在使用包含 exec
.NET Core 1.1 支持 Type.GetInterfaces() 方法,该方法提供在给定类型上实现的接口(interface)列表。遗憾的是,Type.GetInterfaces() 在 .
我有一个插件架构,我需要检测所有实现接口(interface) ISurface 的插件任何类型(即测试 ISurface<> )。我看到这里有一些使用 LINQ 的建议(例如 this one ),
我是一名优秀的程序员,十分优秀!