зеркало из https://github.com/mozilla/pjs.git
Serialization fixes to correctly restore members and related fields in NativeJavaClass.
This commit is contained in:
Родитель
05d4e426a8
Коммит
7f84dd1785
|
@ -61,10 +61,20 @@ import java.io.*;
|
|||
|
||||
public class NativeJavaClass extends NativeJavaObject implements Function {
|
||||
|
||||
public NativeJavaClass() {
|
||||
}
|
||||
|
||||
public NativeJavaClass(Scriptable scope, Class cl) {
|
||||
super(scope, cl, JavaMembers.lookupClass(scope, cl, cl));
|
||||
fieldAndMethods = members.getFieldAndMethodsObjects(this, javaObject,
|
||||
true);
|
||||
this.parent = scope;
|
||||
this.javaObject = cl;
|
||||
initMembers();
|
||||
}
|
||||
|
||||
protected void initMembers() {
|
||||
Class cl = (Class)javaObject;
|
||||
members = JavaMembers.lookupClass(parent, cl, cl);
|
||||
staticFieldAndMethods
|
||||
= members.getFieldAndMethodsObjects(this, cl, true);
|
||||
}
|
||||
|
||||
public String getClassName() {
|
||||
|
@ -86,8 +96,8 @@ public class NativeJavaClass extends NativeJavaObject implements Function {
|
|||
|
||||
Object result = Scriptable.NOT_FOUND;
|
||||
|
||||
if (fieldAndMethods != null) {
|
||||
result = fieldAndMethods.get(name);
|
||||
if (staticFieldAndMethods != null) {
|
||||
result = staticFieldAndMethods.get(name);
|
||||
if (result != null)
|
||||
return result;
|
||||
}
|
||||
|
@ -99,7 +109,8 @@ public class NativeJavaClass extends NativeJavaObject implements Function {
|
|||
try {
|
||||
String nestedName = getClassObject().getName() + '$' + name;
|
||||
Class nestedClass = ScriptRuntime.loadClassName(nestedName);
|
||||
Scriptable nestedValue = wrap(ScriptableObject.getTopLevelScope(this), nestedClass);
|
||||
NativeJavaClass nestedValue = new NativeJavaClass
|
||||
(ScriptableObject.getTopLevelScope(this), nestedClass);
|
||||
nestedValue.setParentScope(this);
|
||||
result = nestedValue;
|
||||
} catch (ClassNotFoundException ex) {
|
||||
|
@ -124,11 +135,6 @@ public class NativeJavaClass extends NativeJavaObject implements Function {
|
|||
return (Class) super.unwrap();
|
||||
}
|
||||
|
||||
// XXX ??
|
||||
public static NativeJavaClass wrap(Scriptable scope, Class cls) {
|
||||
return new NativeJavaClass(scope, cls);
|
||||
}
|
||||
|
||||
public Object getDefaultValue(Class hint) {
|
||||
if (hint == null || hint == ScriptRuntime.StringClass)
|
||||
return this.toString();
|
||||
|
@ -169,40 +175,40 @@ public class NativeJavaClass extends NativeJavaObject implements Function {
|
|||
if (! (Modifier.isInterface(modifiers) ||
|
||||
Modifier.isAbstract(modifiers)))
|
||||
{
|
||||
Constructor[] ctors = members.getConstructors();
|
||||
Member member = NativeJavaMethod.findFunction(ctors, args);
|
||||
Constructor ctor = (Constructor) member;
|
||||
if (ctor == null) {
|
||||
String sig = NativeJavaMethod.scriptSignature(args);
|
||||
Constructor[] ctors = members.getConstructors();
|
||||
Member member = NativeJavaMethod.findFunction(ctors, args);
|
||||
Constructor ctor = (Constructor) member;
|
||||
if (ctor == null) {
|
||||
String sig = NativeJavaMethod.scriptSignature(args);
|
||||
throw Context.reportRuntimeError2(
|
||||
"msg.no.java.ctor", classObject.getName(), sig);
|
||||
}
|
||||
}
|
||||
|
||||
// Found the constructor, so try invoking it.
|
||||
return NativeJavaClass.constructSpecific(cx, scope,
|
||||
this, ctor, args);
|
||||
// Found the constructor, so try invoking it.
|
||||
return NativeJavaClass.constructSpecific(cx, scope,
|
||||
this, ctor, args);
|
||||
} else {
|
||||
Scriptable topLevel = ScriptableObject.getTopLevelScope(this);
|
||||
String msg = "";
|
||||
try {
|
||||
// trying to construct an interface; use JavaAdapter to
|
||||
// construct a new class on the fly that implements this
|
||||
// interface.
|
||||
Object v = topLevel.get("JavaAdapter", topLevel);
|
||||
if (v != NOT_FOUND) {
|
||||
Function f = (Function) v;
|
||||
Object[] adapterArgs = { this, args[0] };
|
||||
return (Scriptable) f.construct(cx, topLevel,
|
||||
adapterArgs);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
// fall through to error
|
||||
String m = ex.getMessage();
|
||||
if (m != null)
|
||||
msg = m;
|
||||
Scriptable topLevel = ScriptableObject.getTopLevelScope(this);
|
||||
String msg = "";
|
||||
try {
|
||||
// trying to construct an interface; use JavaAdapter to
|
||||
// construct a new class on the fly that implements this
|
||||
// interface.
|
||||
Object v = topLevel.get("JavaAdapter", topLevel);
|
||||
if (v != NOT_FOUND) {
|
||||
Function f = (Function) v;
|
||||
Object[] adapterArgs = { this, args[0] };
|
||||
return (Scriptable) f.construct(cx, topLevel,
|
||||
adapterArgs);
|
||||
}
|
||||
throw Context.reportRuntimeError2(
|
||||
"msg.cant.instantiate", msg, classObject.getName());
|
||||
} catch (Exception ex) {
|
||||
// fall through to error
|
||||
String m = ex.getMessage();
|
||||
if (m != null)
|
||||
msg = m;
|
||||
}
|
||||
throw Context.reportRuntimeError2(
|
||||
"msg.cant.instantiate", msg, classObject.getName());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -268,22 +274,5 @@ public class NativeJavaClass extends NativeJavaObject implements Function {
|
|||
return false;
|
||||
}
|
||||
|
||||
private Hashtable fieldAndMethods;
|
||||
|
||||
// beard: need a scope for finding top-level prototypes.
|
||||
private Scriptable parent;
|
||||
|
||||
public void writeExternal(ObjectOutput out) throws IOException {
|
||||
super.writeExternal(out);
|
||||
out.writeObject(parent);
|
||||
}
|
||||
|
||||
public void readExternal(ObjectInput in)
|
||||
throws IOException, ClassNotFoundException
|
||||
{
|
||||
super.readExternal(in);
|
||||
parent = (Scriptable)in.readObject();
|
||||
fieldAndMethods = members.getFieldAndMethodsObjects(this, javaObject,
|
||||
true);
|
||||
}
|
||||
private Hashtable staticFieldAndMethods;
|
||||
}
|
||||
|
|
|
@ -57,16 +57,7 @@ import java.util.Enumeration;
|
|||
|
||||
public class NativeJavaObject implements Scriptable, Wrapper, Externalizable {
|
||||
|
||||
public NativeJavaObject() {
|
||||
}
|
||||
|
||||
public NativeJavaObject(Scriptable scope, Object javaObject,
|
||||
JavaMembers members)
|
||||
{
|
||||
this.parent = scope;
|
||||
this.javaObject = javaObject;
|
||||
this.members = members;
|
||||
}
|
||||
public NativeJavaObject() { }
|
||||
|
||||
public NativeJavaObject(Scriptable scope, Object javaObject,
|
||||
Class staticType)
|
||||
|
@ -74,10 +65,19 @@ public class NativeJavaObject implements Scriptable, Wrapper, Externalizable {
|
|||
this.parent = scope;
|
||||
this.javaObject = javaObject;
|
||||
this.staticType = staticType;
|
||||
Class dynamicType = javaObject != null ? javaObject.getClass()
|
||||
: staticType;
|
||||
members = JavaMembers.lookupClass(scope, dynamicType, staticType);
|
||||
fieldAndMethods = members.getFieldAndMethodsObjects(this, javaObject, false);
|
||||
initMembers();
|
||||
}
|
||||
|
||||
protected void initMembers() {
|
||||
Class dynamicType;
|
||||
if (javaObject != null) {
|
||||
dynamicType = javaObject.getClass();
|
||||
} else {
|
||||
dynamicType = staticType;
|
||||
}
|
||||
members = JavaMembers.lookupClass(parent, dynamicType, staticType);
|
||||
fieldAndMethods
|
||||
= members.getFieldAndMethodsObjects(this, javaObject, false);
|
||||
}
|
||||
|
||||
public boolean has(String name, Scriptable start) {
|
||||
|
@ -130,7 +130,7 @@ public class NativeJavaObject implements Scriptable, Wrapper, Externalizable {
|
|||
}
|
||||
|
||||
public Scriptable getPrototype() {
|
||||
if (prototype == null && javaObject.getClass() == ScriptRuntime.StringClass) {
|
||||
if (prototype == null && javaObject instanceof String) {
|
||||
return ScriptableObject.getClassPrototype(parent, "String");
|
||||
}
|
||||
return prototype;
|
||||
|
@ -858,28 +858,11 @@ WrapFactory#wrap(Context cx, Scriptable scope, Object obj, Class)}
|
|||
value.toString(), NativeJavaMethod.javaSignature(type));
|
||||
}
|
||||
|
||||
/**
|
||||
* The prototype of this object.
|
||||
*/
|
||||
protected Scriptable prototype;
|
||||
|
||||
/**
|
||||
* The parent scope of this object.
|
||||
*/
|
||||
protected Scriptable parent;
|
||||
|
||||
protected Object javaObject;
|
||||
protected JavaMembers members;
|
||||
protected Class staticType;
|
||||
private Hashtable fieldAndMethods;
|
||||
|
||||
public void writeExternal(ObjectOutput out)
|
||||
throws IOException
|
||||
{
|
||||
out.writeObject(prototype);
|
||||
out.writeObject(parent);
|
||||
out.writeObject(staticType != null ? staticType.getClass().getName()
|
||||
: null);
|
||||
|
||||
if (javaObject != null) {
|
||||
Class joClass = javaObject.getClass();
|
||||
|
@ -897,18 +880,27 @@ WrapFactory#wrap(Context cx, Scriptable scope, Object obj, Class)}
|
|||
out.writeObject(interfaceNames);
|
||||
|
||||
try {
|
||||
out.writeObject(joClass.getField("delegee").get(javaObject));
|
||||
out.writeObject(joClass.getField("self").get(javaObject));
|
||||
Object delegee
|
||||
= joClass.getField("delegee").get(javaObject);
|
||||
Object self
|
||||
= joClass.getField("self").get(javaObject);
|
||||
out.writeObject(delegee);
|
||||
out.writeObject(self);
|
||||
} catch (IllegalAccessException e) {
|
||||
} catch (NoSuchFieldException e) {
|
||||
}
|
||||
|
||||
} else {
|
||||
out.writeBoolean(false);
|
||||
out.writeObject(javaObject);
|
||||
}
|
||||
} else {
|
||||
out.writeBoolean(false);
|
||||
out.writeObject(javaObject);
|
||||
}
|
||||
|
||||
if (staticType != null) {
|
||||
out.writeObject(staticType.getClass().getName());
|
||||
} else {
|
||||
out.writeObject(null);
|
||||
}
|
||||
}
|
||||
|
@ -919,9 +911,6 @@ WrapFactory#wrap(Context cx, Scriptable scope, Object obj, Class)}
|
|||
prototype = (Scriptable)in.readObject();
|
||||
parent = (Scriptable)in.readObject();
|
||||
|
||||
String className = (String)in.readObject();
|
||||
staticType = className != null ? Class.forName(className) : null;
|
||||
|
||||
if (in.readBoolean()) {
|
||||
Class superclass = Class.forName((String)in.readObject());
|
||||
|
||||
|
@ -937,12 +926,30 @@ WrapFactory#wrap(Context cx, Scriptable scope, Object obj, Class)}
|
|||
javaObject = in.readObject();
|
||||
}
|
||||
|
||||
Class dynamicType = javaObject != null ? javaObject.getClass()
|
||||
: staticType;
|
||||
members = JavaMembers.lookupClass(parent, dynamicType, staticType);
|
||||
fieldAndMethods = members.getFieldAndMethodsObjects(this, javaObject,
|
||||
false);
|
||||
String className = (String)in.readObject();
|
||||
if (className != null) {
|
||||
staticType = Class.forName(className);
|
||||
} else {
|
||||
staticType = null;
|
||||
}
|
||||
|
||||
initMembers();
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* The prototype of this object.
|
||||
*/
|
||||
protected Scriptable prototype;
|
||||
|
||||
/**
|
||||
* The parent scope of this object.
|
||||
*/
|
||||
protected Scriptable parent;
|
||||
|
||||
protected Object javaObject;
|
||||
|
||||
protected Class staticType;
|
||||
protected JavaMembers members;
|
||||
private Hashtable fieldAndMethods;
|
||||
|
||||
}
|
||||
|
|
|
@ -217,7 +217,7 @@ public class NativeJavaPackage extends ScriptableObject {
|
|||
Class newClass = classLoader != null
|
||||
? classLoader.loadClass(newPackage)
|
||||
: ScriptRuntime.loadClassName(newPackage);
|
||||
newValue = NativeJavaClass.wrap(getTopLevelScope(this), newClass);
|
||||
newValue = new NativeJavaClass(getTopLevelScope(this), newClass);
|
||||
newValue.setParentScope(this);
|
||||
newValue.setPrototype(this.prototype);
|
||||
} catch (ClassNotFoundException ex) {
|
||||
|
|
Загрузка…
Ссылка в новой задаче