| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package org.mozilla.javascript; |
| 8 |
|
| 9 |
import java.io.IOException; |
| 10 |
import java.io.ObjectInputStream; |
| 11 |
import java.util.HashSet; |
| 12 |
import java.util.Set; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public class NativeJavaPackage extends ScriptableObject |
| 28 |
{ |
| 29 |
static final long serialVersionUID = 7445054382212031523L; |
| 30 |
|
| 31 |
NativeJavaPackage(boolean internalUsage, String packageName, |
| 32 |
ClassLoader classLoader) |
| 33 |
{ |
| 34 |
this.packageName = packageName; |
| 35 |
this.classLoader = classLoader; |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
@Deprecated |
| 43 |
public NativeJavaPackage(String packageName, ClassLoader classLoader) { |
| 44 |
this(false, packageName, classLoader); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
@Deprecated |
| 52 |
public NativeJavaPackage(String packageName) { |
| 53 |
this(false, packageName, |
| 54 |
Context.getCurrentContext().getApplicationClassLoader()); |
| 55 |
} |
| 56 |
|
| 57 |
@Override |
| 58 |
public String getClassName() { |
| 59 |
return "JavaPackage"; |
| 60 |
} |
| 61 |
|
| 62 |
@Override |
| 63 |
public boolean has(String id, Scriptable start) { |
| 64 |
return true; |
| 65 |
} |
| 66 |
|
| 67 |
@Override |
| 68 |
public boolean has(int index, Scriptable start) { |
| 69 |
return false; |
| 70 |
} |
| 71 |
|
| 72 |
@Override |
| 73 |
public void put(String id, Scriptable start, Object value) { |
| 74 |
|
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
public void put(int index, Scriptable start, Object value) { |
| 79 |
throw Context.reportRuntimeError0("msg.pkg.int"); |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
public Object get(String id, Scriptable start) { |
| 84 |
return getPkgProperty(id, start, true); |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
public Object get(int index, Scriptable start) { |
| 89 |
return NOT_FOUND; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
NativeJavaPackage forcePackage(String name, Scriptable scope) |
| 95 |
{ |
| 96 |
Object cached = super.get(name, this); |
| 97 |
if (cached != null && cached instanceof NativeJavaPackage) { |
| 98 |
return (NativeJavaPackage) cached; |
| 99 |
} else { |
| 100 |
String newPackage = packageName.length() == 0 |
| 101 |
? name |
| 102 |
: packageName + "." + name; |
| 103 |
NativeJavaPackage pkg = new NativeJavaPackage(true, newPackage, classLoader); |
| 104 |
ScriptRuntime.setObjectProtoAndParent(pkg, scope); |
| 105 |
super.put(name, this, pkg); |
| 106 |
return pkg; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
synchronized Object getPkgProperty(String name, Scriptable start, |
| 111 |
boolean createPkg) |
| 112 |
{ |
| 113 |
Object cached = super.get(name, start); |
| 114 |
if (cached != NOT_FOUND) |
| 115 |
return cached; |
| 116 |
if (negativeCache != null && negativeCache.contains(name)) { |
| 117 |
|
| 118 |
return null; |
| 119 |
} |
| 120 |
|
| 121 |
String className = (packageName.length() == 0) |
| 122 |
? name : packageName + '.' + name; |
| 123 |
Context cx = Context.getContext(); |
| 124 |
ClassShutter shutter = cx.getClassShutter(); |
| 125 |
Scriptable newValue = null; |
| 126 |
if (shutter == null || shutter.visibleToScripts(className)) { |
| 127 |
Class<?> cl = null; |
| 128 |
if (classLoader != null) { |
| 129 |
cl = Kit.classOrNull(classLoader, className); |
| 130 |
} else { |
| 131 |
cl = Kit.classOrNull(className); |
| 132 |
} |
| 133 |
if (cl != null) { |
| 134 |
WrapFactory wrapFactory = cx.getWrapFactory(); |
| 135 |
newValue = wrapFactory.wrapJavaClass(cx, getTopLevelScope(this), cl); |
| 136 |
newValue.setPrototype(getPrototype()); |
| 137 |
} |
| 138 |
} |
| 139 |
if (newValue == null) { |
| 140 |
if (createPkg) { |
| 141 |
NativeJavaPackage pkg; |
| 142 |
pkg = new NativeJavaPackage(true, className, classLoader); |
| 143 |
ScriptRuntime.setObjectProtoAndParent(pkg, getParentScope()); |
| 144 |
newValue = pkg; |
| 145 |
} else { |
| 146 |
|
| 147 |
if (negativeCache == null) |
| 148 |
negativeCache = new HashSet<String>(); |
| 149 |
negativeCache.add(name); |
| 150 |
} |
| 151 |
} |
| 152 |
if (newValue != null) { |
| 153 |
|
| 154 |
|
| 155 |
super.put(name, start, newValue); |
| 156 |
} |
| 157 |
return newValue; |
| 158 |
} |
| 159 |
|
| 160 |
@Override |
| 161 |
public Object getDefaultValue(Class<?> ignored) { |
| 162 |
return toString(); |
| 163 |
} |
| 164 |
|
| 165 |
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { |
| 166 |
in.defaultReadObject(); |
| 167 |
this.classLoader = Context.getCurrentContext().getApplicationClassLoader(); |
| 168 |
} |
| 169 |
|
| 170 |
@Override |
| 171 |
public String toString() { |
| 172 |
return "[JavaPackage " + packageName + "]"; |
| 173 |
} |
| 174 |
|
| 175 |
@Override |
| 176 |
public boolean equals(Object obj) { |
| 177 |
if(obj instanceof NativeJavaPackage) { |
| 178 |
NativeJavaPackage njp = (NativeJavaPackage)obj; |
| 179 |
return packageName.equals(njp.packageName) && |
| 180 |
classLoader == njp.classLoader; |
| 181 |
} |
| 182 |
return false; |
| 183 |
} |
| 184 |
|
| 185 |
@Override |
| 186 |
public int hashCode() { |
| 187 |
return packageName.hashCode() ^ |
| 188 |
(classLoader == null ? 0 : classLoader.hashCode()); |
| 189 |
} |
| 190 |
|
| 191 |
private String packageName; |
| 192 |
private transient ClassLoader classLoader; |
| 193 |
private Set<String> negativeCache = null; |
| 194 |
} |