001: /*
002: * $Id: CompiledScope.java,v 1.7 2002/09/16 08:05:04 jkl Exp $
003: *
004: * Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
005: *
006: * Use is subject to license terms, as defined in
007: * Anvil Sofware License, Version 1.1. See LICENSE
008: * file, or http://njet.org/license-1.1.txt
009: */
010: package anvil.script.compiler;
011:
012: import anvil.core.Any;
013: import anvil.core.runtime.AnyFunction;
014: import anvil.core.runtime.AnyType;
015: import anvil.doc.Doc;
016: import anvil.script.Context;
017: import anvil.script.Type;
018: import anvil.script.Scope;
019: import anvil.script.ClassType;
020: import anvil.script.Function;
021: import anvil.script.CompilableFunction;
022: import anvil.script.InterfaceType;
023: import anvil.script.MethodType;
024: import anvil.script.ConstantVariableType;
025: import anvil.script.MemberVariableType;
026: import anvil.script.StaticVariableType;
027: import anvil.java.util.Hashlist;
028: import anvil.java.util.BindingEnumeration;
029: import java.lang.reflect.Field;
030: import java.lang.reflect.Method;
031: import java.lang.reflect.Constructor;
032: import java.util.Enumeration;
033:
034: /**
035: * class Compiled
036: *
037: * @author: Jani Lehtimäki
038: */
039: public abstract class CompiledScope extends Compiled implements Scope {
040:
041: protected Hashlist _types = new Hashlist();
042: protected Class _class = null;
043: protected String _descriptor = null;
044:
045: public CompiledScope(Scope parent, Class clazz, String name,
046: Doc document) {
047: super (parent, name, document);
048: if (clazz == null) {
049: clazz = this .getClass();
050: }
051: _class = clazz;
052: _descriptor = clazz.getName().replace('.', '/');
053: }
054:
055: protected void initializeMembers(ClassLoader classloader) {
056: try {
057: Method[] methods = _class.getDeclaredMethods();
058: Constructor[] constructors = _class.getConstructors();
059: Class[] classes = _class.getDeclaredClasses();
060: Object[] members = (Object[]) getstatic(_class, "_members");
061: int n = members.length;
062: for (int i = 0; i < n; i += 4) {
063: if (members[i] == null) {
064: break;
065: }
066:
067: int type = ((Integer) members[i]).intValue();
068: String name = (String) members[i + 1];
069: Doc doc = (Doc) members[i + 2];
070: Object data = members[i + 3];
071:
072: switch (type) {
073: case Type.MODULE: {
074: onScript(name, doc);
075: }
076: break;
077:
078: case NAMESPACE: {
079: Class innercls = findNamespace(classes, name);
080: onNamespace(classloader, innercls, name, doc);
081: }
082: break;
083:
084: case CLASS: {
085: Class innercls = findClass(classes, name);
086: onClass(classloader, innercls, name, doc);
087: }
088: break;
089:
090: case INTERFACE: {
091: Class innercls = findInterface(classes, name);
092: onInterface(classloader, innercls, name, doc);
093: }
094: break;
095:
096: case FUNCTION: {
097: Object[] parameters = (Object[]) data;
098: Method method = findMethod(methods, "f_" + name);
099: onFunction(method, name, parameters, doc);
100: }
101: break;
102:
103: case METHOD: {
104: Method method = findMethod(methods, "m_" + name);
105: Object[] parameters = (Object[]) data;
106: onMethod(method, name, parameters, doc);
107: }
108: break;
109:
110: case INTERFACE_METHOD: {
111: Method method = findMethod(methods, "m_" + name);
112: Object[] parameters = (Object[]) data;
113: onInterfaceMethod(method, name, parameters, doc);
114: }
115: break;
116:
117: case CONSTRUCTOR: {
118: Method method = findMethod(methods, "m_" + name);
119: Object[] parameters = (Object[]) data;
120: Constructor constructor = null;
121: int lastlength = -1;
122: for (int j = 0; j < constructors.length; j++) {
123: Class[] types = constructors[j]
124: .getParameterTypes();
125: if (types.length > lastlength) {
126: constructor = constructors[j];
127: lastlength = types.length;
128: }
129: }
130: onConstructor(method, constructor, name,
131: parameters, doc);
132: }
133: break;
134:
135: case CONSTANT_VARIABLE: {
136: Field field = _class.getDeclaredField("c_" + name);
137: onConstantVariable(field, name, doc);
138: }
139: break;
140:
141: case STATIC_VARIABLE: {
142: Field field = _class.getDeclaredField("s_" + name);
143: onStaticVariable(field, name, doc);
144: }
145: break;
146:
147: case MEMBER_VARIABLE: {
148: Field field = _class.getDeclaredField("f_" + name);
149: onMemberVariable(field, name, doc);
150: }
151: break;
152:
153: default: {
154: anvil.Log.log().error(
155: "Invalid member code " + type + " for '"
156: + name + "' at initialization of "
157: + _class.getName());
158: }
159: break;
160:
161: }
162: }
163:
164: } catch (Throwable t) {
165: anvil.Log.log()
166: .error(
167: "Initialization of " + _class.getName()
168: + " failed", t);
169: }
170:
171: }
172:
173: protected void onScript(String name, Doc doc) {
174: }
175:
176: protected Scope onNamespace(ClassLoader classloader, Class cls,
177: String name, Doc doc) {
178: Scope namespace = new CompiledNamespace(classloader, this , cls,
179: name, doc);
180: declare(namespace);
181: return namespace;
182: }
183:
184: protected ClassType onClass(ClassLoader classloader, Class cls,
185: String name, Doc doc) {
186: ClassType clazz = new CompiledClassType(classloader, this , cls,
187: name, doc);
188: declare(clazz);
189: return clazz;
190: }
191:
192: protected InterfaceType onInterface(ClassLoader classloader,
193: Class cls, String name, Doc doc) {
194: InterfaceType intrface = new CompiledInterfaceType(classloader,
195: this , cls, name, doc);
196: declare(intrface);
197: return intrface;
198: }
199:
200: protected CompilableFunction onFunction(Method method, String name,
201: Object[] parameters, Doc doc) {
202: CompilableFunction function = new FunctionBase(this , method,
203: name, parameters, doc);
204: putstatic(_class, "f_" + name, function);
205: putstatic(_class, "F_" + name, new AnyFunction(function));
206: declare(function);
207: return function;
208: }
209:
210: protected MethodType onMethod(Method method, String name,
211: Object[] parameters, Doc doc) {
212: MethodType function = new MethodBase(this , method, name,
213: parameters, doc);
214: putstatic(_class, "m_" + name, function);
215: putstatic(_class, "M_" + name, new AnyFunction(function));
216: declare(function);
217: return function;
218: }
219:
220: protected MethodType onInterfaceMethod(Method method, String name,
221: Object[] parameters, Doc doc) {
222: MethodType function = new InterfaceMethodBase(this , method,
223: name, parameters, doc);
224: putstatic(_class, "m_" + name, function);
225: putstatic(_class, "M_" + name, new AnyFunction(function));
226: declare(function);
227: return function;
228: }
229:
230: protected MethodType onConstructor(Method method,
231: Constructor constructor, String name, Object[] parameters,
232: Doc doc) {
233: MethodType function = new ConstructorBase(this , constructor,
234: method, name, parameters, doc);
235: putfield(_class, null, "m_" + name, function);
236: putfield(_class, null, "M_" + name, new AnyFunction(function));
237: declare(function);
238: return function;
239: }
240:
241: protected ConstantVariableType onConstantVariable(Field field,
242: String name, Doc doc) {
243: ConstantVariableType constant = new ConstantVariable(this ,
244: name, field, doc);
245: _types.put(name, constant);
246: return constant;
247: }
248:
249: protected StaticVariableType onStaticVariable(Field field,
250: String name, Doc doc) {
251: StaticVariableType variable = new StaticVariable(this , name,
252: field, doc);
253: _types.put(name, variable);
254: return variable;
255: }
256:
257: protected MemberVariableType onMemberVariable(Field field,
258: String name, Doc doc) {
259: MemberVariableType variable = new MemberVariable(this , name,
260: field, doc);
261: _types.put(name, variable);
262: return variable;
263: }
264:
265: public Type lookupDeclaration(String name) {
266: return (Type) _types.get(name);
267: }
268:
269: public final Enumeration getDeclarations() {
270: return _types.elements();
271: }
272:
273: protected final void declare(Type type) {
274: _types.put(type.getName(), type);
275: }
276:
277: protected final void declare(String name, Type type) {
278: _types.put(name, type);
279: }
280:
281: public int getTypeRef(anvil.codec.ConstantPool pool) {
282: return pool.addClass(_descriptor);
283: }
284:
285: public String getDescriptor() {
286: return _descriptor;
287: }
288:
289: public CompiledModule getModule() {
290: Type type = this ;
291: while (type != null) {
292: Type parent = type.getParent();
293: if (parent == null) {
294: return (CompiledModule) type;
295: }
296: type = parent;
297: }
298: return null;
299: }
300:
301: public String buildQualifiedName() {
302: StringBuffer buffer = new StringBuffer(24);
303: Scope scope = this ;
304: out: while (true) {
305: switch (scope.getType()) {
306: case CLASS:
307: case NAMESPACE:
308: if (buffer.length() > 0) {
309: buffer.insert(0, '.');
310: }
311: buffer.insert(0, scope.getName());
312: scope = scope.getParent();
313: break;
314: default:
315: break out;
316: }
317: }
318: return buffer.toString();
319: }
320:
321: }
|