001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.nativequery.optimization;
022:
023: import java.lang.reflect.Field;
024: import java.lang.reflect.Method;
025:
026: import EDU.purdue.cs.bloat.file.ClassSource;
027:
028: import com.db4o.nativequery.expr.cmp.operand.*;
029:
030: class TypeDeducingVisitor implements ComparisonOperandVisitor {
031: private Class _predicateClass;
032: private Class _candidateClass;
033: private Class _clazz;
034: private ClassSource _classSource;
035:
036: public TypeDeducingVisitor(Class predicateClass,
037: Class candidateClass, ClassSource classSource) {
038: this ._predicateClass = predicateClass;
039: this ._candidateClass = candidateClass;
040: this ._classSource = classSource;
041: _clazz = null;
042: }
043:
044: public void visit(PredicateFieldRoot root) {
045: _clazz = _predicateClass;
046: }
047:
048: public void visit(CandidateFieldRoot root) {
049: _clazz = _candidateClass;
050: }
051:
052: public void visit(StaticFieldRoot root) {
053: try {
054: _clazz = _classSource.loadClass(root.className());
055: } catch (ClassNotFoundException e) {
056: e.printStackTrace();
057: }
058: }
059:
060: public Class operandClass() {
061: return _clazz;
062: }
063:
064: public void visit(ArithmeticExpression operand) {
065: }
066:
067: public void visit(ConstValue operand) {
068: _clazz = operand.value().getClass();
069: }
070:
071: public void visit(FieldValue operand) {
072: operand.parent().accept(this );
073: try {
074: _clazz = fieldFor(_clazz, operand.fieldName()).getType();
075: } catch (Exception e) {
076: e.printStackTrace();
077: }
078: }
079:
080: public void visit(ArrayAccessValue operand) {
081: operand.parent().accept(this );
082: _clazz = _clazz.getComponentType();
083: }
084:
085: Field fieldFor(Class clazz, String fieldName) {
086: while (clazz != null) {
087: try {
088: return clazz.getDeclaredField(fieldName);
089: } catch (Exception e) {
090: }
091: }
092: return null;
093: }
094:
095: public void visit(MethodCallValue operand) {
096: operand.parent().accept(this );
097: Method method = ReflectUtil.methodFor(_clazz, operand
098: .methodName(), operand.paramTypes());
099: _clazz = method.getReturnType();
100: }
101: }
|