1 package org.ashkelon; 2 7 8 import java.io.Serializable ; 9 import java.sql.Connection ; 10 import java.sql.PreparedStatement ; 11 import java.sql.ResultSet ; 12 import java.sql.SQLException ; 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.List ; 16 import java.util.Map ; 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 33 public class Member implements JDoc, Serializable 34 { 35 private ClassType containingClass; 36 private String name; 37 private String qualifiedName; 38 private DocInfo doc; 39 private String containingClassName; 40 41 private boolean isStatic; 42 private boolean isFinal; 43 private int accessibility; 44 private String modifiers; 45 46 private int memberType; 47 48 private int id; 49 private boolean idSet = false; 50 51 private static String SEQUENCE = "MEMBER_SEQ"; 52 private static String TABLENAME = "MEMBER"; 53 54 55 public static final int FIELD_MEMBER = 1; 56 57 public static final int CONSTRUCTOR_MEMBER = 2; 58 59 public static final int METHOD_MEMBER = 3; 60 61 public static final String [] MEMBERTYPES = {"field", "constructor", "method"}; 62 63 64 public Member(String 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 conn) throws SQLException 103 { 104 Map fieldInfo = new HashMap (10); 105 fieldInfo.put("ID", new Integer (getId(conn))); 106 fieldInfo.put("CLASSID", new Integer (getContainingClass().getId(conn))); 107 fieldInfo.put("DOCID", new Integer (getDoc().getId(conn))); 108 fieldInfo.put("NAME", StringUtils.truncate(getName(), 100)); 109 fieldInfo.put("QUALIFIEDNAME", StringUtils.truncate(getQualifiedName(), 150)); 110 111 int boolvalue = isStatic() ? 1 : 0; 113 fieldInfo.put("ISSTATIC", new Integer (boolvalue)); 114 115 boolvalue = isFinal() ? 1 : 0; 117 fieldInfo.put("ISFINAL", new Integer (boolvalue)); 118 119 fieldInfo.put("ACCESSIBILITY", new Integer (getAccessibility())); 120 fieldInfo.put("MODIFIER", StringUtils.truncate(getModifiers(), 51)); 121 fieldInfo.put("TYPE", new Integer (getMemberType())); 122 DBUtils.insert(conn, TABLENAME, fieldInfo); 123 124 getDoc().store(conn); 125 } 126 127 public int getId(Connection conn) throws SQLException 128 { 129 if (!idSet) 130 { 131 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 conn, int memberid, int docid) throws SQLException 150 { 151 DocInfo.delete(conn, docid); 152 153 String sql = "update REFERENCE set refdoc_id=null, refdoc_type=null" + 155 " where refdoc_id=?"; 156 PreparedStatement pstmt = conn.prepareStatement(sql); 157 pstmt.setInt(1, memberid); 158 pstmt.executeUpdate(); 159 pstmt.close(); 160 161 Map constraint = new HashMap (); 162 constraint.put("ID", new Integer (memberid)); 163 DBUtils.delete(conn, TABLENAME, constraint); 164 } 165 166 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 getName() { return name; } 174 public void setName(String name) { this.name = name; } 175 176 public String getQualifiedName() { return qualifiedName; } 177 public void setQualifiedName(String 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 getModifiers() { return modifiers; } 189 public void setModifiers(String modifiers) { this.modifiers = modifiers; } 190 191 public int getMemberType() { return memberType; } 192 public void setMemberType(int memberType) { this.memberType = memberType; } 193 194 public String 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 getContainingClassName() { return containingClassName; } 205 public void setContainingClassName(String containingClassName) 206 { 207 this.containingClassName = containingClassName; 208 } 209 210 211 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 makeMembersFor(Connection conn, int memberId) throws SQLException 242 { 243 Logger log = Logger.getInstance(); 244 245 String sql = "select qualifiedname, type from MEMBER where id=?"; 246 PreparedStatement pstmt = conn.prepareStatement(sql); 247 pstmt.setInt(1, memberId); 248 ResultSet rset = pstmt.executeQuery(); 249 250 int memberType = 0; 251 String 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 [][] 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 members = new ArrayList (); 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 } 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 } 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 } } 369 } 371 rset.close(); 372 pstmt.close(); 373 374 return members; 375 376 } 378 379 public String getSummaryDescription() { return getDoc().getSummaryDescription(); } 380 public String getDescription() { return getDoc().getDescription(); } 381 public String getSince() { return getDoc().getSince(); } 382 public String getDeprecatedDescr() { return getDoc().getDeprecated(); } 383 public boolean isDeprecated() { return getDoc().isDeprecated(); } 384 public List getReferences() { return getDoc().getReferences(); } 385 386 public String getStyle() { return getMemberTypeName(); } 387 388 } 389 | Popular Tags |