1515import java .util .ArrayList ;
1616import java .util .Arrays ;
1717import java .util .Collection ;
18+ import java .util .Collections ;
1819import java .util .HashMap ;
1920import java .util .HashSet ;
2021import java .util .Iterator ;
@@ -548,6 +549,11 @@ public IBinding findUnresolvedBinding(String bindingKey) {
548549 return findBinding (bindingKey );
549550 }
550551
552+ final boolean bkExtends = bindingKey .startsWith ("+" );
553+ final boolean bkSuper = bindingKey .startsWith ("-" );
554+
555+ String withoutSuperExtends = bindingKey .startsWith ("+" ) || bindingKey .startsWith ("-" ) ? bindingKey .substring (1 ) : bindingKey ;
556+
551557 HashSet <String > validNames = new HashSet <String >();
552558 validNames .add (bindingKey );
553559 compoundListWithAction (validNames , x -> x .replaceAll ("\\ ." , "/" ));
@@ -556,25 +562,41 @@ public IBinding findUnresolvedBinding(String bindingKey) {
556562 compoundListWithAction (validNames , x -> x .lastIndexOf ("." , x .length () - 1 ) != -1 ? x .substring (x .lastIndexOf ("." ) + 1 ) : null );
557563 compoundListWithAction (validNames , x -> x .startsWith ("Q" ) ? x .substring (1 ) : null );
558564 compoundListWithAction (validNames , x -> x .contains ("<Q" ) ? x .replaceAll ("<Q" , "<" ) : null );
559- String bindingKeySimpleName = Signature .getSignatureSimpleName (bindingKey );
565+ compoundListWithAction (validNames , x -> x .startsWith ("+Q" ) ? x .replaceAll ("\\ +Q" , "? extends " ) : null );
566+ compoundListWithAction (validNames , x -> x .startsWith ("-Q" ) ? x .replaceAll ("-Q" , "? super " ) : null );
567+ String bindingKeySimpleName = Signature .getSignatureSimpleName (withoutSuperExtends );
560568 validNames .add (bindingKeySimpleName );
561569
562570 Collection <JavacTypeBinding > c = new ArrayList <>(this .bindings .typeBinding .values ());
563- List <JavacTypeBinding > strongMatch = new ArrayList <>();
571+ int matchesKey = 0x80 ;
572+ int matchesSimpleName = 0x40 ;
573+ int matchesSuperExtends = 0x10 ;
574+ record Pair (JavacTypeBinding binding , int weight ) {};
575+ List <Pair > collector = new ArrayList <Pair >();
576+
564577 c .stream ().forEach (x -> {
578+ int total = 0 ;
579+ String k = x .getKey ();
580+ if ( validNames .contains (k ))
581+ total += matchesKey ;
565582 String n = x .getName ();
566583 if ( validNames .contains (n )) {
567- strongMatch .add (x );
568- return ;
569- }
570- String k = x .getKey ();
571- if ( validNames .contains (k )) {
572- strongMatch .add (x );
573- return ;
584+ total += matchesSimpleName ;
585+ }
586+ if ( bkExtends || bkSuper ) {
587+ if ( x .isWildcardType () && x .getBound () != null ) {
588+ if ( bkExtends && x .isUpperbound ()) {
589+ total += matchesSuperExtends ;
590+ } else if ( bkSuper && !x .isUpperbound () ) {
591+ total += matchesSuperExtends ;
592+ }
593+ }
574594 }
595+ collector .add (new Pair (x , total ));
575596 });
576597
577- return strongMatch .size () > 0 ? strongMatch .get (0 ) : null ;
598+ Collections .sort (collector , (o1 , o2 ) -> o2 .weight - o1 .weight );
599+ return collector .size () > 0 ? collector .get (0 ).binding : null ;
578600 }
579601
580602 @ Override
0 commit comments