KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > ql > ArgExpr


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.ejb.ql;
31
32 import com.caucho.amber.type.EntityType;
33 import com.caucho.bytecode.JClass;
34 import com.caucho.bytecode.JField;
35 import com.caucho.bytecode.JMethod;
36 import com.caucho.config.ConfigException;
37 import com.caucho.ejb.cfg.EjbEntityBean;
38 import com.caucho.util.CharBuffer;
39
40 /**
41  * Expression representing a select method argument.
42  */

43 class ArgExpr extends Expr {
44   // method index
45
private int _index;
46
47   private Class JavaDoc _coerceType;
48
49   /**
50    * Creates a new argument expression.
51    *
52    * @param index the method index
53    */

54   ArgExpr(Query query, int index)
55     throws ConfigException
56   {
57     _query = query;
58     _index = index;
59
60     evalTypes();
61   }
62
63   /**
64    * Returns the method index.
65    */

66   int getIndex()
67   {
68     return _query.getArgIndex(_index);
69   }
70
71   /**
72    * Evaluates the types for the expression
73    */

74   void evalTypes()
75     throws ConfigException
76   {
77     if (getJavaType() != null)
78       return;
79
80     JMethod method = _query.getMethod();
81
82     JClass []args = method.getParameterTypes();
83
84     if (args.length <= _index - 1)
85       throw error(L.l("`{0}' exceeds number of arguments", "?" + _index));
86
87     setJavaType(args[_index - 1]);
88
89     _query.setArgSize(_index, getComponentCount());
90   }
91
92   void setCoerceType(Class JavaDoc javaType)
93   {
94     _coerceType = javaType;
95   }
96
97   /**
98    * Only args can be coerced.
99    */

100   boolean canCoerce()
101   {
102     return true;
103   }
104
105   int getComponentCount()
106   {
107     JClass javaType = getJavaType();
108
109     if (javaType.isPrimitive() ||
110         javaType.isArray() ||
111         javaType.getName().startsWith("java."))
112       return 1;
113
114     if (javaType.isAssignableTo(javax.ejb.EJBLocalObject JavaDoc.class)) {
115       EjbEntityBean bean = _query.getConfig().findEntityByLocal(javaType);
116
117       EntityType type = bean.getEntityType();
118     
119       return type.getId().getKeys().size();
120     }
121
122     JField []fields = javaType.getFields();
123     return fields.length;
124   }
125   
126   /**
127    * Prints the where SQL for this expression
128    *
129    * @param gen the java code generator
130    */

131   void generateWhere(CharBuffer cb)
132   {
133     cb.append("?" + getIndex());
134   }
135
136   void generateComponent(CharBuffer cb, int index)
137   {
138     cb.append("?" + (getIndex() + index));
139   }
140
141   /**
142    * Printable version of the object.
143    */

144   public String JavaDoc toString()
145   {
146     return "?" + _index;
147   }
148 }
149
Popular Tags