1 package gnu.xquery.util; 2 import gnu.kawa.reflect.*; 3 import gnu.xquery.lang.XQuery; 4 import gnu.bytecode.*; 5 import gnu.kawa.xml.*; 6 import gnu.expr.*; 7 8 public class CastableAs extends InstanceOf 9 { 10 public static CastableAs castableAs = new CastableAs(); 11 12 CastableAs () 13 { 14 super(XQuery.getInstance(), "castable as"); 15 } 16 17 public Object apply2 (Object arg1, Object arg2) 18 { 19 Type type = language.asType(arg2); 20 boolean result; 21 if (type instanceof XDataType) 22 result = ((XDataType) type).castable(arg1); 23 else 24 result = type.isInstance(arg1); 25 return language.booleanObject(result); 26 } 27 28 static final Method castableMethod 29 = CastAs.typeXDataType.getDeclaredMethod("castable", 1); 30 31 public Expression inline (ApplyExp exp, ExpWalker walker) 32 { 33 exp = Invoke.inlineClassName(exp, 1, (InlineCalls) walker); 34 Expression[] args = exp.getArgs(); 35 if (args.length != 2 || ! (args[1] instanceof QuoteExp)) 36 return exp; 37 Object type = ((QuoteExp) args[1]).getValue(); 38 if (type instanceof XDataType) 39 { 40 XDataType xtype = (XDataType) type; 41 return new ApplyExp(castableMethod, 42 new Expression[] { args[1], args[0] }); 43 } 44 return exp; 45 } 46 47 public void compile (ApplyExp exp, Compilation comp, Target target) 48 { 49 ApplyExp.compile(exp, comp, target); 51 } 52 } 53 | Popular Tags |