2

I'm writing function that will be used with HttpGet and HttpPost. It's something like that:

private void initialize(HttpRequestBase method)
{
    if(method == new HttpPost())
    {
    String body = "body";
        HttpEntity entity = new ByteArrayEntity(body.getBytes("UTF-8"));
        method.setEntity(entity);
}

Problem is that HttpRequestBase doesn't support .setEntity. How can I write function that support HttpGet and HttpPost without problems like that?

5
  • 1
    You know that your if condition will never be true, don't you? Any existing object reference compared with a new object will return false. Commented Feb 15, 2017 at 16:28
  • Oh, you're right. Any approach? Commented Feb 15, 2017 at 16:30
  • if(method instanceof HttpPost) Commented Feb 15, 2017 at 16:31
  • 1
    Sure, use instanceof. Then you'll also be able to cast. But this is very basic Java. Perhaps you should review Inheritance. Commented Feb 15, 2017 at 16:32
  • Thanks for replies, I'll try Commented Feb 15, 2017 at 16:33

1 Answer 1

2

The way to work around this problem is to check the type using instanceof and use a cast. Like this:

private void initialize(HttpRequestBase method)
{
    if(method instanceof HttpPost)
    {
        String body = "body";
        HttpEntity entity = new ByteArrayEntity(body.getBytes("UTF-8"));
        ((HttpPost) method).setEntity(entity);
    }
}

But whenever you use a cast, you should consider that there may be a more elegant solution. In this case, I would argue that a more elegant solution would be to use method overloading and have a specific method for HttpPost instances.

private void initialize(HttpPost method)
{
    String body = "body";
    HttpEntity entity = new ByteArrayEntity(body.getBytes("UTF-8"));
    method.setEntity(entity);
}

Of course this would mean you would need a separate method for HttpGet (and any other subclasses of HttpRequestBase you wish to support). Any common code shared between GET and POST should be extracted into smaller methods that are called by both initialize() methods.

private void initialize(HttpGet method)
{
    // ...
}

This, of course, you might argue defeats the whole point of trying to create one handler for both GET and POST. And you would be right. But you should then question the entire exercise of trying to create one method that handles both. Perhaps the most elegant design is to treat them separately. After all, that is precisely what the authors of the framework you're using chose to do.

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

3 Comments

Creating two separated methods - that was something I wanted to avoid, because 90% of code in that methods would be the same
Encapsulate common functionality in smaller methods that are called by both initializers.
@SamuelTeixeira Good idea. Done.

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.