001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.dev.shell;
017:
018: import java.lang.reflect.Field;
019: import java.lang.reflect.Member;
020: import java.lang.reflect.Method;
021:
022: /**
023: * Class for wrapping Java things for JavaScript.
024: */
025: public class JavaDispatchImpl implements JavaDispatch {
026:
027: private final CompilingClassLoader classLoader;
028:
029: private final Object target;
030:
031: /**
032: * This constructor initializes a dispatcher for handling static members.
033: *
034: * @param ccl class loader to use for dispatching member access
035: */
036: public JavaDispatchImpl(CompilingClassLoader ccl) {
037: classLoader = ccl;
038: target = null;
039: }
040:
041: /**
042: * This constructor initializes a dispatcher around a particular instance.
043: *
044: * @param ccl class loader to use for dispatching member access
045: * @param target the instance object to use for dispatching member accesses
046: *
047: * @throws NullPointerException if target is null
048: */
049: public JavaDispatchImpl(CompilingClassLoader ccl, Object target) {
050: if (target == null) {
051: throw new NullPointerException("target cannot be null");
052: }
053:
054: classLoader = ccl;
055: this .target = target;
056: }
057:
058: /**
059: * @param dispId the unique number of a field
060: * @return the field
061: */
062: public Field getField(int dispId) {
063: return (Field) getMember(dispId);
064: }
065:
066: /**
067: * @param dispId the unique number of a field
068: * @return true the value of the field
069: * @throws IllegalArgumentException
070: */
071: public Object getFieldValue(int dispId) {
072: Field field = (Field) getMember(dispId);
073: try {
074: return field.get(target);
075: } catch (IllegalAccessException e) {
076: // should never, ever happen
077: e.printStackTrace();
078: throw new RuntimeException(e);
079: }
080: }
081:
082: /**
083: * @param dispId the unique number of a method
084: * @return the method
085: */
086: public Method getMethod(int dispId) {
087: return (Method) getMember(dispId);
088: }
089:
090: public Object getTarget() {
091: return target;
092: }
093:
094: /**
095: * @param dispId the unique number of a method or field
096: * @return true if the dispId represents a field
097: */
098: public boolean isField(int dispId) {
099: if (dispId < 0) {
100: return false;
101: }
102:
103: return getMember(dispId) instanceof Field;
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;
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 value) {
124: Field field = (Field) getMember(dispId);
125: try {
126: field.set(target, value);
127: } catch (IllegalAccessException e) {
128: // should never, ever happen
129: e.printStackTrace();
130: throw new RuntimeException(e);
131: }
132: }
133:
134: /**
135: * @param dispId the unique number of a method or field
136: * @return the member
137: */
138: protected Member getMember(int dispId) {
139: DispatchClassInfo clsInfo = classLoader
140: .getClassInfoByDispId(dispId);
141: return clsInfo.getMember(dispId);
142: }
143: }
|