KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > nativequery > optimization > TypeDeducingVisitor


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.nativequery.optimization;
22
23 import java.lang.reflect.*;
24
25 import com.db4o.nativequery.expr.cmp.*;
26 import com.db4o.nativequery.expr.cmp.field.*;
27
28 class TypeDeducingVisitor implements ComparisonOperandVisitor {
29     private Class JavaDoc _predicateClass;
30     private Class JavaDoc _candidateClass;
31     private Class JavaDoc _clazz;
32     
33     public TypeDeducingVisitor(Class JavaDoc predicateClass, Class JavaDoc candidateClass) {
34         this._predicateClass = predicateClass;
35         this._candidateClass = candidateClass;
36         _clazz=null;
37     }
38
39     public void visit(PredicateFieldRoot root) {
40         _clazz=_predicateClass;
41     }
42
43     public void visit(CandidateFieldRoot root) {
44         _clazz=_candidateClass;
45     }
46
47     public void visit(StaticFieldRoot root) {
48         try {
49             _clazz=Class.forName(root.className());
50         } catch (ClassNotFoundException JavaDoc e) {
51             e.printStackTrace();
52         }
53     }
54     
55     public Class JavaDoc operandClass() {
56         return _clazz;
57     }
58
59     public void visit(ArithmeticExpression operand) {
60     }
61
62     public void visit(ConstValue operand) {
63         _clazz=operand.value().getClass();
64     }
65
66     public void visit(FieldValue operand) {
67         operand.parent().accept(this);
68         try {
69             _clazz=fieldFor(_clazz,operand.fieldName()).getType();
70         } catch (Exception JavaDoc e) {
71             e.printStackTrace();
72         }
73     }
74
75     public void visit(ArrayAccessValue operand) {
76         operand.parent().accept(this);
77         _clazz=_clazz.getComponentType();
78     }
79     
80     Field fieldFor(Class JavaDoc clazz,String JavaDoc fieldName) {
81         while(clazz!=null) {
82             try {
83                 return clazz.getDeclaredField(fieldName);
84             } catch (Exception JavaDoc e) {
85             }
86         }
87         return null;
88     }
89
90     public void visit(MethodCallValue operand) {
91         operand.parent().accept(this);
92         Method method=ReflectUtil.methodFor(_clazz, operand.methodName(), operand.paramTypes());
93         _clazz=method.getReturnType();
94     }
95 }
Popular Tags