
ru.stqa.selenium.wrapper.AbstractWrapper Maven / Gradle / Ivy
/*
* Copyright 2013 Alexei Barantsev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package ru.stqa.selenium.wrapper;
import java.lang.reflect.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public abstract class AbstractWrapper implements WrapsSomething {
private T original;
private final WebDriverWrapper driverWrapper;
public AbstractWrapper(final WebDriverWrapper driverWrapper, final T original) {
this.original = original;
if (this instanceof WebDriverWrapper) {
this.driverWrapper = (WebDriverWrapper) this;
} else {
this.driverWrapper = driverWrapper;
}
}
public final T getWrappedOriginal() {
return original;
}
protected void setWrappedOriginal(final T original) {
this.original = original;
}
public WebDriverWrapper getDriverWrapper() {
return driverWrapper;
}
/**
* Builds a {@link java.lang.reflect.Proxy} implementing all interfaces of original object. It will delegate calls to
* wrapper when wrapper implements the requested method otherwise to original object.
*
* @param driverWrapper the underlying driver's wrapper
* @param original the underlying original object
* @param wrapperClass the class of a wrapper
* @return a proxy that wraps the original object
*/
public static T wrapOriginal(final WebDriverWrapper driverWrapper, final T original, final Class extends AbstractWrapper> wrapperClass) {
AbstractWrapper wrapper;
Constructor extends AbstractWrapper> constructor = null;
if (driverWrapper == null) { // top level WebDriverWrapper
constructor = findMatchingConstructor(wrapperClass, original.getClass());
if (constructor == null) {
throw new Error("Wrapper class " + wrapperClass + " does not provide an appropriate constructor");
}
try {
wrapper = constructor.newInstance(original);
} catch (Exception e) {
throw new Error("Can't create a new wrapper object", e);
}
} else { // enclosed wrapper
if (wrapperClass.getEnclosingClass() != null) {
try {
constructor = findMatchingConstructor(wrapperClass, wrapperClass.getEnclosingClass(), original.getClass());
} catch (Exception e) {
throw new Error("Can't create a new wrapper object", e);
}
}
if (constructor == null) {
try {
constructor = findMatchingConstructor(wrapperClass, WebDriverWrapper.class, original.getClass());
} catch (Exception e) {
throw new Error("Can't create a new wrapper object", e);
}
}
if (constructor == null) {
throw new Error("Wrapper class " + wrapperClass + " does not provide an appropriate constructor");
}
try {
wrapper = constructor.newInstance(driverWrapper, original);
} catch (Exception e) {
throw new Error("Can't create a new wrapper object", e);
}
}
return wrapper.wrapOriginal();
}
/**
* Builds a {@link java.lang.reflect.Proxy} implementing all interfaces of original object. It will delegate calls to
* wrapper when wrapper implements the requested method otherwise to original object.
*
* @return a proxy that wraps the original object
*/
public final T wrapOriginal() {
final T original = getWrappedOriginal();
final Set> wrapperInterfaces = extractInterfaces(this);
final InvocationHandler handler = new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
if (wrapperInterfaces.contains(method.getDeclaringClass())) {
boolean isUnwrap = method.getName().equals("getWrappedElement");
if (! isUnwrap) {
beforeMethod(method, args);
}
Object result = callMethod(method, args);
if (! isUnwrap) {
afterMethod(method, result, args);
}
return result;
}
return method.invoke(original, args);
} catch (InvocationTargetException e) {
return onError(method, e, args);
}
}
};
Set> allInterfaces = extractInterfaces(original);
allInterfaces.addAll(wrapperInterfaces);
Class>[] allInterfacesArray = allInterfaces.toArray(new Class>[allInterfaces.size()]);
return (T) Proxy.newProxyInstance(
this.getClass().getClassLoader(),
allInterfaces.toArray(allInterfacesArray),
handler);
}
protected Object unwrap(Object result) {
if (result instanceof WrapsSomething) {
return ((WrapsSomething) result).getWrappedOriginal();
}
if (result instanceof List) {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy