Showing posts with label Reflection. Show all posts
Showing posts with label Reflection. Show all posts

Saturday, September 27, 2014

Sample

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

public class ReflectionUpdate {
        private static class InterceptCall
        {
            private Object ObjectToCall1;
            private Object ObjectToCall2;
            private String returnValueString;
            private List<?> returnValueList;

            public InterceptCall(Object ObjectToCall1, Object ObjectToCall2)
            {
                Object ret1 = null;
                Object ret2 = null;
             
                    Method method1 = ObjectToCall1.getClass().getMethod("getString", new Class[0]);
                    ret1 = method1.invoke(ObjectToCall1, new Object[0]);
                    Method method2 = ObjectToCall1.getClass().getMethod("getList", new Class[0]);
                    ret2 = method2.invoke(ObjectToCall2, new Object[0]); //getoredCriteria is called
                    returnValueString = (String)ret1;
                    returnValueList = (List<?>)ret2;
}
            }
       
        ReflectionUpdate(){
           
        }
       public int  updateObjectState( Object originalObject, Object ObjectThatChangeState){
           Method method1 = originalObject.getClass().getMethod("TransformationState", new Class[0]);
           Method methods[] = originalObject.getClass().getMethods();
           String test="abiskar";
           for(int i = 0; i < methods.length; i++)
           {
               Method method = methods[i];
               try
               {
                   if(method.getName().lastIndexOf(test) > 0 && method.getName().startsWith("set"))
                       method.invoke(originalObject, new Object[] {
                           ""
                       });
   
           method1.invoke(originalObject, new Object[] {
               ObjectThatChangeState
           });
         
           InterceptCall parms = new InterceptCall(originalObject,originalObject);
        }
       
}

Sample



public class ClassDemo {

   public static void main(String[] args) {

     try {
        ClassDemo cls = new ClassDemo();
        Class c = cls.getClass();

        // class object associated with BaseClass
        Class bClass = BaseClass.class;

        // checks whether BaseClass is assignable from ClassDemo
        boolean retval = bClass.isAssignableFrom(c);
        System.out.println("Return Value = " + retval);

        // checks whether ClassDemo is assignable from BaseClass
        retval = c.isAssignableFrom(bClass);
        System.out.println("Return Value  = " + retval);
     }
   
     catch(Exception e) {
     System.out.println(e.toString());
     }
   }
}

// base class
class BaseClass extends ClassDemo {

    public BaseClass() {
       // no argument constructor
    }
}

Reflection Example 1

import java.lang.reflect.Field;


public class Reflection {
    String str = "A";
    void test(){
        Class test=    Reflection.class;
        try {
      Field aa=      test.getDeclaredField("str");
      aa.setAccessible(true);
      try {
         
        aa.set(this, "B");
        System.out.println(str);
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
   
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       
    }


public static void main(String[] args) {
    Reflection test=new Reflection();
    test.test();
}
}