KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > jdo > jdoql > Type


1 package org.apache.ojb.jdo.jdoql;
2
3 /* Copyright 2003-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import org.apache.ojb.broker.util.ClassHelper;
19
20 /**
21  * Encapsulates a type, either primitive or reference.
22  *
23  * @author <a HREF="mailto:tomdz@apache.org">Thomas Dudziak</a>
24  */

25 public class Type extends QueryTreeNode
26 {
27     /** The (possibly unqualified) name of the type */
28     private String JavaDoc _name;
29     /** The type resolved using the imports of the query */
30     private Class JavaDoc _type;
31
32     /**
33      * Creates a new type object.
34      *
35      * @param typeName The (possibly unqualified) name of the variable
36      * @param isPrimitive Whether the type is a primitive type
37      */

38     public Type(String JavaDoc typeName, boolean isPrimitive)
39     {
40         _name = typeName;
41         if (isPrimitive)
42         {
43             resolvePrimitiveType();
44         }
45     }
46
47     /**
48      * Resolves the primitive type of this variable by determining the corresponding
49      * <code>java.lang</code> type.
50      */

51     private void resolvePrimitiveType()
52     {
53         final ClassLoader JavaDoc loader = ClassHelper.getClassLoader();
54         try
55         {
56             _type = Class.forName(_name, true, loader);
57         }
58         catch (ClassNotFoundException JavaDoc ex)
59         {
60             // won't happen because the parser is restricted to valid type names
61
}
62     }
63     
64     /**
65      * Returns the actual class object of the type if already resolved.
66      *
67      * @return The type or <code>null</code> if not yet resolved
68      */

69     public Class JavaDoc getType()
70     {
71         return _type;
72     }
73
74     /**
75      * Sets the resolved type.
76      *
77      * @param type The type's class object
78      */

79     public void setType(Class JavaDoc type)
80     {
81         _type = type;
82         if (_type != null)
83         {
84             _name = type.getName();
85         }
86     }
87
88     /**
89      * Returns the variable type's name.
90      *
91      * @return The type name
92      */

93     public String JavaDoc getName()
94     {
95         return _name;
96     }
97
98     /* (non-Javadoc)
99      * @see org.apache.ojb.jdo.jdoql.Acceptor#accept(org.apache.ojb.jdo.jdoql.Visitor)
100      */

101     public void accept(Visitor visitor)
102     {
103         visitor.visit(this);
104     }
105
106     /* (non-Javadoc)
107      * @see java.lang.Object#toString()
108      */

109     public String JavaDoc toString()
110     {
111         return _type == null ? _name : _type.getName();
112     }
113 }
114
Popular Tags