Debian Codegraph @

rhino/src/org/mozilla/javascript/LazilyLoadedCtor.java

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 package org.mozilla.javascript;
8
9 import java.lang.reflect.*;
10 import java.security.AccessController;
11 import java.security.PrivilegedAction;
12
13 /**
14 * Avoid loading classes unless they are used.
15 *
16 * <p> This improves startup time and average memory usage.
17 */
18 public final class LazilyLoadedCtor implements java.io.Serializable {
19 private static final long serialVersionUID = 1L;
20
21 private static final int STATE_BEFORE_INIT = 0;
22 private static final int STATE_INITIALIZING = 1;
23 private static final int STATE_WITH_VALUE = 2;
24
25 private final ScriptableObject scope;
26 private final String propertyName;
27 private final String className;
28 private final boolean sealed;
29 private final boolean privileged;
30 private Object initializedValue;
31 private int state;
32
33 public LazilyLoadedCtor(ScriptableObject scope, String propertyName,
34 String className, boolean sealed)
35 {
36 this(scope, propertyName, className, sealed, false);
37 }
38
39 LazilyLoadedCtor(ScriptableObject scope, String propertyName,
40 String className, boolean sealed, boolean privileged)
41 {
42
43 this.scope = scope;
44 this.propertyName = propertyName;
45 this.className = className;
46 this.sealed = sealed;
47 this.privileged = privileged;
48 this.state = STATE_BEFORE_INIT;
49
50 scope.addLazilyInitializedValue(propertyName, 0, this,
51 ScriptableObject.DONTENUM);
52 }
53
54 void init()
55 {
56 synchronized (this) {
57 if (state == STATE_INITIALIZING)
58 throw new IllegalStateException(
59 "Recursive initialization for "+propertyName);
60 if (state == STATE_BEFORE_INIT) {
61 state = STATE_INITIALIZING;
62 // Set value now to have something to set in finally block if
63 // buildValue throws.
64 Object value = Scriptable.NOT_FOUND;
65 try {
66 value = buildValue();
67 } finally {
68 initializedValue = value;
69 state = STATE_WITH_VALUE;
70 }
71 }
72 }
73 }
74
75 Object getValue()
76 {
77 if (state != STATE_WITH_VALUE)
78 throw new IllegalStateException(propertyName);
79 return initializedValue;
80 }
81
82 private Object buildValue()
83 {
84 if(privileged)
85 {
86 return AccessController.doPrivileged(new PrivilegedAction<Object>()
87 {
88 public Object run()
89 {
90 return buildValue0();
91 }
92 });
93 }
94 else
95 {
96 return buildValue0();
97 }
98 }
99
100 private Object buildValue0()
101 {
102 Class<? extends Scriptable> cl = cast(Kit.classOrNull(className));
103 if (cl != null) {
104 try {
105 Object value = ScriptableObject.buildClassCtor(scope, cl,
106 sealed, false);
107 if (value != null) {
108 return value;
109 }
110 else {
111 // cl has own static initializer which is expected
112 // to set the property on its own.
113 value = scope.get(propertyName, scope);
114 if (value != Scriptable.NOT_FOUND)
115 return value;
116 }
117 } catch (InvocationTargetException ex) {
118 Throwable target = ex.getTargetException();
119 if (target instanceof RuntimeException) {
120 throw (RuntimeException)target;
121 }
122 } catch (RhinoException ex) {
123 } catch (InstantiationException ex) {
124 } catch (IllegalAccessException ex) {
125 } catch (SecurityException ex) {
126 }
127 }
128 return Scriptable.NOT_FOUND;
129 }
130
131 @SuppressWarnings({"unchecked"})
132 private Class<? extends Scriptable> cast(Class<?> cl) {
133 return (Class<? extends Scriptable>)cl;
134 }
135
136 }