KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ashkelon > Member


1 package org.ashkelon;
2 /**
3  * Ashkelon
4  * Copyright UptoData Inc. 2001
5  * March 2001
6  */

7
8 import java.io.Serializable JavaDoc;
9 import java.sql.Connection JavaDoc;
10 import java.sql.PreparedStatement JavaDoc;
11 import java.sql.ResultSet JavaDoc;
12 import java.sql.SQLException JavaDoc;
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.ashkelon.db.DBUtils;
19 import org.ashkelon.db.PKManager;
20 import org.ashkelon.util.JDocUtil;
21 import org.ashkelon.util.Logger;
22 import org.ashkelon.util.StringUtils;
23
24 import com.sun.javadoc.MemberDoc;
25
26 /**
27  * Part of Persistable javadoc object model
28  * Analog of com.sun.javadoc.MemberDoc
29  *
30  * @author Eitan Suez
31  * @version 2.0
32  */

33 public class Member implements JDoc, Serializable JavaDoc
34 {
35    private ClassType containingClass;
36    private String JavaDoc name;
37    private String JavaDoc qualifiedName;
38    private DocInfo doc;
39    private String JavaDoc containingClassName;
40
41    private boolean isStatic;
42    private boolean isFinal;
43    private int accessibility;
44    private String JavaDoc modifiers;
45    
46    private int memberType;
47
48    private int id;
49    private boolean idSet = false;
50    
51    private static String JavaDoc SEQUENCE = "MEMBER_SEQ";
52    private static String JavaDoc TABLENAME = "MEMBER";
53
54    /** constants representing the three member types */
55    public static final int FIELD_MEMBER = 1;
56    /** constants representing the three member types */
57    public static final int CONSTRUCTOR_MEMBER = 2;
58    /** constants representing the three member types */
59    public static final int METHOD_MEMBER = 3;
60
61     public static final String JavaDoc[] MEMBERTYPES = {"field", "constructor", "method"};
62    
63    
64    public Member(String JavaDoc qualifiedName, int memberType)
65    {
66       setQualifiedName(qualifiedName);
67       setMemberType(memberType);
68       setName(JDocUtil.unqualify(qualifiedName));
69       setContainingClassName(qualifiedName.substring(0, qualifiedName.lastIndexOf(".")));
70       setContainingClass(new ClassType(getContainingClassName()));
71       setDoc(new DocInfo());
72       setModifiers("");
73    }
74    
75    public Member(Member member)
76    {
77       this(member.getQualifiedName(), member.getMemberType());
78       setId(member.getId());
79       setStatic(member.isStatic());
80       setFinal(member.isFinal());
81       setAccessibility(member.getAccessibility());
82       setModifiers(member.getModifiers());
83       setDoc(member.getDoc());
84       setContainingClass(member.getContainingClass());
85    }
86    
87    public Member(MemberDoc memberdoc, ClassType containingClass)
88    {
89       setDoc(new DocInfo(memberdoc));
90       setName(memberdoc.name());
91       setQualifiedName(memberdoc.qualifiedName());
92       setMemberType(getMemberType(memberdoc));
93
94       setStatic(memberdoc.isStatic());
95       setFinal(memberdoc.isFinal());
96       setAccessibility(JDocUtil.getAccessibility(memberdoc));
97       setModifiers(memberdoc.modifiers());
98
99       setContainingClass(containingClass);
100    }
101    
102    public void store(Connection JavaDoc conn) throws SQLException JavaDoc
103    {
104       Map JavaDoc fieldInfo = new HashMap JavaDoc(10);
105       fieldInfo.put("ID", new Integer JavaDoc(getId(conn)));
106       fieldInfo.put("CLASSID", new Integer JavaDoc(getContainingClass().getId(conn)));
107       fieldInfo.put("DOCID", new Integer JavaDoc(getDoc().getId(conn)));
108       fieldInfo.put("NAME", StringUtils.truncate(getName(), 100));
109       fieldInfo.put("QUALIFIEDNAME", StringUtils.truncate(getQualifiedName(), 150));
110       
111       //fieldInfo.put("ISSTATIC", new Boolean(isStatic()));
112
int boolvalue = isStatic() ? 1 : 0;
113       fieldInfo.put("ISSTATIC", new Integer JavaDoc(boolvalue));
114       
115       //fieldInfo.put("ISFINAL", new Boolean(isFinal()));
116
boolvalue = isFinal() ? 1 : 0;
117       fieldInfo.put("ISFINAL", new Integer JavaDoc(boolvalue));
118       
119       fieldInfo.put("ACCESSIBILITY", new Integer JavaDoc(getAccessibility()));
120       fieldInfo.put("MODIFIER", StringUtils.truncate(getModifiers(), 51));
121       fieldInfo.put("TYPE", new Integer JavaDoc(getMemberType()));
122       DBUtils.insert(conn, TABLENAME, fieldInfo);
123
124       getDoc().store(conn);
125    }
126    
127    public int getId(Connection JavaDoc conn) throws SQLException JavaDoc
128    {
129       if (!idSet)
130       {
131          //id = DBUtils.getNextVal(conn, SEQUENCE);
132
id = PKManager.getInstance().nextVal(SEQUENCE);
133          idSet = true;
134       }
135       return id;
136    }
137    
138    public int getId()
139    {
140       return id;
141    }
142    
143    public void setId(int id)
144    {
145       this.id = id;
146       idSet = true;
147    }
148    
149    public static void delete(Connection JavaDoc conn, int memberid, int docid) throws SQLException JavaDoc
150    {
151       DocInfo.delete(conn, docid);
152
153       // sever references to reference
154
String JavaDoc sql = "update REFERENCE set refdoc_id=null, refdoc_type=null" +
155                    " where refdoc_id=?";
156       PreparedStatement JavaDoc pstmt = conn.prepareStatement(sql);
157       pstmt.setInt(1, memberid);
158       pstmt.executeUpdate();
159       pstmt.close();
160       
161       Map JavaDoc constraint = new HashMap JavaDoc();
162       constraint.put("ID", new Integer JavaDoc(memberid));
163       DBUtils.delete(conn, TABLENAME, constraint);
164    }
165    
166    // accessor methods
167
public ClassType getContainingClass() { return containingClass; }
168    public void setContainingClass(ClassType containingClass) { this.containingClass = containingClass; }
169    
170    public DocInfo getDoc() { return doc; }
171    public void setDoc(DocInfo doc) { this.doc = doc; }
172    
173    public String JavaDoc getName() { return name; }
174    public void setName(String JavaDoc name) { this.name = name; }
175    
176    public String JavaDoc getQualifiedName() { return qualifiedName; }
177    public void setQualifiedName(String JavaDoc qualifiedName) { this.qualifiedName = qualifiedName; }
178
179    public boolean isStatic() { return isStatic; }
180    public void setStatic(boolean isStatic) { this.isStatic = isStatic; }
181    
182    public boolean isFinal() { return isFinal; }
183    public void setFinal(boolean isFinal) { this.isFinal = isFinal; }
184    
185    public int getAccessibility() { return accessibility; }
186    public void setAccessibility(int accessibility) { this.accessibility = accessibility; }
187    
188    public String JavaDoc getModifiers() { return modifiers; }
189    public void setModifiers(String JavaDoc modifiers) { this.modifiers = modifiers; }
190
191    public int getMemberType() { return memberType; }
192    public void setMemberType(int memberType) { this.memberType = memberType; }
193    
194    public String JavaDoc getMemberTypeName()
195    {
196       int memberType = getMemberType();
197       if (memberType > MEMBERTYPES.length || memberType <= 0)
198       {
199          return "unknown";
200       }
201       return MEMBERTYPES[memberType-1];
202    }
203    
204    public String JavaDoc getContainingClassName() { return containingClassName; }
205    public void setContainingClassName(String JavaDoc containingClassName)
206    {
207       this.containingClassName = containingClassName;
208    }
209    
210    
211    /**
212     * @return type for a given memberdoc (field, method, constructor)
213     */

214    public static int getMemberType(MemberDoc memberdoc)
215    {
216       if (memberdoc.isMethod())
217       {
218          return METHOD_MEMBER;
219       }
220       else if (memberdoc.isConstructor())
221       {
222          return CONSTRUCTOR_MEMBER;
223       }
224       else if (memberdoc.isField())
225       {
226          return FIELD_MEMBER;
227       }
228       else
229       {
230          return JDocUtil.UNKNOWN_TYPE;
231       }
232    }
233    
234    public JPackage getPackage()
235    {
236       if (getContainingClass() == null) { return null; }
237       return getContainingClass().getPackage();
238    }
239    
240    
241    public static List JavaDoc makeMembersFor(Connection JavaDoc conn, int memberId) throws SQLException JavaDoc
242    {
243       Logger log = Logger.getInstance();
244       
245       String JavaDoc sql = "select qualifiedname, type from MEMBER where id=?";
246       PreparedStatement JavaDoc pstmt = conn.prepareStatement(sql);
247       pstmt.setInt(1, memberId);
248       ResultSet JavaDoc rset = pstmt.executeQuery();
249       
250       int memberType = 0;
251       String JavaDoc qualifiedname = "";
252       
253       while (rset.next())
254       {
255          qualifiedname = rset.getString(1);
256          memberType = rset.getInt(2);
257       }
258       
259       rset.close();
260       
261       if (memberType == 0) return null;
262
263       
264       int FIELDS = 0; int TABLES = 1; int CONDITIONS = 2;
265       
266       String JavaDoc[][] memberInfo =
267          {
268             {" , f.typename, f.typedimension, f.typeid ", " , FIELD f ", " and m.id=f.id "},
269             {" , ex.signature , ex.fullyqualifiedname ", " , EXECMEMBER ex ", " and m.id=ex.id "},
270             {" , ex.signature , meth.isabstract, meth.returntypename, meth.returntypedimension, meth.returntypeid, meth.returndescription , ex.fullyqualifiedname ",
271              " , EXECMEMBER ex , METHOD meth ",
272              " and m.id=ex.id and m.id=meth.id "}
273          };
274          
275       sql =
276             " select m.id, m.qualifiedname, m.type, " +
277             " m.isstatic, m.isfinal, m.accessibility, m.modifier, " +
278             " m.name, m.classid, " +
279             " d.summarydescription, d.since, d.deprecated, d.description, d.id, " +
280             " c.packageid, c.type, c.qualifiedname " +
281             memberInfo[memberType-1][FIELDS] +
282             " from MEMBER m, DOC d, CLASSTYPE c " +
283             memberInfo[memberType-1][TABLES] +
284             " where m.qualifiedname=? and m.docid=d.id and m.classid=c.id " +
285             memberInfo[memberType-1][CONDITIONS] +
286             " order by m.type, m.name " ;
287       
288       log.debug("member sql statement:\n\t" + sql);
289       
290       pstmt = conn.prepareStatement(sql);
291       pstmt.setString(1, qualifiedname);
292       rset = pstmt.executeQuery();
293
294       List JavaDoc members = new ArrayList JavaDoc();
295
296       Member member = null;
297       DocInfo doc;
298       ClassType container;
299       
300       while (rset.next())
301       {
302          member = new Member(rset.getString(2), rset.getInt(3));
303          member.setId(rset.getInt(1));
304          member.setStatic(rset.getBoolean(4));
305          member.setFinal(rset.getBoolean(5));
306          member.setAccessibility(rset.getInt(6));
307          member.setModifiers(rset.getString(7));
308       
309          doc = new DocInfo(rset.getString(10), rset.getString(11), rset.getString(12));
310          doc.setDescription(rset.getString(13));
311          doc.setId(rset.getInt(14));
312          doc.fetchRefs(conn);
313          
314          member.setDoc(doc);
315          
316          container = new ClassType(rset.getString(17));
317          container.setId(rset.getInt(9));
318          container.getPackage().setId(rset.getInt(15));
319          container.setClassType(rset.getInt(16));
320          
321          member.setContainingClass(container);
322
323          switch (memberType)
324          {
325             case Member.FIELD_MEMBER:
326             {
327                FieldMember fm = new FieldMember(member, rset.getString(18));
328                fm.setTypeDimension(rset.getInt(19));
329                int typeid = rset.getInt(20);
330                if (typeid > 0)
331                {
332                   fm.setType(new ClassType(fm.getTypeName()));
333                   fm.getType().setId(typeid);
334                }
335                members.add(fm);
336                break;
337             } // end case
338
case Member.CONSTRUCTOR_MEMBER:
339             {
340                ConstructorMember cm = new ConstructorMember(member, rset.getString(18));
341                cm.setFullyQualifiedName(rset.getString(19));
342                cm.fetchParameters(conn);
343                cm.fetchExceptions(conn);
344                members.add(cm);
345                break;
346             } // end case
347
case Member.METHOD_MEMBER:
348             {
349                
350                MethodMember mm = new MethodMember(member, rset.getString(18));
351                mm.setAbstract(rset.getBoolean(19));
352                mm.setReturnTypeName(rset.getString(20));
353                mm.setReturnTypeDimension(rset.getInt(21));
354                mm.setReturnDescription(rset.getString(23));
355                mm.setFullyQualifiedName(rset.getString(24));
356                int typeid = rset.getInt(22);
357                if (typeid > 0)
358                {
359                   mm.setReturnType(new ClassType(mm.getReturnTypeName()));
360                   mm.getReturnType().setId(typeid);
361                }
362                mm.fetchParameters(conn);
363                mm.fetchExceptions(conn);
364                members.add(mm);
365                break;
366             } // end case
367
} // end switch
368

369       } // end while
370

371       rset.close();
372       pstmt.close();
373       
374       return members;
375       
376    } // end makeMembersFor
377

378    
379    public String JavaDoc getSummaryDescription() { return getDoc().getSummaryDescription(); }
380    public String JavaDoc getDescription() { return getDoc().getDescription(); }
381    public String JavaDoc getSince() { return getDoc().getSince(); }
382    public String JavaDoc getDeprecatedDescr() { return getDoc().getDeprecated(); }
383    public boolean isDeprecated() { return getDoc().isDeprecated(); }
384    public List JavaDoc getReferences() { return getDoc().getReferences(); }
385    
386    public String JavaDoc getStyle() { return getMemberTypeName(); }
387
388 }
389
Popular Tags