你能在 GWT 客户端中使用 Java Reflection api吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4195233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:15:43  来源:igfitidea点击:

Can you use Java Reflection api in GWT client

javareflectiongwtclient-side

提问by Farouk Alhassan

IS it possible to use the java reflection api in GWT client side? I want to use reflections to find the value of a property on a Javabean. Is this possible?

是否可以在 GWT 客户端使用 java 反射 api?我想使用反射来查找 Javabean 上的属性值。这可能吗?

采纳答案by Joao Pereira

I've been there and the solution indeed is to use Deferred Binding and Generators. You can see a use of Generators to overcome the lack of Reflection in GWT client here:

我去过那里,解决方案确实是使用延迟绑定和生成器。您可以在此处看到使用生成器来克服 GWT 客户端中缺少反射的情况:

http://jpereira.eu/2011/01/30/wheres-my-java-reflection/

http://jpereira.eu/2011/01/30/wheres-my-java-reflection/

Hope it helps.

希望能帮助到你。

回答by Adrian Aslau

You can use the GWT Generators functionality that allows you to generate code during the GWT compile phase.

您可以使用 GWT Generators 功能,该功能允许您在 GWT 编译阶段生成代码。

Your bean, that you want to introspect, can extend a class that has a method defined as

您想要内省的 bean 可以扩展一个类,该类的方法定义为

public Object getProperty(String propertyName){}

Let's call this class IntrospectionBean.

让我们称这个类IntrospectionBean

Let's say that you then have your bean defined as:

假设您将 bean 定义为:

public class MyBean extends IntrospectionBean {
    private String prop1;
    private String prop2;
}

The GWT generator will have access to all fields of MyBean and it can generate the getProperty(String propertyName)method during GWT compile time, after iterating through all fields of MyBean.

GWT 生成器将有权访问 MyBean 的所有字段,并且它可以getProperty(String propertyName)在 GWT 编译期间在遍历 MyBean 的所有字段后生成该方法。

The generated class might look like this:

生成的类可能如下所示:

public class MyBean extends IntrospectionBean {
    private String prop1;
    private String prop2;

    public Object getProperty(String propertyName) {
        if ("propr1".equals(propertyName)) {
            return prop1;
        }
        if ("propr2".equals(propertyName)) {
            return prop2;
        }

        return null;
    }
}

You could simply then use myBean.getProperty("prop1")in order to retrieve a property based on it's name at runtime.

然后您可以简单地使用myBean.getProperty("prop1")以在运行时根据其名称检索属性。

Hereyou can find an example of how to implement a gwt generator

在这里您可以找到如何实现 gwt 生成器的示例

回答by Faisal Feroz

Since GWT code is translated to Javascript direct usage of reflection API is not supported.

由于 GWT 代码被转换为 Javascript,因此不支持直接使用反射 API。

There is a small project GWT-Reflection, that allows to use reflection in GWT.

有一个小项目GWT-Reflection,它允许在 GWT 中使用反射。

回答by Ajax

I have made my gwt-reflection library public.

我已经公开了我的 gwt-reflection 库。

https://github.com/WeTheInternet/xapi/tree/master/gwt/gwt-reflecthttps://github.com/WeTheInternet/gwt-sandbox/tree/xapi-gwt/user/src/com/google/gwt/reflect

https://github.com/WeTheInternet/xapi/tree/master/gwt/gwt-reflecthttps://github.com/WeTheInternet/gwt-sandbox/tree/xapi-gwt/user/src/com/google/gwt /反映

Due to classpath issues with trying to make Gwt pick my version of Class.java over its own, I finally just forked Gwt, added java 8 and reflection support, and now maintain net.wetheinter:gwt-*:2.7.0 which has this support baked in (I will release a 2.8 some time after Gwt 2.8 goes live)

由于试图让 Gwt 选择我自己的 Class.java 版本的类路径问题,我最终只对 Gwt 进行了分叉,添加了 java 8 和反射支持,现在维护 net.wetheinter:gwt-*:2.7.0 有这个支持出炉(我将在 Gwt 2.8 上线后的某个时间发布 2.8)

It supports three levels of reflection:

它支持三个级别的反射:

Monolithic:

单体:

// Embeds all data needed to perform reflection into hidden fields of class
GwtReflect.magicClass(SomeClass.class);
SomeClass.getField(fieldName).set(null, 1);

Lightweight:

轻的:

// Allows direct reflection, provided ALL parameters are literals, or traced to literals
SomeClass.class.getField("FIELD_NAME").set(null, 1);

Flyweight:

蝇量级:

// Skips creating a Field object entirely, and just invokes the accessor you want
// All params must be literals here as well
GwtReflect.set(SomeClass.class, "FIELD_NAME", null, 1);

These examples also work for Methods and Constructors. There's basic support for annotations, and more to come in the future.

这些示例也适用于方法和构造函数。有对注释的基本支持,未来还会有更多支持。

回答by Mohammad Sadegh Rafiei

GWT not support reflection fully, you can see bellow link :

GWT 不完全支持反射,你可以看到下面的链接:

http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsCompatibility.html

http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsCompatibility.html

You should note the border between java and javascript. In GWT, all code compiles to javascript, so you have to check if JavaScript is a well-defined reflection.

您应该注意 java 和 javascript 之间的边界。在 GWT 中,所有代码都编译为 javascript,因此您必须检查 JavaScript 是否是定义良好的反射。

回答by Ajax

If you just want to use reflection to grab a private field, consider using jsni (javascript native interface) instead; it has no notion of private or public, so you can just grab anything you want like so:

如果只想使用反射来抓取私有字段,请考虑使用 jsni(javascript 原生接口);它没有私人或公共的概念,所以你可以像这样抓住任何你想要的东西:

package com.foo;

class SomeClass {
    private String someField;
    private static int someInt;
}

//accessors:
native String ripField(SomeClass from)
/*-{
  return [email protected]::someField;
}-*/;
native int ripInt()
/*-{
  return @com.foo.SomeClass::someInt;
}-*/;

Also, I am in the middle of finishing up emulation for java.lang.ClassnewInstance/ reflection.

另外,我正在完成java.lang.ClassnewInstance/反射的模拟。

I'll post back here with a link in about two days if you'd like to play with it.

如果你想玩它,我会在大约两天后用链接发回这里。

It requires that you pass a class through a method which I route to a custom generator (like GWT.create, except it returns a generated java.lang.Class with field and method accessors that just point to jsni methods / fields. :)

它要求您通过一个我路由到自定义生成器的方法传递一个类(例如GWT.create,除了它返回一个生成的 java.lang.Class 带有仅指向 jsni 方法/字段的字段和方法访问器。:)