Reputation: 2901
I have a class whose member is another class object among other members. I would like to know how to determine whether the this field is a java class (user defined). That is, I have something like this: The user passes the name of a java class. If the package contains a class by this name, then I get all the declared fields of this class and do a DFS kind of operation on those data members that are class objects.
class A {
private String c;
private B b;
}
class B {
private int d;
private String f;
}
So now in A, I only have to select B and visit B's members. How to do this ?
Thanks.
Upvotes: 1
Views: 1707
Reputation: 8842
There is one more trick. But results strictly depends on a classloader. It may help you checking if a class is custom user class. Try running this code to determine location of jar:
ProtectionDomain domain = allDeclaredFields[i].getClass().getProtectionDomain();
System.out.println("domain: " + domain);
CodeSource codeSource = domain.getCodeSource();
if(codeSource != null) {
System.out.println("location: " + codeSource.getLocation());
} else {
System.out.println("null location, probably JRE class");
}
Upvotes: 1
Reputation: 15477
If i got your qusestion correctly you are trying to find whether a object belongs to a class or not.There is a java operator called instanceof to check it
if(b instanceof B){
//b is an object of B
}
Upvotes: 0
Reputation: 89
You'll want to use Java reflection. See http://download.oracle.com/javase/tutorial/reflect/index.html for more information (click on members, should be what you need)
Upvotes: 0