2003/05/12 05:10:30
[org.ibex.core.git] / src / org / mozilla / javascript / LazilyLoadedCtor.java
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-\r
2  *\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
7  *\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
12  *\r
13  * The Original Code is Rhino code, released\r
14  * May 6, 1999.\r
15  *\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
19  * Rights Reserved.\r
20  *\r
21  * Contributor(s):\r
22  * Norris Boyd\r
23  *\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
34  */\r
35 \r
36 package org.mozilla.javascript;\r
37 \r
38 import java.lang.reflect.*;\r
39 \r
40 /**\r
41  * Avoid loading classes unless they are used.\r
42  *\r
43  * <p> This improves startup time and average memory usage.\r
44  */\r
45 public final class LazilyLoadedCtor {\r
46 \r
47     public LazilyLoadedCtor(ScriptableObject scope,\r
48                      String ctorName, String className, boolean sealed)\r
49     {\r
50 \r
51         this.className = className;\r
52         this.ctorName = ctorName;\r
53         this.sealed = sealed;\r
54 \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
59         }\r
60 \r
61         try {\r
62             scope.defineProperty(ctorName, this, getter, setter,\r
63                                  ScriptableObject.DONTENUM);\r
64         }\r
65         catch (PropertyException e) {\r
66             throw WrappedException.wrapException(e);\r
67         }\r
68     }\r
69 \r
70     public Object getProperty(ScriptableObject obj) {\r
71         synchronized (obj) {\r
72             if (!isReplaced) {\r
73                 boolean removeOnError = false;\r
74 \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
80 \r
81                 Class cl = null;\r
82                 try { cl = Class.forName(className); }\r
83                 catch (ClassNotFoundException ex) { removeOnError = true; }\r
84                 catch (SecurityException ex) { removeOnError = true; }\r
85 \r
86                 if (cl != null) {\r
87                     try {\r
88                         ScriptableObject.defineClass(obj, cl, sealed);\r
89                         isReplaced = true;\r
90                     }\r
91                     catch (InstantiationException e) {\r
92                         throw WrappedException.wrapException(e);\r
93                     }\r
94                     catch (IllegalAccessException e) {\r
95                         throw WrappedException.wrapException(e);\r
96                     }\r
97                     catch (InvocationTargetException e) {\r
98                         throw WrappedException.wrapException(e);\r
99                     }\r
100                     catch (ClassDefinitionException e) {\r
101                         throw WrappedException.wrapException(e);\r
102                     }\r
103                     catch (PropertyException e) {\r
104                         throw WrappedException.wrapException(e);\r
105                     }\r
106                     catch (SecurityException ex) {\r
107                         removeOnError = true;\r
108                     }\r
109                 }\r
110                 if (removeOnError) {\r
111                     obj.delete(ctorName);\r
112                     return Scriptable.NOT_FOUND;\r
113                 }\r
114             }\r
115         }\r
116         // Get just added object\r
117         return obj.get(ctorName, obj);\r
118     }\r
119 \r
120     public Object setProperty(ScriptableObject obj, Object val) {\r
121         synchronized (obj) {\r
122             isReplaced = true;\r
123             return val;\r
124         }\r
125     }\r
126 \r
127     private static Method getter, setter;\r
128 \r
129     private String ctorName;\r
130     private String className;\r
131     private boolean sealed;\r
132     private boolean isReplaced;\r
133 }\r