edu.vub.at.objects.symbiosis
Class JavaObject

java.lang.Object
  extended byedu.vub.at.objects.natives.NATNil
      extended byedu.vub.at.objects.natives.NATByRef
          extended byedu.vub.at.objects.natives.NATCallframe
              extended byedu.vub.at.objects.natives.NATObject
                  extended byedu.vub.at.objects.symbiosis.JavaObject
All Implemented Interfaces:
ATAbstractGrammar, ATConversions, ATExpression, ATNil, ATObject, ATStatement, java.io.Serializable

public final class JavaObject
extends NATObject
implements ATObject

JavaObject instances represent java objects under symbiosis. A Java Object is represented in AmbientTalk as an AmbientTalk object where: - the Java Object's fields and methods correspond to the AmbientTalk object's fields and methods - overloaded methods are coerced into a single AmbientTalk method, which must be disambiguated at call site - the Java Object has a shares-a relation with its class (i.e. the class of the object is its dynamic parent) In addition, a JavaObject carries with it a static type. The static type is used during method dispatch to select the appropriate Java method to be invoked. A JavaObject's static type can be altered by casting it. Casting can be done using JavaClass instances.

Author:
tvcutsem
See Also:
Serialized Form

Field Summary
private static java.lang.ThreadLocal _JAVAOBJECT_POOL_
          A thread-local hashmap pooling all of the JavaObject wrappers for the current actor, referring to them using SOFT references, such that unused wrappers can be GC-ed when running low on memory.
private  java.lang.Object wrappedObject_
           
 
Fields inherited from class edu.vub.at.objects.natives.NATObject
_EQL_NAME_, _INI_NAME_, _IS_A_, _NEW_NAME_, _NO_STRIPES_, _SHARES_A_, _SUPER_NAME_, stripes_
 
Fields inherited from class edu.vub.at.objects.natives.NATCallframe
customFields_, lexicalParent_, stateVector_, variableMap_
 
Fields inherited from class edu.vub.at.objects.natives.NATNil
_INSTANCE_
 
Constructor Summary
private JavaObject(java.lang.Object wrappedObject)
          A JavaObject wrapping an object o has a dynamic SHARES-A parent pointing to the wrapper of o's class.
 
Method Summary
 JavaObject asJavaObjectUnderSymbiosis()
           
 java.lang.Object getWrappedObject()
           
 boolean isJavaObjectUnderSymbiosis()
           
 ATNil meta_addMethod(ATMethod method)
          Methods can be added to a symbiotic Java object provided they do not already exist in the Java object's class.
 ATNil meta_assignField(ATObject receiver, ATSymbol name, ATObject value)
          Fields can be assigned within a symbiotic Java object if that object's class has a mutable field with a matching name.
 ATNil meta_assignVariable(ATSymbol name, ATObject value)
          Variables can be assigned within a symbiotic Java object if that object's class has a mutable field with a matching name.
 ATObject meta_clone()
          Cloning a symbiotic object is not always possible as Java has no uniform cloning semantics.
 ATNil meta_defineField(ATSymbol name, ATObject value)
          Fields can be defined within a symbiotic Java object.
 ATField meta_grabField(ATSymbol fieldName)
          Fields can be grabbed from a symbiotic Java object.
 ATMethod meta_grabMethod(ATSymbol methodName)
          Methods can be grabbed from a symbiotic Java object.
 ATObject meta_invoke(ATObject receiver, ATSymbol atSelector, ATTable arguments)
          When a method is invoked upon a symbiotic Java object, an underlying Java method with the same name as the AmbientTalk selector is invoked.
 ATBoolean meta_isCloneOf(ATObject original)
          Detects whether this object an the passed parameter are the result of cloning from a common ancestor (possibly either one of the objects itself).
 ATTable meta_listFields()
          Querying a symbiotic Java object for its fields results in a table containing both the 'native' Java fields and the fields of its AT symbiont
 ATTable meta_listMethods()
          Querying a symbiotic Java object for its methods results in a table containing both all 'native' Java instance methods and the methods of its AT symbiont
 ATObject meta_lookup(ATSymbol selector)
          A variable lookup is resolved by first checking whether the Java object has a field with a matching name.
 ATObject meta_newInstance(ATTable initargs)
          Invoking new on a JavaObject will exhibit the same behaviour as if new was invoked on the parent class.
 ATObject meta_pass()
          Passing a Java Object wrapper to another actor has the following effect: - if the wrapped Java object is serializable, the symbiotic AmbientTalk object is treated as by copy (i.e. as an isolate)
 NATText meta_print()
          Prints out the object in a human-readable way.
 ATObject meta_resolve()
          If the wrapped object was serializable, we may be asked to resolve ourselves.
 ATBoolean meta_respondsTo(ATSymbol atSelector)
          A symbiotic Java object responds to all of the public non-static selectors of its Java class plus all of the per-instance selectors added to its AmbientTalk symbiont.
 ATObject meta_select(ATObject receiver, ATSymbol selector)
          When selecting a field from a symbiotic Java object, if the Java symbiont object's class has a non-static field with a matching selector, it is automatically read; if it has a corresponding method, the method is returned in a closure.
static JavaObject wrapperFor(java.lang.Object o)
           
 
Methods inherited from class edu.vub.at.objects.natives.NATObject
asAmbientTalkObject, base_asActorMirror, base_asAsyncMessage, base_asBegin, base_asBoolean, base_asClosure, base_asDefinition, base_asExpression, base_asField, base_asHandler, base_asMessage, base_asMessageCreation, base_asMethod, base_asMirror, base_asNumber, base_asSplice, base_asStatement, base_asStripe, base_asSymbol, base_asTable, base_asUnquoteSplice, base_isBoolean, base_isCallFrame, base_isClosure, base_isMethod, base_isMirror, base_isSplice, base_isStripe, base_isSymbol, base_isTable, base_isUnquoteSplice, createChild, createClone, createIsolate, isAmbientTalkObject, isPrimitive, listTransitiveFields, listTransitiveMethods, meta_extend, meta_getStripes, meta_isRelatedTo, meta_isStripedWith, meta_share
 
Methods inherited from class edu.vub.at.objects.natives.NATCallframe
getLocalCustomField, getLocalField, hasLocalCustomField, hasLocalField, hasLocalNativeField, meta_addField, meta_doesNotUnderstand, meta_getDynamicParent, meta_getLexicalParent, setLocalField
 
Methods inherited from class edu.vub.at.objects.natives.NATNil
asJavaClassUnderSymbiosis, asNativeBoolean, asNativeException, asNativeFarReference, asNativeFraction, asNativeNumber, asNativeNumeric, asNativeTable, asNativeText, base__opeql__opeql_, base_asFarReference, base_asVariableAssignment, base_init, base_isFarReference, base_isMessageCreation, base_isVariableAssignment, base_new, equals, isNativeBoolean, isNativeField, isNativeText, meta_eval, meta_quote, meta_receive, meta_send, readResolve, toString, writeReplace
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface edu.vub.at.objects.ATObject
base__opeql__opeql_, base_init, base_new, meta_addField, meta_doesNotUnderstand, meta_eval, meta_extend, meta_getDynamicParent, meta_getLexicalParent, meta_getStripes, meta_isRelatedTo, meta_isStripedWith, meta_quote, meta_receive, meta_send, meta_share
 
Methods inherited from interface edu.vub.at.objects.coercion.ATConversions
asAmbientTalkObject, asJavaClassUnderSymbiosis, asNativeBoolean, asNativeException, asNativeFarReference, asNativeFraction, asNativeNumber, asNativeNumeric, asNativeTable, asNativeText, base_asActorMirror, base_asAsyncMessage, base_asBegin, base_asBoolean, base_asClosure, base_asDefinition, base_asExpression, base_asFarReference, base_asField, base_asHandler, base_asMessage, base_asMessageCreation, base_asMethod, base_asMirror, base_asNumber, base_asSplice, base_asStatement, base_asStripe, base_asSymbol, base_asTable, base_asUnquoteSplice, base_asVariableAssignment, base_isBoolean, base_isCallFrame, base_isClosure, base_isFarReference, base_isMessageCreation, base_isMethod, base_isMirror, base_isSplice, base_isStripe, base_isSymbol, base_isTable, base_isUnquoteSplice, base_isVariableAssignment, isAmbientTalkObject, isNativeBoolean, isNativeField, isNativeText
 

Field Detail

_JAVAOBJECT_POOL_

private static final java.lang.ThreadLocal _JAVAOBJECT_POOL_
A thread-local hashmap pooling all of the JavaObject wrappers for the current actor, referring to them using SOFT references, such that unused wrappers can be GC-ed when running low on memory.


wrappedObject_

private final java.lang.Object wrappedObject_
Constructor Detail

JavaObject

private JavaObject(java.lang.Object wrappedObject)
A JavaObject wrapping an object o has a dynamic SHARES-A parent pointing to the wrapper of o's class. A symbiotic Java object is striped with all of the Java interface stripes that correspond to the interface types implemented by the wrapped Java object's class.

Method Detail

wrapperFor

public static final JavaObject wrapperFor(java.lang.Object o)

getWrappedObject

public java.lang.Object getWrappedObject()

isJavaObjectUnderSymbiosis

public boolean isJavaObjectUnderSymbiosis()
Specified by:
isJavaObjectUnderSymbiosis in interface ATConversions
Overrides:
isJavaObjectUnderSymbiosis in class NATNil

asJavaObjectUnderSymbiosis

public JavaObject asJavaObjectUnderSymbiosis()
                                      throws XTypeMismatch
Specified by:
asJavaObjectUnderSymbiosis in interface ATConversions
Overrides:
asJavaObjectUnderSymbiosis in class NATNil
Throws:
XTypeMismatch

meta_invoke

public ATObject meta_invoke(ATObject receiver,
                            ATSymbol atSelector,
                            ATTable arguments)
                     throws InterpreterException
When a method is invoked upon a symbiotic Java object, an underlying Java method with the same name as the AmbientTalk selector is invoked. Its arguments are converted into their Java equivalents. Conversely, the result of the method invocation is converted into an AmbientTalk object. If no such method exists, a method is searched for in the symbiotic AmbientTalk part.

Specified by:
meta_invoke in interface ATObject
Overrides:
meta_invoke in class NATObject
Throws:
InterpreterException

meta_respondsTo

public ATBoolean meta_respondsTo(ATSymbol atSelector)
                          throws InterpreterException
A symbiotic Java object responds to all of the public non-static selectors of its Java class plus all of the per-instance selectors added to its AmbientTalk symbiont.

Specified by:
meta_respondsTo in interface ATObject
Overrides:
meta_respondsTo in class NATObject
Throws:
InterpreterException

meta_select

public ATObject meta_select(ATObject receiver,
                            ATSymbol selector)
                     throws InterpreterException
When selecting a field from a symbiotic Java object, if the Java symbiont object's class has a non-static field with a matching selector, it is automatically read; if it has a corresponding method, the method is returned in a closure. If no matching field is found, the fields and methods of the AmbientTalk symbiont are checked.

Specified by:
meta_select in interface ATObject
Overrides:
meta_select in class NATObject
Parameters:
receiver - the original receiver of the selection
selector - the selector to look up
Returns:
the value of the found field, or a closure wrapping a found method
Throws:
InterpreterException

meta_lookup

public ATObject meta_lookup(ATSymbol selector)
                     throws InterpreterException
A variable lookup is resolved by first checking whether the Java object has a field with a matching name. If not, the symbiotic AmbientTalk object is checked.

Specified by:
meta_lookup in interface ATObject
Overrides:
meta_lookup in class NATObject
Throws:
InterpreterException

meta_defineField

public ATNil meta_defineField(ATSymbol name,
                              ATObject value)
                       throws InterpreterException
Fields can be defined within a symbiotic Java object. They are added to its AmbientTalk symbiont, but only if they do not clash with already existing field names.

Specified by:
meta_defineField in interface ATObject
Overrides:
meta_defineField in class NATObject
Throws:
InterpreterException

meta_assignVariable

public ATNil meta_assignVariable(ATSymbol name,
                                 ATObject value)
                          throws InterpreterException
Variables can be assigned within a symbiotic Java object if that object's class has a mutable field with a matching name.

Specified by:
meta_assignVariable in interface ATObject
Overrides:
meta_assignVariable in class NATCallframe
Throws:
InterpreterException

meta_assignField

public ATNil meta_assignField(ATObject receiver,
                              ATSymbol name,
                              ATObject value)
                       throws InterpreterException
Fields can be assigned within a symbiotic Java object if that object's class has a mutable field with a matching name. Field assignment is first resolved in the wrapped Java object and afterwards in the AT symbiont.

Specified by:
meta_assignField in interface ATObject
Overrides:
meta_assignField in class NATObject
Parameters:
value - the value to assign to the field
name - the field to assign
Returns:
NIL
Throws:
InterpreterException

meta_clone

public ATObject meta_clone()
                    throws InterpreterException
Cloning a symbiotic object is not always possible as Java has no uniform cloning semantics. Even if the symbiotic object implements java.lang.Cloneable, a clone cannot be made of the wrapped object as java.lang.Object's clone method is protected, and must be overridden by a public clone method in the cloneable subclass.

Specified by:
meta_clone in interface ATObject
Overrides:
meta_clone in class NATObject
Throws:
InterpreterException

meta_newInstance

public ATObject meta_newInstance(ATTable initargs)
                          throws InterpreterException
Invoking new on a JavaObject will exhibit the same behaviour as if new was invoked on the parent class.

Specified by:
meta_newInstance in interface ATObject
Overrides:
meta_newInstance in class NATObject
Throws:
InterpreterException

meta_addMethod

public ATNil meta_addMethod(ATMethod method)
                     throws InterpreterException
Methods can be added to a symbiotic Java object provided they do not already exist in the Java object's class.

Specified by:
meta_addMethod in interface ATObject
Overrides:
meta_addMethod in class NATObject
Throws:
InterpreterException

meta_grabField

public ATField meta_grabField(ATSymbol fieldName)
                       throws InterpreterException
Fields can be grabbed from a symbiotic Java object. Fields that correspond to fields in the Java object's class are returned as JavaField instances.

Specified by:
meta_grabField in interface ATObject
Overrides:
meta_grabField in class NATCallframe
Throws:
InterpreterException

meta_grabMethod

public ATMethod meta_grabMethod(ATSymbol methodName)
                         throws InterpreterException
Methods can be grabbed from a symbiotic Java object. Methods that correspond to methods in the Java object's class are returned as JavaMethod instances.

Specified by:
meta_grabMethod in interface ATObject
Overrides:
meta_grabMethod in class NATObject
Throws:
InterpreterException

meta_listFields

public ATTable meta_listFields()
                        throws InterpreterException
Querying a symbiotic Java object for its fields results in a table containing both the 'native' Java fields and the fields of its AT symbiont

Specified by:
meta_listFields in interface ATObject
Overrides:
meta_listFields in class NATCallframe
Throws:
InterpreterException

meta_listMethods

public ATTable meta_listMethods()
                         throws InterpreterException
Querying a symbiotic Java object for its methods results in a table containing both all 'native' Java instance methods and the methods of its AT symbiont

Specified by:
meta_listMethods in interface ATObject
Overrides:
meta_listMethods in class NATObject
Throws:
InterpreterException

meta_isCloneOf

public ATBoolean meta_isCloneOf(ATObject original)
                         throws InterpreterException
Description copied from interface: ATObject
Detects whether this object an the passed parameter are the result of cloning from a common ancestor (possibly either one of the objects itself).

Specified by:
meta_isCloneOf in interface ATObject
Overrides:
meta_isCloneOf in class NATObject
Throws:
InterpreterException

meta_print

public NATText meta_print()
                   throws InterpreterException
Description copied from interface: ATObject
Prints out the object in a human-readable way.

Specified by:
meta_print in interface ATObject
Overrides:
meta_print in class NATObject
Throws:
InterpreterException

meta_pass

public ATObject meta_pass()
                   throws InterpreterException
Passing a Java Object wrapper to another actor has the following effect: - if the wrapped Java object is serializable, the symbiotic AmbientTalk object is treated as by copy (i.e. as an isolate). - if the wrapped Java object is not serializable, the symbiotic AmbientTalk object is treated as by reference and a far reference will be passed instead.

Specified by:
meta_pass in interface ATObject
Overrides:
meta_pass in class NATObject
Throws:
InterpreterException

meta_resolve

public ATObject meta_resolve()
                      throws InterpreterException
If the wrapped object was serializable, we may be asked to resolve ourselves.

Specified by:
meta_resolve in interface ATObject
Overrides:
meta_resolve in class NATObject
Throws:
InterpreterException