1 /*
   2  * Copyright (c) 2007, 2017 Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation. Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  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 }