Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.IllegalAccessException: is related to public / private attribute for classes?

Tags:

java

exception

I get the following error in my Java code:

java.lang.IllegalAccessException: Class org.apache.commons.digester.ObjectCreateRule can not access a member of class Response with modifiers ""

Is it maybe because class Response is not public class ? If so, how can I make it accessible and keep the class Response in the same file with the main class ?

thanks

Update, Code: http://www.smipple.net/snippet/aneuryzma/on:%20is%20related%20to%20public%20/%20private%20attribute%20for%20classes%20%3F

like image 386
aneuryzm Avatar asked Feb 24 '11 13:02

aneuryzm


2 Answers

As far as I remember your class Response should follow the bean convention: should be public, should have public default constructor and should have setters and getters for all fields that you refer from your xml.

like image 54
AlexR Avatar answered Sep 30 '22 18:09

AlexR


Yes it is, as IllegalAccessException documentation says.

You can circumvent access modifiers with reflection. For example, to access private fields, use Class.getDeclaredField(String) to get a specific Field (works also for private fields - using plain getField can only get public members), and then set Field.setAccessible(true). Now the field can be used as if it was public.

You can also circumvent access modifier with JNI. It doesn't care about access modifiers at all. Yet one more way is to generate custom bytecode with a tool such as ASM.

like image 23
Joonas Pulakka Avatar answered Sep 30 '22 18:09

Joonas Pulakka