Uploaded image for project: 'Weld'
  1. Weld
  2. WELD-1284

Use @Exactly to fix an ambiguous dependency

    XMLWordPrintable

Details

    • Feature Request
    • Resolution: Won't Do
    • Major
    • None
    • None
    • None
    • None
    • Low

    Description

      Sometimes we want to fit a parametrized injection point with your exact bean. The most common case is when you trying to inject a Class<>.

      Let's see:

      class Animal {

      }

      class Dog extends Animal {

      }

      class PitBull extends Dog {

      }

      class ShowClassName<T> {

      @Inject
      public ShowClassName(Class<T> clazz)

      { System.out.println(clazz.getName()); }

      }

      public class ExactlyTestCase {

      @Inject
      private ShowClassName<Animal> animalShowClassName;

      @Inject
      private ShowClassName<Dog> dogShowClassName;

      @Inject
      private ShowClassName<PitBull> pitBullShowClassName;

      @Produces
      public Class<Animal> createAnimalClass() { return Animal.class; }

      @Produces
      public Class<Dog> createDogClass() { return Dog.class; }

      @Produces
      public Class<PitBull> createPitBullClass() { return PitBull.class; }

      }

      Class<PitBull> fit with 3 and Class<Dog> fit with 2. I know this behavior is frequently expected but in some cases it's not, as I showed you. In theses cases use qualifiers does not help because i must have one qualifier for each Class and have one ShowClassName Bean for each Class with its especific qualifier annotated, wich does not provide reuse.

      Have this will be very nice:

      class ShowClassName<T> {

      @Inject
      public ShowClassName(@Exactly Class<T> clazz) { System.out.println(clazz.getName()); }

      }

      Thanks

      Attachments

        Activity

          People

            rhn-engineering-jharting Jozef Hartinger
            brunohansen_jira Bruno Hansen (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: