This discussion is archived
9 Replies Latest reply: Apr 17, 2010 2:46 PM by jschellSomeoneStoleMyAlias RSS

Setting static field value via reflection

843798 Newbie
Currently Being Moderated
I'm currently trying to set a static field's value before the static initialization block runs using reflection. Is this possible? If so, how?
  • 1. Re: Setting static field value via reflection
    843798 Newbie
    Currently Being Moderated
    Static block are executed when a class is loaded into JRE, hence it seems not possible to do that - when You start the app static blocks will be already executed. Maybe describe in a few words what You'd like to achieve?
  • 2. Re: Setting static field value via reflection
    EJP Guru
    Currently Being Moderated
    Is this possible?
    No. It's a contradiction in terms. Using the class in any way first initializes it, which executes the static block.

    What are you really trying to accomplish?
  • 3. Re: Setting static field value via reflection
    843798 Newbie
    Currently Being Moderated
    I have a dll inside a jar file. I extract the dll to a "unique" directory and try to load it via System.load(). I've tried to use System.load outside a static block, but it doesn't load the dll. For some reason, I can only load the dll from a static block. Hence, I tried using reflection to set a static member field before the static initialization block ran.

    My Requirements are (for dynamic plugins):
    1) The dll must be in a unique location on the filesystem (i.e. its individual plugin directory)
    2) The class that loads the dll must be instantiated via reflection
  • 4. Re: Setting static field value via reflection
    843798 Newbie
    Currently Being Moderated
    Make the stuff rely upon another class. Make sure to initalize it (possibly setting a static member) before referring to the depending class, which uses the dll.
  • 5. Re: Setting static field value via reflection
    796085 Newbie
    Currently Being Moderated
    billmanhillman wrote:
    I have a dll inside a jar file. I extract the dll to a "unique" directory and try to load it via System.load(). I've tried to use System.load outside a static block, but it doesn't load the dll. For some reason, I can only load the dll from a static block. Hence, I tried using reflection to set a static member field before the static initialization block ran.
    There is no reason whatsoever that System.load() should only work inside a static block. I suspect you're doing something else wrong.

    It's not clear from the requirements what variable you need to set and/or why?
  • 6. Re: Setting static field value via reflection
    843798 Newbie
    Currently Being Moderated
    So, can the System.load() option be invoked from an instance object created via reflection? When I try this, it cannot find the method signatures in the dll (unsatisfiedlinkerror). Please note, the System.load() option I was trying uses a variable for the dll to load. That's what really complicates this issue.
  • 7. Re: Setting static field value via reflection
    EJP Guru
    Currently Being Moderated
    There is no reason whatsoever that System.load() should only work inside a static block.
    You must load the DLL before you call any native methods. If the class has static native methods the only bulletproof solution to that is loading it in a static initializer block.
  • 8. Re: Setting static field value via reflection
    843798 Newbie
    Currently Being Moderated
    In my situation, there aren't any static native methods, only one non-static native method. So, loading via non-reflexive calls finds the native methods. Loading via reflection works, but can't find the native methods when a java class calls the native method. Very strange.
  • 9. Re: Setting static field value via reflection
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    billmanhillman wrote:
    In my situation, there aren't any static native methods, only one non-static native method. So, loading via non-reflexive calls finds the native methods. Loading via reflection works, but can't find the native methods when a java class calls the native method. Very strange.
    When you load a class, regardless of how that occurs, it gets initialized. If initialization fails then the class load fails. This can be demonstrated by having a static initializer throw an exception. That will cause the load to fail.

    What you have described above suggests, strongly, that you are failing to load the dll. Two (perhaps only) reasons for that.
    1. The code that is doing the load is not in fact running. Perhaps due to a conditional check.
    2. The load fails but you eat the exception.

    Natually standard failure reasons also apply such as loading the wrong (older) dll.