/*
* $Id: AnyClass.java,v 1.35 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 anvil.script.Scope;
import anvil.script.ClassType;
import anvil.script.Context;
import anvil.script.Type;
import anvil.script.Function;
import anvil.script.Module;
import anvil.server.Address;
import anvil.java.util.BindingEnumeration;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InvalidClassException;
import java.io.IOException;
import java.io.NotSerializableException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OptionalDataException;
import java.io.OutputStream;
import java.io.StreamCorruptedException;
import java.io.Writer;
/**
* class AnyClass
*
* @author Jani Lehtim�ki
* @version $Revision: 1.35 $
*/
public abstract class AnyClass extends Any
{
public AnyClass newInstance()
{
return null;
}
public int typeOf()
{
return IS_CLASS;
}
public boolean isMutable()
{
return true;
}
public boolean isClass()
{
return true;
}
public abstract ClassType classOf();
public Object clone()
{
return this;
}
public Any copy()
{
return this;
}
public boolean toBoolean()
{
return true;
}
public Object toObject()
{
return null;
}
public String toString()
{
StringBuffer buffer = new StringBuffer();
ClassType classtype = classOf();
buffer.append("class ");
buffer.append(classtype.getQualifiedName());
buffer.append('@');
buffer.append(Integer.toHexString(System.identityHashCode(this)));
buffer.append('@');
buffer.append(classtype.getPathinfo());
buffer.append(' ');
buffer.append('{');
boolean isFirst = true;
BindingEnumeration e = getAllMembers();
while(e.hasMoreElements()) {
if (isFirst) {
isFirst = false;
} else {
buffer.append(',');
buffer.append(' ');
}
buffer.append(e.nextKey().toString());
buffer.append('=');
buffer.append(e.nextElement().toString());
}
buffer.append('}');
return buffer.toString();
}
public AnyClass toClass()
{
return this;
}
public int hashCode()
{
return System.identityHashCode(this);
}
public Function getConstructor()
{
return classOf().getConstructor();
}
public boolean equals(Object obj)
{
return this == obj;
}
protected int _compareTo(Any other)
{
return (this == other) ? 0 : 1;
}
protected int _test(Any other)
{
return (this == other) ? 0 : 1;
}
public Any getReference(Context context, Any index)
{
if (index.isString()) {
return getAttribute(context, index.toString());
} else {
return super.getReference(context, index);
}
}
public Any setReference(Context context, Any index, Any value)
{
if (index.isString()) {
return setAttribute(context, index.toString(), value);
} else {
return super.setReference(context, index, value);
}
}
public Any checkReference(Context context, Any index)
{
if (index.isString()) {
return checkAttribute(context, index.toString());
} else {
return super.checkReference(context, index);
}
}
public Any getAttribute(Context context, String name)
{
return getMember(context, name);
}
public Any checkAttribute(Context context, String name)
{
return checkMember(context, name);
}
public Any setAttribute(Context context, String name, Any value)
{
return setMember(context, name, value);
}
public boolean deleteAttribute(Context context, String name)
{
return deleteMember(context, name);
}
public Any getMember(Context context, String name)
{
throw context.NoSuchMember(classNameOf() + '.' + name);
}
public Any checkMember(Context context, String name)
{
return UNDEFINED;
}
public Any setMember(Context context, String name, Any value)
{
throw context.NoSuchMember(classNameOf() + '.' + name);
}
public boolean deleteMember(Context context, String name)
{
throw context.AttributeError(classNameOf() + '.' + name);
}
public BindingEnumeration getMembers()
{
return BindingEnumeration.EMPTY;
}
public BindingEnumeration getAllMembers()
{
return BindingEnumeration.EMPTY;
}
public BindingEnumeration enumeration()
{
return getAllMembers();
}
public Any _invoke(Context context, int methodIndex, Any[] parameters)
{
return classOf().getDispatcher(context).invoke(context, this, methodIndex, parameters);
}
public Any _invoke(Context context, int methodIndex)
{
return classOf().getDispatcher(context).invoke(context, this, methodIndex);
}
public Any _invoke(Context context, int methodIndex, Any param1)
{
return classOf().getDispatcher(context).invoke(context, this, methodIndex, param1);
}
public Any _invoke(Context context, int methodIndex, Any param1, Any param2)
{
return classOf().getDispatcher(context).invoke(context, this, methodIndex, param1, param2);
}
public Any _invoke(Context context, int methodIndex, Any param1, Any param2, Any param3)
{
return classOf().getDispatcher(context).invoke(context, this, methodIndex, param1, param2, param3);
}
public Any _invoke(Context context, int methodIndex, Any param1, Any param2, Any param3, Any param4)
{
return classOf().getDispatcher(context).invoke(context, this, methodIndex, param1, param2, param3, param4);
}
public void _serialize(Serializer serializer) throws IOException
{
}
public void serialize(Serializer serializer) throws IOException
{
if (serializer.register(this)) {
return;
}
serializer.write('o');
ClassType type = classOf();
serializer.write(type.getQualifiedName());
serializer.write(type.getPathinfo());
_serialize(serializer);
}
public void _unserialize(Unserializer unserializer) throws UnserializationException
{
}
public static Any unserialize(Unserializer unserializer) throws UnserializationException
{
Context context = unserializer.getContext();
unserializer.consume('s');
String classname = unserializer.getUTF16String();
unserializer.consume('s');
String pathinfo = unserializer.getUTF16String();
Scope scope = context.import_(pathinfo);
AnyClass self = null;
Type type = anvil.script.Grammar.follow(scope, classname);
if (type != null) {
if (type.getType() == Type.CLASS) {
self = ((ClassType)type).newInstance();
}
}
if (self == null) {
throw new UnserializationException("Couldn't create instance of '" + classname + "' at '" + pathinfo + "'");
}
unserializer.register(self);
self._unserialize(unserializer);
return self;
}
}