📄 javaadapter.java
字号:
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * * Free Software Foundation, Inc. * 59 Temple Place, Suite 330 * Boston, MA 02111-1307 USA * * @author Scott Ferguson */package com.caucho.quercus.env;import com.caucho.quercus.UnimplementedException;import com.caucho.quercus.expr.Expr;import com.caucho.quercus.function.Marshal;import com.caucho.quercus.function.MarshalFactory;import com.caucho.quercus.program.AbstractFunction;import com.caucho.quercus.program.JavaClassDef;import com.caucho.vfs.WriteStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;import java.lang.ref.WeakReference;import java.lang.reflect.Array;import java.util.*;import java.util.logging.Level;import java.util.logging.Logger;/** * Interface for marshalled Java data structures. */abstract public class JavaAdapter extends ArrayValue implements Serializable{ private static final Logger log = Logger.getLogger(JavaAdapter.class.getName()); private WeakReference<Env> _envRef; private Object _object; private JavaClassDef _classDef; // Vars to update when matching array item is modified private HashMap<Value,Value> _refs; protected JavaAdapter(Env env, Object object, JavaClassDef def) { if (env != null) _envRef = new WeakReference<Env>(env); _object = object; _classDef = def; } public JavaClassDef getClassDef() { return _classDef; } public Env getEnv() { return _envRef.get(); } public Value wrapJava(Object obj) { return _envRef.get().wrapJava(obj); } /** * Converts to an object. */ public Object toObject() { return null; } /** * Converts to a Java object. */ @Override public Object toJavaObject() { return _object; } /** * Converts to a java object. */ @Override public Object toJavaObjectNotNull(Env env, Class type) { if (type.isAssignableFrom(_object.getClass())) { return _object; } else { env.warning(L.l("Can't assign {0} to {1}", _object.getClass().getName(), type.getName())); return null; } } // // Conversions // /** * Converts to an object. */ public Value toObject(Env env) { Value obj = env.createObject(); for (Map.Entry<Value,Value> entry : entrySet()) { Value key = entry.getKey(); if (key instanceof StringValue) { // XXX: intern? obj.putField(env, key.toString(), entry.getValue()); } } return obj; } /** * Converts to a java List object. */ @Override public Collection toJavaCollection(Env env, Class type) { Collection coll = null; if (type.isAssignableFrom(HashSet.class)) { coll = new HashSet(); } else if (type.isAssignableFrom(TreeSet.class)) { coll = new TreeSet(); } else { try { coll = (Collection) type.newInstance(); } catch (Throwable e) { log.log(Level.FINE, e.toString(), e); env.warning(L.l("Can't assign array to {0}", type.getName())); return null; } } for (Map.Entry entry : objectEntrySet()) { coll.add(entry.getValue()); } return coll; } /** * Converts to a java List object. */ @Override public List toJavaList(Env env, Class type) { List list = null; if (type.isAssignableFrom(ArrayList.class)) { list = new ArrayList(); } else if (type.isAssignableFrom(LinkedList.class)) { list = new LinkedList(); } else if (type.isAssignableFrom(Vector.class)) { list = new Vector(); } else { try { list = (List) type.newInstance(); } catch (Throwable e) { log.log(Level.FINE, e.toString(), e); env.warning(L.l("Can't assign array to {0}", type.getName())); return null; } } for (Map.Entry entry : objectEntrySet()) { list.add(entry.getValue()); } return list; } /** * Converts to a java object. */ @Override public Map toJavaMap(Env env, Class type) { Map map = null; if (type.isAssignableFrom(TreeMap.class)) { map = new TreeMap(); } else if (type.isAssignableFrom(LinkedHashMap.class)) { map = new LinkedHashMap(); } else { try { map = (Map) type.newInstance(); } catch (Throwable e) { log.log(Level.FINE, e.toString(), e); env.warning(L.l("Can't assign array to {0}", type.getName())); return null; } } for (Map.Entry entry : objectEntrySet()) { map.put(entry.getKey(), entry.getValue()); } return map; } /** * Copy for assignment. */ abstract public Value copy(); /** * Copy for serialization */ abstract public Value copy(Env env, IdentityHashMap<Value,Value> map); /** * Returns the size. */ abstract public int getSize(); /** * Clears the array */ abstract public void clear(); /** * Adds a new value. */ public final Value put(Value value) { return put(createTailKey(), value); } /** * Adds a new value. */ public final Value put(Value key, Value value) { Value retValue = putImpl(key, value); if (_refs == null) _refs = new HashMap<Value,Value>(); if (value instanceof Var) { Var var = (Var) value; var.setReference(); _refs.put(key, var); } else { Value ref = _refs.get(key); if (ref != null) ref.set(value); } return retValue; } /** * Adds a new value. */ abstract public Value putImpl(Value key, Value value); /** * Add to front. */ public ArrayValue unshift(Value value) { throw new UnsupportedOperationException(); } /** * Splices. */ public ArrayValue splice(int begin, int end, ArrayValue replace) { throw new UnsupportedOperationException(); } /** * Returns the value as an argument which may be a reference. */ public Value getArg(Value index) { return get(index); } /** * Sets the array ref. */ public Value putRef() { throw new UnsupportedOperationException(); } /** * Creatse a tail index. */ abstract public Value createTailKey(); /** * Returns a union of this array and the rValue as array. * If the rValue is not an array, the returned union contains the elements * of this array only. * * To append a value to this ArrayValue use the {@link #put(Value)} method. */ public Value add(Value rValue) { rValue = rValue.toValue(); if (! (rValue instanceof ArrayValue)) return copy(); ArrayValue rArray = (ArrayValue) rValue; ArrayValue result = new ArrayValueImpl(rArray); for (Map.Entry<Value,Value> entry : entrySet()) { result.put(entry.getKey(), entry.getValue()); } return result; } /** * Returns the field values. */ public Collection<Value> getIndices() { throw new UnsupportedOperationException(); } /** * Gets a new value. */ abstract public Value get(Value key); /** * Removes a value. */ abstract public Value remove(Value key); /** * Returns the array ref. */ public Var getRef(Value index) { Var var = new JavaAdapterVar(this, index); if (_refs == null) _refs = new HashMap<Value,Value>(); _refs.put(index, var); return var; } /** * Returns an iterator of the entries. */ @Override public Set<Value> keySet() { return new KeySet(getEnv()); } /** * Returns a set of all the entries. */ abstract public Set<Map.Entry<Value,Value>> entrySet(); /** * Returns a java object set of all the entries. */ abstract public Set<Map.Entry<Object,Object>> objectEntrySet(); /** * Returns a collection of the values. */ public Collection<Value> values() { throw new UnimplementedException(); } /** * Appends as an argument - only called from compiled code * * XXX: change name to appendArg */ public ArrayValue append(Value key, Value value) { put(key, value); return this; } /** * Pops the top value. */ public Value pop() { throw new UnsupportedOperationException(); } /** * Shuffles the array */ public void shuffle() { throw new UnsupportedOperationException(); } /** * Returns the head. */ public Entry getHead() { throw new UnsupportedOperationException(); } /** * Returns the tail. */ protected Entry getTail() { throw new UnsupportedOperationException(); } /** * Returns the current value. */ public Value current() { throw new UnsupportedOperationException(); } /** * Returns the current key */ public Value key() { throw new UnsupportedOperationException(); } /** * Returns true if there are more elements. */ public boolean hasCurrent() { throw new UnsupportedOperationException(); } /** * Returns the next value. */ public Value next() { throw new UnsupportedOperationException(); } /** * Returns the previous value. */ public Value prev() { throw new UnsupportedOperationException(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -