I'm wondering if there's a way to do this in a "simple way" and maybe someone knows a solution:
I'm working with javax.swing.text.html.HTMLDocument class, but for some reason, at least 2 methods that I need are non-public, even so, I need to "override" them, to change a few things, but, in some way, I need to keep the HTMLDocument class because I'm using a lot of the package javax.swing.text.html...
So, what I first do, was create some MyHTMLDocument extends HTMLDocument, and tryed to override some methods... when that failed, I tried to solve it using reflection over some methods, but is not working.. so, being desesperated, I've "copy - paste all" HTMLDocument code as MyHTMLDocument, (extending HTMLDocument), change what I needed and put it on my own "javax.swing.text.html" package, and it seems to work for now, but...
...when I finally run it, I'm getting stuck on some "Illegal Access errors", when I call TagActions for example... and I'm getting frustrated...
please, if somebody could help me, I really appreciate it so much.
UPDATE:
Ok, let me clarify:
On the class javax.swing.text.html.HTMLDocument, you can find three methods:
public getReader(int pos)
public getReader(int pos, int pos, int popDepth, int pushDepth, HTML.Tag insertTag)
getReader(int pos, int pos, int popDepth, int pushDepth, HTML.Tag insertTag, boolean insertInsertTag)
the last one, is non public or protected.
Also, there is an inner-Class called HTMLReader and it has 3 constructors:
public HTMLReader(int offset)
public HTMLReader(int offset, int popDepth, int pushDepth, HTML.Tag insertTag)
HTMLReader(int offset, int popDepth, int pushDepth, HTML.Tag insertTag, boolean insertInsertTag, boolean insertAfterImplied, boolean wantsTrailingNewline)
again, the last one, is non public.
What I need to do is to call at least these 2 non-public from my custom class: public class MyHTMLDocument extends HTMLDocument{
} but I just dont know how to do it.. I even tryed with some reflection and it appears to work for methods, but I cant find a way to do the same for constructors... Thanks again.
Those methods are both package private, which is why you can't get to them. I agree that it might be a bug, given the comments in the source:
356 /**
357 * Fetches the reader for the parser to use to load the document
358 * with HTML. This is implemented to return an instance of
359 * HTMLDocument.HTMLReader. Subclasses can reimplement this
360 * method to change how the document get structured if desired
361 * (e.g. to handle custom tags, structurally represent character
362 * style elements, etc.).
363 *
...
If you really wanted to do this, you could put it in javax.swing.text.html
inside your own project. There's nothing stopping you, but that way lies potential confusion later on.
Whether or not that technique is acceptable is an interesting question in its own right. There might be class-loading issues, especially if you're running in a container that plays games with classloading.
Basically: As I already commented, there is a way to do it... isn't a simple way, but it could be helpful:
Create a package in the solution with the same name that the used class (for example HTMLDocument), in this case: "javax.swing.text.html"
Create a class that extends the original class (CorrectHTMLDocument)
There are lot of considerable things: All privated members will raise an IllegallAccessException, so I used reflection over HTMLDocument.class to obtain all of them and made them accessible.
To "Override" the wrong HTMLReader, I copy / paste the original source code, of it and put it as innerClass in CorrectHTMLDocument, and using the same technique described, solve the IllegalAccessExceptions
There are a few things, there are some calls to "default" access classes like javax.swing.text.html.Map, to solve the IllegalAccessError, again, I used reflection over class and changed the original Map declaration members for Object.
Now, it's working fine... I hope it helps to any one with same issue... if there are some...