-2

I want the sub classes to pass specific enum values to parent class in extends, such that these specific enum value would be used in parent class in some annotation. Below is what I exactly want to achieve.

public enum SomeEnum {
    VAL1, VAL2;
}

public @interface SomeAnnotation {
    SomeEnum someValue();
}

//below starts all fuzzy things : I want a specific value of enum 
public class parent<FetchType> {// not sure what exactly should write between  < >

    // below,at someValue I want the specific enum passed by child , means
    // the value passed between < and > 
    @SomeAnnotation(someValue = null /* here  should be the specific enum value passed 
    as type parameter of this class */) 
    private String someString;
        //getter-setter
}
//as i said , for child1 , i need SomeEnum.VAL1 to be passed to parent and
// SomeEnum.VAL2 for child2.
public class child1 extends parent<SomeEnum.VAL1>{}
public class child2 extends parent<SomeEnum.VAL2>{}
Flimzy
  • 60,850
  • 13
  • 104
  • 147
Priyank Doshi
  • 11,959
  • 16
  • 54
  • 81
  • It's helpful to include a question in your question. – James Montagne Feb 08 '13 at 17:56
  • @JamesMontagne : question is already in form of comments in code. though i am adding for better understanding now. – Priyank Doshi Feb 08 '13 at 17:58
  • I am unable to understand the question. What do you mean by " I want the specific enum passed by child". I am not sure annotations are the right way to solve whatever problem you are trying to solve – Miserable Variable Feb 08 '13 at 17:59
  • 2
    An enum value cannot be a type parameter to a generic class – Miserable Variable Feb 08 '13 at 17:59
  • 1
    Enums are values, not types. They can't be used in type parameters. – Louis Wasserman Feb 08 '13 at 18:00
  • @LouisWasserman then how to solve above problem? – Priyank Doshi Feb 08 '13 at 18:02
  • @downvoters : care to explain why? – Priyank Doshi Feb 08 '13 at 18:03
  • @PriyankDoshi: You can't do what you're trying to do. You can't specify the enum value you want in the type system, only in the actual logic. – Louis Wasserman Feb 08 '13 at 18:04
  • You want to pass an enum to a base class at class definition time. This is not possible. You can try to pass it to the base class at object creation time by passing it through the constructor. But you can't apply the value to the annotation because you can't, as much as I can tell, modify an annotation at runtime. Therefore you should rethink what you are trying to do and find another way to solve whatever underlying problem you are trying to solve. – Cyrille Ka Feb 08 '13 at 18:13

1 Answers1

0

I'll go out on a limb and suggest you don't need Enum and annotations but perhaps something like the following?

interface NotEnum  {
  interface Val1 extends NotEnum {}
  interface Val2 extends NotEnum {}
}

class Parent<T extends NotEnum> {...}

class Child1 extends Parent<NotEnum.Val1> {...}
class Child2 extends Parent<NotEnum.Val2> {...}
Miserable Variable
  • 27,314
  • 13
  • 69
  • 124