1 2 12 package com.versant.core.jdo.tools.enhancer.utils; 13 14 import com.versant.lib.bcel.classfile.ClassParser; 15 import com.versant.lib.bcel.classfile.Field; 16 import com.versant.lib.bcel.classfile.JavaClass; 17 import com.versant.lib.bcel.generic.Type; 18 import com.versant.core.jdo.tools.enhancer.info.*; 19 import com.versant.core.metadata.ClassMetaData; 20 import com.versant.core.metadata.FieldMetaData; 21 import com.versant.core.metadata.MDStatics; 22 import com.versant.core.metadata.parser.*; 23 24 import javax.jdo.InstanceCallbacks; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.util.*; 28 29 32 public class MetaDataToInfoMapper { 33 34 private HashMap classResourceMap; 35 private ClassLoader callingClass; 36 private Hashtable primitives; 37 private HashMap classMap = new HashMap(); 38 39 40 public MetaDataToInfoMapper(HashMap classResourceMap, ClassLoader callingClass) { 41 this.callingClass = callingClass; 42 43 this.classResourceMap = classResourceMap; 44 primitives = new Hashtable(); 45 primitives.put("int","java.lang.Integer"); 46 primitives.put("byte","java.lang.Byte"); 47 primitives.put("char","java.lang.Character"); 48 primitives.put("short","java.lang.Short"); 49 primitives.put("float","java.lang.Float"); 50 primitives.put("double","java.lang.Double"); 51 primitives.put("long","java.lang.Long"); 52 primitives.put("boolean","java.lang.Boolean"); 53 54 } 55 56 public List getClassInfoList(){ 57 for (Iterator iterator = classMap.keySet().iterator();iterator.hasNext();) { 58 String name = (String ) iterator.next(); 59 ClassInfo clsInfo = (ClassInfo)classMap.get(name); 60 if (clsInfo.getTopName() != null ){ 61 ClassInfo top = (ClassInfo)classMap.get(clsInfo.getTopName()); 62 clsInfo.setTopPCSuperClass(top); 63 if (clsInfo.getIdentityType() == MDStatics.IDENTITY_TYPE_APPLICATION){ 64 clsInfo.setObjectidClass(top.getObjectidClass()); 65 66 } 67 } 68 } 69 70 for (Iterator iterator = classMap.keySet().iterator();iterator.hasNext();) { 71 String name = (String ) iterator.next(); 72 ClassInfo clsInfo = (ClassInfo)classMap.get(name); 73 if (clsInfo.getTopName().equals(clsInfo.getClassName()) ){ 74 clsInfo.setTopPCSuperClass(null); 75 } 76 } 77 return new ArrayList(classMap.values()); 78 } 79 80 81 82 private JavaClass getJavaClass(String className)throws IOException { 83 String classFileName = className.replace('.','/')+".class"; 84 InputStream inputStream = callingClass.getResourceAsStream(classFileName); 85 if (inputStream == null){ 86 inputStream = callingClass.getResourceAsStream("/"+classFileName); 87 if (inputStream == null){ 88 throw new javax.jdo.JDOFatalUserException( 89 "Class not found: " + className + 90 " (" + classResourceMap.get(className) + ")"); 91 } 92 93 } 94 ClassParser parser = new ClassParser(inputStream, classFileName); 95 return parser.parse(); 96 } 97 98 99 100 101 102 public void setClassInfo(JdoClass jdoClass,ClassMetaData metaData){ 103 ClassInfo infoClass = new ClassInfo(); 104 105 try{ 106 107 infoClass.setInstanceCallbacks(InstanceCallbacks.class.isAssignableFrom(metaData.cls)); 108 infoClass.setClassName(metaData.cls.getName()); 109 infoClass.setIdentityType(metaData.identityType); 110 infoClass.setKeyGen(jdoClass.hasKeyGen()); 111 if (metaData.objectIdClass != null){ 112 infoClass.setObjectidClass(metaData.objectIdClass.getName()); 113 } 114 115 if (metaData.pcSuperClass != null){ 116 infoClass.setPersistenceCapableSuperclass(metaData.pcSuperClass.getName()); 117 } 118 119 if (metaData.top != null){ 120 infoClass.setTopName(metaData.top.cls.getName()); 121 } 122 123 JavaClass clazz = getJavaClass(metaData.cls.getName()); 124 125 Field[] fields = clazz.getFields(); 126 for (int i = 0;i < fields.length; i++) { 127 Field f = fields[i]; 128 FieldMetaData fmd = metaData.getFieldMetaData(f.getName()); 129 if (fmd == null){ 130 continue; 131 } 132 133 Type type = Type.getReturnType(f.getSignature()); 134 135 136 FieldInfo infoField = new FieldInfo(); 137 infoField.setJdoSetName("jdoSet"+f.getName()); 138 infoField.setJdoGetName("jdoGet"+f.getName()); 139 infoField.isPublic(f.isPublic()); 140 infoField.isPrivate(f.isPrivate()); 141 infoField.isProtected(f.isProtected()); 142 infoField.setFieldName(f.getName()); 143 infoField.setSignature(f.getSignature()); 144 infoField.setType(Type.getReturnType(f.getSignature())); 145 infoField.primaryKey(fmd.primaryKey); 146 infoField.setPersistenceModifier(fmd.persistenceModifier); 147 infoField.setFieldNo(fmd.managedFieldNo); 148 infoField.defaultFetchGroup(fmd.isJDODefaultFetchGroup()); 149 int end1 = type.toString().indexOf("["); 150 if (end1 != -1){ 151 String returnType = type.toString().substring(0, end1); infoField.isArray(true); 153 infoField.setReturnType(returnType); 154 } 155 156 157 if(fmd.category == MDStatics.CATEGORY_ARRAY){ int end = type.toString().indexOf("["); 159 String returnType = type.toString().substring(0,end); infoField.isArray(true); 161 infoField.setReturnType(returnType); 162 } else { 163 infoField.setReturnType(type.toString()); 164 String stringType = type.toString(); 165 if (primitives.containsKey(stringType)){ infoField.isPrimative(true); 167 infoField.setPrimativeTypeObject((String )primitives.get(type.toString())); 168 } 169 } 170 171 if (infoField.getPersistenceModifier() != MDStatics.PERSISTENCE_MODIFIER_NONE){ 172 infoClass.getFieldList().add(infoField); 173 } 174 } 175 classMap.put(infoClass.getClassName(),infoClass); 176 }catch (Exception x){ 177 x.printStackTrace(); 178 } 179 } 180 181 182 183 184 } 185 | Popular Tags |