< prev index next >

core/JemmyCore/src/org/jemmy/control/AbstractWrapper.java

Print this page




  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package org.jemmy.control;
  26 
  27 
  28 import java.lang.reflect.Constructor;
  29 import java.lang.reflect.InvocationTargetException;
  30 import org.jemmy.env.Environment;
  31 
  32 
  33 /**
  34  * This is an implementation of the {@code Wrapper} which instantiates a wrap
  35  * of a class returned by {@code getWrapClass(Class)}.
  36  * @author shura
  37  */
  38 public abstract class AbstractWrapper implements Wrapper {
  39 
  40     private Environment env;
  41 
  42     /**
  43      *
  44      * @param env
  45      */
  46     @SuppressWarnings("unchecked")
  47     public AbstractWrapper(Environment env) {
  48         this.env = env;
  49     }
  50 
  51     /**
  52      *
  53      * @return
  54      */
  55     public Environment getEnvironment() {
  56         return env;
  57     }
  58 
  59     protected abstract Class<Wrap> getWrapClass(Class controlClass);
  60 
  61     /**
  62      *
  63      * @param <T>
  64      * @param controlClass
  65      * @param control
  66      * @return Wrap
  67      */
  68     public <T> Wrap<? extends T> wrap(Class<T> controlClass, T control) {
  69         Class cls = control.getClass();
  70         Class<Wrap> wrp;
  71         do {
  72             wrp = getWrapClass(cls);
  73             if (wrp != null) {
  74                 try {
  75                     return doWrap(control, cls, wrp);
  76                 } catch (InstantiationException ex) {
  77                     throw new WrapperException(cls, wrp, ex);
  78                 } catch (IllegalAccessException ex) {
  79                     throw new WrapperException(cls, wrp, ex);
  80                 } catch (IllegalArgumentException ex) {
  81                     throw new WrapperException(cls, wrp, ex);
  82                 } catch (InvocationTargetException ex) {
  83                     throw new WrapperException(cls, wrp, ex);
  84                 } catch (NoSuchMethodException ex) {
  85                     throw new WrapperException(cls, wrp, ex);
  86                 } catch (SecurityException ex) {
  87                     throw new WrapperException(cls, wrp, ex);
  88                 }
  89             }
  90         } while ((cls = cls.getSuperclass()) != null);
  91         throw new WrapperException(control);
  92     }
  93 
  94     /**
  95      *
  96      * @param <T>
  97      * @param control
  98      * @param controlClass
  99      * @param wrapperClass
 100      * @return Wrap
 101      * @throws java.lang.NoSuchMethodException
 102      * @throws java.lang.InstantiationException
 103      * @throws java.lang.IllegalAccessException
 104      * @throws java.lang.IllegalArgumentException
 105      * @throws java.lang.reflect.InvocationTargetException
 106      */
 107     @SuppressWarnings("unchecked")
 108     protected <T> Wrap<? extends T> doWrap(T control, Class controlClass, Class wrapperClass) throws NoSuchMethodException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
 109         Constructor cns = null;
 110         Class cls = controlClass;
 111         do {
 112             try {
 113                 cns = wrapperClass.getConstructor(Environment.class, cls);
 114             } catch (NoSuchMethodException e) {
 115             }
 116         } while ((cls = cls.getSuperclass()) != null);
 117         if (cns != null) {
 118             return (Wrap<T>) cns.newInstance(new Environment(env), control);
 119         } else {
 120             throw new WrapperException(controlClass, wrapperClass);
 121         }
 122     }
 123 }


  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package org.jemmy.control;
  26 
  27 
  28 import java.lang.reflect.Constructor;
  29 import java.lang.reflect.InvocationTargetException;
  30 import org.jemmy.env.Environment;
  31 
  32 
  33 /**
  34  * This is an implementation of the {@code Wrapper} which instantiates a wrap
  35  * of a class returned by {@code getWrapClass(Class)}.
  36  * @author shura
  37  */
  38 public abstract class AbstractWrapper implements Wrapper {
  39 
  40     private Environment env;
  41 





  42     public AbstractWrapper(Environment env) {
  43         this.env = env;
  44     }
  45 




  46     public Environment getEnvironment() {
  47         return env;
  48     }
  49 
  50     protected abstract Class<Wrap> getWrapClass(Class controlClass);
  51 







  52     public <T> Wrap<? extends T> wrap(Class<T> controlClass, T control) {
  53         Class cls = control.getClass();
  54         Class<Wrap> wrp;
  55         do {
  56             wrp = getWrapClass(cls);
  57             if (wrp != null) {
  58                 try {
  59                     return doWrap(control, cls, wrp);
  60                 } catch (InstantiationException ex) {
  61                     throw new WrapperException(cls, wrp, ex);
  62                 } catch (IllegalAccessException ex) {
  63                     throw new WrapperException(cls, wrp, ex);
  64                 } catch (IllegalArgumentException ex) {
  65                     throw new WrapperException(cls, wrp, ex);
  66                 } catch (InvocationTargetException ex) {
  67                     throw new WrapperException(cls, wrp, ex);
  68                 } catch (NoSuchMethodException ex) {
  69                     throw new WrapperException(cls, wrp, ex);
  70                 } catch (SecurityException ex) {
  71                     throw new WrapperException(cls, wrp, ex);
  72                 }
  73             }
  74         } while ((cls = cls.getSuperclass()) != null);
  75         throw new WrapperException(control);
  76     }
  77 














  78     protected <T> Wrap<? extends T> doWrap(T control, Class controlClass, Class wrapperClass) throws NoSuchMethodException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
  79         Constructor cns = null;
  80         Class cls = controlClass;
  81         do {
  82             try {
  83                 cns = wrapperClass.getConstructor(Environment.class, cls);
  84             } catch (NoSuchMethodException e) {
  85             }
  86         } while ((cls = cls.getSuperclass()) != null);
  87         if (cns != null) {
  88             return (Wrap<T>) cns.newInstance(new Environment(env), control);
  89         } else {
  90             throw new WrapperException(controlClass, wrapperClass);
  91         }
  92     }
  93 }
< prev index next >