0

I'm trying to create an annotation which can accept multiple classes as input. Typical usage would be

@Prerequisites{FirstPrerequisite.class, SecondPrerequisite.class}

For this I can create an annotation as shown below

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Inherited
public @interface Prerequisites {
    public Class<?>[] value();
}

I want to restrict the types that are allowed in the prerequisites annotation. If I give something like public Class<? extends Dependency>[] value(); It is working however, the problem is My FirstPrerequisite and SecondsPrerequisite may bot be extended from same type. I tried the following but none seemed to work, they are giving compilation errors.

public Class<? extends Dependency & TestCaseStep>[] value();
public Class<? extends Dependency , TestCaseStep>[] value();
public Class<T extends Dependency & TestCaseStep>[] value();

How to bound Generics to take inputs of two different types?

Buddha
  • 4,339
  • 2
  • 27
  • 51

1 Answers1

0

One option is to create a marker interface and have your classes implement the interface. You can then provide a bound with that interface type.

Another alternative is to simply move the constraint checking at runtime instead of at compile time. Have your annotation processor validate the Class arguments that were provided.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • May be second options is for me because I have to work with two different types created in past now, I'm supposed to bring them together. Thanks. – Buddha Jul 11 '14 at 04:13