< prev index next >

src/java.base/share/classes/sun/reflect/annotation/AnnotatedTypeFactory.java

Print this page




 190             if (owner == null) // top-level, local or anonymous
 191                 return null;
 192             if (inner.isPrimitive() || inner == Void.TYPE)
 193                 return null;
 194 
 195             LocationInfo outerLoc = nestingForType(owner, getLocation().popAllLocations((byte)1));
 196             TypeAnnotation[]all = getTypeAnnotations();
 197             List<TypeAnnotation> l = new ArrayList<>(all.length);
 198 
 199             for (TypeAnnotation t : all)
 200                 if (t.getLocationInfo().isSameLocationInfo(outerLoc))
 201                     l.add(t);
 202 
 203             return buildAnnotatedType(owner, outerLoc, l.toArray(EMPTY_TYPE_ANNOTATION_ARRAY), all, getDecl());
 204 
 205         }
 206 
 207         @Override // java.lang.Object
 208         public String toString() {
 209             // Reusable toString implementation, but needs to be
 210             // specialized for quirks of arrays.
 211             return annotationsToString(getAnnotations(), false) + type.toString();


 212         }
 213 
 214         protected String annotationsToString(Annotation[] annotations, boolean leadingSpace) {
 215             if (annotations != null && annotations.length > 0) {
 216                 StringJoiner sj = new StringJoiner(" ");
 217                 if (leadingSpace) {
 218                     sj.add(""); // Add a space
 219                 }
 220 
 221                 for (Annotation annotation : annotations) {
 222                     sj.add(annotation.toString());
 223                 }
 224 
 225                 if (!leadingSpace) {
 226                     sj.add("");
 227                 }
 228                 return sj.toString();
 229             } else {
 230                 return "";
 231             }


 360         AnnotatedTypeVariableImpl(TypeVariable<?> type, LocationInfo location,
 361                 TypeAnnotation[] actualTypeAnnotations, TypeAnnotation[] allOnSameTargetTypeAnnotations,
 362                 AnnotatedElement decl) {
 363             super(type, location, actualTypeAnnotations, allOnSameTargetTypeAnnotations, decl);
 364         }
 365 
 366         @Override
 367         public AnnotatedType[] getAnnotatedBounds() {
 368             return getTypeVariable().getAnnotatedBounds();
 369         }
 370 
 371         @Override
 372         public AnnotatedType getAnnotatedOwnerType() {
 373             return null;
 374         }
 375 
 376         private TypeVariable<?> getTypeVariable() {
 377             return (TypeVariable)getType();
 378         }
 379 







 380         @Override
 381         public boolean equals(Object o) {
 382             if (o instanceof AnnotatedTypeVariable) {
 383                 AnnotatedTypeVariable that = (AnnotatedTypeVariable) o;
 384                 return equalsTypeAndAnnotations(that) &&
 385                     Arrays.equals(getAnnotatedBounds(), that.getAnnotatedBounds());
 386             } else {
 387                 return false;
 388             }
 389         }
 390 
 391         @Override
 392         public int hashCode() {
 393             return baseHashCode() ^
 394                 Objects.hash((Object[])getAnnotatedBounds());
 395         }
 396     }
 397 
 398     private static final class AnnotatedParameterizedTypeImpl extends AnnotatedTypeBaseImpl
 399             implements AnnotatedParameterizedType {


 428         public AnnotatedType getAnnotatedOwnerType() {
 429             Type owner = getParameterizedType().getOwnerType();
 430             if (owner == null)
 431                 return null;
 432             LocationInfo outerLoc = nestingForType(owner, getLocation().popAllLocations((byte)1));
 433             TypeAnnotation[]all = getTypeAnnotations();
 434             List<TypeAnnotation> l = new ArrayList<>(all.length);
 435 
 436             for (TypeAnnotation t : all)
 437                 if (t.getLocationInfo().isSameLocationInfo(outerLoc))
 438                     l.add(t);
 439 
 440             return buildAnnotatedType(owner, outerLoc, l.toArray(EMPTY_TYPE_ANNOTATION_ARRAY), all, getDecl());
 441         }
 442 
 443         private ParameterizedType getParameterizedType() {
 444             return (ParameterizedType)getType();
 445         }
 446 
 447         @Override


















 448         public boolean equals(Object o) {
 449             if (o instanceof AnnotatedParameterizedType) {
 450                 AnnotatedParameterizedType that = (AnnotatedParameterizedType) o;
 451                 return equalsTypeAndAnnotations(that) &&
 452                     Arrays.equals(getAnnotatedActualTypeArguments(), that.getAnnotatedActualTypeArguments());
 453             } else {
 454                 return false;
 455             }
 456         }
 457 
 458         @Override
 459         public int hashCode() {
 460             return baseHashCode() ^
 461                 Objects.hash((Object[])getAnnotatedActualTypeArguments());
 462         }
 463     }
 464 
 465     private static final class AnnotatedWildcardTypeImpl extends AnnotatedTypeBaseImpl implements AnnotatedWildcardType {
 466         private final boolean hasUpperBounds;
 467         AnnotatedWildcardTypeImpl(WildcardType type, LocationInfo location,


 505                 List<TypeAnnotation> l = new ArrayList<>(initialCapacity);
 506                 for (TypeAnnotation t : getTypeAnnotations())
 507                     if (t.getLocationInfo().isSameLocationInfo(newLoc))
 508                         l.add(t);
 509                 res[i] = buildAnnotatedType(bounds[i],
 510                         newLoc,
 511                         l.toArray(EMPTY_TYPE_ANNOTATION_ARRAY),
 512                         getTypeAnnotations(),
 513                         getDecl());
 514             }
 515             return res;
 516         }
 517 
 518         private WildcardType getWildcardType() {
 519             return (WildcardType)getType();
 520         }
 521 
 522         private boolean hasUpperBounds() {
 523             return hasUpperBounds;
 524         }



























 525 
 526         @Override
 527         public boolean equals(Object o) {
 528             if (o instanceof AnnotatedWildcardType) {
 529                 AnnotatedWildcardType that = (AnnotatedWildcardType) o;
 530                 return equalsTypeAndAnnotations(that) &&
 531                     // Treats ordering as significant
 532                     Arrays.equals(getAnnotatedLowerBounds(), that.getAnnotatedLowerBounds()) &&
 533                     // Treats ordering as significant
 534                     Arrays.equals(getAnnotatedUpperBounds(), that.getAnnotatedUpperBounds());
 535             } else {
 536                 return false;
 537             }
 538         }
 539 
 540         @Override
 541         public int hashCode() {
 542             return baseHashCode() ^
 543                 Objects.hash((Object[])getAnnotatedLowerBounds()) ^
 544                 Objects.hash((Object[])getAnnotatedUpperBounds());


 190             if (owner == null) // top-level, local or anonymous
 191                 return null;
 192             if (inner.isPrimitive() || inner == Void.TYPE)
 193                 return null;
 194 
 195             LocationInfo outerLoc = nestingForType(owner, getLocation().popAllLocations((byte)1));
 196             TypeAnnotation[]all = getTypeAnnotations();
 197             List<TypeAnnotation> l = new ArrayList<>(all.length);
 198 
 199             for (TypeAnnotation t : all)
 200                 if (t.getLocationInfo().isSameLocationInfo(outerLoc))
 201                     l.add(t);
 202 
 203             return buildAnnotatedType(owner, outerLoc, l.toArray(EMPTY_TYPE_ANNOTATION_ARRAY), all, getDecl());
 204 
 205         }
 206 
 207         @Override // java.lang.Object
 208         public String toString() {
 209             // Reusable toString implementation, but needs to be
 210             // specialized for quirks of arrays and interior types of
 211             // wildcards, etc.
 212             return annotationsToString(getAnnotations(), false) + 
 213                 ((type instanceof Class) ? type.getTypeName(): type.toString());
 214         }
 215 
 216         protected String annotationsToString(Annotation[] annotations, boolean leadingSpace) {
 217             if (annotations != null && annotations.length > 0) {
 218                 StringJoiner sj = new StringJoiner(" ");
 219                 if (leadingSpace) {
 220                     sj.add(""); // Add a space
 221                 }
 222 
 223                 for (Annotation annotation : annotations) {
 224                     sj.add(annotation.toString());
 225                 }
 226 
 227                 if (!leadingSpace) {
 228                     sj.add("");
 229                 }
 230                 return sj.toString();
 231             } else {
 232                 return "";
 233             }


 362         AnnotatedTypeVariableImpl(TypeVariable<?> type, LocationInfo location,
 363                 TypeAnnotation[] actualTypeAnnotations, TypeAnnotation[] allOnSameTargetTypeAnnotations,
 364                 AnnotatedElement decl) {
 365             super(type, location, actualTypeAnnotations, allOnSameTargetTypeAnnotations, decl);
 366         }
 367 
 368         @Override
 369         public AnnotatedType[] getAnnotatedBounds() {
 370             return getTypeVariable().getAnnotatedBounds();
 371         }
 372 
 373         @Override
 374         public AnnotatedType getAnnotatedOwnerType() {
 375             return null;
 376         }
 377 
 378         private TypeVariable<?> getTypeVariable() {
 379             return (TypeVariable)getType();
 380         }
 381 
 382         // For toString, the declaration of a type variable should
 383         // including information about its bounds, etc. However, the
 384         // use of a type variable should not. For that reason, it is
 385         // acceptable for the toString implementation of
 386         // AnnotatedTypeVariableImpl to use the inherited
 387         // implementation from AnnotatedTypeBaseImpl.
 388 
 389         @Override
 390         public boolean equals(Object o) {
 391             if (o instanceof AnnotatedTypeVariable) {
 392                 AnnotatedTypeVariable that = (AnnotatedTypeVariable) o;
 393                 return equalsTypeAndAnnotations(that) &&
 394                     Arrays.equals(getAnnotatedBounds(), that.getAnnotatedBounds());
 395             } else {
 396                 return false;
 397             }
 398         }
 399 
 400         @Override
 401         public int hashCode() {
 402             return baseHashCode() ^
 403                 Objects.hash((Object[])getAnnotatedBounds());
 404         }
 405     }
 406 
 407     private static final class AnnotatedParameterizedTypeImpl extends AnnotatedTypeBaseImpl
 408             implements AnnotatedParameterizedType {


 437         public AnnotatedType getAnnotatedOwnerType() {
 438             Type owner = getParameterizedType().getOwnerType();
 439             if (owner == null)
 440                 return null;
 441             LocationInfo outerLoc = nestingForType(owner, getLocation().popAllLocations((byte)1));
 442             TypeAnnotation[]all = getTypeAnnotations();
 443             List<TypeAnnotation> l = new ArrayList<>(all.length);
 444 
 445             for (TypeAnnotation t : all)
 446                 if (t.getLocationInfo().isSameLocationInfo(outerLoc))
 447                     l.add(t);
 448 
 449             return buildAnnotatedType(owner, outerLoc, l.toArray(EMPTY_TYPE_ANNOTATION_ARRAY), all, getDecl());
 450         }
 451 
 452         private ParameterizedType getParameterizedType() {
 453             return (ParameterizedType)getType();
 454         }
 455 
 456         @Override
 457         public String toString() {
 458             StringBuilder sb = new StringBuilder();
 459             sb.append(annotationsToString(getAnnotations(), false));
 460 
 461             Type t = getParameterizedType().getRawType();
 462             sb.append(t.getTypeName());
 463 
 464             StringJoiner sj = new StringJoiner(", ", "<", ">");
 465             sj.setEmptyValue("");
 466             for(AnnotatedType typeArg: getAnnotatedActualTypeArguments()) {
 467                 sj.add(typeArg.toString());
 468             }
 469             sb.append(sj.toString());
 470 
 471             return sb.toString();
 472         }
 473 
 474         @Override
 475         public boolean equals(Object o) {
 476             if (o instanceof AnnotatedParameterizedType) {
 477                 AnnotatedParameterizedType that = (AnnotatedParameterizedType) o;
 478                 return equalsTypeAndAnnotations(that) &&
 479                     Arrays.equals(getAnnotatedActualTypeArguments(), that.getAnnotatedActualTypeArguments());
 480             } else {
 481                 return false;
 482             }
 483         }
 484 
 485         @Override
 486         public int hashCode() {
 487             return baseHashCode() ^
 488                 Objects.hash((Object[])getAnnotatedActualTypeArguments());
 489         }
 490     }
 491 
 492     private static final class AnnotatedWildcardTypeImpl extends AnnotatedTypeBaseImpl implements AnnotatedWildcardType {
 493         private final boolean hasUpperBounds;
 494         AnnotatedWildcardTypeImpl(WildcardType type, LocationInfo location,


 532                 List<TypeAnnotation> l = new ArrayList<>(initialCapacity);
 533                 for (TypeAnnotation t : getTypeAnnotations())
 534                     if (t.getLocationInfo().isSameLocationInfo(newLoc))
 535                         l.add(t);
 536                 res[i] = buildAnnotatedType(bounds[i],
 537                         newLoc,
 538                         l.toArray(EMPTY_TYPE_ANNOTATION_ARRAY),
 539                         getTypeAnnotations(),
 540                         getDecl());
 541             }
 542             return res;
 543         }
 544 
 545         private WildcardType getWildcardType() {
 546             return (WildcardType)getType();
 547         }
 548 
 549         private boolean hasUpperBounds() {
 550             return hasUpperBounds;
 551         }
 552 
 553         @Override
 554         public String toString() {
 555             StringBuilder sb = new StringBuilder();
 556             sb.append(annotationsToString(getAnnotations(), false));
 557             sb.append("?");
 558 
 559             AnnotatedType[] bounds = getAnnotatedLowerBounds();
 560             if (bounds.length > 0) {
 561                 sb.append(" super ");
 562             } else {
 563                 bounds = getAnnotatedUpperBounds();
 564                 if (bounds.length > 0) {
 565                     sb.append(" extends ");
 566                 }
 567                 // With extra work, could check for and elide " extends
 568                 // Object" if Object was not annotated.
 569             }
 570             
 571             StringJoiner sj = new StringJoiner(" & ");
 572             for(AnnotatedType bound : bounds) {
 573                 sj.add(bound.toString());
 574             }
 575             sb.append(sj.toString());
 576             return sb.toString();
 577         }
 578 
 579 
 580         @Override
 581         public boolean equals(Object o) {
 582             if (o instanceof AnnotatedWildcardType) {
 583                 AnnotatedWildcardType that = (AnnotatedWildcardType) o;
 584                 return equalsTypeAndAnnotations(that) &&
 585                     // Treats ordering as significant
 586                     Arrays.equals(getAnnotatedLowerBounds(), that.getAnnotatedLowerBounds()) &&
 587                     // Treats ordering as significant
 588                     Arrays.equals(getAnnotatedUpperBounds(), that.getAnnotatedUpperBounds());
 589             } else {
 590                 return false;
 591             }
 592         }
 593 
 594         @Override
 595         public int hashCode() {
 596             return baseHashCode() ^
 597                 Objects.hash((Object[])getAnnotatedLowerBounds()) ^
 598                 Objects.hash((Object[])getAnnotatedUpperBounds());
< prev index next >