🔒 Locked
[java] dynamic object identification problem
Advertisement
Original Post
ok, I have a class that takes 3 inputs. Variable(NAME, TYPE, DATA); the name is a string, the type is a string, however the DATA is where it gets tricky. DATA depends on what the string TYPE is. Example if TYPE is a String DATA isa String if TYPE is a VectorObject DATA isa VectorObject you get the idea right? Now, here's the problem. I don't know what to identify DATA as. It's type is determined at runtime, not compiletime, so I don't know what to define it as, since a string cannot be coverted into VectorObject, plus i can't keep redefining DATA. Example: if TYPE isa VectorObject DATA isa VectorObject would be shown as VectorObject DATA = new VectorObject(); the problem arises when i try to store or return the DATA object! public GetData { return DATA;} There's got to be a better way to do this or something small that im missing. like an interface.. but i dont know how to implment the interface if so.... btw this is for a script system, which reads the name,type,value fromthe file...
Here's what you do:
You make DATA an Object. Use instanceof to determine it's runtime type.
For example:
You make DATA an Object. Use instanceof to determine it's runtime type.
For example:
// Variable.javapublic class Variable { private String NAME; private String TYPE; private Object DATA; //data can be any type public Variable(String name, String type) { NAME = name; TYPE = type; if (type.equals("String")) DATA = ""; else DATA = new VectorObject(); } public Object getData() { return DATA; }}// Some class where you use the Variable classpublic class Thing { public static void main(String[] args) { Variable v = new Variable("Kevin", "String"); Object o = v.getData(); //this is the important part if (o instanceof String) //treat o like a String if (o instanceof VectorObject) //treat o like a VectorObject }}
Sounds like you need to do some templating, assuming C++.
I'm kind of new to this, but this should work (or some variation).
Others will throw in my corrections...?
EDIT: You said runtime...? Above post would work.
I'm kind of new to this, but this should work (or some variation).
Others will throw in my corrections...?
template<typename T>T& GetData(string NAME, string TYPE, T DATA){ .... return T;}EDIT: You said runtime...? Above post would work.
BTW: Please note that the nomenclature I will use in the following is definitely different from yours but is more standard. Please take into account to use those terms in the future.)
The most common type an _argument_ of a _method_ may have in Java is Object. It can be anything but the simple data types char, byte, short, int, float, or double. So the signature of the method in question may be:
Even if you want to pass e.g. an int, you could wrap that by an Integer object (for the other simple types also such wrapper classes exist; see the java.lang package for them), so that you become compatible to the method's interface.
If you pass in an Object, you could try to determine its runtime type by the "instanceof" operator, or by fetching its class and investigating its name.
However, if the overhanded argument always denotes a variable of the script system, then you may take into account to define a basic class Variable, and to derive the more elaborate variable types by inheritance.
And so on for other types...
Then you may pass objects of the Variable type and may investigate the runtime type this way:
Of course, it would be better to use polymorphism to avoid investigation of the runtime type anyway!!
BTW: Please don't use all capital letters for something else than static final fields.
EDIT: Damned, beaten two times :)
The most common type an _argument_ of a _method_ may have in Java is Object. It can be anything but the simple data types char, byte, short, int, float, or double. So the signature of the method in question may be:
// for input onlyvoid myMethod(String name, String type, Object data);// for output onlyObject myMethod(String name, String type);Even if you want to pass e.g. an int, you could wrap that by an Integer object (for the other simple types also such wrapper classes exist; see the java.lang package for them), so that you become compatible to the method's interface.
If you pass in an Object, you could try to determine its runtime type by the "instanceof" operator, or by fetching its class and investigating its name.
However, if the overhanded argument always denotes a variable of the script system, then you may take into account to define a basic class Variable, and to derive the more elaborate variable types by inheritance.
public abstract class Variable extends Object { public Variable(String withName) { this.name = withName; } public String getName() { return this.name; } private String name; public abstract void setValueFrom(String newValue); public abtract Object getValue();}public class IntegerVariable extends Variable { IntegerVariable(String withName) { super(withName); } public void setValueFrom(String newValue) { this.value = new Integer(newValue); } public Object getValue() { return this.value; } private Integer value;}And so on for other types...
Then you may pass objects of the Variable type and may investigate the runtime type this way:
void anyMethodUsingAVariable(Variable aVariable) { if(aVariable instanceof Integer) { final Integer anInteger = (Integer)aVariable; // ... } else { // ... }}Of course, it would be better to use polymorphism to avoid investigation of the runtime type anyway!!
BTW: Please don't use all capital letters for something else than static final fields.
EDIT: Damned, beaten two times :)
see, now all those ideas are great- and polymorphism would be an excellent idea if i knew how to implement it.
However, please keep in mind that i'm a C programmer moving to java, so OOP is somewhat difficultfor me to get used to since i'm a procedurally based moron...
Variable is really only meant as a container class, to contain the variables taken from the file in the format:
\/String \/String \/Dependant on what stringType says data is.
varName varType varData
In the most basic sense, I'm unsure of how to store the varData properly.
Variable class is meant only as a container for each ofthe values.
It properly determines what type Data should be then holds it in memory(to be contained in the Variable class).
I store several Variable objects in an ArrayList so i can access the values during program execution.
pretty much for easy access, so i can just scan the arraylist for the properly named variable or type and apply it to a variable at runtime.
EX:
//camera vector
Vector3D camera_facing;
camera_facing=ArrayList.searchforVariablewithName("camera_facing").getData();
that's the basic logic i have for that.Now in C all that would have been done with various pointers, and iterator, and alot of fscanf'ing... so i'm trying really hard to work with OOP...
when i pass 3 values to class Variable's constructor Variable(String name, String type, Object Data) the likelyhood of Data being a float,integer,string,Vector3D,Point3D object is high -- so i don't know if making it an Object will work (as you pointed out before...).
You all have given me great ideas for the completion of my task and i appreciate your help.Any more suggestions on my logic or system would be greatly appreciated, if you'd like to see the source to see my idea, please just ask and i'll post it.
Thanks again!
However, please keep in mind that i'm a C programmer moving to java, so OOP is somewhat difficultfor me to get used to since i'm a procedurally based moron...
Variable is really only meant as a container class, to contain the variables taken from the file in the format:
\/String \/String \/Dependant on what stringType says data is.
varName varType varData
In the most basic sense, I'm unsure of how to store the varData properly.
Variable class is meant only as a container for each ofthe values.
It properly determines what type Data should be then holds it in memory(to be contained in the Variable class).
I store several Variable objects in an ArrayList so i can access the values during program execution.
pretty much for easy access, so i can just scan the arraylist for the properly named variable or type and apply it to a variable at runtime.
EX:
//camera vector
Vector3D camera_facing;
camera_facing=ArrayList.searchforVariablewithName("camera_facing").getData();
that's the basic logic i have for that.Now in C all that would have been done with various pointers, and iterator, and alot of fscanf'ing... so i'm trying really hard to work with OOP...
when i pass 3 values to class Variable's constructor Variable(String name, String type, Object Data) the likelyhood of Data being a float,integer,string,Vector3D,Point3D object is high -- so i don't know if making it an Object will work (as you pointed out before...).
You all have given me great ideas for the completion of my task and i appreciate your help.Any more suggestions on my logic or system would be greatly appreciated, if you'd like to see the source to see my idea, please just ask and i'll post it.
Thanks again!
All your worries would vanish if you would switch to Java 1.5.
Autoboxing will solve that problem for you automatically.
In Java 1.5, stuff like this is legal:
In other flavors of Java, it's a simple matter of doing this yourself:
[Edited by - Kevinator on December 21, 2005 4:11:29 AM]
Autoboxing will solve that problem for you automatically.
In Java 1.5, stuff like this is legal:
public class Thing { public static void main(String[] args) { int n = doSomething(10); } private static int doSomething(Object o) { if(o instanceof Integer) //Java automatically wraps the int into the Integer class so it is an object return 5 + o.intValue(); }}In other flavors of Java, it's a simple matter of doing this yourself:
public class Thing { public static void main(String[] args) { int n = doSomething(new Integer(10)); } private static int doSomething(Object o) { if(o instanceof Integer) return 5 + o.intValue(); }}[Edited by - Kevinator on December 21, 2005 4:11:29 AM]
Quote:
Original post by theclown
pretty much for easy access, so i can just scan the arraylist for the properly named variable or type and apply it to a variable at runtime.
EX:
//camera vector
Vector3D camera_facing;
camera_facing=ArrayList.searchforVariablewithName("camera_facing").getData();
This can't work in general. You cannot assign a general type to a more specific type, only vice-versa. You could follow one of these ways to work-around it (but notice that the class ArrayList is one of Java and does not support searchforVariablewithName directly, so I've used a static method of class Variable in the following):
(1) Type-casting at assignment (requires 1 getter only):
camera_facing = (Vector3D)Variable.searchForName("camera_facing").getData();
(2) Type-casting inside getter (requires several getters, one for each type):
camera_facing = Variable.searchForName("camera_facing").getDataAsVector3D();
(3) Convenient type-casting in static getter (requires several getters, one for each type):
camera_facing = Variable.getVector3D("camera_facing");
yeah, im still trying to get used to all that object stuff for java, in C
i could just define the variable as
void data;
then just assignit a value later,
data=new bool;
I thought java had that form too, so it was really just my ignorance of java that is causing this problem :)
Thanks alot you guys for your advice, it's extremely helpful!
Moving from procedural to OOP s tougher than i thought it wouldbe :)
But also, if i were to have a static method in Variable to search for the specific type of data, would I have to define the ArrayList in Variable itself??
See i defined the ArrayList in the Script class, so i'm assuming it's not possible to code a method from Variable to modify the arraylist in Script(or search the arraylist for that matter).
i could just define the variable as
void data;
then just assignit a value later,
data=new bool;
I thought java had that form too, so it was really just my ignorance of java that is causing this problem :)
Thanks alot you guys for your advice, it's extremely helpful!
Moving from procedural to OOP s tougher than i thought it wouldbe :)
But also, if i were to have a static method in Variable to search for the specific type of data, would I have to define the ArrayList in Variable itself??
See i defined the ArrayList in the Script class, so i'm assuming it's not possible to code a method from Variable to modify the arraylist in Script(or search the arraylist for that matter).
Quote:
But also, if i were to have a static method in Variable to search for the specific type of data, would I have to define the ArrayList in Variable itself??
Absolutely not.
Quote:
See i defined the ArrayList in the Script class, so i'm assuming it's not possible to code a method from Variable to modify the arraylist in Script(or search the arraylist for that matter).
Your assumption is incorrect. This is definitely possible, because objects in Java are passed by reference. Although since we're not modifying the arraylist, it doesn't matter wether it's passed by reference or not. However, since objects in Java are passed by reference, you could actually modify the arraylist from outside the class quite easily. Here's what the method in Variable would look like:
import java.util.*;public class Variable { public static Object getVariableWithName(String name, ArrayList list) { for (Variable v : list) if (v.getName().equals(name)) return v.getData(); return null; }}This is how you would invoke the method if your ArrayList is a private member of the class Script:
public class Main { public static void main(String[] args) { Script s = new Script(); //... Object var = Variable.getVariableWithName("camera", s.getList()); //here we pass the name we want to find and also we pass the arraylist you want to search, which happens to be a member of object s. You'll need to code a method in class Script that returns the list, or if the list is public, you can just refer to it directly. doStuff(var); }}[Edited by - Kevinator on December 21, 2005 6:29:03 PM]
Quote:
Original post by theclown
yeah, im still trying to get used to all that object stuff for java, in C
i could just define the variable as
void data;
then just assignit a value later,
data=new bool;
I thought java had that form too, so it was really just my ignorance of java that is causing this problem :)
Thanks alot you guys for your advice, it's extremely helpful!
Moving from procedural to OOP s tougher than i thought it wouldbe :)
But also, if i were to have a static method in Variable to search for the specific type of data, would I have to define the ArrayList in Variable itself??
See i defined the ArrayList in the Script class, so i'm assuming it's not possible to code a method from Variable to modify the arraylist in Script(or search the arraylist for that matter).
You might want to use Reflection to do this. However, if you are a beginner in Java, Reflection can seem complicated. But, I think reflection is the way to go.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement