|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * See the NOTICE file distributed with this work for additional |
| 5 | + * information regarding copyright ownership. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.appium.java_client.proxy; |
| 18 | + |
| 19 | +import com.google.common.base.Preconditions; |
| 20 | +import net.bytebuddy.ByteBuddy; |
| 21 | +import net.bytebuddy.dynamic.loading.ClassLoadingStrategy; |
| 22 | +import net.bytebuddy.implementation.MethodDelegation; |
| 23 | +import net.bytebuddy.matcher.ElementMatchers; |
| 24 | + |
| 25 | +import java.util.Collection; |
| 26 | +import java.util.Collections; |
| 27 | + |
| 28 | +public class Helpers { |
| 29 | + private Helpers() { |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Creates a transparent proxy instance for the given class. |
| 34 | + * It is possible to provide one or more method execution listeners |
| 35 | + * or replace particular method calls completely. Callbacks |
| 36 | + * defined in these listeners are going to be called when any |
| 37 | + * **public** method of the given class is invoked. Overridden callbacks |
| 38 | + * are expected to be skipped if they throw |
| 39 | + * {@link io.appium.java_client.proxy.NotImplementedException}. |
| 40 | + * |
| 41 | + * @param cls the class to which the proxy should be created. |
| 42 | + * Must not be an interface. |
| 43 | + * @param constructorArgs Array of constructor arguments. Could be an |
| 44 | + * empty array if the class provides a constructor without arguments. |
| 45 | + * @param constructorArgTypes Array of constructor argument types. Must |
| 46 | + * represent types of constructorArgs. |
| 47 | + * @param listeners One or more method invocation listeners. |
| 48 | + * @param <T> Any class derived from Object |
| 49 | + * @return Proxy instance |
| 50 | + */ |
| 51 | + public static <T> T createProxy( |
| 52 | + Class<T> cls, |
| 53 | + Object[] constructorArgs, |
| 54 | + Class<?>[] constructorArgTypes, |
| 55 | + Collection<MethodCallListener> listeners |
| 56 | + ) { |
| 57 | + Preconditions.checkArgument(constructorArgs.length == constructorArgTypes.length, |
| 58 | + String.format( |
| 59 | + "Constructor arguments array length %d must be equal to the types array length %d", |
| 60 | + constructorArgs.length, constructorArgTypes.length |
| 61 | + ) |
| 62 | + ); |
| 63 | + Preconditions.checkArgument(!listeners.isEmpty(), "The collection of listeners must not be empty"); |
| 64 | + Preconditions.checkArgument(cls != null, "Class must not be null"); |
| 65 | + Preconditions.checkArgument(!cls.isInterface(), "Class must not be an interface"); |
| 66 | + |
| 67 | + //noinspection resource |
| 68 | + Class<?> proxy = new ByteBuddy() |
| 69 | + .subclass(cls) |
| 70 | + .method(ElementMatchers.isPublic() |
| 71 | + .and(ElementMatchers.not( |
| 72 | + ElementMatchers.isDeclaredBy(Object.class) |
| 73 | + .or(ElementMatchers.isOverriddenFrom(Object.class)) |
| 74 | + ))) |
| 75 | + .intercept(MethodDelegation.to(Interceptor.class)) |
| 76 | + .make() |
| 77 | + .load(cls.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER) |
| 78 | + .getLoaded() |
| 79 | + .asSubclass(cls); |
| 80 | + |
| 81 | + try { |
| 82 | + //noinspection unchecked |
| 83 | + T instance = (T) proxy |
| 84 | + .getConstructor(constructorArgTypes) |
| 85 | + .newInstance(constructorArgs); |
| 86 | + Interceptor.LISTENERS.put(instance, listeners); |
| 87 | + return instance; |
| 88 | + } catch (SecurityException | ReflectiveOperationException e) { |
| 89 | + throw new IllegalStateException(String.format("Unable to create a proxy of %s", cls.getName()), e); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Creates a transparent proxy instance for the given class. |
| 95 | + * It is possible to provide one or more method execution listeners |
| 96 | + * or replace particular method calls completely. Callbacks |
| 97 | + * defined in these listeners are going to be called when any |
| 98 | + * **public** method of the given class is invoked. Overridden callbacks |
| 99 | + * are expected to be skipped if they throw NotImplementedException. |
| 100 | + * |
| 101 | + * @param cls the class to which the proxy should be created. |
| 102 | + * Must not be an interface. Must expose a constructor |
| 103 | + * without arguments. |
| 104 | + * @param listeners One or more method invocation listeners. |
| 105 | + * @param <T> Any class derived from Object |
| 106 | + * @return Proxy instance |
| 107 | + */ |
| 108 | + public static <T> T createProxy(Class<T> cls, Collection<MethodCallListener> listeners) { |
| 109 | + return createProxy(cls, new Object[]{}, new Class[]{}, listeners); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Creates a transparent proxy instance for the given class. |
| 114 | + * It is possible to provide one or more method execution listeners |
| 115 | + * or replace particular method calls completely. Callbacks |
| 116 | + * defined in these listeners are going to be called when any |
| 117 | + * **public** method of the given class is invoked. Overridden callbacks |
| 118 | + * are expected to be skipped if they throw NotImplementedException. |
| 119 | + * |
| 120 | + * @param cls the class to which the proxy should be created. |
| 121 | + * Must not be an interface. Must expose a constructor |
| 122 | + * without arguments. |
| 123 | + * @param listener Method invocation listener. |
| 124 | + * @param <T> Any class derived from Object |
| 125 | + * @return Proxy instance |
| 126 | + */ |
| 127 | + public static <T> T createProxy(Class<T> cls, MethodCallListener listener) { |
| 128 | + return createProxy(cls, new Object[]{}, new Class[]{}, Collections.singletonList(listener)); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Creates a transparent proxy instance for the given class. |
| 133 | + * It is possible to provide one or more method execution listeners |
| 134 | + * or replace particular method calls completely. Callbacks |
| 135 | + * defined in these listeners are going to be called when any |
| 136 | + * **public** method of the given class is invoked. Overridden callbacks |
| 137 | + * are expected to be skipped if they throw NotImplementedException. |
| 138 | + * |
| 139 | + * @param cls the class to which the proxy should be created. |
| 140 | + * Must not be an interface. |
| 141 | + * @param constructorArgs Array of constructor arguments. Could be an |
| 142 | + * empty array if the class provides a constructor without arguments. |
| 143 | + * @param constructorArgTypes Array of constructor argument types. Must |
| 144 | + * represent types of constructorArgs. |
| 145 | + * @param listener Method invocation listener. |
| 146 | + * @param <T> Any class derived from Object |
| 147 | + * @return Proxy instance |
| 148 | + */ |
| 149 | + public static <T> T createProxy( |
| 150 | + Class<T> cls, |
| 151 | + Object[] constructorArgs, |
| 152 | + Class<?>[] constructorArgTypes, |
| 153 | + MethodCallListener listener |
| 154 | + ) { |
| 155 | + return createProxy(cls, constructorArgs, constructorArgTypes, Collections.singletonList(listener)); |
| 156 | + } |
| 157 | +} |
0 commit comments