Skip to content

Commit

Permalink
Replace cglib with bytebuddy
Browse files Browse the repository at this point in the history
Apparently this is the best thing to use these days.
  • Loading branch information
shs96c committed Sep 1, 2017
1 parent 90611ad commit 07b7b54
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 68 deletions.
11 changes: 11 additions & 0 deletions .idea/libraries/byte_buddy.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 0 additions & 9 deletions .idea/libraries/cglib.xml

This file was deleted.

2 changes: 1 addition & 1 deletion java/client/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
<classpathentry kind="lib" path="/third-party/java/commons/commons-exec-1.3.jar" sourcepath="/third-party/java/commons/commons-exec-1.3-sources.jar"/>
<classpathentry kind="lib" path="/third-party/java/jetty/jetty-repacked.jar"/>
<classpathentry kind="lib" path="/third-party/java/jetty/jetty-util-9.4.5.v20170502.jar" sourcepath="/third-party/java/jetty/jetty-util-9.4.5.v20170502-sources.jar"/>
<classpathentry kind="lib" path="/third-party/java/cglib/cglib-nodep-3.2.4.jar"/>
<classpathentry kind="lib" path="/third-party/java/servlet/javax.servlet-api-3.1.0.jar" sourcepath="/third-party/java/servlet/javax.servlet-api-3.1.0-sources.jar"/>
<classpathentry kind="lib" path="/third-party/java/phantomjs-driver/phantomjsdriver-1.4.0.jar" sourcepath="/third-party/java/phantomjs-driver/phantomjsdriver-1.4.0-sources.jar"/>
<classpathentry kind="lib" path="/third-party/java/httpcomponents/httpclient-4.5.3.jar" sourcepath="/third-party/java/httpcomponents/httpclient-4.5.3-sources.jar"/>
Expand Down Expand Up @@ -45,5 +44,6 @@
<classpathentry kind="lib" path="/third-party/java/gson/gson-2.8.0.jar"/>
<classpathentry kind="lib" path="/third-party/java/testng/testng-6.9.9.jar" sourcepath="/third-party/java/testng/testng-6.9.9-sources.jar"/>
<classpathentry kind="lib" path="/third-party/java/github/org.eclipse.egit.github.core-2.1.5.jar" sourcepath="/third-party/java/github/org.eclipse.egit.github.core-2.1.5.jar-sources.jar"/>
<classpathentry kind="lib" path="/third-party/java/bytebuddy/byte-buddy-1.7.5.jar" sourcepath="/third-party/java/bytebuddy/byte-buddy-1.7.5-sources.jar"/>
<classpathentry kind="output" path="build/production"/>
</classpath>
1 change: 1 addition & 0 deletions java/client/client.iml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="byte-buddy" level="project" />
<orderEntry type="library" name="cglib" level="project" />
<orderEntry type="library" scope="RUNTIME" name="commons-codec" level="project" />
<orderEntry type="library" scope="RUNTIME" name="commons-collections" level="project" />
Expand Down
87 changes: 46 additions & 41 deletions java/client/src/org/openqa/selenium/remote/Augmenter.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,20 @@
package org.openqa.selenium.remote;


import static net.bytebuddy.matcher.ElementMatchers.any;
import static net.bytebuddy.matcher.ElementMatchers.named;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Sets;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.description.annotation.AnnotationDescription;
import net.bytebuddy.implementation.FixedValue;
import net.bytebuddy.implementation.InvocationHandlerAdapter;

import org.openqa.selenium.WebDriver;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
Expand All @@ -37,6 +41,7 @@
import java.util.Set;
import java.util.logging.Logger;


/**
* Enhance the interfaces implemented by an instance of the
* {@link org.openqa.selenium.remote.RemoteWebDriver} based on the returned
Expand All @@ -49,8 +54,10 @@ public class Augmenter extends BaseAugmenter {
private static final Logger logger = Logger.getLogger(Augmenter.class.getName());

@Override
protected <X> X create(RemoteWebDriver driver,
Map<String, AugmenterProvider> augmentors, X objectToAugment) {
protected <X> X create(
RemoteWebDriver driver,
Map<String, AugmenterProvider> augmentors,
X objectToAugment) {
CompoundHandler handler = determineAugmentation(driver, augmentors, objectToAugment);

X augmented = performAugmentation(handler, objectToAugment);
Expand All @@ -62,12 +69,10 @@ protected <X> X create(RemoteWebDriver driver,

@Override
protected RemoteWebDriver extractRemoteWebDriver(WebDriver driver) {
if (driver.getClass().isAnnotationPresent(Augmentable.class) ||
driver.getClass().getName().startsWith(
"org.openqa.selenium.remote.RemoteWebDriver$$EnhancerByCGLIB")) {
if (driver.getClass().isAnnotationPresent(Augmentable.class)) {
return (RemoteWebDriver) driver;

}

logger.warning("Augmenter should be applied to the instances of @Augmentable classes " +
"or previously augmented instances only (instance class was: " + driver.getClass() + ")");
return null;
Expand All @@ -91,10 +96,6 @@ private void copyField(Object source, Object target, Field field) {
return;
}

if (field.getName().startsWith("CGLIB$")) {
return;
}

try {
field.setAccessible(true);
Object value = field.get(source);
Expand All @@ -104,8 +105,10 @@ private void copyField(Object source, Object target, Field field) {
}
}

private CompoundHandler determineAugmentation(RemoteWebDriver driver,
Map<String, AugmenterProvider> augmentors, Object objectToAugment) {
private CompoundHandler determineAugmentation(
RemoteWebDriver driver,
Map<String, AugmenterProvider> augmentors,
Object objectToAugment) {
Map<String, ?> capabilities = driver.getCapabilities().asMap();

CompoundHandler handler = new CompoundHandler(driver, objectToAugment);
Expand All @@ -131,39 +134,41 @@ private CompoundHandler determineAugmentation(RemoteWebDriver driver,
protected <X> X performAugmentation(CompoundHandler handler, X from) {
if (handler.isNeedingApplication()) {
Class<?> superClass = from.getClass();
while (Enhancer.isEnhanced(superClass)) {
superClass = superClass.getSuperclass();
}

Enhancer enhancer = new Enhancer();
enhancer.setCallback(handler);
enhancer.setSuperclass(superClass);

Set<Class<?>> interfaces = Sets.newHashSet();
interfaces.addAll(ImmutableList.copyOf(from.getClass().getInterfaces()));
interfaces.addAll(handler.getInterfaces());
enhancer.setInterfaces(interfaces.toArray(new Class<?>[interfaces.size()]));

return (X) enhancer.create();
Class<?> loaded = new ByteBuddy()
.subclass(superClass)
.implement(ImmutableList.copyOf(handler.getInterfaces()))
.annotateType(AnnotationDescription.Builder.ofType(Augmentable.class).build())
.method(any()).intercept(InvocationHandlerAdapter.of(handler))
.method(named("isAugmented")).intercept(FixedValue.value(true))
.make()
.load(superClass.getClassLoader())
.getLoaded()
.asSubclass(from.getClass());

try {
return (X) loaded.newInstance();
} catch (ReflectiveOperationException e) {
throw new RuntimeException("Unable to create subclass", e);
}
}

return from;
}

private class CompoundHandler implements MethodInterceptor {

private Map<Method, InterfaceImplementation> handlers = new HashMap<>();
private Set<Class<?>> interfaces = new HashSet<>();
private class CompoundHandler implements InvocationHandler {

private final RemoteWebDriver driver;
private final ExecuteMethod execute;
private final Object originalInstance;
private final Map<Method, InterfaceImplementation> handlers = new HashMap<>();
private final Set<Class<?>> interfaces = new HashSet<>();

private CompoundHandler(RemoteWebDriver driver, Object originalInstance) {
this.driver = driver;
this.execute = new RemoteExecuteMethod(driver);
this.originalInstance = originalInstance;
}

public void addCapabilityHander(Class<?> fromInterface, InterfaceImplementation handledBy) {
void addCapabilityHander(Class<?> fromInterface, InterfaceImplementation handledBy) {
if (fromInterface.isInterface()) {
interfaces.add(fromInterface);
}
Expand All @@ -172,16 +177,16 @@ public void addCapabilityHander(Class<?> fromInterface, InterfaceImplementation
}
}

public Set<Class<?>> getInterfaces() {
Set<Class<?>> getInterfaces() {
return interfaces;
}

public boolean isNeedingApplication() {
boolean isNeedingApplication() {
return !handlers.isEmpty();
}

public Object intercept(Object self, Method method, Object[] args, MethodProxy methodProxy)
throws Throwable {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
InterfaceImplementation handler = handlers.get(method);

if (handler == null) {
Expand All @@ -192,7 +197,7 @@ public Object intercept(Object self, Method method, Object[] args, MethodProxy m
}
}

return handler.invoke(new RemoteExecuteMethod(driver), self, method, args);
return handler.invoke(execute, proxy, method, args);
}
}
}
2 changes: 1 addition & 1 deletion java/client/src/org/openqa/selenium/remote/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ java_library(name = 'remote',
'//java/client/src/org/openqa/selenium:selenium',
],
deps = [
'//third_party/java/cglib:cglib',
'//third_party/java/bytebuddy:bytebuddy',
'//third_party/java/guava:guava',
],
visibility = ['PUBLIC'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,7 @@ public Class<?> getDescribedInterface() {
}

public InterfaceImplementation getImplementation(Object value) {
return new InterfaceImplementation() {
public Object invoke(ExecuteMethod executeMethod, Object self, Method method,
Object... args) {
return "Hello World";
}
};
return (executeMethod, self, method, args) -> "Hello World";
}
});

Expand Down
10 changes: 0 additions & 10 deletions third_party/java/cglib/BUCK

This file was deleted.

Binary file removed third_party/java/cglib/cglib-nodep-3.2.4-sources.jar
Binary file not shown.
Binary file removed third_party/java/cglib/cglib-nodep-3.2.4.jar
Binary file not shown.

0 comments on commit 07b7b54

Please sign in to comment.