Skip to content

Commit

Permalink
Add convenience method for resolving a lookup based class loading str…
Browse files Browse the repository at this point in the history
…ategy.
  • Loading branch information
raphw committed Mar 28, 2020
1 parent 1eba9dd commit fd579fa
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.lang.instrument.Instrumentation;
import java.security.ProtectionDomain;
import java.util.Map;
import java.util.concurrent.Callable;

/**
* A strategy for loading a collection of types.
Expand Down Expand Up @@ -459,6 +460,28 @@ public static ClassLoadingStrategy<ClassLoader> of(Object lookup) {
return new UsingLookup(ClassInjector.UsingLookup.of(lookup));
}

/**
* Resolves a class loading strategy using a lookup if available on the current JVM. If the current JVM supports method handles
* lookups, a lookup instance will be used. Alternatively, unsafe class definition is used, if supported. If neither strategy is
* supported, an exception is thrown.
*
* @param lookup A resolver for a lookup instance if the current JVM allows for lookup-based class injection.
* @return An appropriate class loading strategy for the current JVM that uses a method handles lookup if available.
*/
public static ClassLoadingStrategy<ClassLoader> withFallback(Callable<?> lookup) {
if (ClassInjector.UsingLookup.isAvailable()) {
try {
return of(lookup.call());
} catch (Exception exception) {
throw new IllegalStateException(exception);
}
} else if (ClassInjector.UsingUnsafe.isAvailable()) {
return new ClassLoadingStrategy.ForUnsafeInjection();
} else {
throw new IllegalStateException("Neither lookup or unsafe class injection is available");
}
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.test.utility.MockitoRule;
import net.bytebuddy.utility.JavaConstant;
import net.bytebuddy.utility.JavaType;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -10,8 +12,10 @@

import java.util.Collections;
import java.util.Map;
import java.util.concurrent.Callable;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -45,6 +49,16 @@ public void testInjection() throws Exception {
assertThat(type.getName(), is(Foo.class.getName()));
}

@Test
public void testFallback() {
assertThat(ClassLoadingStrategy.UsingLookup.withFallback(new Callable<Object>() {
@Override
public Object call() throws Exception {
return JavaType.METHOD_HANDLES.load().getMethod("lookup").invoke(null);
}
}), notNullValue(ClassLoadingStrategy.class));
}

private static class Foo {
/* empty */
}
Expand Down

0 comments on commit fd579fa

Please sign in to comment.