KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > compile > StaticClassFieldReferenceNode


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.StaticClassFieldReferenceNode
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.sql.compile;
23
24 import org.apache.derby.iapi.services.monitor.Monitor;
25
26 import org.apache.derby.iapi.services.compiler.MethodBuilder;
27
28 import org.apache.derby.iapi.services.sanity.SanityManager;
29
30 import org.apache.derby.iapi.error.StandardException;
31
32 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
33 import org.apache.derby.iapi.reference.SQLState;
34
35 import org.apache.derby.iapi.services.loader.ClassInspector;
36
37 import org.apache.derby.iapi.store.access.Qualifier;
38
39 import org.apache.derby.impl.sql.compile.ExpressionClassBuilder;
40 import org.apache.derby.iapi.sql.compile.CompilerContext;
41
42 import org.apache.derby.iapi.util.JBitSet;
43
44 import java.lang.reflect.Member JavaDoc;
45 import java.lang.reflect.Modifier JavaDoc;
46
47 import java.util.Vector JavaDoc;
48
49 /**
50  * A StaticClassFieldReferenceNode represents a Java static field reference from
51  * a Class (as opposed to an Object). Field references can be
52  * made in DML (as expressions).
53  *
54  * @author Jerry Brenner
55  */

56
57 public final class StaticClassFieldReferenceNode extends JavaValueNode
58 {
59     /*
60     ** Name of the field.
61     */

62     private String JavaDoc fieldName;
63
64     /* The class name */
65     private String JavaDoc javaClassName;
66     private boolean classNameDelimitedIdentifier;
67
68     /**
69         The field we are going to access.
70     */

71     private Member JavaDoc field;
72
73     /**
74      * Initializer for a StaticClassFieldReferenceNode
75      *
76      * @param javaClassName The class name
77      * @param fieldName The field name
78      */

79     public void init(Object JavaDoc javaClassName, Object JavaDoc fieldName, Object JavaDoc classNameDelimitedIdentifier)
80     {
81         this.fieldName = (String JavaDoc) fieldName;
82         this.javaClassName = (String JavaDoc) javaClassName;
83         this.classNameDelimitedIdentifier = ((Boolean JavaDoc) classNameDelimitedIdentifier).booleanValue();
84     }
85
86     /**
87      * Bind this expression. This means binding the sub-expressions,
88      * as well as figuring out what the return type is for this expression.
89      *
90      * @param fromList The FROM list for the query this
91      * expression is in, for binding columns.
92      * @param subqueryList The subquery list being built as we find SubqueryNodes
93      * @param aggregateVector The aggregate vector being built as we find AggregateNodes
94      *
95      * @return Nothing
96      *
97      * @exception StandardException Thrown on error
98      */

99
100     public JavaValueNode bindExpression(FromList fromList, SubqueryList subqueryList,
101         Vector JavaDoc aggregateVector)
102             throws StandardException
103     {
104         ClassInspector classInspector = getClassFactory().getClassInspector();
105
106
107         if (((getCompilerContext().getReliability() & CompilerContext.INTERNAL_SQL_ILLEGAL) != 0)
108             || !javaClassName.startsWith("java.sql.")) {
109
110             throw StandardException.newException(SQLState.LANG_SYNTAX_ERROR, javaClassName + "::" + fieldName);
111         }
112
113         javaClassName = verifyClassExist(javaClassName, ! classNameDelimitedIdentifier);
114
115         /*
116         ** Find the field that is public.
117         */

118         field = classInspector.findPublicField(javaClassName,
119                                         fieldName,
120                                         true);
121         /* Get the field type */
122         setJavaTypeName( classInspector.getType(field) );
123
124         return this;
125
126     }
127
128     /**
129      * Preprocess an expression tree. We do a number of transformations
130      * here (including subqueries, IN lists, LIKE and BETWEEN) plus
131      * subquery flattening.
132      * NOTE: This is done before the outer ResultSetNode is preprocessed.
133      *
134      * @param numTables Number of tables in the DML Statement
135      * @param outerFromList FromList from outer query block
136      * @param outerSubqueryList SubqueryList from outer query block
137      * @param outerPredicateList PredicateList from outer query block
138      *
139      * @exception StandardException Thrown on error
140      */

141     public void preprocess(int numTables,
142                             FromList outerFromList,
143                             SubqueryList outerSubqueryList,
144                             PredicateList outerPredicateList)
145                     throws StandardException
146     {
147     }
148
149     /**
150      * Categorize this predicate. Initially, this means
151      * building a bit map of the referenced tables for each predicate.
152      * If the source of this ColumnReference (at the next underlying level)
153      * is not a ColumnReference or a VirtualColumnNode then this predicate
154      * will not be pushed down.
155      *
156      * For example, in:
157      * select * from (select 1 from s) a (x) where x = 1
158      * we will not push down x = 1.
159      * NOTE: It would be easy to handle the case of a constant, but if the
160      * inner SELECT returns an arbitrary expression, then we would have to copy
161      * that tree into the pushed predicate, and that tree could contain
162      * subqueries and method calls.
163      * RESOLVE - revisit this issue once we have views.
164      *
165      * @param referencedTabs JBitSet with bit map of referenced FromTables
166      * @param simplePredsOnly Whether or not to consider method
167      * calls, field references and conditional nodes
168      * when building bit map
169      *
170      * @return boolean Whether or not source.expression is a ColumnReference
171      * or a VirtualColumnNode.
172      */

173     public boolean categorize(JBitSet referencedTabs, boolean simplePredsOnly)
174     {
175         return true;
176     }
177
178     /**
179      * Remap all ColumnReferences in this tree to be clones of the
180      * underlying expression.
181      *
182      * @return JavaValueNode The remapped expression tree.
183      *
184      * @exception StandardException Thrown on error
185      */

186     public JavaValueNode remapColumnReferencesToExpressions()
187         throws StandardException
188     {
189         return this;
190     }
191
192     /**
193      * Return the variant type for the underlying expression.
194      * The variant type can be:
195      * VARIANT - variant within a scan
196      * (method calls and non-static field access)
197      * SCAN_INVARIANT - invariant within a scan
198      * (column references from outer tables)
199      * QUERY_INVARIANT - invariant within the life of a query
200      * CONSTANT - constant
201      *
202      * @return The variant type for the underlying expression.
203      */

204     protected int getOrderableVariantType()
205     {
206         if (SanityManager.DEBUG)
207         {
208             SanityManager.ASSERT(field != null,
209                     "field is expected to be non-null");
210         }
211         /* Static field references are invariant for the life
212          * of the query, non-static are variant.
213          */

214         if (Modifier.isFinal(field.getModifiers()))
215         {
216             return Qualifier.CONSTANT;
217         }
218         else
219         {
220             return Qualifier.VARIANT;
221         }
222     }
223
224     /**
225      * @see QueryTreeNode#generate
226      *
227      * @exception StandardException Thrown on error
228      */

229     public void generateExpression(ExpressionClassBuilder acb,
230                                             MethodBuilder mb)
231     throws StandardException
232     {
233         /*
234         ** Generate the following:
235         **
236         ** <javaClassName>.<field name>
237         */

238
239         mb.getStaticField(field.getDeclaringClass().getName(),
240                                  fieldName,
241                                  getJavaTypeName());
242     }
243
244 }
245
Popular Tags