4

In RESTeasy this...

@GET
@Path("request")
public String requestJson(@QueryParam("arg1") @DefaultValue("") String arg1,
                          @QueryParam("arg2") @DefaultValue("0") Integer arg2);

...allows you to define any subset of parameters defined in the method signature. Surely it is then possible to use this same pattern on any method signature something like this:

@Method
public String requestJson(@OptionalParameter("arg1") @DefaultValue("") String arg1,
                          @OptionalParameter("arg2") @DefaultValue("0") Integer arg2);

Is it possible to do this? If so how?

1
  • 1
    The annotations (which are from JSR311, btw) merely tell the container (RESTeasy in this case) to inject a value if the parameter is null, which is very different from optional named parameters, which isn't something that Java supports. Commented Jul 7, 2011 at 2:17

2 Answers 2

3

The annotations in the RESTEasy example are possible due to the fact that the object is managed by the RESTEasy framework. It can inspect the method and determine how to invoke it based on those annotations. It is possible to create any annotations you'd want, the key is that the code calling the method needs to know about the annotations and how to process them.

Sign up to request clarification or add additional context in comments.

Comments

3

First of all, what @laz said was exactly right. This is possible in RESTEasy because RESTEasy is managing the calls to your class.

I think the pertinent question here is what you would expect an invocation of requestJson() to look like. Some examples: requestJson(1) and requestJson("String") versus something like requestJson(null, 1) and requestJson("String", null).

There are five ways I can think of to handle this:

  1. The least automated way (but ultimately, best way): Write the variant methods yourself either by hand or set your IDE up to generate them.

  2. Make it the caller's responsibility to set the value to a default value if it doesn't have one (i.e. the caller determines "I'm calling a method which has a "

  3. Intercept the call to requestJson() and plugin the missing values as needed (this gets into some messy reflection or runtime code generation voodoo; it's possible, but it's not going to be simple -- basically you're building your own AOP library which will be managing all the calls to requestJson()).

  4. Plug-in to the compiler / build chain to generate the required methods, i.e.:

    public String requestJson() { requestJson("", 0); }
    public String requestJson(String arg1) { requestJson(arg1, 0); }
    public String requestJson(Int arg2) { requestJson("", arg2); }
    public String requestJson(String arg1, Int arg2) {...}
    

    (This is similar to what is discussed here)

  5. Add the code to determine the missing values to the top of your requestJson implementation. This would only enable things like requestJson(null, 1), and it probably not what you are looking for (as it requires extra handing in requestJson()). It may be possible to extract the boiler plate into a library.

2 Comments

thanks for your reply. I have in fact previously considered most of the approaches you mention here. I guess the way I would hope to see 'requestJson' invoked would be like '@method requestJson(@param({name="arg1",value="1"})' where the annotation '@method' can reflect the method it wraps, extract the number of parameters in the method signature then internally overload the method as required for any missing parameters and then pull in the value defined in any '@param' annotations to be passed to the overloaded method. Can methods be reflected in this way?
That syntax you're using seems to have annotations at the point the method is invoked, which won't compile.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.