Java 类org.mockito.cglib.proxy.MethodProxy 实例源码
项目:mockito-cglib
文件:MethodInterceptorFilter.java
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy)
throws Throwable {
if (objectMethodsGuru.isEqualsMethod(method)) {
return proxy == args[0];
} else if (objectMethodsGuru.isHashCodeMethod(method)) {
return hashCodeForMock(proxy);
} else if (acrossJVMSerializationFeature.isWriteReplace(method)) {
return acrossJVMSerializationFeature.writeReplace(proxy);
}
MockitoMethodProxy mockitoMethodProxy = createMockitoMethodProxy(methodProxy);
new CGLIBHacker().setMockitoNamingPolicy(methodProxy);
MockitoMethod mockitoMethod = createMockitoMethod(method);
CleanTraceRealMethod realMethod = new CleanTraceRealMethod(mockitoMethodProxy);
Invocation invocation = new InvocationImpl(proxy, mockitoMethod, args, SequenceNumber.next(), realMethod);
return handler.handle(invocation);
}
项目:astor
文件:MethodInterceptorFilter.java
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy)
throws Throwable {
if (objectMethodsGuru.isEqualsMethod(method)) {
return proxy == args[0];
} else if (objectMethodsGuru.isHashCodeMethod(method)) {
return hashCodeForMock(proxy);
}
MockitoMethodProxy mockitoMethodProxy = createMockitoMethodProxy(methodProxy);
cglibHacker.setMockitoNamingPolicy(mockitoMethodProxy);
MockitoMethod mockitoMethod = createMockitoMethod(method);
FilteredCGLIBProxyRealMethod realMethod = new FilteredCGLIBProxyRealMethod(mockitoMethodProxy);
Invocation invocation = new Invocation(proxy, mockitoMethod, args, SequenceNumber.next(), realMethod);
return handler.handle(invocation);
}
项目:astor
文件:CGLIBHacker.java
public void setMockitoNamingPolicy(MockitoMethodProxy mockitoMethodProxy) {
try {
MethodProxy methodProxy = mockitoMethodProxy.getMethodProxy();
Field createInfoField = reflectOnCreateInfo(methodProxy);
createInfoField.setAccessible(true);
Object createInfo = createInfoField.get(methodProxy);
Field namingPolicyField = createInfo.getClass().getDeclaredField("namingPolicy");
namingPolicyField.setAccessible(true);
if (namingPolicyField.get(createInfo) == null) {
namingPolicyField.set(createInfo, MockitoNamingPolicy.INSTANCE);
}
} catch (Exception e) {
throw new RuntimeException(
"Unable to set MockitoNamingPolicy on cglib generator which creates FastClasses", e);
}
}
项目:astor
文件:MethodInterceptorFilter.java
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy)
throws Throwable {
if (objectMethodsGuru.isEqualsMethod(method)) {
return proxy == args[0];
} else if (objectMethodsGuru.isHashCodeMethod(method)) {
return hashCodeForMock(proxy);
} else if (acrossJVMSerializationFeature.isWriteReplace(method)) {
return acrossJVMSerializationFeature.writeReplace(proxy);
}
MockitoMethodProxy mockitoMethodProxy = createMockitoMethodProxy(methodProxy);
cglibHacker.setMockitoNamingPolicy(mockitoMethodProxy);
MockitoMethod mockitoMethod = createMockitoMethod(method);
FilteredCGLIBProxyRealMethod realMethod = new FilteredCGLIBProxyRealMethod(mockitoMethodProxy);
Invocation invocation = new InvocationImpl(proxy, mockitoMethod, args, SequenceNumber.next(), realMethod);
return handler.handle(invocation);
}
项目:astor
文件:CGLIBHacker.java
public void setMockitoNamingPolicy(MockitoMethodProxy mockitoMethodProxy) {
try {
MethodProxy methodProxy = mockitoMethodProxy.getMethodProxy();
Field createInfoField = reflectOnCreateInfo(methodProxy);
createInfoField.setAccessible(true);
Object createInfo = createInfoField.get(methodProxy);
Field namingPolicyField = createInfo.getClass().getDeclaredField("namingPolicy");
namingPolicyField.setAccessible(true);
if (namingPolicyField.get(createInfo) == null) {
namingPolicyField.set(createInfo, MockitoNamingPolicy.INSTANCE);
}
} catch (Exception e) {
throw new RuntimeException(
"Unable to set MockitoNamingPolicy on cglib generator which creates FastClasses", e);
}
}
项目:securemock
文件:CGLIBHacker.java
public void setMockitoNamingPolicy(MockitoMethodProxy mockitoMethodProxy) {
try {
MethodProxy methodProxy = mockitoMethodProxy.getMethodProxy();
final Field createInfoField = reflectOnCreateInfo(methodProxy);
AccessController.doPrivileged(new PrivilegedAction<Void>() {
@Override
public Void run() {
createInfoField.setAccessible(true);
return null;
}
});
Object createInfo = createInfoField.get(methodProxy);
final Field namingPolicyField = createInfo.getClass().getDeclaredField("namingPolicy");
AccessController.doPrivileged(new PrivilegedAction<Void>() {
@Override
public Void run() {
namingPolicyField.setAccessible(true);
return null;
}
});
if (namingPolicyField.get(createInfo) == null) {
namingPolicyField.set(createInfo, MockitoNamingPolicy.INSTANCE);
}
} catch (Exception e) {
throw new RuntimeException(
"Unable to set MockitoNamingPolicy on cglib generator which creates FastClasses", e);
}
}
项目:securemock
文件:CGLIBHacker.java
@SuppressWarnings("unchecked")
private Field reflectOnCreateInfo(MethodProxy methodProxy) throws SecurityException, NoSuchFieldException {
Class cglibMethodProxyClass = methodProxy.getClass();
// in case methodProxy was extended by user, let's traverse the object
// graph to find the cglib methodProxy
// with all the fields we would like to change
while (cglibMethodProxyClass != MethodProxy.class) {
cglibMethodProxyClass = methodProxy.getClass().getSuperclass();
}
return cglibMethodProxyClass.getDeclaredField("createInfo");
}
项目:mockito-cglib
文件:CGLIBHacker.java
public void setMockitoNamingPolicy(MethodProxy methodProxy) {
try {
Field createInfoField = reflectOnCreateInfo(methodProxy);
createInfoField.setAccessible(true);
Object createInfo = createInfoField.get(methodProxy);
Field namingPolicyField = createInfo.getClass().getDeclaredField("namingPolicy");
namingPolicyField.setAccessible(true);
if (namingPolicyField.get(createInfo) == null) {
namingPolicyField.set(createInfo, MockitoNamingPolicy.INSTANCE);
}
} catch (Exception e) {
throw new RuntimeException(
"Unable to set MockitoNamingPolicy on cglib generator which creates FastClasses", e);
}
}
项目:mockito-cglib
文件:CGLIBHacker.java
@SuppressWarnings("unchecked")
private Field reflectOnCreateInfo(MethodProxy methodProxy) throws SecurityException, NoSuchFieldException {
Class cglibMethodProxyClass = methodProxy.getClass();
// in case methodProxy was extended by user, let's traverse the object
// graph to find the cglib methodProxy
// with all the fields we would like to change
while (cglibMethodProxyClass != MethodProxy.class) {
cglibMethodProxyClass = methodProxy.getClass().getSuperclass();
}
return cglibMethodProxyClass.getDeclaredField("createInfo");
}
项目:mockito-cglib
文件:SerializableMockitoMethodProxy.java
public SerializableMockitoMethodProxy(MethodProxy methodProxy) {
assert methodProxy != null;
Object info = Whitebox.getInternalState(methodProxy, "createInfo");
c1 = (Class<?>) Whitebox.getInternalState(info, "c1");
c2 = (Class<?>) Whitebox.getInternalState(info, "c2");
desc = methodProxy.getSignature().getDescriptor();
name = methodProxy.getSignature().getName();
superName = methodProxy.getSuperName();
this.methodProxy = methodProxy;
}
项目:mockito-cglib
文件:MethodInterceptorFilterTest.java
@Test
public void shouldCreateSerializableMethodProxyIfIsSerializableMock() throws Exception {
MethodInterceptorFilter filter = new MethodInterceptorFilter(handler, (MockSettingsImpl) withSettings().serializable());
MethodProxy methodProxy = MethodProxy.create(String.class, String.class, "", "toString", "toString");
// when
MockitoMethodProxy mockitoMethodProxy = filter.createMockitoMethodProxy(methodProxy);
// then
assertThat(mockitoMethodProxy, instanceOf(SerializableMockitoMethodProxy.class));
}
项目:mockito-cglib
文件:MethodInterceptorFilterTest.java
@Test
public void shouldCreateNONSerializableMethodProxyIfIsNotSerializableMock() throws Exception {
MethodInterceptorFilter filter = new MethodInterceptorFilter(handler, (MockSettingsImpl) withSettings());
MethodProxy methodProxy = MethodProxy.create(String.class, String.class, "", "toString", "toString");
// when
MockitoMethodProxy mockitoMethodProxy = filter.createMockitoMethodProxy(methodProxy);
// then
assertThat(mockitoMethodProxy, instanceOf(DelegatingMockitoMethodProxy.class));
}
项目:astor
文件:ReturnsSmartNulls.java
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
if (new ObjectMethodsGuru().isToString(method)) {
return "SmartNull returned by this unstubbed method call on a mock:\n" +
invocation.toString();
}
new Reporter().smartNullPointerException(invocation.toString(), location);
return null;
}
项目:astor
文件:CGLIBHacker.java
@SuppressWarnings("unchecked")
private Field reflectOnCreateInfo(MethodProxy methodProxy) throws SecurityException, NoSuchFieldException {
Class cglibMethodProxyClass = methodProxy.getClass();
// in case methodProxy was extended by user, let's traverse the object
// graph to find the cglib methodProxy
// with all the fields we would like to change
while (cglibMethodProxyClass != MethodProxy.class) {
cglibMethodProxyClass = methodProxy.getClass().getSuperclass();
}
return cglibMethodProxyClass.getDeclaredField("createInfo");
}
项目:astor
文件:SerializableMockitoMethodProxy.java
public SerializableMockitoMethodProxy(MethodProxy methodProxy) {
Object info = Whitebox.getInternalState(methodProxy, "createInfo");
c1 = (Class<?>) Whitebox.getInternalState(info, "c1");
c2 = (Class<?>) Whitebox.getInternalState(info, "c2");
desc = methodProxy.getSignature().getDescriptor();
name = methodProxy.getSignature().getName();
superName = methodProxy.getSuperName();
this.methodProxy = methodProxy;
}
项目:astor
文件:SerializableMockitoMethodProxyTest.java
@Test
public void shouldCreateCorrectCreationInfo() throws Exception {
// given
MethodProxy proxy = MethodProxy.create(String.class, Integer.class, "", "", "");
SerializableMockitoMethodProxy serializableMockitoMethodProxy = new SerializableMockitoMethodProxy(proxy);
// when
Object methodProxy = Whitebox.invokeMethod(serializableMockitoMethodProxy, "getMethodProxy", new Object[0]);
// then
Object info = Whitebox.getInternalState(methodProxy, "createInfo");
assertEquals(String.class, Whitebox.getInternalState(info, "c1"));
assertEquals(Integer.class, Whitebox.getInternalState(info, "c2"));
}
项目:astor
文件:SerializableMockitoMethodProxyTest.java
@Test
public void shouldCreateCorrectSignatures() throws Exception {
// given
MethodProxy proxy = MethodProxy.create(String.class, Integer.class, "a", "b", "c");
SerializableMockitoMethodProxy serializableMockitoMethodProxy = new SerializableMockitoMethodProxy(proxy);
// when
MethodProxy methodProxy = (MethodProxy) Whitebox.invokeMethod(serializableMockitoMethodProxy, "getMethodProxy", new Object[0]);
// then
assertEquals("a", methodProxy.getSignature().getDescriptor());
assertEquals("b", methodProxy.getSignature().getName());
assertEquals("c", methodProxy.getSuperName());
}
项目:astor
文件:MethodInterceptorFilterTest.java
@Test
public void shouldCreateSerializableMethodProxyIfIsSerializableMock() throws Exception {
MethodInterceptorFilter filter = new MethodInterceptorFilter(handler, (MockSettingsImpl) withSettings().serializable());
MethodProxy methodProxy = MethodProxy.create(String.class, String.class, "", "toString", "toString");
// when
MockitoMethodProxy mockitoMethodProxy = filter.createMockitoMethodProxy(methodProxy);
// then
assertThat(mockitoMethodProxy, instanceOf(SerializableMockitoMethodProxy.class));
}
项目:astor
文件:MethodInterceptorFilterTest.java
@Test
public void shouldCreateNONSerializableMethodProxyIfIsNotSerializableMock() throws Exception {
MethodInterceptorFilter filter = new MethodInterceptorFilter(handler, (MockSettingsImpl) withSettings());
MethodProxy methodProxy = MethodProxy.create(String.class, String.class, "", "toString", "toString");
// when
MockitoMethodProxy mockitoMethodProxy = filter.createMockitoMethodProxy(methodProxy);
// then
assertThat(mockitoMethodProxy, instanceOf(DelegatingMockitoMethodProxy.class));
}
项目:astor
文件:CGLIBHacker.java
@SuppressWarnings("unchecked")
private Field reflectOnCreateInfo(MethodProxy methodProxy) throws SecurityException, NoSuchFieldException {
Class cglibMethodProxyClass = methodProxy.getClass();
// in case methodProxy was extended by user, let's traverse the object
// graph to find the cglib methodProxy
// with all the fields we would like to change
while (cglibMethodProxyClass != MethodProxy.class) {
cglibMethodProxyClass = methodProxy.getClass().getSuperclass();
}
return cglibMethodProxyClass.getDeclaredField("createInfo");
}
项目:astor
文件:SerializableMockitoMethodProxy.java
public SerializableMockitoMethodProxy(MethodProxy methodProxy) {
Object info = Whitebox.getInternalState(methodProxy, "createInfo");
c1 = (Class<?>) Whitebox.getInternalState(info, "c1");
c2 = (Class<?>) Whitebox.getInternalState(info, "c2");
desc = methodProxy.getSignature().getDescriptor();
name = methodProxy.getSignature().getName();
superName = methodProxy.getSuperName();
this.methodProxy = methodProxy;
}
项目:astor
文件:SerializableMockitoMethodProxyTest.java
@Test
public void shouldCreateCorrectCreationInfo() throws Exception {
// given
MethodProxy proxy = MethodProxy.create(String.class, Integer.class, "", "", "");
SerializableMockitoMethodProxy serializableMockitoMethodProxy = new SerializableMockitoMethodProxy(proxy);
// when
Object methodProxy = Whitebox.invokeMethod(serializableMockitoMethodProxy, "getMethodProxy", new Object[0]);
// then
Object info = Whitebox.getInternalState(methodProxy, "createInfo");
assertEquals(String.class, Whitebox.getInternalState(info, "c1"));
assertEquals(Integer.class, Whitebox.getInternalState(info, "c2"));
}
项目:astor
文件:SerializableMockitoMethodProxyTest.java
@Test
public void shouldCreateCorrectSignatures() throws Exception {
// given
MethodProxy proxy = MethodProxy.create(String.class, Integer.class, "a", "b", "c");
SerializableMockitoMethodProxy serializableMockitoMethodProxy = new SerializableMockitoMethodProxy(proxy);
// when
MethodProxy methodProxy = (MethodProxy) Whitebox.invokeMethod(serializableMockitoMethodProxy, "getMethodProxy", new Object[0]);
// then
assertEquals("a", methodProxy.getSignature().getDescriptor());
assertEquals("b", methodProxy.getSignature().getName());
assertEquals("c", methodProxy.getSuperName());
}
项目:astor
文件:MethodInterceptorFilterTest.java
@Test
public void shouldCreateSerializableMethodProxyIfIsSerializableMock() throws Exception {
MethodInterceptorFilter filter = new MethodInterceptorFilter(handler, (MockSettingsImpl) withSettings().serializable());
MethodProxy methodProxy = MethodProxy.create(String.class, String.class, "", "toString", "toString");
// when
MockitoMethodProxy mockitoMethodProxy = filter.createMockitoMethodProxy(methodProxy);
// then
assertThat(mockitoMethodProxy, instanceOf(SerializableMockitoMethodProxy.class));
}
项目:astor
文件:MethodInterceptorFilterTest.java
@Test
public void shouldCreateNONSerializableMethodProxyIfIsNotSerializableMock() throws Exception {
MethodInterceptorFilter filter = new MethodInterceptorFilter(handler, (MockSettingsImpl) withSettings());
MethodProxy methodProxy = MethodProxy.create(String.class, String.class, "", "toString", "toString");
// when
MockitoMethodProxy mockitoMethodProxy = filter.createMockitoMethodProxy(methodProxy);
// then
assertThat(mockitoMethodProxy, instanceOf(DelegatingMockitoMethodProxy.class));
}
项目:mockito-cglib
文件:MethodInterceptorFilter.java
public MockitoMethodProxy createMockitoMethodProxy(MethodProxy methodProxy) {
if (mockSettings.isSerializable())
return new SerializableMockitoMethodProxy(methodProxy);
return new DelegatingMockitoMethodProxy(methodProxy);
}
项目:mockito-cglib
文件:DelegatingMockitoMethodProxy.java
public DelegatingMockitoMethodProxy(MethodProxy methodProxy) {
this.methodProxy = methodProxy;
}
项目:mockito-cglib
文件:SerializableMockitoMethodProxy.java
MethodProxy getMethodProxy() {
if (methodProxy == null) {
methodProxy = MethodProxy.create(c1, c2, desc, name, superName);
}
return methodProxy;
}
项目:mockito-cglib
文件:ClassImposterizerTest.java
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
return null;
}
项目:mockito-cglib
文件:SerializeMockToFile.java
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
return methodProxy.invokeSuper(o, objects);
}
项目:astor
文件:MethodInterceptorFilter.java
public MockitoMethodProxy createMockitoMethodProxy(MethodProxy methodProxy) {
if (mockSettings.isSerializable())
return new SerializableMockitoMethodProxy(methodProxy);
return new DelegatingMockitoMethodProxy(methodProxy);
}
项目:astor
文件:DelegatingMockitoMethodProxy.java
public DelegatingMockitoMethodProxy(MethodProxy methodProxy) {
this.methodProxy = methodProxy;
}
项目:astor
文件:DelegatingMockitoMethodProxy.java
public MethodProxy getMethodProxy() {
return methodProxy;
}
项目:astor
文件:SerializableMockitoMethodProxy.java
public MethodProxy getMethodProxy() {
if (methodProxy == null)
methodProxy = MethodProxy.create(c1, c2, desc, name, superName);
return methodProxy;
}
项目:astor
文件:ClassImposterizerTest.java
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
return null;
}
项目:astor
文件:MethodInterceptorFilter.java
public MockitoMethodProxy createMockitoMethodProxy(MethodProxy methodProxy) {
if (mockSettings.isSerializable())
return new SerializableMockitoMethodProxy(methodProxy);
return new DelegatingMockitoMethodProxy(methodProxy);
}
项目:astor
文件:DelegatingMockitoMethodProxy.java
public DelegatingMockitoMethodProxy(MethodProxy methodProxy) {
this.methodProxy = methodProxy;
}
项目:astor
文件:DelegatingMockitoMethodProxy.java
public MethodProxy getMethodProxy() {
return methodProxy;
}
项目:astor
文件:SerializableMockitoMethodProxy.java
public MethodProxy getMethodProxy() {
if (methodProxy == null)
methodProxy = MethodProxy.create(c1, c2, desc, name, superName);
return methodProxy;
}
项目:astor
文件:SerializeMockToFile.java
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
return methodProxy.invokeSuper(o, objects);
}