An instance of an abstract class with a hidden constructor

I need to instantiate an abstract class with a hidden constructor, the class looks like this:

public abstract class TestClass {
    /**
    * @hide
    */
    public TestClass() {
    }
}

Creating a concrete class does not work because the constructor is not displayed and calling the constructor through the reflection API also does not work because the class is abstract.

I need to create an instance of android.print.PrintDocumentAdapter.LayoutResultCallback

+4
source share
2 answers

I ran into the same problem (exactly for the same class), and I have a better solution than replacing android.jar with framework.jar, as suggested in another answer.

dexmaker. ( dexmaker.1.4.jar dexmaker-dx.1.4.jar). , - VM Dalvik ( , android).

ProxyBuilder, . - , , java.lang.reflect.InvocationHandler, .

ProxyBuilder java.lang.refect.Proxy, , java.lang.refect.Proxy , dexmaker ProxyBuilder , .

- :

public static PrintDocumentAdapter.LayoutResultCallback getLayoutResultCallback(InvocationHandler invocationHandler,
                                                                                File dexCacheDir) throws  IOException{
    return ProxyBuilder.forClass(PrintDocumentAdapter.LayoutResultCallback.class)
            .dexCache(dexCacheDir)
            .handler(invocationHandler)
            .build();
}

invocationHandler, . cacheDir - , dexmaker .

+2

All Articles