Posted December 17, 20186 yr 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 December 17, 20186 yr by CorruptedVulture
December 17, 20186 yr 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
December 17, 20186 yr Author 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);
December 17, 20186 yr 4 minutes ago, CorruptedVulture said: So is there any other solution? Like to use the class instance I already have instead of declaring a new one? Well, yes, you need to use the instance you want to update the value of.
December 17, 20186 yr Author Except I can't do that because using the instance I want to change results in this errorjava.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 December 17, 20186 yr by CorruptedVulture
December 17, 20186 yr 15 minutes ago, CorruptedVulture said: //SetMS.invoke(clad, 1); Class != Instance of said class.
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.