Jump to content

Can't set variable with Java Reflection


CorruptedVulture

Recommended Posts

Using Java 8, Forge 1.12.2
I've been trying to use reflection to change a private variable in another class, I'm able to get the class, and the variable,


*I've tried to use the public set method to change the variable, that doesn't work, and ends in this error:

java.lang.IllegalArgumentException: object is not an instance of declaring class


*I've tried changing the variable to accesible = true and then changing it, resulting in this error:

java.lang.IllegalArgumentException: Can not set int field playerProperties.VanillaSpellsCapability.mirrorspellSpread to java.lang.Class

 

The Code:

Class clad = Class.forName(message.cladName);
                
                Field MSS = clad.getDeclaredField(message.mirrorspellSpread);
                
                MSS.setAccessible(true);

                MSS.set(clad, 1);
                //Method SetMS = clad.getMethod("setIsMSSpreadOut", Integer.TYPE);
                //SetMS.invoke(clad, 1);

Edited by CorruptedVulture
Link to comment
Share on other sites

8 minutes ago, CorruptedVulture said:

*I've tried to use the public set method to change the variable, that doesn't work, and ends in this error:

java.lang.IllegalArgumentException: object is not an instance of declaring class

If the method is public then you don't need reflection. Anyway, as the error clearly states you are passing an object as an instance of the class while the object in question isn't an instance of said class. This is one of reflection's basics.

 

9 minutes ago, CorruptedVulture said:

java.lang.IllegalArgumentException: Can not set int field playerProperties.VanillaSpellsCapability.mirrorspellSpread to java.lang.Class

Same here - you are passing a Class as the instance, which will never be the case. 

 

https://www.oracle.com/technetwork/articles/java/javareflection-1536171.html

 

Link to comment
Share on other sites

Ok, I tried with the new instance by declaring a new object, and setting it to be the class's new instance, and my intentions with this was to update a capability variable on the client-side, however it's just as I thought that making a new instance wouldn't affect the player's capability.
So is there any other solution? Like to use the class instance I already have instead of declaring a new one?

The new Code:
 

Class clad = Class.forName(message.cladName);
                Object cc = clad.newInstance();
                //Field MSS = clad.getDeclaredField(message.mirrorspellSpread);
                /*
                MSS.setAccessible(true);

                MSS.set(clad, 1);*/

                Method SetMS = clad.getMethod("setIsMSSpreadOut", Integer.TYPE);
                SetMS.invoke(cc, 1);

Link to comment
Share on other sites

Except I can't do that because using the instance I want to change results in this error
java.lang.IllegalArgumentException: object is not an instance of declaring class
Using this code
//Method SetMS = clad.getMethod("setIsMSSpreadOut", Integer.TYPE);

//SetMS.invoke(clad, 1);

Keep in mind, not declaring a new object, like here

 Object cc = clad.newInstance();

So then is there anything else I can do, to use the instance I got?

Edited by CorruptedVulture
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.