001: /*
002: * @(#)_interface.java 1.2 04/12/06
003: *
004: * Copyright (c) 1997-2004 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * See the file "LICENSE.txt" for information on usage and redistribution
007: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008: */
009: package org.pnuts.lib;
010:
011: import pnuts.lang.Context;
012: import pnuts.lang.PnutsFunction;
013: import pnuts.lang.PnutsException;
014: import pnuts.lang.Package;
015: import pnuts.lang.Runtime;
016: import pnuts.compiler.*;
017: import java.util.Enumeration;
018: import java.util.Map;
019: import java.util.ArrayList;
020: import java.util.Collection;
021: import java.util.List;
022: import org.pnuts.lang.SubtypeGenerator;
023: import org.pnuts.lang.ClassFileLoader;
024:
025: public class _interface extends PnutsFunction {
026:
027: private ClassFileHandler handler;
028:
029: public _interface() {
030: this (null);
031: }
032:
033: public _interface(ClassFileHandler handler) {
034: super ("interface");
035: this .handler = handler;
036: }
037:
038: public boolean defined(int nargs) {
039: return nargs == 3;
040: }
041:
042: protected Object exec(Object[] args, Context context) {
043: int nargs = args.length;
044: if (nargs != 3) {
045: undefined(args, context);
046: return null;
047: }
048: String className = (String) args[0];
049:
050: Object super types = args[1];
051: ArrayList list;
052:
053: Enumeration en;
054: list = new ArrayList();
055: if (super types != null) {
056: en = Runtime.toEnumeration(super types, context);
057: if (en == null) {
058: throw new IllegalArgumentException(String
059: .valueOf(super types));
060: }
061: while (en.hasMoreElements()) {
062: Object type = en.nextElement();
063: if (!(type instanceof Class)
064: || !((Class) type).isInterface()) {
065: throw new IllegalArgumentException(String
066: .valueOf(type));
067: }
068: list.add(type);
069: }
070: }
071: Class[] super Interfaces = new Class[list.size()];
072: list.toArray(super Interfaces);
073:
074: Object arg2 = args[2];
075: list = new ArrayList();
076: en = Runtime.toEnumeration(arg2, context);
077: if (en == null) {
078: throw new IllegalArgumentException(String.valueOf(arg2));
079: }
080: while (en.hasMoreElements()) {
081: Object sig = en.nextElement();
082: if (!(sig instanceof String)) {
083: throw new IllegalArgumentException(String.valueOf(sig));
084: }
085: list.add(sig);
086: }
087: String[] signatures = new String[list.size()];
088: list.toArray(signatures);
089:
090: try {
091: if (handler == null) {
092: ClassLoader cl = SubtypeGenerator.mergeClassLoader(
093: super Interfaces, context.getClassLoader());
094: handler = new ClassFileLoader(cl);
095: context.setClassLoader((ClassLoader) handler);
096: }
097: ClassFile cf = SubtypeGenerator
098: .getClassFileForInterface(
099: className,
100: super Interfaces,
101: signatures,
102: context,
103: (short) (Constants.ACC_PUBLIC | Constants.ACC_INTERFACE));
104: return handler.handle(cf);
105: } catch (Exception e) {
106: throw new PnutsException(e, context);
107: }
108: }
109:
110: public String toString() {
111: return "function interface(className, supertypes, signatures)";
112: }
113: }
|