How to provide implementation of annotations reading alongside the annotation
up vote
0
down vote
favorite
Let's say have I have an annotation that's to be used like this:
public void foo (@IntParam int quantity, @FooParam Foo bar) {
...
}
I would like to call this method, access the params from an object and pass it like this:
public void invokeFoo (Method fooMethod, Map<String, String> context) throws ... {
List<Object> params = Arrays.stream(fooMethod.getAnnotations()).map(annos -> {
if (annos[0] instanceof IntParam) {
return context.get( ... ); // <-
} else if (annos[0] instanceof FooParam) {
return new Foo(context); // <-
}
}).collect(Collectors.toList());
fooMethod.invoke(params.toArray());
}
The problem with this implentation is that I need to edit 2 different files if I want to add new types of params, which I would like to avoid for several reasons.Is there a way to write those lines in the same annotation file ?. The only way would be using a static/default method on the interface but either way this would type poorly.
Any other way ?
java annotations
add a comment |
up vote
0
down vote
favorite
Let's say have I have an annotation that's to be used like this:
public void foo (@IntParam int quantity, @FooParam Foo bar) {
...
}
I would like to call this method, access the params from an object and pass it like this:
public void invokeFoo (Method fooMethod, Map<String, String> context) throws ... {
List<Object> params = Arrays.stream(fooMethod.getAnnotations()).map(annos -> {
if (annos[0] instanceof IntParam) {
return context.get( ... ); // <-
} else if (annos[0] instanceof FooParam) {
return new Foo(context); // <-
}
}).collect(Collectors.toList());
fooMethod.invoke(params.toArray());
}
The problem with this implentation is that I need to edit 2 different files if I want to add new types of params, which I would like to avoid for several reasons.Is there a way to write those lines in the same annotation file ?. The only way would be using a static/default method on the interface but either way this would type poorly.
Any other way ?
java annotations
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Let's say have I have an annotation that's to be used like this:
public void foo (@IntParam int quantity, @FooParam Foo bar) {
...
}
I would like to call this method, access the params from an object and pass it like this:
public void invokeFoo (Method fooMethod, Map<String, String> context) throws ... {
List<Object> params = Arrays.stream(fooMethod.getAnnotations()).map(annos -> {
if (annos[0] instanceof IntParam) {
return context.get( ... ); // <-
} else if (annos[0] instanceof FooParam) {
return new Foo(context); // <-
}
}).collect(Collectors.toList());
fooMethod.invoke(params.toArray());
}
The problem with this implentation is that I need to edit 2 different files if I want to add new types of params, which I would like to avoid for several reasons.Is there a way to write those lines in the same annotation file ?. The only way would be using a static/default method on the interface but either way this would type poorly.
Any other way ?
java annotations
Let's say have I have an annotation that's to be used like this:
public void foo (@IntParam int quantity, @FooParam Foo bar) {
...
}
I would like to call this method, access the params from an object and pass it like this:
public void invokeFoo (Method fooMethod, Map<String, String> context) throws ... {
List<Object> params = Arrays.stream(fooMethod.getAnnotations()).map(annos -> {
if (annos[0] instanceof IntParam) {
return context.get( ... ); // <-
} else if (annos[0] instanceof FooParam) {
return new Foo(context); // <-
}
}).collect(Collectors.toList());
fooMethod.invoke(params.toArray());
}
The problem with this implentation is that I need to edit 2 different files if I want to add new types of params, which I would like to avoid for several reasons.Is there a way to write those lines in the same annotation file ?. The only way would be using a static/default method on the interface but either way this would type poorly.
Any other way ?
java annotations
java annotations
asked Nov 10 at 18:41
Vinz243
3,55653062
3,55653062
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242228%2fhow-to-provide-implementation-of-annotations-reading-alongside-the-annotation%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown