@Test public void testMethodRenaming() throws InvalidProxyDescriptionException { @ProxyFor class Subject extends BasicSubject { @ProxyMethod(methodName = "equals") public boolean thisActuallyisEquals(Object other) { return false; } } IProxyBuildPlan bp = ProxyBuildPlanImpl.create(Subject.class, "myname", ProxyBuildPlanImplTest.class.getClassLoader()); assertThat(bp.getMethods().size(), equalTo(1)); assertThat(bp.getMethods().get(0).getMethodName(), equalTo("equals")); }
@Test public void testParameterCorrectCasting() throws InvalidProxyDescriptionException { @ProxyFor() class Subject extends BasicSubject { @ProxyMethod(parameterTypes = { "java.io.File" }) public void castMe(Object thisIsActuallyAFile) { } } IProxyBuildPlan bp = ProxyBuildPlanImpl.create(Subject.class, "myname", ProxyBuildPlanImplTest.class.getClassLoader()); assertThat(bp.getMethods().size(), equalTo(1)); assertThat(bp.getMethods().get(0).getParameterTypes().size(), equalTo(1)); assertThat(bp.getMethods().get(0).getParameterTypes().get(0).getName(), equalTo("java.io.File")); }
@Test public void testReturnTypeCorrectCasting() throws InvalidProxyDescriptionException { @ProxyFor() class Subject extends BasicSubject { @ProxyMethod(returnType = "java.io.File") public Object castMe() { return null; } } IProxyBuildPlan bp = ProxyBuildPlanImpl.create(Subject.class, "myname", ProxyBuildPlanImplTest.class.getClassLoader()); assertThat(bp.getMethods().size(), equalTo(1)); assertThat(bp.getMethods().get(0).getReturnType().getName(), equalTo("java.io.File")); }
Method invokeMethod = new Method("invoke", IInstrumenterConstant.OBJECT_TYPE, invokeMethodArgs); Method meth = new Method(plan.getMethodName(), Type.getType(plan.getReturnType()), getTypes(plan.getParameterTypes())); GeneratorAdapter mg = new GeneratorAdapter(ACC_PUBLIC, meth, null, new Type[] {}, cv); Class<?> returnType = plan.getReturnType(); if (returnType == void.class) { mg.pop();
/** * After the proxy class has been generated and injected, the static fields storing the * delegation targets have to be injected. This is done by this method. * * @param plan * the buildplan used for building the proxy * @param methodFields * the map mapping the proxied methods to their field names. * @param proxyClass * the generated and loaded proxy class. */ private void injectDelegationTargetFields(IProxyBuildPlan plan, Map<IMethodBuildPlan, String> methodFields, Class<?> proxyClass) { try { for (IMethodBuildPlan mplan : plan.getMethods()) { Field f = proxyClass.getDeclaredField(methodFields.get(mplan)); f.setAccessible(true); f.set(null, mplan.getTargetMethod()); } } catch (Exception e) { throw new RuntimeException(e); } }