KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > dev > shell > JavaDispatchImpl


1 /*
2  * Copyright 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.dev.shell;
17
18 import java.lang.reflect.Field JavaDoc;
19 import java.lang.reflect.Member JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
21
22 /**
23  * Class for wrapping Java things for JavaScript.
24  */

25 public class JavaDispatchImpl implements JavaDispatch {
26
27   private final CompilingClassLoader classLoader;
28
29   private final Object JavaDoc target;
30
31   /**
32    * This constructor initializes a dispatcher for handling static members.
33    *
34    * @param ccl class loader to use for dispatching member access
35    */

36   public JavaDispatchImpl(CompilingClassLoader ccl) {
37     classLoader = ccl;
38     target = null;
39   }
40
41   /**
42    * This constructor initializes a dispatcher around a particular instance.
43    *
44    * @param ccl class loader to use for dispatching member access
45    * @param target the instance object to use for dispatching member accesses
46    *
47    * @throws NullPointerException if target is null
48    */

49   public JavaDispatchImpl(CompilingClassLoader ccl, Object JavaDoc target) {
50     if (target == null) {
51       throw new NullPointerException JavaDoc("target cannot be null");
52     }
53
54     classLoader = ccl;
55     this.target = target;
56   }
57
58   /**
59    * @param dispId the unique number of a field
60    * @return the field
61    */

62   public Field JavaDoc getField(int dispId) {
63     return (Field JavaDoc) getMember(dispId);
64   }
65
66   /**
67    * @param dispId the unique number of a field
68    * @return true the value of the field
69    * @throws IllegalArgumentException
70    */

71   public Object JavaDoc getFieldValue(int dispId) {
72     Field JavaDoc field = (Field JavaDoc) getMember(dispId);
73     try {
74       return field.get(target);
75     } catch (IllegalAccessException JavaDoc e) {
76       // should never, ever happen
77
e.printStackTrace();
78       throw new RuntimeException JavaDoc(e);
79     }
80   }
81
82   /**
83    * @param dispId the unique number of a method
84    * @return the method
85    */

86   public Method JavaDoc getMethod(int dispId) {
87     return (Method JavaDoc) getMember(dispId);
88   }
89
90   public Object JavaDoc getTarget() {
91     return target;
92   }
93
94   /**
95    * @param dispId the unique number of a method or field
96    * @return true if the dispId represents a field
97    */

98   public boolean isField(int dispId) {
99     if (dispId < 0) {
100       return false;
101     }
102
103     return getMember(dispId) instanceof Field JavaDoc;
104   }
105
106   /**
107    * @param dispId the unique number of a method or field
108    * @return true if the dispId represents a method
109    */

110   public boolean isMethod(int dispId) {
111     if (dispId < 0) {
112       return false;
113     }
114
115     return getMember(dispId) instanceof Method JavaDoc;
116   }
117
118   /**
119    * @param dispId the unique number of a field
120    * @param value the value to assign to the field
121    * @throws IllegalArgumentException
122    */

123   public void setFieldValue(int dispId, Object JavaDoc value) {
124     Field JavaDoc field = (Field JavaDoc) getMember(dispId);
125     try {
126       field.set(target, value);
127     } catch (IllegalAccessException JavaDoc e) {
128       // should never, ever happen
129
e.printStackTrace();
130       throw new RuntimeException JavaDoc(e);
131     }
132   }
133
134   /**
135    * @param dispId the unique number of a method or field
136    * @return the member
137    */

138   protected Member JavaDoc getMember(int dispId) {
139     DispatchClassInfo clsInfo = classLoader.getClassInfoByDispId(dispId);
140     return clsInfo.getMember(dispId);
141   }
142 }
143
Popular Tags