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