Skip to content

Commit

Permalink
Fixed Java 7 support for method handle extraction.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafael Winterhalter committed Mar 31, 2015
1 parent 55998c1 commit 3d3166f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
Expand Up @@ -459,10 +459,13 @@ public String toString() {
} }


/** /**
* The Java method to reveal the execution context of a {@code MethodHandle}. * The Java method to reveal the execution context of a {@code MethodHandle} as {@code MethodHandleInfo}.
*/ */
private static final JavaMethod REVEAL_DIRECT; private static final JavaMethod REVEAL_DIRECT;


/**
* The Java method to construct a {@code MethodHandleInfo} instance for Java 7 where revealing is not yet supported.
*/
private static final JavaMethod NEW_METHOD_HANDLE_INFO; private static final JavaMethod NEW_METHOD_HANDLE_INFO;


/** /**
Expand Down
Expand Up @@ -180,7 +180,7 @@ public int hashCode() {


@Override @Override
public String toString() { public String toString() {
return "JavaMethod.ForLoadedMethod{" + return "JavaMethod.ForLoadedConstructor{" +
"constructor=" + constructor + "constructor=" + constructor +
'}'; '}';
} }
Expand Down
Expand Up @@ -3,11 +3,13 @@
import net.bytebuddy.test.utility.ObjectPropertyAssertion; import net.bytebuddy.test.utility.ObjectPropertyAssertion;
import org.junit.Test; import org.junit.Test;


import java.lang.reflect.Constructor;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Arrays; import java.util.Arrays;
import java.util.Iterator; import java.util.Iterator;


import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;


Expand All @@ -32,13 +34,28 @@ public void testLoadedMethod() throws Exception {
assertThat(javaMethod.invoke(instance), is((Object) instance.toString())); assertThat(javaMethod.invoke(instance), is((Object) instance.toString()));
} }


@Test
public void testLoadedConstructor() throws Exception {
Constructor<?> constructor = Object.class.getDeclaredConstructor();
JavaMethod javaMethod = new JavaMethod.ForLoadedConstructor(constructor);
assertThat(javaMethod.isInvokable(), is(true));
assertThat(javaMethod.invokeStatic(), notNullValue(Object.class));
}

@Test @Test
public void testObjectProperties() throws Exception { public void testObjectProperties() throws Exception {
final Iterator<Method> iterator = Arrays.asList(Object.class.getDeclaredMethods()).iterator(); final Iterator<Method> methodIterator = Arrays.asList(Object.class.getDeclaredMethods()).iterator();
ObjectPropertyAssertion.of(JavaMethod.ForLoadedMethod.class).create(new ObjectPropertyAssertion.Creator<Method>() { ObjectPropertyAssertion.of(JavaMethod.ForLoadedMethod.class).create(new ObjectPropertyAssertion.Creator<Method>() {
@Override @Override
public Method create() { public Method create() {
return iterator.next(); return methodIterator.next();
}
}).apply();
final Iterator<Constructor<?>> constructorIterator = Arrays.<Constructor<?>>asList(String.class.getDeclaredConstructors()).iterator();
ObjectPropertyAssertion.of(JavaMethod.ForLoadedConstructor.class).create(new ObjectPropertyAssertion.Creator<Constructor<?>>() {
@Override
public Constructor<?> create() {
return constructorIterator.next();
} }
}).apply(); }).apply();
ObjectPropertyAssertion.of(JavaMethod.ForUnavailableMethod.class).apply(); ObjectPropertyAssertion.of(JavaMethod.ForUnavailableMethod.class).apply();
Expand Down

0 comments on commit 3d3166f

Please sign in to comment.