1 7 8 package java.lang.reflect; 9 10 import sun.reflect.ConstructorAccessor; 11 import sun.reflect.Reflection; 12 import sun.reflect.generics.repository.ConstructorRepository; 13 import sun.reflect.generics.factory.CoreReflectionFactory; 14 import sun.reflect.generics.factory.GenericsFactory; 15 import sun.reflect.generics.scope.ConstructorScope; 16 import java.lang.annotation.Annotation ; 17 import java.util.Map ; 18 import sun.reflect.annotation.AnnotationParser; 19 20 21 39 public final 40 class Constructor<T> extends AccessibleObject implements 41 GenericDeclaration , 42 Member { 43 44 private Class <T> clazz; 45 private int slot; 46 private Class [] parameterTypes; 47 private Class [] exceptionTypes; 48 private int modifiers; 49 private transient String signature; 51 private transient ConstructorRepository genericInfo; 53 private byte[] annotations; 54 private byte[] parameterAnnotations; 55 56 57 private GenericsFactory getFactory() { 60 return CoreReflectionFactory.make(this, ConstructorScope.make(this)); 62 } 63 64 private ConstructorRepository getGenericInfo() { 66 if (genericInfo == null) { 68 genericInfo = 70 ConstructorRepository.make(getSignature(), 71 getFactory()); 72 } 73 return genericInfo; } 75 76 private volatile ConstructorAccessor constructorAccessor; 77 private Constructor <T> root; 81 82 87 Constructor(Class <T> declaringClass, 88 Class [] parameterTypes, 89 Class [] checkedExceptions, 90 int modifiers, 91 int slot, 92 String signature, 93 byte[] annotations, 94 byte[] parameterAnnotations) 95 { 96 this.clazz = declaringClass; 97 this.parameterTypes = parameterTypes; 98 this.exceptionTypes = checkedExceptions; 99 this.modifiers = modifiers; 100 this.slot = slot; 101 this.signature = signature; 102 this.annotations = annotations; 103 this.parameterAnnotations = parameterAnnotations; 104 } 105 106 111 Constructor <T> copy() { 112 Constructor <T> res = new Constructor <T>(clazz, 120 parameterTypes, 121 exceptionTypes, modifiers, slot, 122 signature, 123 annotations, 124 parameterAnnotations); 125 res.root = this; 126 res.constructorAccessor = constructorAccessor; 128 return res; 129 } 130 131 135 public Class <T> getDeclaringClass() { 136 return clazz; 137 } 138 139 144 public String getName() { 145 return getDeclaringClass().getName(); 146 } 147 148 155 public int getModifiers() { 156 return modifiers; 157 } 158 159 174 public TypeVariable <Constructor <T>>[] getTypeParameters() { 175 if (getSignature() != null) { 176 return (TypeVariable <Constructor <T>>[])getGenericInfo().getTypeParameters(); 177 } else 178 return (TypeVariable <Constructor <T>>[])new TypeVariable [0]; 179 } 180 181 182 191 public Class <?>[] getParameterTypes() { 192 return (Class <?>[]) parameterTypes.clone(); 193 } 194 195 196 222 public Type [] getGenericParameterTypes() { 223 if (getSignature() != null) 224 return getGenericInfo().getParameterTypes(); 225 else 226 return getParameterTypes(); 227 } 228 229 230 239 public Class <?>[] getExceptionTypes() { 240 return (Class <?>[])exceptionTypes.clone(); 241 } 242 243 244 269 public Type [] getGenericExceptionTypes() { 270 Type [] result; 271 if (getSignature() != null && 272 ( (result = getGenericInfo().getExceptionTypes()).length > 0 )) 273 return result; 274 else 275 return getExceptionTypes(); 276 } 277 278 284 public boolean equals(Object obj) { 285 if (obj != null && obj instanceof Constructor ) { 286 Constructor other = (Constructor )obj; 287 if (getDeclaringClass() == other.getDeclaringClass()) { 288 289 Class [] params1 = parameterTypes; 290 Class [] params2 = other.parameterTypes; 291 if (params1.length == params2.length) { 292 for (int i = 0; i < params1.length; i++) { 293 if (params1[i] != params2[i]) 294 return false; 295 } 296 return true; 297 } 298 } 299 } 300 return false; 301 } 302 303 308 public int hashCode() { 309 return getDeclaringClass().getName().hashCode(); 310 } 311 312 327 public String toString() { 328 try { 329 StringBuffer sb = new StringBuffer (); 330 int mod = getModifiers(); 331 if (mod != 0) { 332 sb.append(Modifier.toString(mod) + " "); 333 } 334 sb.append(Field.getTypeName(getDeclaringClass())); 335 sb.append("("); 336 Class [] params = parameterTypes; for (int j = 0; j < params.length; j++) { 338 sb.append(Field.getTypeName(params[j])); 339 if (j < (params.length - 1)) 340 sb.append(","); 341 } 342 sb.append(")"); 343 Class [] exceptions = exceptionTypes; if (exceptions.length > 0) { 345 sb.append(" throws "); 346 for (int k = 0; k < exceptions.length; k++) { 347 sb.append(exceptions[k].getName()); 348 if (k < (exceptions.length - 1)) 349 sb.append(","); 350 } 351 } 352 return sb.toString(); 353 } catch (Exception e) { 354 return "<" + e + ">"; 355 } 356 } 357 358 385 public String toGenericString() { 386 try { 387 StringBuilder sb = new StringBuilder (); 388 int mod = getModifiers(); 389 if (mod != 0) { 390 sb.append(Modifier.toString(mod) + " "); 391 } 392 Type [] typeparms = getTypeParameters(); 393 if (typeparms.length > 0) { 394 boolean first = true; 395 sb.append("<"); 396 for(Type typeparm: typeparms) { 397 if (!first) 398 sb.append(","); 399 if (typeparm instanceof Class ) 400 sb.append(((Class )typeparm).getName()); 401 else 402 sb.append(typeparm.toString()); 403 first = false; 404 } 405 sb.append("> "); 406 } 407 sb.append(Field.getTypeName(getDeclaringClass())); 408 sb.append("("); 409 Type [] params = getGenericParameterTypes(); 410 for (int j = 0; j < params.length; j++) { 411 sb.append((params[j] instanceof Class )? 412 Field.getTypeName((Class )params[j]): 413 (params[j].toString()) ); 414 if (j < (params.length - 1)) 415 sb.append(","); 416 } 417 sb.append(")"); 418 Type [] exceptions = getGenericExceptionTypes(); 419 if (exceptions.length > 0) { 420 sb.append(" throws "); 421 for (int k = 0; k < exceptions.length; k++) { 422 sb.append((exceptions[k] instanceof Class )? 423 ((Class )exceptions[k]).getName(): 424 exceptions[k].toString()); 425 if (k < (exceptions.length - 1)) 426 sb.append(","); 427 } 428 } 429 return sb.toString(); 430 } catch (Exception e) { 431 return "<" + e + ">"; 432 } 433 } 434 435 478 public T newInstance(Object ... initargs) 479 throws InstantiationException , IllegalAccessException , 480 IllegalArgumentException , InvocationTargetException 481 { 482 if (!override) { 483 if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) { 484 Class caller = Reflection.getCallerClass(2); 485 if (securityCheckCache != caller) { 486 Reflection.ensureMemberAccess(caller, clazz, null, modifiers); 487 securityCheckCache = caller; 488 } 489 } 490 } 491 if ((clazz.getModifiers() & Modifier.ENUM) != 0) 492 throw new IllegalArgumentException ("Cannot reflectively create enum objects"); 493 if (constructorAccessor == null) acquireConstructorAccessor(); 494 return (T) constructorAccessor.newInstance(initargs); 495 } 496 497 506 public boolean isVarArgs() { 507 return (getModifiers() & Modifier.VARARGS) != 0; 508 } 509 510 518 public boolean isSynthetic() { 519 return Modifier.isSynthetic(getModifiers()); 520 } 521 522 private void acquireConstructorAccessor() { 528 ConstructorAccessor tmp = null; 531 if (root != null) tmp = root.getConstructorAccessor(); 532 if (tmp != null) { 533 constructorAccessor = tmp; 534 return; 535 } 536 tmp = reflectionFactory.newConstructorAccessor(this); 538 setConstructorAccessor(tmp); 539 } 540 541 ConstructorAccessor getConstructorAccessor() { 544 return constructorAccessor; 545 } 546 547 void setConstructorAccessor(ConstructorAccessor accessor) { 550 constructorAccessor = accessor; 551 if (root != null) { 553 root.setConstructorAccessor(accessor); 554 } 555 } 556 557 int getSlot() { 558 return slot; 559 } 560 561 String getSignature() { 562 return signature; 563 } 564 565 byte[] getRawAnnotations() { 566 return annotations; 567 } 568 569 byte[] getRawParameterAnnotations() { 570 return parameterAnnotations; 571 } 572 573 public <T extends Annotation > T getAnnotation(Class <T> annotationClass) { 574 if (annotationClass == null) 575 throw new NullPointerException (); 576 577 return (T) declaredAnnotations().get(annotationClass); 578 } 579 580 private static final Annotation [] EMPTY_ANNOTATION_ARRAY=new Annotation [0]; 581 582 public Annotation [] getDeclaredAnnotations() { 583 return declaredAnnotations().values().toArray(EMPTY_ANNOTATION_ARRAY); 584 } 585 586 private transient Map <Class , Annotation > declaredAnnotations; 587 588 private synchronized Map <Class , Annotation > declaredAnnotations() { 589 if (declaredAnnotations == null) { 590 declaredAnnotations = AnnotationParser.parseAnnotations( 591 annotations, sun.misc.SharedSecrets.getJavaLangAccess(). 592 getConstantPool(getDeclaringClass()), 593 getDeclaringClass()); 594 } 595 return declaredAnnotations; 596 } 597 598 614 public Annotation [][] getParameterAnnotations() { 615 int numParameters = parameterTypes.length; 616 if (parameterAnnotations == null) 617 return new Annotation [numParameters][0]; 618 619 Annotation [][] result = AnnotationParser.parseParameterAnnotations( 620 parameterAnnotations, 621 sun.misc.SharedSecrets.getJavaLangAccess(). 622 getConstantPool(getDeclaringClass()), 623 getDeclaringClass()); 624 if (result.length != numParameters) 625 throw new java.lang.annotation.AnnotationFormatError ( 626 "Parameter annotations don't match number of parameters"); 627 return result; 628 } 629 } 630 | Popular Tags |