Calling a local class from a run-time generated class

F

Florian Weimer

I'm trying to create, using the ASM library, a class which calls
methods in an another class specified at run-time (passed as its Class
object). I don't think my problem is ASM-related, that's why I'm
asking here.

My approach works just fine when the class being called is declared
"public", but not if it's a local class in some member method.

The local class I'm trying to call looks like this in javap:

class de.enyo.pcre.PatternTest$1T extends java.lang.Object
SourceFile: "PatternTest.java"
InnerClass:
#87= #1; //T=class de/enyo/pcre/PatternTest$1T
EnclosingMethod: length = 0x4
00 59 00 5B
minor version: 0
major version: 50
Constant pool:
const #1 = class #2; // de/enyo/pcre/PatternTest$1T
[...]
const #22 = Asciz ()V;
[...]
const #87 = Asciz T;
const #88 = Asciz EnclosingMethod;
const #89 = class #90; // de/enyo/pcre/PatternTest
const #90 = Asciz de/enyo/pcre/PatternTest;
const #91 = NameAndType #92:#22;// testDispatcher:()V
const #92 = Asciz testDispatcher;

The run-time generated class looks like this:

public class de.enyo.pcre.PatternTest$1T$RegExpDispatcher extends de.enyo.pcre.DispatcherASM
Signature: length = 0x2
00 03
EnclosingMethod: length = 0x4
00 07 00 0A
InnerClass:
#2; //class de/enyo/pcre/PatternTest$1T$RegExpDispatcher
minor version: 0
major version: 50
Constant pool:
const #1 = Asciz de/enyo/pcre/PatternTest$1T$RegExpDispatcher;
const #2 = class #1; // de/enyo/pcre/PatternTest$1T$RegExpDispatcher
const #3 = Asciz Lde/enyo/pcre/DispatcherASM<de/enyo/pcre/PatternTest$1T>;;
[...]
const #7 = class #6; // de/enyo/pcre/PatternTest
const #8 = Asciz testDispatcher;
const #9 = Asciz ()V;
const #10 = NameAndType #8:#9;// testDispatcher:()V

I had hoped that the similarity is sufficient. Howver, executing the
generated class results in an IllegalAccessError exception ("tried to
access class de.enyo.pcre.PatternTest$1T from class
de.enyo.pcre.PatternTest$1T$RegExpDispatcher").

I'm using a trivial class loader which specifies the loader of
PatternTest$1T as its parent. The "public" attribute of the generated
class is required because I need it to instantiate the class.

Any ideas? How can I work around these accessibility checks?
 
D

Daniel Pitts

Florian said:
I'm trying to create, using the ASM library, a class which calls
methods in an another class specified at run-time (passed as its Class
object). I don't think my problem is ASM-related, that's why I'm
asking here.

My approach works just fine when the class being called is declared
"public", but not if it's a local class in some member method.

The local class I'm trying to call looks like this in javap:
[snip]
I had hoped that the similarity is sufficient. Howver, executing the
generated class results in an IllegalAccessError exception ("tried to
access class de.enyo.pcre.PatternTest$1T from class
de.enyo.pcre.PatternTest$1T$RegExpDispatcher").

I'm using a trivial class loader which specifies the loader of
PatternTest$1T as its parent. The "public" attribute of the generated
class is required because I need it to instantiate the class.

Any ideas? How can I work around these accessibility checks?

My guess is that the local class needs to either be public, or package
local and in the same package as the generated class. The JVM does
inforce access modifiers to some degree.
 
F

Florian Weimer

* Daniel Pitts:
My guess is that the local class needs to either be public, or package
local and in the same package as the generated class. The JVM does
inforce access modifiers to some degree.

Ah, that's it. If I added the ACC_PUBLIC flag to the existing class,
it works. Thanks.

Unfortunately, the Java Language Specification explicitly forbids
specifying a "public" modifier for local classes. Maybe I should
simply document that this won't work. Or add some for of compile-time
annotation processing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top