KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > spi > persistence > support > sqlstore > query > util > type > ClassType


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * ClassType.java
26  *
27  * Created on March 8, 2000
28  */

29
30 package com.sun.jdo.spi.persistence.support.sqlstore.query.util.type;
31
32 import java.util.Map JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.ArrayList JavaDoc;
36
37 import java.lang.reflect.Field JavaDoc;
38 import java.security.AccessController JavaDoc;
39 import java.security.PrivilegedAction JavaDoc;
40
41 import com.sun.jdo.spi.persistence.utility.FieldTypeEnumeration;
42 import com.sun.jdo.api.persistence.support.JDOFatalUserException;
43 import com.sun.jdo.api.persistence.support.JDOFatalInternalException;
44 import com.sun.jdo.api.persistence.model.Model;
45 import com.sun.jdo.api.persistence.model.jdo.PersistenceClassElement;
46 import com.sun.jdo.api.persistence.model.jdo.PersistenceFieldElement;
47
48 /**
49  *
50  * @author Michael Bouschen
51  * @version 0.1
52  */

53 public class ClassType
54     extends Type
55 {
56     /**
57      * The associated type table.
58      */

59     protected TypeTable typetab;
60
61     /**
62      *
63      */

64     protected Map JavaDoc fieldInfos;
65
66     /**
67      *
68      */

69     protected PersistenceClassElement pce;
70
71     /**
72      *
73      */

74     public ClassType(String JavaDoc name, Class JavaDoc clazz, int enumType, TypeTable typetab)
75     {
76         super(name, clazz, enumType);
77         this.typetab = typetab;
78         this.fieldInfos = new HashMap JavaDoc();
79         // get JDO model element if available
80
ClassLoader JavaDoc classLoader = clazz.getClassLoader();
81         if (classLoader != null)
82         {
83             try
84             {
85                 this.pce = typetab.model.getPersistenceClass(name, classLoader);
86             }
87             catch (IllegalArgumentException JavaDoc ex)
88             {
89                 // IllegalArgumentException indicates class loader problem
90
throw new JDOFatalUserException(ex.getMessage());
91             }
92         }
93     }
94
95     /**
96      *
97      */

98     public ClassType(String JavaDoc name, Class JavaDoc clazz, TypeTable typetab)
99     {
100         this(name, clazz, FieldTypeEnumeration.NOT_ENUMERATED, typetab);
101     }
102
103     /**
104      * Checks the compatibility of this with the specified type.
105      * A ClassType object is compatible to
106      * errorType, to the type of null (NullType), to itself
107      * and to a super class (direct or indirect).
108      * @param type type for compatibility check
109      * @return true if this is compatible with type;
110      * false otherwise.
111      * @see Type#isCompatibleWith(Type)
112      */

113     public boolean isCompatibleWith(Type type)
114     {
115         boolean result = false;
116         if (type instanceof ClassType)
117         {
118             result = ((ClassType)type).clazz.isAssignableFrom(clazz);
119         }
120         return result;
121     }
122
123     /**
124      * Returns whether this represents a type with an
125      * defined order.
126      * @return true if an order is defined for this;
127      * false otherwise.
128      */

129         public boolean isOrderable()
130         {
131         Type comparable = typetab.checkType("java.lang.Comparable"); //NOI18N
132
return isCompatibleWith(comparable);
133         }
134
135     /**
136      * Returns true if this is defined as persistence capable class.
137      * @return true if this is a persistence capable class;
138      * false otherwise.
139      */

140         public boolean isPersistenceCapable()
141         {
142         return (pce != null);
143         }
144
145     // --------------------
146
// Field handling
147
// --------------------
148

149     /**
150      * Returns an array of fieldInfos for all declared fields.
151      */

152     public FieldInfo[] getFieldInfos()
153     {
154         // Initialize the fieldInfos map with the field declared for this class.
155
// NOTE, this code does not work for inheritance!
156
//Field[] fields = clazz.getDeclaredFields();
157

158         final Class JavaDoc cl = clazz;
159
160         Field JavaDoc[] fields = (Field JavaDoc[]) AccessController.doPrivileged(new PrivilegedAction JavaDoc() {
161                                 public Object JavaDoc run () {
162                                         return cl.getDeclaredFields();
163                                 }
164                         });
165
166         synchronized(fieldInfos) {
167             for (int i = 0; i < fields.length; i++)
168             {
169                 String JavaDoc fieldName = fields[i].getName();
170                 FieldInfo fieldInfo = (FieldInfo)fieldInfos.get(fieldName);
171                 if (fieldInfo == null)
172                     fieldInfos.put(fieldName, new FieldInfo(fields[i], this));
173             }
174         }
175         return (FieldInfo[])fieldInfos.values().toArray(new FieldInfo[0]);
176     }
177
178     /**
179      * Return FieldInfo object for the field with the specified name.
180      */

181     public FieldInfo getFieldInfo(final String JavaDoc fieldName)
182     {
183         synchronized(fieldInfos) {
184             FieldInfo fieldInfo = (FieldInfo)fieldInfos.get(fieldName);
185             if (fieldInfo == null)
186             {
187                 // NOTE, no inheritance!
188
final Class JavaDoc cl = clazz;
189                 Field JavaDoc field = (Field JavaDoc) AccessController.doPrivileged(new PrivilegedAction JavaDoc()
190                     {
191                         public Object JavaDoc run ()
192                         {
193                             try
194                             {
195                                 return cl.getDeclaredField(fieldName);
196                             }
197                             catch (NoSuchFieldException JavaDoc ex)
198                             {
199                                 return null; // do nothing, just return null
200
}
201                         }
202                     });
203
204                 if (field != null)
205                 {
206                     fieldInfo = new FieldInfo(field, this);
207                     fieldInfos.put(fieldName, fieldInfo);
208                 }
209             }
210             return fieldInfo;
211         }
212     }
213
214     /**
215      * Return the list of key field names
216      */

217     public List JavaDoc getKeyFieldNames()
218     {
219         if (pce != null)
220         {
221             PersistenceFieldElement[] persistentFields = pce.getFields();
222             List JavaDoc names = new ArrayList JavaDoc();
223             for (int i = 0; i < persistentFields.length; i++)
224             {
225                 if (persistentFields[i].isKey())
226                     names.add(persistentFields[i].getName());
227             }
228             return names;
229         }
230         return null;
231     }
232
233 }
234
235
Popular Tags