Discussion:
accessing package private class
Ted Yu
2013-12-31 15:16:11 UTC
Permalink
Hi,
In a class A there is another class:

    static final class LimitedInputStream extends FilterInputStream {
      private int limit;

      LimitedInputStream(InputStream in, int limit) {

How can I access its ctor through reflection ?

Thanks
Stone Zhong
2013-12-31 16:58:30 UTC
Permalink
Hi Yu,

Here is what JDK said:

http://docs.oracle.com/javase/6/docs/api/java/lang/reflect/Constructor.html#newInstance%28java.lang.Object...%29
newInstance
public T newInstance(Object... initargs) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
...
If the constructor's declaring class is an inner class in a non-static context, the first argument to the constructor needs to be the enclosing instance; see The Java Language Specification, section 15.9.3.

...




On Tuesday, December 31, 2013 7:16 AM, Ted Yu <ted_yu-/***@public.gmane.org> wrote:

 
Hi,
In a class A there is another class:

    static final class LimitedInputStream extends FilterInputStream {
      private int limit;

      LimitedInputStream(InputStream in, int limit) {

How can I access its ctor through reflection ?

Thanks
P.Hill
2014-01-02 06:15:41 UTC
Permalink
You quoted the wrong section. Bold underscore emphasis mine.

-Paul
Post by Stone Zhong
Hi Yu,
[...]
If the constructor's declaring class is an inner class in a
_*non-static contex*_t, the first argument to the constructor needs to
be the enclosing instance; see /The Java Language Specification/,
section 15.9.3.
[...]
Post by Stone Zhong
[...]
_*static*_ final class LimitedInputStream extends FilterInputStream {
private int limit;
LimitedInputStream(InputStream in, int limit) {
How can I access its ctor through reflection ?
Loading...