My favorites | Sign in
Project Logo
                
Search
for
Updated Aug 23, 2009 by JamesLuo.au
Labels: Featured, Phase-Design, Reflection, GWT
UseReflection  
How to use reflection provided by gwt-ent

Introduction

For maximum efficiency, GWT compiles your Java source into a monolithic script. Now gwt-ent provide a reflection implement by gwt code generator mechanism.

This Framework support

  1. Class http://code.google.com/p/gwt-ent/source/browse/trunk/gwtent/src/com/gwtent/client/reflection/ClassType.java
  2. Field http://code.google.com/p/gwt-ent/source/browse/trunk/gwtent/src/com/gwtent/client/reflection/Field.java
  3. Method http://code.google.com/p/gwt-ent/source/browse/trunk/gwtent/src/com/gwtent/client/reflection/Method.java
  4. Constructor http://code.google.com/p/gwt-ent/source/browse/trunk/gwtent/src/com/gwtent/client/reflection/Constructor.java
  5. Metedata http://code.google.com/p/gwt-ent/source/browse/trunk/gwtent/src/com/gwtent/client/reflection/HasMetaData.java
  6. Annotation http://code.google.com/p/gwt-ent/source/browse/trunk/gwtent/src/com/gwtent/client/reflection/HasAnnotations.java

How to use

# add Annotation Or Reflection Marker interface into your pojo class

@Reflectable
public class TestReflection{
}

public class TestReflection implements Reflection{
}

@Reflect_Domain
public class TestReflection{
}

# get ClassType.

ClassType classType = TypeOracle.Instance.getClassType(TestReflection.class);

For example:

public void testReflection(){
    TestReflection test = new TestReflection();
    ClassType classType = TypeOracle.Instance.getClassType(TestReflection.class);
    
    test.setString("set by code");
    System.out.println("after SetByCode:" + test.getString());
    System.out.println("after SetByCode:" + classType.invoke(test, "getString", null));
    
    classType.invoke(test, "setString", new Object[]{"set by invoke"});
    System.out.println("after SetByInvoke:" + classType.invoke(test, "getString", null));
  }
}

The output is :

after SetByCode:set by code
after SetByCode:set by code
after SetByInvoke:set by invoke

Control you javascript size

GWTENT provide you more power to control how much reflection information created.

The Class Scope

You can control whether have:
  1. Class Annotation
  2. Fields
  3. Methods
  4. Constructors
  5. Field and Method Annotations
  6. Relation Types(The field type, the return type of method, the type of Parameters)
  7. Super Classes ( all the super classes of this class will generate reflection information)
  8. Assignable Classes (all assignable classes. if annotate to a class, this is mean All subClasses will generate reflection information; if annotate to a interface, then all implement classes will generate reflection information; if annotate to an annotation, then all types annotated by this annotation will generate reflection information)
Click Here For more Information

The Field/Method Scope

You can control whether have:
  1. Annotations
  2. Field Type
  3. Method Return Type
  4. Method Parameter Type
Click Here For more Information

The TestCase

ReflectionTestCase

Understand Reflection Classes

http://gwt-ent.googlecode.com/svn/trunk/gwtent/src/com/gwtent/client/reflection/


Comment by dandante, Jan 22, 2008

Is there a way to do the equivalent of Class.forName(), assuming you don't know the name of the class beforehand? Or must you know it beforehand and use GWT.create()?

Comment by coll...@laboiteaprog.com, Mar 17, 2008

did somebody was able to use the reflection profided by this project?

Comment by visumagic, Apr 06, 2008

Please provide some sample application.

Thanks Raghu

Comment by JamesLuo.au, Sep 15, 2008

To visumagic

The sample application available for download now.
To Coll...
Now GWTENT divided into three package(gwtent, gwtent-acegi, gwtent-ext), so it's easy to setup this project. Reflection is in "gwtent.jar"
To dandante
I think For now you must using GWT.create()

Comment by jacob.schultz, Feb 16, 2009

any chance of a 1.5 binary? Since my project depends on 1.5 I tried to build for 1.5 myself, but get this error at runtime; Errors in 'transient source for com.gwtent.client.reflection.TypeOracle_Visitor?'

Line 1199: T cannot be resolved to a type Line 1199: Syntax error on token "extends", . expected

Comment by JamesLuo.au, Feb 17, 2009

Hi, jacob, I can't got this issue in my computer, can you try get the source code from svn, then try to compile it?

Comment by jacob.schultz, Feb 25, 2009

Hi James, When I try to use the gwtent.jar directly, I get: "ERROR? Failure to load module 'com.clcbio.CLCServer' java.lang.UnsupportedClassVersionError?: Bad version number in .class file

at java.lang.ClassLoader?.defineClass1(Native Method)"
I guess this is beacuse the library is build for Java 6. Since I have to use Java 5, my only option is to build the jar-file myself. I have checked out the project from SVN, but cant build the jar-file. Instead I tried to include the source and compiled (Java 5) classes from the Eclipse project, into my own project. That lead to the above error, and I stopped trying. Perhaps you could compile a version af the jar-files for Java 5 or have a look at the ant build file so I might build it myself.

Thanks a lot! Jacob

Comment by jacob.schultz, Feb 25, 2009

now I managed to run the ant script and build the jar. At runtime, I get this error again;

ERROR? Errors in 'transient source for com.gwtent.client.reflection.TypeOracle_Visitor?' ERROR? Line 1312: T cannot be resolved to a type ERROR? Line 1312: Syntax error on token "extends", . expected INFO? See snapshot: /tmp/com.gwtent.client.reflection.TypeOracle_Visitor15589?.java

The source from tmp/com.gwtent.client.reflection.TypeOracle_Visitor15589?.java, line 1300;

return Boolean.valueOf(content.retainAll(((java.util.Collection)args0?)));
} else if (methodName.equals("set")) {
checkInvokeParams(methodName, 2, args); return (Object)content.set((((Integer)args0?).intValue()), ((java.lang.Object)args1?));
} else if (methodName.equals("size")) {
checkInvokeParams(methodName, 0, args); return (int)content.size();
} else if (methodName.equals("toArray")) {
checkInvokeParams(methodName, 0, args); return (Object)content.toArray();
} else if (methodName.equals("toArray")) {
checkInvokeParams(methodName, 1, args); return (Object)content.toArray(((T extends java.lang.Object)args0?));
} else return super.invoke(instance, methodName, args);
}

Comment by jacob.schultz, Feb 25, 2009

line 1312 is this one:

return (Object)content.toArray(((T extends java.lang.Object)args0?));
Comment by JamesLuo.au, Mar 09, 2009

Hi, jacob, the issues you logged before was fixed, please refer to this link for more information.

http://code.google.com/p/gwt-ent/issues/detail?id=2&can=1 Thank you.

Comment by vin4friends, Apr 13, 2009

Hi James,

I am new to GWT. In my application I am trying to use gwt-ent 0.6 for reflection. I have used gwtent.jar file and kept it in my classpath and added the mapping for this in my gwt.xml file also. But the application is still not working. At runtime it is throwing lot of errors.

Errors:

Errors in 'jar:file:/D:/ITGCMSWorkspace/itgCMSClient/lib/gwtent.jar!/com/gwtent/client/validate/ClientValidatorFactory.java'
Line 15: No source code is available for type javax.validation.Validator<T>; did you forget to inherit a required module?

and many other errors but of same nature. Please help.

Thanks, Vineet

Comment by archa...@yahoo.co.in, Apr 23, 2009

Hi, I am trying to instantiate a class and I am getting this exception

Failed to create an instance of 'com.gwtent.client.reflection.TypeOracle?' via deferred binding
java.lang.NoClassDefFoundError?: javax/validation/Constraint

Comment by archa...@yahoo.co.in, Apr 23, 2009

Can I use this library to instantiate any class which implements the reflection interface?

Comment by adligo7, Apr 29, 2009

Hi, could you provide a bit more info about how to import the classes?

For instance I added the following to my gwt.xml

<inherits name="com.google.gwt.user.User" /> <inherits name="com.google.gwt.json.JSON" /> <inherits name="com.google.gwt.http.HTTP" /> <inherits name="com.google.gwt.xml.XML" /> <inherits name="org.aspectj.lang.annotation.AspectJAnnotation" />

and the gwtent.jar and gwt-validation.jar to my classpath

but I am getting errors like The import com.google.gwt.validation cannot be resolved exc

TIA, Scott

Comment by david.cmps, Jun 04, 2009

Hi, I can't use your library, I got a lot of errors when I'm trying to use it. I have the following inherits on the gwt.xml file:

<inherits name='com.google.gwt.user.User'/> <inherits name="com.google.gwt.http.HTTP" /> <inherits name="com.google.gwt.xml.XML" /> <inherits name="javax.validation.Validation"/> <inherits name="com.gwtext.GwtExt"/> <inherits name="com.gwtent.GwtEnt"/>

I'm getting a lot of errors, like:

  1. The method assertTrue(boolean) is undefined for the type ValidateTestCase?
2. The method assertNotNull(ClassType?) is undefined for the type ReflectionTestCase? 3. The method create(Class<?>) from the type GWT refers to the missing type IValidator

What i'm missing? Regards.

Comment by y.sowjanya, Jul 08, 2009

Remove client/test and client/validate folders and build the jar again and try...

Comment by sergey.grigorchuk, Sep 13, 2009

How about specifying class to be reflected in .gwt.xml file? I use a POJO from a library and cannot add @Reflectable to POJO so it would be great for me if I could specify this POJO class somewhere e.g. in my .gwt.xml file. Thanks

Comment by JamesLuo.au, Sep 14, 2009

Hi, Sergey, I don't know if gwt provider some way to read constom settings from .gwt.xml file. GWT provide a way to process relation type, you can create your POJO class, using the POJO from other library, then using "@Reflectable(relationTypes=true)" in your POJO class, this will generate reflection information for that class as well. by default, relationTypes is true for @Reflect_Domain?

Comment by JamesLuo.au, Sep 14, 2009

Thanks david & y.sowjanya, I will fix this in next version

Comment by sergey.grigorchuk, Sep 19, 2009

Thanks James, it's exactly what I need.

Comment by plahoda, Jan 07, 2010

Hi James, is <inherits name="com.gwtent.GwtEnt"/> correct module name ? I just put the gwtent.jar to my project but I can't get the setup right. There is also no info on this in your wiki. Thanks

Comment by bg.varun.kumar, Feb 01, 2010

Hi James, I get a lot of errors when i use the gwtent.jar file to implement reflection. Could you please provide some help in terms of the setup that we have to do in order to get it running. I am using gwt 2.0 and Java 5. Please do help. Thank you.

Comment by kmj7777, Mar 11 (3 days ago)

@dandante Just get the Constructor object from ClassType? and use newInstance from that. You'll need to probably use Field access and Method access after that to manipulate it since it returns an Object.


Sign in to add a comment
Powered by Google Project Hosting