001/* 002 * Copyright 2011-2012 Stephen Connolly. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.jszip.rhino; 018 019import org.mozilla.javascript.NativeJavaPackage; 020import org.mozilla.javascript.Scriptable; 021import org.mozilla.javascript.ScriptableObject; 022 023import java.io.Serializable; 024import java.util.HashMap; 025import java.util.Map; 026 027/** 028 * A proxy for a real {@link NativeJavaPackage} that allows modifications to be made. 029 */ 030public class ProxyNativeJavaPackage extends ScriptableObject implements Serializable { 031 static final long serialVersionUID = 1L; 032 033 protected final NativeJavaPackage delegate; 034 private final Map<String, Object> mutations = new HashMap<String, Object>(); 035 036 public ProxyNativeJavaPackage(NativeJavaPackage delegate) { 037 delegate.getClass(); 038 this.delegate = delegate; 039 } 040 041 @Override 042 public String getClassName() { 043 return delegate.getClassName(); 044 } 045 046 @Override 047 public boolean has(String id, Scriptable start) { 048 return mutations.containsKey(id) ? mutations.get(id) != null : delegate.has(id, start); 049 } 050 051 @Override 052 public boolean has(int index, Scriptable start) { 053 return delegate.has(index, start); 054 } 055 056 @Override 057 public void put(String id, Scriptable start, Object value) { 058 mutations.put(id, value); 059 } 060 061 @Override 062 public void put(int index, Scriptable start, Object value) { 063 delegate.put(index, start, value); 064 } 065 066 @Override 067 public Object get(String id, Scriptable start) { 068 if (mutations.containsKey(id)) { 069 return mutations.get(id); 070 } 071 return delegate.get(id, start); 072 } 073 074 @Override 075 public Object get(int index, Scriptable start) { 076 return delegate.get(index, start); 077 } 078 079 @Override 080 public Object getDefaultValue(Class<?> ignored) { 081 return toString(); 082 } 083 084 @Override 085 public String toString() { 086 return delegate.toString(); 087 } 088 089 @Override 090 public boolean equals(Object obj) { 091 if (obj instanceof ProxyNativeJavaPackage) { 092 ProxyNativeJavaPackage that = (ProxyNativeJavaPackage) obj; 093 return delegate.equals(that.delegate) && mutations.equals(that.mutations); 094 } 095 return false; 096 } 097 098 @Override 099 public int hashCode() { 100 return delegate.hashCode(); 101 } 102}