Annotations - interface as attribute type

Hey, I haven't found any useful resources on this. Maybe I'm using the wrong keywords, so I figured I should ask here: Why do annotations not allow interfaces as attribute types?
public interface MyInterface {
String upper();
}

public @interface MyAnnotation {
MyInterface value(); // Invalid type 'MyInterface' for annotation member
}
public interface MyInterface {
String upper();
}

public @interface MyAnnotation {
MyInterface value(); // Invalid type 'MyInterface' for annotation member
}
For some more context: I actually want to pass an enum, but use an interface to define a standard method (implementation can vary). This is just an example:
public enum MyEnum implements MyInterface {
1("One"),
2("Two");

private String string;

public MyEnum(String string) {
this.string = string;
}

@Override
public String upper() {
return string.toUpperCase();
}
}

@MyAnnotation(MyEnum.1)
public class MyClass {
// ... could then get the value and use `MyInterface#upper`
}
public enum MyEnum implements MyInterface {
1("One"),
2("Two");

private String string;

public MyEnum(String string) {
this.string = string;
}

@Override
public String upper() {
return string.toUpperCase();
}
}

@MyAnnotation(MyEnum.1)
public class MyClass {
// ... could then get the value and use `MyInterface#upper`
}
8 Replies
JavaBot
JavaBot7mo ago
This post has been reserved for your question.
Hey @Aze! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Kyo-chan
Kyo-chan7mo ago
Annotations are fully defined at compile time. Like, what they are is exactly how they're written. Running the program can't change anything about them. And while an enum is a compile-time constant, a general type like an interface or class, isn't. Like if it was accepted, then I could do:
public class MyClass implements MyInterface {
private final String salt;
public MyClass(String salt) {
this.salt = salt + LocalDateTime.now();
}
@Override
public String upper() {
return salt;
}
}
public class MyClass implements MyInterface {
private final String salt;
public MyClass(String salt) {
this.salt = salt + LocalDateTime.now();
}
@Override
public String upper() {
return salt;
}
}
and then
@MyAnnotation(new MyClass("kyo-chan"))
public class MyClass {
// ... could then get the value and use `MyInterface#upper`
}
@MyAnnotation(new MyClass("kyo-chan"))
public class MyClass {
// ... could then get the value and use `MyInterface#upper`
}
Which is completely contradictory with the concept of annotations
Aze
AzeOP7mo ago
Right, I understand the value has to be a constant, but in my example the interface is implemented by an enum, and I know interfaces can also be implemented by normal classes, like in you example. But since enums can implement interfaces, why disallow it completely?
Kyo-chan
Kyo-chan7mo ago
Because other things can also implement interfaces
Aze
AzeOP7mo ago
When the interface is applied to an enum it would be a constant too, no? If I drop the interface and implement the method straight into the enum it works
Kyo-chan
Kyo-chan7mo ago
You must limit yourself to what can't be mistyped. That's the entire concept of static typing
Aze
AzeOP7mo ago
I guess that makes sense kind of sad, now I have to rethink what I had planned Appreciate the help!
JavaBot
JavaBot7mo ago
Post Closed
This post has been closed by <@398509167351955456>.
Want results from more Discord servers?
Add your server