1 18 19 package org.objectweb.jac.core.rtti; 20 21 import java.lang.reflect.*; 22 import java.util.Arrays ; 23 import java.util.Vector ; 24 25 39 40 public abstract class AbstractMethodItem extends MemberItem { 41 42 47 48 public AbstractMethodItem(Object delegate, ClassItem parent) 49 throws InvalidDelegateException 50 { 51 super(delegate,parent); 52 isStatic = Modifier.isStatic(((Member)delegate).getModifiers()); 53 } 54 55 59 public final Object getAttribute(String name) { 60 Object value = super.getAttribute(name); 61 if (value==null) { 62 ClassItem parent = ((ClassItem)getParent()).getSuperclass(); 63 if (parent!=null) { 64 try { 65 AbstractMethodItem parentMethod = parent.getAbstractMethod(getFullName()); 66 value = parentMethod.getAttribute(name); 67 } catch (NoSuchMethodException e) { 68 ClassItem[] interfaces = parent.getInterfaceItems(); 69 for (int i=0; i<interfaces.length && value==null;i++) { 70 if (parent.hasMethod(getFullName())) { 71 AbstractMethodItem parentMethod = parent.getAbstractMethod(getFullName()); 72 value = parentMethod.getAttribute(name); 73 } 74 } 75 } 76 } 77 } 78 return value; 79 } 80 81 86 public abstract Class [] getParameterTypes(); 87 88 public void setParameter(Object [] params, int i, Object value) { 89 params[i] = value; 90 } 91 92 public Object getParameter(Object [] params, int i) { 93 return params[i]; 94 } 95 96 101 public ClassItem getParameterTypeItem(int n) { 102 return ClassRepository.get().getClass(getParameterTypes()[n]); 103 } 104 105 110 public int getParameterCount() { 111 return getParameterTypes().length; 112 } 113 114 boolean isStatic = false; 115 116 public final boolean isStatic() { 117 return isStatic; 118 } 119 120 private String fullName; 121 127 public String getFullName() { 128 if (fullName==null) { 129 fullName = getFullName(getName(),getParameterTypes()); 130 } 131 return fullName; 132 } 133 134 String realFullName; 135 141 public String getRealFullName() { 142 if (realFullName==null) { 143 realFullName = getFullName(getName(),((Method)delegate).getParameterTypes()); 144 } 145 return realFullName; 146 } 147 148 public static String getFullName(String name, Class [] pts) { 149 StringBuffer ret = new StringBuffer (name.length()+2+pts.length*15); 150 ret.append(name); 151 ret.append('('); 152 for (int i=0; i<pts.length; i++) { 153 ret.append(NamingConventions.getStandardClassName(pts[i])); 154 if (i < pts.length-1) 155 ret.append(','); 156 } 157 ret.append(')'); 158 return ret.toString(); 159 } 160 161 167 public String getCompactFullName() { 168 Class [] pts = getParameterTypes(); 170 StringBuffer ret = new StringBuffer (getName().length()+2+pts.length*15); 171 ret.append(getName()); 172 ret.append('('); 173 for (int i=0; i<pts.length; i++) { 174 ret.append(NamingConventions.getShortClassName(pts[i])); 175 if (i < pts.length-1) 176 ret.append(','); 177 } 178 ret.append(')'); 179 return ret.toString(); 180 } 181 182 private String longName; 183 public String getLongName() { 184 if (longName==null) 185 longName = parent.getName()+"."+getFullName(); 186 return longName; 187 } 188 189 public Object invoke(Object object, Object [] parameters) 190 { 191 if (true) { 192 throw new RuntimeException ( 193 "wrong invocation on an abstract method "+this); 194 } 195 return null; 196 } 197 198 AbstractMethodItem concreteMethod; 199 200 203 public AbstractMethodItem getConcreteMethod() { 204 if (concreteMethod==null) { 205 Member m = (Member)delegate; 206 if (parent.getDelegate() == m.getDeclaringClass()) { 207 return this; 208 } 209 concreteMethod = ClassRepository.get().getClass(m.getDeclaringClass()). 210 getAbstractMethod(getRealFullName()); 211 } 212 return concreteMethod; 213 } 214 215 218 public ClassItem getOwningClass() { 219 return (ClassItem)parent; 220 } 221 222 public abstract boolean isAdder(); 223 public abstract CollectionItem[] getAddedCollections(); 224 230 public final CollectionItem getAddedCollection() { 231 CollectionItem[] colls = getAddedCollections(); 232 return ((colls != null) ? colls[0] : null); 233 } 234 235 public abstract boolean isRemover(); 236 public abstract CollectionItem[] getRemovedCollections(); 237 238 public abstract boolean isAccessor(); 239 public abstract boolean isWriter(); 240 public abstract boolean isGetter(); 241 public abstract boolean isSetter(); 242 243 public abstract boolean isCollectionGetter(); 244 public abstract boolean isCollectionAccessor(); 245 public abstract boolean isCollectionSetter(); 246 247 public abstract boolean isFieldGetter(); 248 public abstract boolean isFieldSetter(); 249 250 public abstract boolean isReferenceGetter(); 251 public abstract boolean isReferenceSetter(); 252 public abstract boolean isReferenceAccessor(); 253 254 public abstract FieldItem getSetField(); 255 256 257 FieldItem[] writtenFields = null; 258 259 263 264 public final FieldItem[] getWrittenFields() { 265 ((ClassItem)parent).buildFieldInfo(); 266 return writtenFields; 267 } 268 269 public final boolean hasWrittenFields() { 270 ((ClassItem)parent).buildFieldInfo(); 271 return writtenFields!=null && writtenFields.length>0; 272 } 273 274 280 281 public final void setWrittenFields(FieldItem[] writtenFields) { 282 this.writtenFields = writtenFields; 283 } 284 285 292 public final void addWrittenField(FieldItem writtenField) { 293 if (writtenFields == null) { 294 writtenFields = new FieldItem[] { writtenField }; 295 } else { 296 FieldItem[] tmp = new FieldItem[writtenFields.length + 1]; 297 System.arraycopy(writtenFields, 0, tmp, 0, writtenFields.length); 298 tmp[writtenFields.length] = writtenField; 299 writtenFields = tmp; 300 } 301 } 302 303 309 public final void removeWrittenField(FieldItem field) { 310 if (writtenFields != null) { 311 Vector v = new Vector (Arrays.asList(writtenFields)); 312 v.remove(field); 313 writtenFields = new FieldItem[v.size()]; 314 System.arraycopy(v.toArray(),0,writtenFields,0,v.size()); 315 } 316 } 317 318 319 int numAccessedReferences = 0; 320 321 322 int numAccessedCollections = 0; 323 324 325 CollectionItem[] modifiedCollections = null; 326 327 332 333 public final CollectionItem[] getModifiedCollections() { 334 ((ClassItem)parent).buildFieldInfo(); 335 return modifiedCollections; 336 } 337 338 341 342 public final boolean hasModifiedCollections() { 343 ((ClassItem)parent).buildFieldInfo(); 344 return modifiedCollections!=null && modifiedCollections.length>0; 345 } 346 347 352 353 public final void addModifiedCollection(CollectionItem modifiedCollection) { 354 if ( modifiedCollections == null ) { 355 modifiedCollections = new CollectionItem[] { modifiedCollection }; 356 } else { 357 CollectionItem[] tmp = new CollectionItem[modifiedCollections.length + 1]; 358 System.arraycopy( modifiedCollections, 0, tmp, 0, modifiedCollections.length ); 359 tmp[modifiedCollections.length] = modifiedCollection; 360 modifiedCollections = tmp; 361 } 362 } 363 364 365 FieldItem[] accessedFields = null; 366 367 371 372 public final FieldItem[] getAccessedFields() { 373 ((ClassItem)parent).buildFieldInfo(); 374 return accessedFields; 375 } 376 377 383 384 public void setAccessedFields(FieldItem[] accessedFields) { 385 this.accessedFields = accessedFields; 386 numAccessedReferences = 0; 387 numAccessedCollections = 0; 388 if (accessedFields!=null) { 389 for(int i=0; i<accessedFields.length; i++) { 390 if (accessedFields[i].isReference()) 391 numAccessedReferences++; 392 else if (accessedFields[i] instanceof CollectionItem) 393 numAccessedCollections++; 394 } 395 } 396 } 397 398 405 406 public void addAccessedField(FieldItem accessedField) { 407 if (accessedField.isReference()) 408 numAccessedReferences++; 409 else if (accessedField instanceof CollectionItem) 410 numAccessedCollections++; 411 if (accessedFields == null) { 412 accessedFields = new FieldItem[] { accessedField }; 413 } else { 414 FieldItem[] tmp = new FieldItem[accessedFields.length + 1]; 415 System.arraycopy( accessedFields, 0, tmp, 0, accessedFields.length ); 416 tmp[accessedFields.length] = accessedField; 417 accessedFields = tmp; 418 } 419 } 420 421 427 public void removeAccessedField(FieldItem field) { 428 if (accessedFields != null) { 429 Vector v = new Vector (Arrays.asList(accessedFields)); 430 if (v.remove(field)) { 431 if (field.isReference()) 432 numAccessedReferences--; 433 else if (field instanceof CollectionItem) 434 numAccessedCollections--; 435 accessedFields = new FieldItem[v.size()]; 436 System.arraycopy(v.toArray(),0,accessedFields,0,v.size()); 437 } 438 } 439 } 440 441 446 public final FieldItem[] getAccessedReferences() { 447 ((ClassItem)parent).buildFieldInfo(); 448 FieldItem[] refs = new FieldItem[numAccessedReferences]; 449 int j=0; 450 for (int i=0; i<accessedFields.length; i++) { 451 if (accessedFields[i].isReference()) 452 refs[j++] = accessedFields[i]; 453 } 454 return refs; 455 } 456 457 public final CollectionItem[] getAccessedCollections() { 458 ((ClassItem)parent).buildFieldInfo(); 459 CollectionItem[] colls = new CollectionItem[numAccessedCollections]; 460 int j=0; 461 for (int i=0; i<accessedFields.length; i++) { 462 if (accessedFields[i] instanceof CollectionItem) 463 colls[j++] = (CollectionItem)accessedFields[i]; 464 } 465 return colls; 466 } 467 468 469 public final boolean isModifier() { 470 ((ClassItem)parent).buildFieldInfo(); 471 return (writtenFields!=null && writtenFields.length>0) 472 || (modifiedCollections!=null && modifiedCollections.length>0) 473 || isAdder() || isRemover(); 474 } 475 476 public String toString() { 477 return getLongName(); 478 } 479 } 480 | Popular Tags |