KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > kawa > functions > GetNamedInstancePart


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 /** The value of the Kawa Scehem expression '*:PART-NAME'.
9  * This function invokes a method or accesses a field,
10  * if the PART-NAME starts with a '.'.
11  *
12  * This syntax is semi-depecated, since instead of
13  * (*:method-name instance args ...) you can now write
14  * (instance:method-name args ...), and
15  * instead of (*:.field-name instance) you can write
16  * instance:field-name (without the parentheses).
17  */

18
19 public class GetNamedInstancePart extends ProcedureN
20   implements Externalizable, CanInline, HasSetter
21 {
22   String JavaDoc pname;
23   boolean isField;
24
25   public static Expression makeExp (Expression member)
26   {
27     String JavaDoc name;
28     if (member instanceof QuoteExp)
29       {
30         Object JavaDoc val = ((QuoteExp) member).getValue();
31         if (val instanceof String JavaDoc)
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 JavaDoc name)
45   {
46     setPartName(name);
47   }
48
49   public void setPartName (String JavaDoc 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 JavaDoc applyN (Object JavaDoc[] args)
89     throws Throwable JavaDoc
90   {
91     checkArgCount(this, args.length);
92     if (isField)
93       return SlotGet.field(args[0], pname);
94     else
95       {
96         Object JavaDoc[] xargs = new Object JavaDoc[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 JavaDoc("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 JavaDoc
118   {
119     setPartName((String JavaDoc) in.readObject());
120   }
121 }
122
123 class SetNamedInstancePart extends Procedure2
124   implements Externalizable, CanInline
125 {
126   String JavaDoc pname;
127
128   public SetNamedInstancePart ()
129   {
130   }
131
132   public SetNamedInstancePart (String JavaDoc name)
133   {
134     setPartName(name);
135   }
136
137   public void setPartName (String JavaDoc 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 JavaDoc apply2 (Object JavaDoc instance, Object JavaDoc value)
153     throws Throwable JavaDoc
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 JavaDoc
167   {
168     setPartName((String JavaDoc) in.readObject());
169   }
170 }
171
Popular Tags