views:

53

answers:

3

Guys, I have got a slightly different problem with Java Dynamic class loading. I have to pass an object( lets say Object A1 of class A) to the constructor of a different object B1 of class B, So that the A1's details are stored inside B1. B1 does not know what kind of object it is receiving. So all it knows is that A1 is an object.

Now I have functions in class B, which accept object C1 and check if it is equal to Object A1. So basically I have to check

  1. If object C1 is also of the type class A. ( Actually I am not past this point. )
  2. If the values inside the objects are the same

I tried a solution :

When storing the Object A1 inside B1 , I also store the name of the class "A" since object B1 only gets the object A1 but not the class name A.


public static void testing(Object C1, String s) //testing is the function of B called by B1
  {
      try{
          Class c = Class.forName(s);

          if( C1 instanceof c) // This is throwing error stating c cannot be resolved to a type
          {
              //further checking
          }


      }catch (Exception e){ System.out.println(e);}
  }

I have been trying different code to get the class type to make a instanceof () comparision but I am not successful . Any suggestion ?

I even tried this but same error


ClassLoader classLoader = B.class.getClassLoader();

      try{
          Class class11 = classLoader.loadClass(s);
          if ( C1 instanceof class11 )
          {

          }
          }catch (Exception e){ System.out.println(e);}

Any pointer on how to proceed would be very helpful !

Solution is to use : isInstance instead of instanceof

There is also other issues attached with this problem . Let me update it with my below comment. I, now, have to compare a value , say AGE, in both C1 and A1, which inside B1. Now How do I do that ? If I try the functions or values , I will give me compiler error because the compiler is still oblivious of the class type of the object C1 and A1

Also I could have saved X1 instead of A1 in the first place. Now I might have to check for a different property check like ADDRESS. This changes the dimension of the problem drastically.

+4  A: 

instanceof operator works only with staticly loaded classes. For dynamically loaded classes, use the isInstance method on the Class class.

if (class11.isInstance(C1)) {
    // do something
}
abhin4v
@abhin4v: Thank you very much. Been breaking my head for almost a day.
bsoundra
I have another query. Dont know If it is ok to post it here. Since it is relevant to the above I shall post it here. If any problem, I shall open a new questionI now have to compare a value , say AGE, in both C1 and A1, which inside B1. Now How do I do that ? If I try the functions or values , I will give me compiler error because the compiler is still oblivious of the class type of the object C1 and A1.
bsoundra
Open a new question.
Stephen C
+2  A: 

Use Class.isInstance() instead of the instanceof operator:

if (c.isInstance(C1)) {
    [...]
}

Note:

  • The instanceof operator can only be used on types (which are known at compile time).
  • The isInstance() method is called on java.lang.Class objects.
Grodriguez
+1  A: 

instanceof cunstruction requires type, not reference to class object:

if ( C1 instanceof MyClass ) {
  // ...
}

You may use:

if (c.isnstance(C1)) {
  // ...
}
Kel