`

gwt反射 http://code.google.com/p/google-gin/wiki/GinTutorial

    博客分类:
  • gwt
阅读更多
GWT Reflection - Overview <!-- <a name="overview"><IMG SRC="img/asioc_09.gif" WIDTH=85 HEIGHT=20 ALT="Overview"></a>-->

GWT Reflection is a small framework which give you ability to use reflection API on client side (yes! on browse side!) of application. Google Web Toolkit obfuscate JavaScript so it is not readable for application in execution time. So.. it is hard to use any kind of reflection on browser side of application. To make JavaScript reflectable we must create some helper that could memorize class description in compilation time. This description will later (in runtime) help us to manipulate classes, even after obfuscate.

GWT Reflection are composed of two component GWT Reflection Wrapper and GWT Reflection Description Generator.

GWT Reflection Wrapper - provides basic functionality for Java 1.4 reflection API, we use this component on client side.
GWT Reflection Description Generator - provides simple tool (you can use it as ANT task) which create helpers for GWT Reflection Wrapper. This description will by used in runtime of application to manipulate your application classes.

To make it more clear let's look at a first example. (Quick start)
<!-- Overview -->Quick start
First of all you should download:

GWT Reflection Wrapper
GWT Reflection Description Generator
GWT Reflection Hello World - sources for following example
<!-- <tr> <td valign="top"><b>Step&nbsp;3.</b></td> <td>Next step is to package our classes to JAR file. If you created project using -ant option you should have file in your project root folder looking like this:<br><br> <table class="source"> <tr> <td> <pre><span class="source"></span></pre> <br /><br /> Only what you must do is to run <i>'compile'</i> and <i>'package'</i> tasks. </td> </tr> </table> <br> </td> </tr> -->
Step 1. Let's create new project in eclipse, name it 'gwtreflect-helloworld'.
Using gwt tool create project

./projectCreator 
	-eclipse gwtreflect-helloworld 
	-ant gwtreflect-helloworld 
	-out ~/workspace/java-eclipse/gwtreflect-helloworld 
	-overwrite	

and create new application

./applicationCreator 
	-eclipse gwtreflect-helloworld 
	-out ~/workspace/java-eclipse/gwtreflect-helloworld 
	pl.rmalinowski.gwtreflect.helloworld.client.GWTRHelloWorld

Now add new library to project (Eclipse menu-> Project propertis -> java build files -> librarys -> Add Jar -> gwtreflect.jar
Step 2. Create new package 'pl.rmalinowski.gwtreflect.helloworld.client.someclasses' and add new classes in here. We will use reflection API on this class. This class will implements Reflectable interface. We use Reflectable interface like IsSerializable, to show that we want to memorize class description for future use (see Step 4). In future you will see that you dont need to implements Reflectable interface, but for now it is easest way.

package pl.rmalinowski.gwtreflect.helloworld.client.someclasses;

import pl.rmalinowski.gwtreflect.client.reflect.Reflectable;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;

public class MyClass implements Reflectable {

    private Integer value;

    public MyClass () {

    }

    public Integer getValue() {
        return value;
    }

    public void setValue(Integer value) {
        this.value = value;
    }

    public String showValue() {
        String msg = "Value = " + value;
        Window.alert(msg);
        GWT.log("Alert shown (" + msg +")", null);
        return msg;
    }

}

Step 3. Next step is to package our classes to JAR file. If you created project using -ant option you should have file in your project root folder looking like this:


<?xml version="1.0" encoding="utf-8" ?>
<project name="gwtreflect-helloworld" 
	default="compile" 
	basedir=".">

<!-- set classpath -->
  <path id="project.class.path">
    <pathelement path="${java.class.path}/"/>
    <pathelement path="/opt/gwt/gwt-user.jar"/>
    <!-- Additional dependencies (such as junit) go here -->
  </path>

  <target name="compile" 
	description="Compile src to bin">

    <mkdir dir="bin"/>
    <javac srcdir="src:test" 
		destdir="bin" includes="**" 
		debug="on" debuglevel="lines,vars,source" 
		source="1.4">
      <classpath refid="project.class.path"/>
    </javac>
  </target>

  <target name="package" 
	depends="compile" 
	description="Package up the project as a jar">

    <jar destfile="gwtreflect-helloworld.jar">
      <fileset dir="bin">
        <include name="**/*.class"/>
      </fileset>
      <!-- Get everything; source, modules, html files -->
      <fileset dir="src">
        <include name="**"/>
      </fileset>
      <fileset dir="test">
        <include name="**"/>
      </fileset>
    </jar>
  </target>

  <target name="clean">
    <!-- Delete the bin directory tree -->
    <delete file="gwtreflect-helloworld.jar"/>
    <delete>
      <fileset dir="bin" includes="**/*.class"/>
    </delete>
  </target>

  <target name="all" depends="package"/>

</project>


Only what you must do is to run 'compile' and 'package' tasks.
Step 4. Now, when we have gwtreflect-helloworld.jar we must generate description for our class. We will use GWT Reflection Description Generator. You can execute this tool from command line, but we will use ant task for do that. Ant task definition and execution code should look like this:

<?xml version="1.0" encoding="UTF-8"?>
<project default="generate-gwtreflect" basedir=".">

	<property name="path.gwtReflectDescGenerator" 
		value="/home/rmalinowski/opt/gwtreflect"/>

	<property name="path.jar.gwtReflectWrapper" 
		value="../gwtreflect/dist/gwtreflect.jar"/>

	<property name="path.helloWorldProj" 
		value="../gwtreflect-helloworld"/>

	
	 <path id="lib.path">
	<fileset dir="${path.gwtReflectDescGenerator}/lib" 
		includes="*.jar"/>
	<fileset dir="${path.gwtReflectDescGenerator}" 
		includes="*.jar"/>
	 </path>
	
	<taskdef name="gwtreflect" 
		classname="pl.rmalinowski.gwtreflect.ant.GWTReflect"
		classpathref="lib.path"
	/>
	
	<target name="generate-gwtreflect">
	<gwtreflect 
		outdir="${path.helloWorldProj}/src" 
		source="${path.helloWorldProj}/gwtreflect-helloworld.jar;
${path.jar.gwtReflectWrapper}" 
		packagein="pl.rmalinowski
.gwtreflect.helloworld.client.someclasses"
		packageout="pl.rmalinowski
.gwtreflect.helloworld.client.gwtr"
		>
		
		
		</gwtreflect>
	</target>
</project>


Run task 'generate-gwtreflect' and refresh project folder in Eclipse IDE.
Step 5. After refresh you should see new package pl.rmalinowski.gwtreflect.helloworld.client.gwtr in your project. In this package you will see :
ClassRegistrator - which are used to make sure that GWT will generate and package your classes to client runtime eviroment.

MyClass__GWTR - where are description for our class MyClass. (see Step 2)

And now you can use reflection API in your browser. Lets extends method onModuleLoad our GWTRHelloWorld class to run and see how it works:
package pl.rmalinowski.gwtreflect.helloworld.client;

import pl.rmalinowski.gwtreflect.client.reflect.Class;
import pl.rmalinowski.gwtreflect.client.reflect.Method;
import pl.rmalinowski.gwtreflect
.client.reflect.NoSuchMethodException;
import pl.rmalinowski.gwtreflect
.helloworld.client.someclasses.MyClass;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;

/**
 * Entry point classes define onModuleLoad().
 */
public class GWTRHelloWorld implements EntryPoint {

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {

		pl.rmalinowski.gwtreflect.helloworld
.client.gwtr.ClassRegistrator.registerAll();

		Class c = null;
		c = Class.forName("pl.rmalinowski
.gwtreflect.helloworld.client.someclasses.MyClass");

		// or
		c = Class.forName(MyClass.class.toString());
		// or
		c = Class.getClass(MyClass.class);
		
		MyClass obj = (MyClass) c.newInstance();
	    
		Method setValue = null;
		
		try {
            setValue = c.getMethod("setValue", 
		new java.lang.Class[]{Integer.class});

        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        setValue.invoke(obj, new Object[]{new Integer(123)});
        
        GWT.log("After setValue invoced, 
		obj.getValue() = "+obj.getValue(), null); 
        

        Method showValue = null;
        try {
            showValue = c.getMethod("showValue", 
			new java.lang.Class[]{});

        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        String retStr = (String) showValue.invoke(obj, null);
        GWT.log("After showValue invoced, retStr = "+retStr, null); 
		
	}
}


Run project and see what happen next :).
<!-- Extended Futures -->Extended Futures
Description of more extanded futures will be soon...
<!-- Road map -->Road Map
This is only beginning of possibilities what have or will have GWT Reflection. First of all there are somu undocumented option and futures. So in the near future I will try to document it. Next I will try to provide in GWT Reflection full compatibility with Java 1.4 reflection API (for now it is approximately 70% of compatibility). And after that and some interest from GWT Community maybe it will be time for some more.
<!-- Text me -->Text me! <!-- <a name="overview"><IMG SRC="img/asioc_09.gif" WIDTH=85 HEIGHT=20 ALT="Overview"></a>-->
If you have any question text me.
分享到:
评论

相关推荐

    Android代码-3D OBJ Viewer

    Online TGA Canvas Demo page is http://npe-net.appspot.com/npesdk/gwt/tgaimagedemo/index.html Online TGA WebGL Texture Demo page is http://npe-net.appspot.com/npesdk/gwt/tgawebgldemo/index.html ...

    gwt-odb-ui:从 code.google.compgwt-odb-ui 自动导出

    组件用Java编写生成所有 javascript (v1.4) 的Google Web Toolkit RDF 数据库(Jena v2.5.5) Dojo在所有浏览器中绘制 SVG/VML 线 (v1.0.2)概念http://gwt.org.ua/odbui/manual/fluxb_layout.html 现场演示...

    java播放器源码-tileboard:从https://storage.googleapis.com/google-code-archive

    java播放器源码瓷砖 Java、GWT 和 C++ 带有用于自动拼字游戏的 protobuf。 应用于解决拼字游戏的通用搜索运算符。 词选择的优化可以使用键匹配应用程序,以及组合优化和启发式方法。..../a.out -d dic

    SEO.Decoded.1523887842.epub

    Sitemaps / GWT / GA Chapter 30. Part #4 – User Interaction Ranking Strategies Chapter 31. Engagement Is Key Chapter 32. Ad Placement Can Seriously Hurt You Chapter 33. Part #5 - Off Page / ...

    gwt-site, gwtproject.org 网站的网页来源.zip

    gwt-site, gwtproject.org 网站的网页来源 文档GWT文档是在 http://www.gwtproject.org/doc/latest/DevGuide.html 发布的。引用Markdown 处理器:https://github.com/sirthias/pegdow

    EXT-GWT2.0.1 API DOC.chm(新版本已可用请看软件说明)

    EXT-GWT2.0.1 java api 文档 chm格式方便查找2009年9月制作 注意:请到下面网址下载新版本, 新版本解决了旧版的部分搜索问题,并且api升级到了2.1.0版,EXT-GWT2.1.0 java api 文档 chm格式 2010年7月制作,地址...

    gwt-comet:从 code.google.compgwt-comet 自动导出

    从 code.google.com/p/gwt-comet 自动导出 概述 这个 gwt-comet 库为 GWT 提供了一个高效的 Comet 实现。 此库已移至 GitHub 该库通过在长期存在的 HTTP 请求上流式传输消息来实现 Comet,以最小化延迟和带宽要求...

    gwt-crypto:从 code.google.compgwt-crypto 自动导出

    从 code.google.com/p/gwt-crypto 自动导出 ===更新 2015-03-12:=== 嗨伙计, 鉴于即将关闭的谷歌代码: : 我将其移至 github: : 我不再支持这个项目了,所以请随意分叉! 担 Brill 说:在 GWT 论坛中,有人...

    自助下单平台PS:发布的所有源码均亲测

    PS:发布的所有源码均亲...演示地址:http://shou.omgmo.xyz/gm/ 安装步骤:上传到网站空,解压(多少级目录都可以),访问http://你的网站域名/install 后台访问访问http://你的网站域名/admin 登录账号admin 密码123456

    org.liveSense.sample.gwt.notes-1.0.5.zip

    org.liveSense.jcr.explorer.zip,LiveSense JCR内容浏览器。这是基于https://code.google.com/p/jackrabbitexplorer/livesense jcr content explorer。基于https://code.google.com/p/jackrabbitexplorer/

    gwt-2.8.2.zip

    谷歌gwt,安装教程:http://www.cnblogs.com/zhwl/p/3560915.html

    GWT简介.docx

    NULL 博文链接:https://mydownload.iteye.com/blog/1157105

    jasmine-wrapper:包装 jasmine 测试库以支持 GWT

    #茉莉花-gwt 用于茉莉花测试框架的简单 GWT 包装器 查看示例模块的内部... 运行示例: gradlew :example:gwtSuperDev 然后导航到: ...

    gwt-dev-plugin-x64.msi

    GWT浏览器插件离线安装包,针对IE的64位安装包,32位安装包转到http://download.csdn.net/detail/promingx/4236601

    gwt-dev-plugin-1.26-rc1.xpi for firefox插件

    原地址如下 http://google-web-toolkit.googlecode.com/files/gwt-dev-plugin-1.26-rc1.xpi

    gwt-scrum-manager:从 code.google.compgwt-scrum-manager 自动导出

    从 code.google.com/p/gwt-scrum-manager 自动导出 大学期间开发的项目。 它完全没用,充满了错误的东西,甚至可能不起作用。 保留在这里只是为了多愁善感的历史目的。 不要用它做任何事情。 原始描述 学术项目 ...

    gwt-2.5.1.zip

    gwt google 安装步骤:http://blog.csdn.net/neuandustbneo/article/details/7932248

    gwt-jet:从 code.google.compgwt-jet 自动导出

    喷气式飞机gwt-jet 库提供了一种快速、灵活且简单的方法来包装要在前端显示的业务对象。 jet 类会自动创建相应的小部件并自动将用户修改的值填充到原始对象中。 我们的第一个 alpha 版本在这里:1.0.9.0 1.0.9.1 - ...

    gwt-2.5.1.part1.rar

    part2:http://download.csdn.net/detail/u011029071/5986791 安装方法http://blog.csdn.net/u011029071/article/details/10143841

Global site tag (gtag.js) - Google Analytics