KickJava   Java API By Example, From Geeks To Geeks.

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


1 package gnu.kawa.functions;
2 import gnu.bytecode.*;
3 import gnu.mapping.*;
4 import gnu.kawa.reflect.*;
5 import gnu.expr.*;
6
7 /** Procedure to get the value of a named component of an object. */
8
9 public class SetNamedPart extends Procedure3 implements HasSetter, CanInline
10 {
11   public static final SetNamedPart setNamedPart = new SetNamedPart();
12   static { setNamedPart.setName("setNamedPart"); }
13
14   public Expression inline (ApplyExp exp, ExpWalker walker)
15   {
16     Expression[] args = exp.getArgs();
17     if (args.length != 3 || ! (args[1] instanceof QuoteExp))
18       return exp;
19     Expression context = args[0];
20     String JavaDoc mname = ((QuoteExp) args[1]).getValue().toString();
21     Type type = context.getType();
22     Compilation comp = walker.getCompilation();
23     Language language = comp.getLanguage();
24     Type typeval = language.getTypeFor(context);
25     ClassType caller = comp == null ? null
26       : comp.curClass != null ? comp.curClass
27       : comp.mainClass;
28     if (typeval instanceof ClassType)
29       return new ApplyExp(SlotSet.set$Mnstatic$Mnfield$Ex, args);
30
31     if (type instanceof ClassType)
32       {
33         Object JavaDoc part = SlotSet.lookupMember((ClassType) type, mname, caller);
34         if (part != null)
35           {
36             // FIXME: future kludge to avoid re-doing SlotGet.getField.
37
// args = new Expression[] { context, new QuoteExp(part) });
38
return new ApplyExp(SlotSet.set$Mnfield$Ex, args);
39           }
40       }
41     return exp;
42   }
43
44   public Object JavaDoc apply3 (Object JavaDoc container, Object JavaDoc part, Object JavaDoc value)
45   {
46     /*
47     if (container implements HasNamedParts)
48       return ((HasNamedParts) container).getNamedPart(part);
49     */

50     if (container instanceof Namespace)
51       {
52         Namespace ns = (Namespace) container;
53         String JavaDoc uri = ns.getName();
54         if (uri.startsWith("class:"))
55           container = ClassType.make(uri.substring(6));
56         else
57           {
58             Symbol sym = ns.getSymbol(part.toString());
59             Environment env = Environment.getCurrent();
60             Environment.getCurrent().put(sym, value);
61             return Values.empty;
62           }
63       }
64     if (container instanceof Class JavaDoc)
65       container = (ClassType) Type.make((Class JavaDoc) container);
66     if (container instanceof ClassType)
67       {
68         try
69           {
70             gnu.kawa.reflect.SlotSet.setStaticField(container, part.toString(), value);
71             return Values.empty;
72           }
73         catch (Throwable JavaDoc ex)
74           {
75             // FIXME!
76
}
77       }
78
79     gnu.kawa.reflect.SlotSet.setField(container, part.toString(), value);
80     return Values.empty;
81   }
82 }
83
Popular Tags