1 package gnu.kawa.functions; 2 import gnu.bytecode.*; 3 import gnu.mapping.*; 4 import gnu.kawa.reflect.*; 5 import gnu.expr.*; 6 import java.io.*; 7 8 18 19 public class GetNamedInstancePart extends ProcedureN 20 implements Externalizable, CanInline, HasSetter 21 { 22 String pname; 23 boolean isField; 24 25 public static Expression makeExp (Expression member) 26 { 27 String name; 28 if (member instanceof QuoteExp) 29 { 30 Object val = ((QuoteExp) member).getValue(); 31 if (val instanceof String ) 32 return QuoteExp.getInstance(new GetNamedInstancePart(val.toString())); 33 } 34 Expression[] args = new Expression[2]; 35 args[0] = new QuoteExp(ClassType.make("gnu.kawa.functions.GetNamedInstancePart")); 36 args[1] = member; 37 return new ApplyExp(Invoke.make, args); 38 } 39 40 public GetNamedInstancePart () 41 { 42 } 43 44 public GetNamedInstancePart (String name) 45 { 46 setPartName(name); 47 } 48 49 public void setPartName (String name) 50 { 51 setName("get-instance-part:"+name); 52 if (name.length() > 1 && name.charAt(0) == '.') 53 { 54 isField = true; 55 pname = name.substring(1); 56 } 57 else 58 { 59 isField = false; 60 pname = name; 61 } 62 } 63 64 public int numArgs() { return isField ? 0x1001 : 0xfffff001; } 65 66 public Expression inline (ApplyExp exp, ExpWalker walker) 67 { 68 Expression[] args = exp.getArgs(); 69 Expression[] xargs; 70 Procedure proc; 71 if (isField) 72 { 73 xargs = new Expression[] { args[0], new QuoteExp(pname) }; 74 proc = SlotGet.field; 75 } 76 else 77 { 78 int nargs = args.length; 79 xargs = new Expression[nargs+1]; 80 xargs[0] = args[0]; 81 xargs[1] = new QuoteExp(pname); 82 System.arraycopy(args, 1, xargs, 2, nargs-1); 83 proc = Invoke.invoke; 84 } 85 return ((InlineCalls) walker).walkApplyOnly(new ApplyExp(proc, xargs)); 86 } 87 88 public Object applyN (Object [] args) 89 throws Throwable 90 { 91 checkArgCount(this, args.length); 92 if (isField) 93 return SlotGet.field(args[0], pname); 94 else 95 { 96 Object [] xargs = new Object [args.length+1]; 97 xargs[0] = args[0]; 98 xargs[1] = pname; 99 System.arraycopy(args, 1, xargs, 2, args.length-1); 100 return Invoke.invoke.applyN(xargs); 101 } 102 } 103 104 public Procedure getSetter() 105 { 106 if (! isField) 107 throw new RuntimeException ("no setter for instance method call"); 108 return new SetNamedInstancePart(pname); 109 } 110 111 public void writeExternal(ObjectOutput out) throws IOException 112 { 113 out.writeObject(isField ? ("."+pname) : pname); 114 } 115 116 public void readExternal(ObjectInput in) 117 throws IOException, ClassNotFoundException 118 { 119 setPartName((String ) in.readObject()); 120 } 121 } 122 123 class SetNamedInstancePart extends Procedure2 124 implements Externalizable, CanInline 125 { 126 String pname; 127 128 public SetNamedInstancePart () 129 { 130 } 131 132 public SetNamedInstancePart (String name) 133 { 134 setPartName(name); 135 } 136 137 public void setPartName (String name) 138 { 139 setName("set-instance-part:."+name); 140 pname = name; 141 } 142 143 public Expression inline (ApplyExp exp, ExpWalker walker) 144 { 145 Expression[] args = exp.getArgs(); 146 Expression[] xargs = new Expression[] 147 { args[0], new QuoteExp(pname), args[1] }; 148 Procedure proc = SlotSet.set$Mnfield$Ex; 149 return ((InlineCalls) walker).walkApplyOnly(new ApplyExp(proc, xargs)); 150 } 151 152 public Object apply2 (Object instance, Object value) 153 throws Throwable 154 { 155 SlotSet.setField(instance, pname, value); 156 return Values.empty; 157 } 158 159 160 public void writeExternal(ObjectOutput out) throws IOException 161 { 162 out.writeObject(pname); 163 } 164 165 public void readExternal(ObjectInput in) 166 throws IOException, ClassNotFoundException 167 { 168 setPartName((String ) in.readObject()); 169 } 170 } 171 | Popular Tags |