Can you use Java Reflection api in GWT client
Asked Answered
W

6

24

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?

Wicklund answered 16/11, 2010 at 14:35 Comment(0)
B
12

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:

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

Hope it helps.

Bac answered 30/1, 2011 at 21:9 Comment(0)
W
13

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

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

public Object getProperty(String propertyName){}

Let's call this class IntrospectionBean.

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

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.

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.

Here you can find an example of how to implement a gwt generator

Wrap answered 16/11, 2010 at 15:19 Comment(0)
B
12

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:

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

Hope it helps.

Bac answered 30/1, 2011 at 21:9 Comment(0)
A
10

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

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

Apocarp answered 16/11, 2010 at 14:43 Comment(3)
Interesting project. Note: Before using it, make sure that you can accept the GPL license.Marquet
Nope can't use that because of the license. Anyway thanks for the pointerWicklund
Note my comments below... I've released an Apache licensed complete implementation of the reflection API: github.com/WeTheInternet/xapi/tree/master/gwt/gwt-reflectMeditate
M
2

I have made my gwt-reflection library public.

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

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)

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.

Meditate answered 29/10, 2013 at 17:0 Comment(0)
K
2

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

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.

Kalpa answered 19/8, 2018 at 3:31 Comment(0)
M
0

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:

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.Class newInstance / reflection.

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. :)

Meditate answered 12/1, 2013 at 9:55 Comment(3)
I got all excited when I first saw this answer, but on further inspection it has serious shortcomings, such as having to know the class type in advance (cannot use in generics) and having to have one 'rip' method per field, instead of supplying the field name. Might just as well have a normal javabean getter.Relapse
...unless you don't control the code you need to access, and would rather not maintain copies of someone else's code to get access to the private members. Also, how would generics help at all with reflection? Generics work via erasure, and jsni matches that.Meditate
Just another thought, you can generate all of this. I maintain a public build of GWT, net.wetheinter:gwt-*:2.7.0 that has reflection implemented via magic method injection. It is a fork of Gwt because I had to hijack the mechanism for GWT.create to find and replace reflection done via class / string literals with generated code that looks like the example above. There used to be a version that would let you enhance a class with GWT.create to avoid having to hack into the compiler, but there was not enough interest to maintain that (it also generated tons of code).Meditate

© 2022 - 2024 — McMap. All rights reserved.