1 21 package oracle.toplink.essentials.internal.parsing; 23 24 import java.lang.reflect.Field ; 25 import java.security.AccessController ; 26 import java.security.PrivilegedActionException ; 27 28 import oracle.toplink.essentials.descriptors.ClassDescriptor; 29 import oracle.toplink.essentials.mappings.AggregateMapping; 30 import oracle.toplink.essentials.mappings.DatabaseMapping; 31 import oracle.toplink.essentials.internal.sessions.AbstractSession; 32 import oracle.toplink.essentials.internal.security.PrivilegedAccessHelper; 33 import oracle.toplink.essentials.internal.security.PrivilegedClassForName; 34 import oracle.toplink.essentials.internal.security.PrivilegedGetField; 35 import oracle.toplink.essentials.internal.helper.BasicTypeHelperImpl; 36 37 42 public class TypeHelperImpl 43 extends BasicTypeHelperImpl implements TypeHelper { 44 45 46 private final AbstractSession session; 47 48 49 private final ClassLoader classLoader; 50 51 52 public TypeHelperImpl(AbstractSession session, ClassLoader classLoader) { 53 this.session = session; 54 this.classLoader = classLoader; 55 } 56 57 58 public Object resolveTypeName(String typeName) { 59 try { 60 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 61 try { 62 return (Class )AccessController.doPrivileged( 63 new PrivilegedClassForName(typeName, true, classLoader)); 64 } catch (PrivilegedActionException exception) { 65 return null; 66 } 67 } else { 68 return PrivilegedAccessHelper.getClassForName(typeName, true, classLoader); 69 } 70 } catch (ClassNotFoundException ex) { 71 return null; 72 } 73 } 74 75 76 public Object resolveAttribute(Object ownerClass, String attribute) { 77 Class clazz = (Class )ownerClass; 78 DatabaseMapping mapping = resolveAttributeMapping((Class )ownerClass, attribute); 79 Object type = getType(mapping); 80 if ((type == null) && (mapping != null)) { 81 Field field = null; 84 try { 85 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 86 try { 87 field = (Field )AccessController.doPrivileged( 88 new PrivilegedGetField(clazz, attribute, true)); 89 } catch (PrivilegedActionException exception) { 90 } 92 } else { 93 field = PrivilegedAccessHelper.getField(clazz, attribute, true); 94 } 95 if (field != null) { 96 type = field.getType(); 97 } 98 } catch (NoSuchFieldException ex) { 99 } 101 } 102 return type; 103 } 104 105 106 public Object resolveSchema(String schemaName) { 107 ClassDescriptor descriptor = session.getDescriptorForAlias(schemaName); 108 return (descriptor != null) ? descriptor.getJavaClass() : null; 109 } 110 111 112 public String getTypeName(Object type) { 113 if (type == null) { 114 return null; 115 } 116 return ((Class )type).getName(); 117 } 118 119 120 public Object resolveEnumConstant(Object type, String constant) { 121 Class clazz = (Class )type; 122 Object [] constants = clazz.getEnumConstants(); 123 if (constants != null) { 124 for (int i = 0; i < constants.length; i++) { 125 if (constant.equals(constants[i].toString())) { 126 return constants[i]; 127 } 128 } 129 } 130 return null; 131 } 132 133 134 public boolean isEntityClass(Object type) { 135 return session.getDescriptor((Class )type) != null; 136 } 137 138 public boolean isEmbeddable(Object type) { 139 return session.getDescriptor((Class )type).isAggregateDescriptor(); 140 } 141 142 public boolean isEmbeddedAttribute(Object ownerClass, String attribute) { 143 ClassDescriptor desc = session.getDescriptor((Class )ownerClass); 144 return desc.getMappingForAttributeName(attribute).isAggregateMapping(); 145 } 146 147 149 150 private DatabaseMapping resolveAttributeMapping(Class ownerClass, String attribute) { 151 ClassDescriptor descriptor = session.getDescriptor(ownerClass); 152 return (descriptor==null) ? null : descriptor.getMappingForAttributeName(attribute); 153 } 154 155 156 private Object getType(DatabaseMapping mapping) { 157 if (mapping == null) { 158 return null; 159 } 160 Object type = null; 161 if (mapping.isForeignReferenceMapping()) { 162 ClassDescriptor descriptor = mapping.getReferenceDescriptor(); 163 type = descriptor == null ? null : descriptor.getJavaClass(); 164 } else if (mapping.isAggregateMapping()) { 165 type = ((AggregateMapping)mapping).getReferenceClass(); 166 } else { 167 type = mapping.getAttributeClassification(); 168 } 169 return type; 170 } 171 172 } 173 | Popular Tags |