• Post Reply Bookmark Topic Watch Topic
  • New Topic

Add a constructor in a subclass  RSS feed

 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using the com.oreilly.servlet class, and wanting to 'tweak' one of its classes.

For a very quick background on *exactly* what I'm trying to do, you can check this article

My problem is with the MultipartWrapper. Code of interest: The problem is the call to MultipartRequest(req, dir).

MultipartRequest has about 8 constructors. I need the one that allows me to set a file upload size larger than 1 MB (which is the default when you don't specify). So I thought to do the following: If I leave the call to super(req, dir) commented out, I get this error:By uncommenting that call to super(req, dir), it compiles. But unfortunately, because of what super(req, dir) *Does* to the req object, I cannot use it again in the next line.. (actually it compiles fine, but runtime you get "unexpected end of part".. which is what happens when you try to read a request object twice, but beyond the scope of the question...

The compiler will insert a call to the no-args constructor, when you don't specify one. But if I specify one, it breaks the functioning of the class. Is there a way around this? Am I doing something that is fundamentally wrong (trying to add constructor signature in a subclass) or is this just a side-effect of the *req* object being ... flakey.. when its touched more than once?
 
norman richards
Author
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like you are out of luck with regards to overridding the constructor. However, you aren't completely out of luck. One sneaky thing you could do (besides not using poorly defined and inextensible 3rd party code) would be to pass in a DIFFERENT HttpServletRequest (think mock object) to the super class constructor.
I don't know what the MultipartRequest constructor is doing to the real HttpServletRequest that is making it unusable, but you can always pass a fake HttpServletRequest to it. Then you can override mreq with the value constructed with the real HttpServletRequest.
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems like this is the sort of thing that might get run into a lot.

Is providing a no-args constructor (even if it does nothing) a good-neighbor sort of thing, considering that at some point, someone might want to extend your class?
 
muthu muruges
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what happens if we put like this?
public MultipartRequestWrapper()
{
super();
}
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is, there is no superclass, no-args constructor.

So calling super() explicitly will result in the same problem as not calling super() explicity and having the compiler do it for you.
 
norman richards
Author
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did the "mock" HttpServletRequest idea work? I don't have the code in from of me, so I can't test it. But I don't see any reason it wouldn't work...
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm very sure it would work, but I've gone ahead and done something different.

The reason I wanted to extend MultipartWrapper was because it defined a whole bunch of other methods of use.

Instead of extending though (since it seems impossible because of the constructor difficulty), I've gone ahead and implemented MultipartRequestWrapper as extending HttpServletRequestWrapper , and i've got 8 constructors for it, to match the 8 constructors of the MultipartRequest. AND i've boiler-plated the other code into my class.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!