com.google.gwt.dev.shell.JsValueOOPHM Maven / Gradle / Ivy
/*
* Copyright 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.gwt.dev.shell;
import com.google.gwt.dev.shell.BrowserChannel.JsObjectRef;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.util.IdentityHashMap;
import java.util.Map;
/**
* Represents a JavaScript value in OOPHM.
*/
public class JsValueOOPHM extends JsValue {
/**
* OOPHM implementation of the DispatchObject interface.
*/
static class DispatchObjectOOPHM implements DispatchObject {
private final CompilingClassLoader classLoader;
private final JavaDispatchImpl javaDispatch;
public DispatchObjectOOPHM(CompilingClassLoader ccl) {
javaDispatch = new JavaDispatchImpl(ccl);
classLoader = ccl;
}
public DispatchObjectOOPHM(CompilingClassLoader ccl, Object val) {
javaDispatch = new JavaDispatchImpl(ccl, val);
classLoader = ccl;
}
@Override
public JsValue getField(int dispId) {
JsValueOOPHM jsValue = new JsValueOOPHM();
if (javaDispatch.isField(dispId)) {
Field field = javaDispatch.getField(dispId);
JsValueGlue.set(jsValue, classLoader, field.getType(),
javaDispatch.getFieldValue(dispId));
} else {
MethodAdaptor method = javaDispatch.getMethod(dispId);
AccessibleObject obj = method.getUnderlyingObject();
DispatchMethod dispMethod = (DispatchMethod) classLoader.getWrapperForObject(obj);
if (dispMethod == null) {
dispMethod = new MethodDispatch(classLoader, method);
classLoader.putWrapperForObject(obj, dispMethod);
}
jsValue.setWrappedFunction(method.toString(), dispMethod);
}
return jsValue;
}
@Override
public JsValue getField(String name) {
int dispId = getFieldId(name);
if (dispId < 0) {
// no field by that name, return undefined
return new JsValueOOPHM();
}
return getField(dispId);
}
@Override
public int getFieldId(String name) {
return classLoader.getDispId(name);
}
@Override
public Object getTarget() {
return javaDispatch.getTarget();
}
@Override
public void setField(int dispId, JsValue jsValue) {
if (javaDispatch.isMethod(dispId)) {
throw new RuntimeException("Cannot reassign method "
+ javaDispatch.getMethod(dispId).getName());
}
Field field = javaDispatch.getField(dispId);
Object val = JsValueGlue.get(jsValue, classLoader, field.getType(),
"setField");
javaDispatch.setFieldValue(dispId, val);
}
@Override
public void setField(String name, JsValue jsValue) {
int dispId = getFieldId(name);
if (dispId < 0) {
// no field by that name, and we do not support expands on Java objects
throw new RuntimeException("No such field " + name);
}
setField(dispId, jsValue);
}
}
/**
* Class used to identify a JavaScript undefined value.
*/
private static class UndefinedValue {
}
public static final String JSE_CLASS = "com.google.gwt.core.client.JavaScriptException";
private static final ThreadLocal