1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
\r
3 * The contents of this file are subject to the Netscape Public
\r
4 * License Version 1.1 (the "License"); you may not use this file
\r
5 * except in compliance with the License. You may obtain a copy of
\r
6 * the License at http://www.mozilla.org/NPL/
\r
8 * Software distributed under the License is distributed on an "AS
\r
9 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express oqr
\r
10 * implied. See the License for the specific language governing
\r
11 * rights and limitations under the License.
\r
13 * The Original Code is Rhino code, released
\r
16 * The Initial Developer of the Original Code is Netscape
\r
17 * Communications Corporation. Portions created by Netscape are
\r
18 * Copyright (C) 1997-2000 Netscape Communications Corporation. All
\r
24 * Alternatively, the contents of this file may be used under the
\r
25 * terms of the GNU Public License (the "GPL"), in which case the
\r
26 * provisions of the GPL are applicable instead of those above.
\r
27 * If you wish to allow use of your version of this file only
\r
28 * under the terms of the GPL and not to allow others to use your
\r
29 * version of this file under the NPL, indicate your decision by
\r
30 * deleting the provisions above and replace them with the notice
\r
31 * and other provisions required by the GPL. If you do not delete
\r
32 * the provisions above, a recipient may use your version of this
\r
33 * file under either the NPL or the GPL.
\r
36 package org.mozilla.javascript;
\r
38 import java.lang.reflect.*;
\r
41 * Avoid loading classes unless they are used.
\r
43 * <p> This improves startup time and average memory usage.
\r
45 public final class LazilyLoadedCtor {
\r
47 public LazilyLoadedCtor(ScriptableObject scope,
\r
48 String ctorName, String className, boolean sealed)
\r
51 this.className = className;
\r
52 this.ctorName = ctorName;
\r
53 this.sealed = sealed;
\r
55 if (getter == null) {
\r
56 Method[] all = FunctionObject.getMethodList(getClass());
\r
57 getter = FunctionObject.findMethods(all, "getProperty")[0];
\r
58 setter = FunctionObject.findMethods(all, "setProperty")[0];
\r
62 scope.defineProperty(ctorName, this, getter, setter,
\r
63 ScriptableObject.DONTENUM);
\r
65 catch (PropertyException e) {
\r
66 throw WrappedException.wrapException(e);
\r
70 public Object getProperty(ScriptableObject obj) {
\r
71 synchronized (obj) {
\r
73 boolean removeOnError = false;
\r
75 // Treat security exceptions as absence of object.
\r
76 // They can be due to the following reasons:
\r
77 // java.lang.RuntimePermission createClassLoader
\r
78 // java.util.PropertyPermission
\r
79 // org.mozilla.javascript.JavaAdapter read
\r
82 try { cl = Class.forName(className); }
\r
83 catch (ClassNotFoundException ex) { removeOnError = true; }
\r
84 catch (SecurityException ex) { removeOnError = true; }
\r
88 ScriptableObject.defineClass(obj, cl, sealed);
\r
91 catch (InstantiationException e) {
\r
92 throw WrappedException.wrapException(e);
\r
94 catch (IllegalAccessException e) {
\r
95 throw WrappedException.wrapException(e);
\r
97 catch (InvocationTargetException e) {
\r
98 throw WrappedException.wrapException(e);
\r
100 catch (ClassDefinitionException e) {
\r
101 throw WrappedException.wrapException(e);
\r
103 catch (PropertyException e) {
\r
104 throw WrappedException.wrapException(e);
\r
106 catch (SecurityException ex) {
\r
107 removeOnError = true;
\r
110 if (removeOnError) {
\r
111 obj.delete(ctorName);
\r
112 return Scriptable.NOT_FOUND;
\r
116 // Get just added object
\r
117 return obj.get(ctorName, obj);
\r
120 public Object setProperty(ScriptableObject obj, Object val) {
\r
121 synchronized (obj) {
\r
127 private static Method getter, setter;
\r
129 private String ctorName;
\r
130 private String className;
\r
131 private boolean sealed;
\r
132 private boolean isReplaced;
\r