Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Reflection

  1. #1
    Member
    Join Date
    Jun 2012
    Posts
    41
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Reflection

    I'm making some mods for a game, and I and using java reflection's Field.setAccessible to get fields from classes that I need but weren't originally intended to be accessed. This approach feels a little hacky. :/ I could just modify the access modifier in the code, but this reduces compatibility with other mods, which is a HUGE deal. Is my way fine, or is there another, better way.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Reflection

    You'll have to provide more details. It's difficult to tell based on the info you've given.

    I can imagine two alternatives which may or may not work:

    1. See if you can't make something work with inheritance and polymorphism

    2. See if you can't replicate the results using other means which don't require modifying accessibility.

    As a general rule I avoid reflection like the plague, but sometimes it is necessary.

  3. #3
    Member
    Join Date
    Jun 2012
    Posts
    41
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Reflection

    Well, as I see it...

    1. I can of course override access modifiers with inheritance, however I have no way to put my new class into the system, without severely limiting compatibility.

    2. I have to access an object that I have no prior control over, and I have no other way to get it. I may be overlooking something, but I don't think I am.

    Is that enough to call reflection necessary, or have I still not given enough info? There's no way I know how to get at the object without reflection. It's a very simple set up. A class with a private variable. It's the first time that variable is brought up in the inheritance chain, and there are no sub classes of the class in question.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Reflection

    The biggest question is is the data private? If it is you may want to strongly consider alternative solutions. Private is used for a reason because they want to limit external access to the variable.

    If it is protected or you can gain access to the variable by extending a class (even if it's just a dummy class to gain access to the variable) I would recommend that route.

    There's a good question about the use of setAccessible over at Stack Overflow. Ultimately you'll have to decide if this is something you're willing to do. Personally I avoid reflection whenever possible, sometimes even reducing functionality to do this. However, it is a useful tool, hence its inclusion into Java.

  5. #5
    Member
    Join Date
    Jun 2012
    Posts
    41
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Reflection

    Thanks. As I see it, the data is private, which is probably because it's part of a gui, and shouldn't be edited (which is exactly what I want to do XD). I really hate the idea of reflection, especially because it pretty much let's you do whatever you want. But I am struggling to find another way. Dummy classes are out of the question, as it can't be inherited. So what are my options, cut and dry? Just reflection?

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Reflection

    Pretty much, unless you go native (in this case a worse option than reflection). But be careful doing it and test your results very carefully.

  7. #7
    Member
    Join Date
    Jun 2012
    Posts
    41
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Reflection

    Cool, Thanks! I will continue using reflection then. Thanks for the new outlook.

Similar Threads

  1. Reflection - null pointer
    By bgalakazam in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 3rd, 2010, 06:55 PM
  2. Generics collections and reflection
    By juza in forum Collections and Generics
    Replies: 2
    Last Post: November 30th, 2010, 03:14 AM
  3. Reflection and finalization
    By Dastagiri in forum Java Theory & Questions
    Replies: 1
    Last Post: July 6th, 2010, 11:20 PM
  4. Generics and Reflection
    By Kassiuz in forum Collections and Generics
    Replies: 3
    Last Post: March 15th, 2010, 09:32 AM
  5. running a class from another projects, reflection
    By led1433 in forum Object Oriented Programming
    Replies: 7
    Last Post: July 29th, 2009, 01:47 PM