1 23 24 29 30 package com.sun.jdo.spi.persistence.support.sqlstore.query.util.type; 31 32 import java.util.Map ; 33 import java.util.HashMap ; 34 import java.util.List ; 35 import java.util.ArrayList ; 36 37 import java.lang.reflect.Field ; 38 import java.security.AccessController ; 39 import java.security.PrivilegedAction ; 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 53 public class ClassType 54 extends Type 55 { 56 59 protected TypeTable typetab; 60 61 64 protected Map fieldInfos; 65 66 69 protected PersistenceClassElement pce; 70 71 74 public ClassType(String name, Class clazz, int enumType, TypeTable typetab) 75 { 76 super(name, clazz, enumType); 77 this.typetab = typetab; 78 this.fieldInfos = new HashMap (); 79 ClassLoader classLoader = clazz.getClassLoader(); 81 if (classLoader != null) 82 { 83 try 84 { 85 this.pce = typetab.model.getPersistenceClass(name, classLoader); 86 } 87 catch (IllegalArgumentException ex) 88 { 89 throw new JDOFatalUserException(ex.getMessage()); 91 } 92 } 93 } 94 95 98 public ClassType(String name, Class clazz, TypeTable typetab) 99 { 100 this(name, clazz, FieldTypeEnumeration.NOT_ENUMERATED, typetab); 101 } 102 103 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 129 public boolean isOrderable() 130 { 131 Type comparable = typetab.checkType("java.lang.Comparable"); return isCompatibleWith(comparable); 133 } 134 135 140 public boolean isPersistenceCapable() 141 { 142 return (pce != null); 143 } 144 145 149 152 public FieldInfo[] getFieldInfos() 153 { 154 158 final Class cl = clazz; 159 160 Field [] fields = (Field []) AccessController.doPrivileged(new PrivilegedAction () { 161 public Object run () { 162 return cl.getDeclaredFields(); 163 } 164 }); 165 166 synchronized(fieldInfos) { 167 for (int i = 0; i < fields.length; i++) 168 { 169 String 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 181 public FieldInfo getFieldInfo(final String fieldName) 182 { 183 synchronized(fieldInfos) { 184 FieldInfo fieldInfo = (FieldInfo)fieldInfos.get(fieldName); 185 if (fieldInfo == null) 186 { 187 final Class cl = clazz; 189 Field field = (Field ) AccessController.doPrivileged(new PrivilegedAction () 190 { 191 public Object run () 192 { 193 try 194 { 195 return cl.getDeclaredField(fieldName); 196 } 197 catch (NoSuchFieldException ex) 198 { 199 return null; } 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 217 public List getKeyFieldNames() 218 { 219 if (pce != null) 220 { 221 PersistenceFieldElement[] persistentFields = pce.getFields(); 222 List names = new ArrayList (); 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 |