Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you use Java Reflection api in GWT client

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?

like image 248
Farouk Alhassan Avatar asked Nov 16 '10 14:11

Farouk Alhassan


People also ask

What is Java reflection API where it is used?

Reflection is a feature in the Java programming language. It allows an executing Java program to examine or "introspect" upon itself, and manipulate internal properties of the program. For example, it's possible for a Java class to obtain the names of all its members and display them.

Why is Java reflection API so important?

In Java, reflection allows us to inspect and manipulate classes, interfaces, constructors, methods, and fields at run time. There is a class in Java named Class that keeps all the information about objects and classes at runtime. The object of Class can be used to perform reflection.

Is reflection safe Java?

1 - Reflection (as a concept) is indeed orthogonal to safety/security. There was a big emphasis in the design of java to make it a safe platform, with static typing, security manager, disciplined usage of class loader, and no way to screw pointers/memory.

Does spring use reflection API?

Spring framework uses dependency injection (DI) to populate the dependencies into beans defined in config files. DI framework actually heavily uses reflection for injecting these bean dependencies.


5 Answers

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

like image 128
Adrian Aslau Avatar answered Sep 30 '22 05:09

Adrian Aslau


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.

like image 39
Joao Pereira Avatar answered Sep 30 '22 04:09

Joao Pereira


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.

like image 34
Faisal Feroz Avatar answered Sep 30 '22 04:09

Faisal Feroz


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.

like image 33
Ajax Avatar answered Sep 30 '22 04:09

Ajax


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.

like image 29
Mohammad Sadegh Rafiei Avatar answered Sep 30 '22 03:09

Mohammad Sadegh Rafiei