KickJava   Java API By Example, From Geeks To Geeks.

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


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 import com.versant.core.metadata.FieldMetaData;
15 import com.versant.core.metadata.ClassMetaData;
16 import com.versant.core.common.Debug;
17
18 import com.versant.core.common.BindingSupportImpl;
19
20 /**
21  * This node is created when the value of a field is required as part
22  * of an expression.
23  */

24 public class FieldNode extends LeafNode {
25
26     public String JavaDoc lexeme;
27     /**
28      * The field whose value is required.
29      */

30     public FieldMetaData fmd;
31     /**
32      * If true then the field is on the candidate instance being tested
33      * (e.g. this.field).
34      */

35     public boolean useCandidateExtent;
36
37     public boolean resolved;
38
39     public FieldNode() {
40     }
41
42     public FieldNode(Node parent, String JavaDoc lexeme) {
43         this.parent = parent;
44         this.lexeme = lexeme;
45     }
46
47     public Object JavaDoc accept(NodeVisitor visitor, Object JavaDoc[] results) {
48       return visitor.visitFieldNode(this, results);
49     }
50
51     public String JavaDoc toString() {
52         return super.toString() + " " + (fmd != null ? fmd.toString() : lexeme) + " asValue " + asValue;
53     }
54
55     /**
56      * Resolve field refs and so on relative to the compiler. This must
57      * recursively resolve any child nodes.
58      */

59     public void resolve(QueryParser comp, ClassMetaData cmd, boolean ordering) {
60         if (Debug.DEBUG) System.out.println("### FieldNode.resolve " + this);
61         final boolean fnn = parent instanceof FieldNavNode;
62         if (!ordering) {
63             if (!resolved && !fnn) {
64                 VarNode v = comp.findVar(lexeme);
65                 Node rep;
66                 if (v != null) {
67                     // if we are the first child of a MethodNode (i.e. the
68
// instance the method is being invoked on) and have not
69
// been bound then we are an unbound variable
70
if (!v.bound && ((parent instanceof BinaryNode)
71                             || (parent instanceof MethodNode && parent.childList == this))) {
72                         v.insertVarBindingNode(parent);
73                     } else {
74                         v.bound = true;
75                         if (Debug.DEBUG) {
76                             System.out.println("### bound " + this);
77                         }
78                     }
79                     if (v.parent != null) rep = new VarNodeProxy(v);
80                     else rep = v;
81                 } else {
82                     rep = v;
83                 }
84                 if (rep == null) {
85                     ParamNode p = comp.findParam(lexeme);
86                     if (p != null && p.parent != null) rep = new ParamNodeProxy(p);
87                     else rep = p;
88                 }
89                 if (rep == null && lexeme.equals("this")) {
90                     rep = new ReservedFieldNode(ReservedFieldNode.TYPE_THIS, "this");
91                     rep.resolve(comp, cmd, false);
92                 }
93                 if (rep != null) {
94                     parent.replaceChild(this, rep);
95                     return;
96                 }
97             }
98         }
99
100         fmd = cmd.getFieldMetaData(lexeme);
101
102         if (fmd == null) {
103             if (parent instanceof FieldNavNode) {
104                 FieldNavNode fNN = (FieldNavNode) parent;
105                 if (fNN.embedded) {
106                     FieldMetaData[] fmds = cmd.fields;
107                     for (int i = 0; i < fmds.length; i++) {
108                         FieldMetaData fieldMetaData = fmds[i];
109                         if (fieldMetaData.name.equals(fNN.fmd.name + "/" + lexeme)) {
110                             fmd = fieldMetaData;
111                             break;
112                         }
113                     }
114                 }
115             }
116         }
117
118         if (fmd == null) {
119             //assume that this is an aliased column name
120
parent.replaceChild(this, new AsValueNode(lexeme));
121             return;
122         }
123         if (fmd == null) {
124             String JavaDoc msg;
125             if (fnn) {
126                 msg = "Field '" + lexeme + "' not found on " +
127                     cmd.qname;
128             } else {
129                 msg = "Identifier '" + lexeme + "' is not a parameter, " +
130                     "variable or field of " + cmd.qname;
131             }
132             throw BindingSupportImpl.getInstance().runtime(msg);
133         }
134
135         /**
136          * Update the cmd for multi-Table inheritance
137          */

138         if (fmd.classMetaData != cmd) {
139             cmd = fmd.classMetaData;
140         }
141     }
142
143     public Field visit(MemVisitor visitor, Object JavaDoc obj) {
144         return visitor.visitFieldNode(this, obj);
145     }
146
147     public Object JavaDoc arrive(NodeVisitor v, Object JavaDoc msg) {
148         return v.arriveFieldNode(this, msg);
149     }
150
151 }
152
153
Popular Tags