KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdo > tools > enhancer > utils > MetaDataToInfoMapper


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

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 JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.util.*;
28
29 /**
30  *
31  */

32 public class MetaDataToInfoMapper {
33
34     private HashMap classResourceMap;
35     private ClassLoader JavaDoc callingClass;
36     private Hashtable primitives;
37     private HashMap classMap = new HashMap();
38
39
40     public MetaDataToInfoMapper(HashMap classResourceMap, ClassLoader JavaDoc 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 JavaDoc name = (String JavaDoc) 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 JavaDoc name = (String JavaDoc) 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 JavaDoc className)throws IOException JavaDoc{
83         String JavaDoc classFileName = className.replace('.','/')+".class";
84         InputStream JavaDoc 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 JavaDoc returnType = type.toString().substring(0, end1); // return type , excluding Array
152
infoField.isArray(true);
153                     infoField.setReturnType(returnType);
154                 }
155
156
157                 if(fmd.category == MDStatics.CATEGORY_ARRAY){ // arrays
158
int end = type.toString().indexOf("[");
159                     String JavaDoc returnType = type.toString().substring(0,end); // return type , excluding Array
160
infoField.isArray(true);
161                     infoField.setReturnType(returnType);
162                 } else {
163                     infoField.setReturnType(type.toString());
164                     String JavaDoc stringType = type.toString();
165                     if (primitives.containsKey(stringType)){ // primitive
166
infoField.isPrimative(true);
167                         infoField.setPrimativeTypeObject((String JavaDoc)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 JavaDoc x){
177             x.printStackTrace();
178         }
179     }
180
181
182
183
184 }
185
Popular Tags