--- /dev/null 2017-12-05 08:24:03.000000000 -0800 +++ new/core/JemmyCore/src/org/jemmy/control/AbstractWrapper.java 2017-12-05 08:24:03.000000000 -0800 @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2007, 2017 Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package org.jemmy.control; + + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import org.jemmy.env.Environment; + + +/** + * This is an implementation of the {@code Wrapper} which instantiates a wrap + * of a class returned by {@code getWrapClass(Class)}. + * @author shura + */ +public abstract class AbstractWrapper implements Wrapper { + + private Environment env; + + /** + * + * @param env + */ + @SuppressWarnings("unchecked") + public AbstractWrapper(Environment env) { + this.env = env; + } + + /** + * + * @return + */ + public Environment getEnvironment() { + return env; + } + + protected abstract Class getWrapClass(Class controlClass); + + /** + * + * @param + * @param controlClass + * @param control + * @return Wrap + */ + public Wrap wrap(Class controlClass, T control) { + Class cls = control.getClass(); + Class wrp; + do { + wrp = getWrapClass(cls); + if (wrp != null) { + try { + return doWrap(control, cls, wrp); + } catch (InstantiationException ex) { + throw new WrapperException(cls, wrp, ex); + } catch (IllegalAccessException ex) { + throw new WrapperException(cls, wrp, ex); + } catch (IllegalArgumentException ex) { + throw new WrapperException(cls, wrp, ex); + } catch (InvocationTargetException ex) { + throw new WrapperException(cls, wrp, ex); + } catch (NoSuchMethodException ex) { + throw new WrapperException(cls, wrp, ex); + } catch (SecurityException ex) { + throw new WrapperException(cls, wrp, ex); + } + } + } while ((cls = cls.getSuperclass()) != null); + throw new WrapperException(control); + } + + /** + * + * @param + * @param control + * @param controlClass + * @param wrapperClass + * @return Wrap + * @throws java.lang.NoSuchMethodException + * @throws java.lang.InstantiationException + * @throws java.lang.IllegalAccessException + * @throws java.lang.IllegalArgumentException + * @throws java.lang.reflect.InvocationTargetException + */ + @SuppressWarnings("unchecked") + protected Wrap doWrap(T control, Class controlClass, Class wrapperClass) throws NoSuchMethodException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + Constructor cns = null; + Class cls = controlClass; + do { + try { + cns = wrapperClass.getConstructor(Environment.class, cls); + } catch (NoSuchMethodException e) { + } + } while ((cls = cls.getSuperclass()) != null); + if (cns != null) { + return (Wrap) cns.newInstance(new Environment(env), control); + } else { + throw new WrapperException(controlClass, wrapperClass); + } + } +}