/*
* $Id: AnyObject.java,v 1.20 2002/09/16 08:05:02 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.core;
import java.util.Enumeration;
import anvil.core.reflect.Reflection;
import anvil.java.util.BindingEnumeration;
import anvil.script.Context;
import anvil.script.VariableType;
import anvil.script.ConstantVariableType;
import anvil.script.MemberVariableType;
import anvil.script.Type;
/**
* class <code>AnyObject</code>
*
* @author Jani Lehtim�ki
*/
public class AnyObject extends AnyAbstractClass
{
transient public static final anvil.script.compiler.NativeClass __class__ =
new anvil.script.compiler.NativeClass("Object", AnyObject.class,
//DOC{{
""
//}}DOC
);
protected Object _object;
protected Reflection _reflection;
public AnyObject(Object object)
{
_object = object;
_reflection = ObjectPool.createReflection(object.getClass());
}
public anvil.script.ClassType classOf()
{
return _reflection;
}
public Object toObject()
{
return _object;
}
public boolean isMutable()
{
return true;
}
public String toString()
{
return _object.toString();
}
public int hashCode()
{
return _object.hashCode();
}
public int sizeOf()
{
return _reflection.arrayLength(_object);
}
public boolean equals(Object obj)
{
if (this == obj) {
return true;
}
if (obj instanceof AnyObject) {
obj = ((AnyObject)obj).toObject();
return _object.equals(obj);
}
return false;
}
protected int compare(Any other)
{
Object obj = other.toObject();
if (_object instanceof Comparable && obj instanceof Comparable) {
return ((Comparable)_object).compareTo(obj);
}
if (_object.equals(obj)) {
return 0;
} else {
return System.identityHashCode(_object) < System.identityHashCode(obj) ? -1 : 1;
}
}
public Any getAttribute(Context context, String attribute)
{
Type type = _reflection.lookupDeclaration(attribute);
if (type != null) {
switch(type.getType()) {
case Type.CONSTANT_VARIABLE:
case Type.STATIC_VARIABLE:
return ((VariableType)type).getValue();
case Type.MEMBER_VARIABLE:
return ((MemberVariableType)type).getValue(_object);
}
}
return UNDEFINED;
}
public Any checkAttribute(Context context, String attribute)
{
return getAttribute(context, attribute);
}
public Any setAttribute(Context context, String attribute, Any value)
{
Type type = _reflection.lookupDeclaration(attribute);
if (type != null) {
switch(type.getType()) {
case Type.STATIC_VARIABLE:
return ((VariableType)type).setValue(value);
case Type.MEMBER_VARIABLE:
return ((MemberVariableType)type).setValue(_object, value);
}
}
return UNDEFINED;
}
public Any getReference(Context context, Any index)
{
if (index.isInt()) {
return _reflection.arrayGet(_object, index.toInt());
} else {
return getAttribute(context, index.toString());
}
}
public Any checkReference(Context context, Any index)
{
return getReference(context, index);
}
public Any setReference(Context context, Any index, Any value)
{
if (index.isInt()) {
_reflection.arraySet(_object, index.toInt(), value);
return value;
} else {
return setAttribute(context, index.toString(), value);
}
}
public boolean has(String name)
{
return _reflection.has(name);
}
public Any invoke(Context context, int methodIndex, Any[] parameters)
{
return _reflection.invoke(context, _object, methodIndex, parameters);
}
public Any invoke(Context context, int methodIndex)
{
return _reflection.invoke(context, _object, methodIndex, Any.ARRAY0);
}
public Any invoke(Context context, int methodIndex, Any param1)
{
return _reflection.invoke(context, _object, methodIndex, new Any[] { param1 });
}
public Any invoke(Context context, int methodIndex, Any param1, Any param2)
{
return _reflection.invoke(context, _object, methodIndex, new Any[] { param1, param2 });
}
public Any invoke(Context context, int methodIndex, Any param1, Any param2, Any param3)
{
return _reflection.invoke(context, _object, methodIndex, new Any[] { param1, param2, param3 });
}
public Any invoke(Context context, int methodIndex, Any param1, Any param2, Any param3, Any param4)
{
return _reflection.invoke(context, _object, methodIndex, new Any[] { param1, param2, param3, param4 });
}
public Any invoke(Context context, String methodName, Any[] parameters)
{
return _reflection.invoke(context, _object, methodName, parameters);
}
public Any invoke(Context context, String methodName)
{
return _reflection.invoke(context, _object, methodName, Any.ARRAY0);
}
public Any invoke(Context context, String methodName, Any param1)
{
return _reflection.invoke(context, _object, methodName, new Any[] { param1 });
}
public Any invoke(Context context, String methodName, Any param1, Any param2)
{
return _reflection.invoke(context, _object, methodName, new Any[] { param1, param2 });
}
public Any invoke(Context context, String methodName, Any param1, Any param2, Any param3)
{
return _reflection.invoke(context, _object, methodName, new Any[] { param1, param2, param3 });
}
public Any invoke(Context context, String methodName, Any param1, Any param2, Any param3, Any param4)
{
return _reflection.invoke(context, _object, methodName, new Any[] { param1, param2, param3, param4 });
}
public BindingEnumeration enumeration()
{
if (_object instanceof Enumeration) {
return new IndexedEnumeration((Enumeration)_object);
} else if (_reflection.isArray()) {
return new ArrayEnumeration((Object[])_object);
} else {
return super.enumeration();
}
}
}