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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package org.jemmy.control; 24 25 26 import java.lang.reflect.Constructor; 27 import java.lang.reflect.InvocationTargetException; 28 import org.jemmy.env.Environment; 29 30 31 /** 32 * This is an implementation of the {@code Wrapper} which instantiates a wrap 33 * of a class returned by {@code getWrapClass(Class)}. 34 * @author shura 35 */ 36 public abstract class AbstractWrapper implements Wrapper { 37 38 private Environment env; 39 40 /** 41 * 42 * @param env 43 */ 44 @SuppressWarnings("unchecked") 45 public AbstractWrapper(Environment env) { 46 this.env = env; 47 } 48 49 /** 50 * 51 * @return 52 */ 53 public Environment getEnvironment() { 54 return env; 55 } 56 57 protected abstract Class<Wrap> getWrapClass(Class controlClass); 58 59 /** 60 * 61 * @param <T> 62 * @param controlClass 63 * @param control 64 * @return Wrap 65 */ 66 public <T> Wrap<? extends T> wrap(Class<T> controlClass, T control) { 67 Class cls = control.getClass(); 68 Class<Wrap> wrp; 69 do { 70 wrp = getWrapClass(cls); 71 if (wrp != null) { 72 try { 73 return doWrap(control, cls, wrp); 74 } catch (InstantiationException ex) { 75 throw new WrapperException(cls, wrp, ex); 76 } catch (IllegalAccessException ex) { 77 throw new WrapperException(cls, wrp, ex); 78 } catch (IllegalArgumentException ex) { 79 throw new WrapperException(cls, wrp, ex); 80 } catch (InvocationTargetException ex) { 81 throw new WrapperException(cls, wrp, ex); 82 } catch (NoSuchMethodException ex) { 83 throw new WrapperException(cls, wrp, ex); 84 } catch (SecurityException ex) { 85 throw new WrapperException(cls, wrp, ex); 86 } 87 } 88 } while ((cls = cls.getSuperclass()) != null); 89 throw new WrapperException(control); 90 } 91 92 /** 93 * 94 * @param <T> 95 * @param control 96 * @param controlClass 97 * @param wrapperClass 98 * @return Wrap 99 * @throws java.lang.NoSuchMethodException 100 * @throws java.lang.InstantiationException 101 * @throws java.lang.IllegalAccessException 102 * @throws java.lang.IllegalArgumentException 103 * @throws java.lang.reflect.InvocationTargetException 104 */ 105 @SuppressWarnings("unchecked") 106 protected <T> Wrap<? extends T> doWrap(T control, Class controlClass, Class wrapperClass) throws NoSuchMethodException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { 107 Constructor cns = null; 108 Class cls = controlClass; 109 do { 110 try { 111 cns = wrapperClass.getConstructor(Environment.class, cls); 112 } catch (NoSuchMethodException e) { 113 } 114 } while ((cls = cls.getSuperclass()) != null); 115 if (cns != null) { 116 return (Wrap<T>) cns.newInstance(new Environment(env), control); 117 } else { 118 throw new WrapperException(controlClass, wrapperClass); 119 } 120 } 121 }