1 Reply Latest reply on May 28, 2004 5:21 AM by aloubyansky

    a question about JDBCCreateBeanClassInstanceCommand

    liu_linux

      Hi, I am reading JBoss 3.2.3 and have a question about the constructor of class JDBCCreateBeanClassInstanceCommand.
      There are follow statements in the constructor:
      beanClass = theContainer.getBeanClass();
      fieldMap = createFieldMap();
      selectorMap = createSelectorMap();
      // use proxy generator to create one implementation
      EntityBridgeInvocationHandler handler = new EntityBridgeInvocationHandler(
      theContainer,
      fieldMap,
      selectorMap,
      beanClass);
      Class[] classes = new Class[] { beanClass };
      ClassLoader classLoader = beanClass.getClassLoader();

      Object o = Proxy.newProxyInstance(classLoader, classes, handler);

      I don?t understand why Proxy.newProxyInstance() executes successfully. Because the second parameter should be an array only contains Interface type. But in fact, the beanClass is a class type. I have tried a simple example, which is in the same structure with previous statements but encountered exception, here are details:

      import java.lang.reflect.*;

      interface EntityBean
      {
      public void fun();
      }

      abstract class BusinessLogicAbstractClass implements EntityBean
      {
      public abstract void abstract_fun();
      }


      class StandardInvocationHandler implements InvocationHandler
      {
      public Object invoke( Object proxy, Method method, Object[] args)
      {
      System.out.println( "invoke "+ proxy.getClass());
      System.out.println( " Method [" + method.toString()+"]");
      return null;
      }
      }

      public class Main
      {
      public static void main(String[] args)
      {
      Main obj = new Main();
      obj.fun();
      }

      public void fun()
      {

      Class beanClass = BusinessLogicAbstractClass.class;

      System.out.println(beanClass);

      InvocationHandler ih = new StandardInvocationHandler();

      Class[] classes = new Class[] { beanClass };
      ClassLoader classLoader = beanClass.getClassLoader();

      Object o = (BusinessLogicAbstractClass)Proxy.newProxyInstance(classLoader, classes, ih);

      try{
      Constructor beanProxyConstructor = o.getClass().getConstructor(new Class[]{InvocationHandler.class});

      StandardInvocationHandler handler = new StandardInvocationHandler();

      BusinessLogicAbstractClass bli =(BusinessLogicAbstractClass) (beanProxyConstructor.newInstance(new Object[]{handler}));

      bli.fun();
      bli.abstract_fun();
      }catch(Exception e){
      System.out.println("exception");
      }
      }
      }

      I:\java>java Main
      class BusinessLogicAbstractClass
      Exception in thread "main" java.lang.IllegalArgumentException: BusinessLogicAbstractClass is not an interface
      at java.lang.reflect.Proxy.getProxyClass(Proxy.java:340)
      at java.lang.reflect.Proxy.newProxyInstance(Proxy.java:552)
      at Main.fun(Main.java:44)
      at Main.main(Main.java:29)

      Could you tell me what?s wrong with the code? I think it is in the same structure with JDBCCreateBeanClassInstanceCommand?s.
      Any help is appreciated!

        • 1. a question about JDBCCreateBeanClassInstanceCommand
          aloubyansky

          Hi, I am reading JBoss 3.2.3 and have a question about the constructor of class JDBCCreateBeanClassInstanceCommand.
          There are follow statements in the constructor:
          beanClass = theContainer.getBeanClass();
          fieldMap = createFieldMap();
          selectorMap = createSelectorMap();
          // use proxy generator to create one implementation
          EntityBridgeInvocationHandler handler = new EntityBridgeInvocationHandler(
          theContainer,
          fieldMap,
          selectorMap,
          beanClass);
          Class[] classes = new Class[] { beanClass };
          ClassLoader classLoader = beanClass.getClassLoader();

          Object o = Proxy.newProxyInstance(classLoader, classes, handler);

          I don?t understand why Proxy.newProxyInstance() executes successfully. Because the second parameter should be an array only contains Interface type. But in fact, the beanClass is a class type. I have tried a simple example, which is in the same structure with previous statements but encountered exception, here are details:

          import java.lang.reflect.*;

          interface EntityBean
          {
          public void fun();
          }

          abstract class BusinessLogicAbstractClass implements EntityBean
          {
          public abstract void abstract_fun();
          }


          class StandardInvocationHandler implements InvocationHandler
          {
          public Object invoke( Object proxy, Method method, Object[] args)
          {
          System.out.println( "invoke "+ proxy.getClass());
          System.out.println( " Method [" + method.toString()+"]");
          return null;
          }
          }

          public class Main
          {
          public static void main(String[] args)
          {
          Main obj = new Main();
          obj.fun();
          }

          public void fun()
          {

          Class beanClass = BusinessLogicAbstractClass.class;

          System.out.println(beanClass);

          InvocationHandler ih = new StandardInvocationHandler();

          Class[] classes = new Class[] { beanClass };
          ClassLoader classLoader = beanClass.getClassLoader();

          Object o = (BusinessLogicAbstractClass)Proxy.newProxyInstance(classLoader, classes, ih);

          try{
          Constructor beanProxyConstructor = o.getClass().getConstructor(new Class[]{InvocationHandler.class});

          StandardInvocationHandler handler = new StandardInvocationHandler();

          BusinessLogicAbstractClass bli =(BusinessLogicAbstractClass) (beanProxyConstructor.newInstance(new Object[]{handler}));

          bli.fun();
          bli.abstract_fun();
          }catch(Exception e){
          System.out.println("exception");
          }
          }
          }

          I:\java>java Main
          class BusinessLogicAbstractClass
          Exception in thread "main" java.lang.IllegalArgumentException: BusinessLogicAbstractClass is not an interface
          at java.lang.reflect.Proxy.getProxyClass(Proxy.java:340)
          at java.lang.reflect.Proxy.newProxyInstance(Proxy.java:552)
          at Main.fun(Main.java:44)
          at Main.main(Main.java:29)

          Could you tell me what?s wrong with the code? I think it is in the same structure with JDBCCreateBeanClassInstanceCommand?s.
          Any help is appreciated!