Bump in the hope someone might actually respond
Being new at Java programming, please bear with me
Using reflection, I run into a problem I hope someone can help me with...
Field f = someClass.getDeclaredField("fieldName");
f.setaccessible(true);
Object fieldValue = f.get(someClass);
This is possible, but what if I don't want to use object but cast the result of f.get to a class... but this class is unknown till runtime; is that possible to do, and how should it be done?
Class x = anotherClass.newInstance();
x objectX = (x) f.get(someClass);
is not possible, but indicates what i am trying to do...
help please...
and...
don't flame the noob
Similar Threads:
By saying 'is not possible, but indicated what I am trying to do...' do you mean not possible as in it gives you an error?
I would try something like
x objectX = (x) fieldValue;
Not sure off the top of my head though.
Actually it is not possible to cast to class x
Class x = anotherClass.newInstance();
x objectX = (x) f.get(someClass);
.................^
The compiler reports "cannot find the Symbol Class x in class main"
This is what I got. I don't see a reason to do this at all nonetheless. f.get would be of type object, and this can be cast, but even converting it to a runtime cast doesn't really make any difference since you cannot operate on it. It is nothing more than an Object, and with no known type in this way you wouldn't be able to compile the code if you attempt to operate on it if its a different type.
Field will have a type associated with it though, so you can pull that off of getType. That can be used in an if/elseif check to determine how to pull the data out or cast as appropriate.
Thanks Cybernator, this is what i found as well.
Would there be a way around it?
I am trying to dynamically access the fields and classes of ypp (similar to what has been done in Omnis and the riggingbot).
The thing I am trying to prevent is that when I access a field of a certain type, that the typename is changed (due to obfuscation) when OOO releases an update.
Finding the required fields is not the issue, the problem is -as you stated- that i cannot access the methods/fields of the found fieldType... casting at runtime does not solve that problem, but what can be done?
Not sure. I'm not that good at Java anyway, so I recomend asking someone else who is better. Sorry
"Class x = ....."
"x objectX = ...."
Please tell me your joking...
x is merely a variable to hold a particular value; it's not an object-type, hence you can not initialise a new object of type "x".
If you are trying to access the fields, then why would you create a new instance of the object? I would find it more appropriate to get the object that the game is using.
I'm not going to tell you how to access all this, and I'd hope Pokerking would refrain from doing the same (as it's not exactly his code)
I think you should learn the basics of Java before you go messing around with Reflection
Last edited by Devereux; 01-07-2012 at 12:43 PM.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Design Sites:
vBulletin Mods
Facebook Covers | Free Facebook Covers
Google Covers | Google Plus Covers
Game Sites:
MMORPG | MW3