KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdo > query > CastNode


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdo.query;
13
14 /**
15  * A cast operation.
16  */

17 public class CastNode extends UnaryNode {
18
19     public int brackets;
20     public String JavaDoc type;
21     public Class JavaDoc cls;
22
23     public CastNode(Node child, int brackets, Object JavaDoc tp) {
24         super(child);
25         this.brackets = brackets;
26         if (tp instanceof Class JavaDoc) cls = (Class JavaDoc)tp;
27         else type = (String JavaDoc)tp;
28     }
29
30     public String JavaDoc toString() {
31         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
32         s.append(super.toString());
33         s.append(" (");
34         if (cls != null) s.append(cls);
35         else s.append(type);
36         s.append(')');
37         return s.toString();
38     }
39
40     public Field visit(MemVisitor visitor, Object JavaDoc obj) {
41         return visitor.visitCastNode(this, obj);
42     }
43
44     /**
45      * Convert us into a FieldNavNode with cast information.
46      */

47     protected void normalizeImp() {
48         super.normalizeImp();
49
50         // convert this tree:
51
// CastNode (Address) [this]
52
// FieldNavNode (PolyRefHolder)data [toRaise]
53
// FieldNode data [toReplace]
54
// FieldNode city [toMove]
55
// SomeOtherNode [toKeep] (may be missing)
56

57         // or this one (note that toRaise is missing):
58
// CastNode (Address) [this]
59
// FieldNode data [toReplace]
60
// FieldNode city [toMove]
61
// SomeOtherNode [toKeep] (may be missing)
62

63         // into:
64
// FieldNavNode (PolyRefHolder)data [toRaise]
65
// FieldNavNode (Address)data [castFnn]
66
// FieldNode city [toMove]
67
// SomeOtherNode [toKeep] (if not null)
68

69         if (next instanceof FieldNode || next instanceof FieldNavNode) {
70
71             // find FieldNode to replace with FieldNavNode including cast
72
FieldNode toReplace = findFieldNode(childList);
73             if (toReplace == null) return;
74             Node toMove = next;
75             Node toKeep = next.next;
76             Node toRaise = childList;
77
78             // create FNN including cast from toReplace's field name
79
FieldNavNode castFnn = new FieldNavNode();
80             castFnn.cast = type;
81             castFnn.lexeme = toReplace.lexeme;
82
83             // make toMove castFnn's only child
84
castFnn.childList = toMove;
85             toMove.parent = castFnn;
86             toMove.next = null;
87
88             // make castFnn toReplace.parent's only child
89
toReplace.parent.childList = castFnn;
90             castFnn.parent = toReplace.parent;
91
92             // if toRaise and toReplace are the same then raise castFnn
93
if (toRaise == toReplace) toRaise = castFnn;
94
95             // make toRaise and toKeep our parents first and second children
96
// removing us from the tree
97
parent.childList = toRaise;
98             toRaise.parent = parent;
99             toRaise.next = toKeep;
100             if (toKeep != null) toKeep.parent = parent;
101         }
102     }
103
104     /**
105      * Find the first FieldNode in tree at root following only the first
106      * child of each node down the tree. Returns null if none found.
107      */

108     private FieldNode findFieldNode(Node root) {
109         for (; root != null && !(root instanceof FieldNode); root = root.childList);
110         return (FieldNode)root;
111     }
112
113     public Object JavaDoc arrive(NodeVisitor v, Object JavaDoc msg) {
114         return v.arriveCastNode(this, msg);
115     }
116
117 }
118
Popular Tags