1 package kawa.lang; 2 import gnu.mapping.*; 3 import gnu.expr.*; 4 import java.io.*; 5 6 11 12 public class AutoloadProcedure extends Procedure implements Externalizable 13 { 14 20 String className; 21 22 Language language; 23 24 25 Procedure loaded; 26 27 public AutoloadProcedure () 28 { 29 } 30 31 public AutoloadProcedure (String name, String className) 32 { 33 super(name); 34 this.className = className; 35 } 36 37 public AutoloadProcedure (String name, String className, Language language) 38 { 39 super(name); 40 this.className = className; 41 this.language = language; 42 } 43 44 public void print(java.io.PrintWriter ps) 45 { 46 ps.print ("#<procedure "); 47 String name = getName(); 48 if (name != null) 49 { 50 ps.print (name); 51 } 53 62 ps.print ('>'); 63 } 64 65 private void throw_error (String prefix) 66 { 67 loaded = null; 68 String name = getName(); 69 throw new RuntimeException (prefix + className 70 + " while autoloading " 71 + (name == null ? "" : name.toString ())); 72 } 73 74 75 void load () 76 { 77 Object name = this.getSymbol(); 78 try 79 { 80 loaded = (Procedure) Class.forName (className).newInstance (); 81 if (loaded == this) 82 throw_error("circularity detected"); 83 if (name != null) 84 { 85 try 86 { 87 Object property = (language.hasSeparateFunctionNamespace() 88 ? EnvironmentKey.FUNCTION 89 : null); 90 Environment env = language.getLangEnvironment(); 92 Symbol sym = (name instanceof Symbol ? (Symbol) name 93 : env.getSymbol(name.toString())); 94 env.put(sym, property, loaded); 95 } 96 catch (UnboundLocationException ex) 97 { 98 } 99 if (loaded.getSymbol() == null) 100 loaded.setSymbol(name); 101 } 102 } 103 catch (ClassNotFoundException ex) 104 { throw_error ("failed to find class "); } 105 catch (InstantiationException ex) 106 { throw_error ("failed to instantiate class "); } 107 catch (IllegalAccessException ex) 108 { throw_error ("illegal access in class "); } 109 } 110 111 public Procedure getLoaded () 112 { 113 if (loaded == null) 114 load(); 115 return loaded; 116 } 117 118 public int numArgs () 119 { 120 return getLoaded().numArgs(); 121 } 122 123 public Object apply0 () throws Throwable 124 { 125 return getLoaded().apply0 (); 126 } 127 128 public Object apply1 (Object arg1) throws Throwable 129 { 130 return getLoaded().apply1 (arg1); 131 } 132 133 public Object apply2 (Object arg1,Object arg2) throws Throwable 134 { 135 return getLoaded().apply2 (arg1, arg2); 136 } 137 138 public Object apply3 (Object arg1, Object arg2, Object arg3) throws Throwable 139 { 140 return getLoaded().apply3 (arg1, arg2, arg3); 141 } 142 143 public Object apply4 (Object arg1, Object arg2, 144 Object arg3, Object arg4) throws Throwable 145 { 146 return getLoaded().apply4 (arg1, arg2, arg3, arg4); 147 } 148 149 public Object applyN (Object [] args) throws Throwable 150 { 151 if (loaded == null) 152 load (); 153 if (loaded instanceof AutoloadProcedure) 154 throw new InternalError ("circularity in autoload of "+getName()); 155 return loaded.applyN (args); 156 } 157 158 public Procedure getSetter() 159 { 160 if (loaded == null) 161 load (); 162 if (loaded instanceof HasSetter) 163 return loaded.getSetter(); 164 return super.getSetter(); 165 } 166 167 public void writeExternal(ObjectOutput out) throws IOException 168 { 169 out.writeObject(getName()); 170 out.writeObject(className); 171 } 172 173 public void readExternal(ObjectInput in) 174 throws IOException, ClassNotFoundException 175 { 176 setName((String ) in.readObject()); 177 className = (String ) in.readObject(); 178 } 179 180 public Object getProperty(Object key, Object defaultValue) 181 { 182 Object value = super.getProperty(key, null); 183 if (value != null) 184 return value; 185 return getLoaded().getProperty(key, defaultValue); 186 } 187 } 188 | Popular Tags |