Reputation: 1407
I'll explain with pictures from Eclipse Debugger
I have an Class called "FieldContext", (I can't edit it, it's compiled in the Java OVal API)
Within "FieldContext" on the eclipse variable tab are "CompileTimeType" and "field"
Q1 Is there a legend for the icons in the variables tab? like what the red box with the "F" means + yellow diamond boxes?
Now I want to access the fields inside the "field" object (RedBox) .. preferably "name"
But the "FieldContext" does not have a "getField()" method, yet it has a "getCompileTimeType()" method.
Q2 So is there anyway to get that field object being a "SerializableField" Class from the "FieldContext"?
If eclipse debugger can see/get/edit them then I hope I can do the same in Java.
Upvotes: 1
Views: 1948
Reputation: 10529
What version of OVal are you using?
I ask because the FieldContext class does have a getField() method:
http://oval.sourceforge.net/api/net/sf/oval/context/FieldContext.html#getField%28%29
The return type is a java.lang.reflect.Field which has a getName() accessor:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/reflect/Field.html#getName%28%29
So you seem to be trying to re-invent the wheel by using a squared boulder :)
Also, unless you are implementing a generic framework, or you need to implement an emergency hack, you should avoid accessing non-public fields with reflection.
Privates are (meant to be) subject to change within the enclosing class; protected ones are subject to change within the inheritance tree; and package-scoped ones are subject to change vertically. The author of such code does not make any guarantees (syntactically and semantically) if his artifacts are accessed via reflection.
Tying your code to them via reflection is a good way to make it brittle and fragile like a pretty petunia blasted by the winder wind of the Siberian tundra ;)
Upvotes: 1
Reputation: 54081
Q1: I think yellow means protected
and red means private
. The "F" means final
.
Q2: That can be done using reflection. Say there is a class C
with a private member field pm
and you want to access instanceOfC.pm
, this is the way to go:
/* exception stuff ommitted (for readability and shortness) */
C instanceOfC = new C();
Class<? extends C> clazz = instanceOfC.getClass();
Field f = clazz.getDeclaredField("pm");
f.setAccessible(true);
... = f.get(instanceOfC); //<-- this will get you the value of `c.pm`
You should probably read the JavaDoc of Class, Field and Method
Upvotes: 3
Reputation: 118734
You need to use the Reflection API. With that you can do almost anything you like with those fields.
I won't discuss whether you should or should not access them, just that you can with reflection.
Upvotes: 1
Reputation: 18900
Q1: The F means "final", so, the contents of the field can not be normally modified by direct access with the code. The yellow diamond means that it is a protected field. The red square means that it is a private field. If it was a green circle it would mean that it is public.
Some info on eclipse debugging, which much more than just the legends:
http://www.ibm.com/developerworks/library/os-ecbug/
Q2: You can use reflection to access any field on a class and invoke any method (if the security settings allow you). Reflection is a technique that allows you to "introspect" any class and access the members by name.
There is several tutorials on the web on how to use reflection:
http://java.sun.com/docs/books/tutorial/reflect/
http://java.sun.com/developer/technicalArticles/ALT/Reflection/
Upvotes: 2