1 package gnu.kawa.functions; 2 import gnu.bytecode.*; 3 import gnu.mapping.*; 4 import gnu.kawa.reflect.*; 5 import gnu.expr.*; 6 7 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 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 part = SlotSet.lookupMember((ClassType) type, mname, caller); 34 if (part != null) 35 { 36 return new ApplyExp(SlotSet.set$Mnfield$Ex, args); 39 } 40 } 41 return exp; 42 } 43 44 public Object apply3 (Object container, Object part, Object value) 45 { 46 50 if (container instanceof Namespace) 51 { 52 Namespace ns = (Namespace) container; 53 String 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 ) 65 container = (ClassType) Type.make((Class ) 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 ex) 74 { 75 } 77 } 78 79 gnu.kawa.reflect.SlotSet.setField(container, part.toString(), value); 80 return Values.empty; 81 } 82 } 83 | Popular Tags |