是否可以静态反映java方法? [英] Is it possible to reflect a java method statically?

查看:44
本文介绍了是否可以静态反映java方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法静态引用 Java 中的反射方法.这是一些示例代码,可让您了解我正在尝试的内容:

Is there a way to statically reference a method for reflection in Java. Here's some example code to give you an idea of what I am attempting:

public void myFunc(int x) { ... }

public void other() {
    Method m1 = getClass().getMethod("myFunc");  // dynamic
    Method m2 = this.myFunc;                     // static
    Method m3 = MyClass.myFunc;                  // static (alternate)
}

我认识到上述语法不起作用,但我想知道是否有某种与此类似的语法确实有效.我想要一种使用反射的方法,而不必担心通过字符串引用方法的固有危险.

I recognize that the above syntax does not work, but I was wondering if there is some sort of syntax similar to this that actually does work. I want a way to use reflection without worrying about the inherent dangers of referencing a method by a string.

有没有办法做到这一点,还是只是白日做梦?

Is there a way to do this, or is it just a pipe-dream?

推荐答案

方法参考说明

这种比较两个 Person 实例的出生日期的方法已经作为 Person.compareByAge 存在.您可以在 lambda 表达式的主体中调用此方法:

this method to compare the birth dates of two Person instances already exists as Person.compareByAge. You can invoke this method instead in the body of the lambda expression:

Arrays.sort(rosterAsArray,
    (a, b) -> Person.compareByAge(a, b)
);

由于此 lambda 表达式调用现有方法,因此您可以使用 > 方法引用代替 lambda 表达式:

Because this lambda expression invokes an existing method, you can use a > method reference instead of a lambda expression:

Arrays.sort(rosterAsArray, Person::compareByAge);

接着解释各种方法引用:

and it goes on to explain the various kinds of method references:

有四种方法引用:

Reference to a static method      ContainingClass::staticMethodName

Reference to an instance method
of a particular object            containingObject::instanceMethodName

Reference to an instance method   ContainingType::methodName
of an arbitrary object of a
particular type

Reference to a constructor        ClassName::new

<小时>

历史记录(写在 Java 8 定稿之前)


HISTORICAL NOTE (Written before Java 8 was finalized)

我认为 Java 闭包提案有类似的内容.Stephen Colebourne 说:

I think the Java closures proposal has something like this. Stephen Colebourne says:

Stefan 和我很高兴地宣布发布了第一类方法的 v0.4:Java 风格的闭包提案.

Stefan and I are pleased to announce the release of v0.4 of the First-class Methods: Java-style closures proposal.

自 v0.3 以来,我们已尝试整合在各种论坛上收到的一些反馈.主要变化如下:

Since v0.3, we have tried to incorporate some of the feedback received on the various forums. The main changes are as follows:

1) 构造函数和字段字面量.现在可以使用 FCM 语法创建类型安全、编译时更改的 java.lang.reflect.ConstructorField 实例:

1) Constructor and Field literals. It is now possible to create type-safe, compile-time changed instances of java.lang.reflect.Constructor and Field using FCM syntax:

// method literal:
Method m = Integer#valueOf(int);

// constructor literal:
Constructor<Integer> c = Integer#(int);

// field literal:
Field f = Integer#MAX_VALUE;

但我认为这种语法在任何发布的 JVM 中都不可用.闭包本身绝对不在 Java 7 中.您可能会在 Java 8 中看到它.

but I don't think this syntax is available in any shipping JVM. Closures themselves are definitely not in Java 7. You might see it in Java 8.

Java 闭包网站 有一个指向 "Method references" 虽然看起来不像,但它是最新的就像他们改变了很多语法一样.

The Java closures site has a pointer to "Method references" which is a bit more up-to-date though it doesn't look like they've changed the syntax much.

这篇关于是否可以静态反映java方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆