/** * 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); } }
/** * Generates the static fields of type java.lang.reflection.Method which store the delegation * targets. * * @param plan * the plan of the proxy to build. * @param cw * the classwriter to use. * @return a map mapping the individual proxied methods to their corresponding field names. */ private Map<IMethodBuildPlan, String> generateDelegationMethodTargetFields(IProxyBuildPlan plan, ClassWriter cw) { if (CollectionUtils.isEmpty(plan.getMethods())) { return Collections.emptyMap(); } Map<IMethodBuildPlan, String> methodFields = new HashMap<IMethodBuildPlan, String>(); int counter = 1; for (IMethodBuildPlan mplan : plan.getMethods()) { String name = METHOD_FIELD_PREFIX + counter; cw.visitField(ACC_STATIC, name, IInstrumenterConstant.REFLECTION_METHOD_TYPE.getDescriptor(), null, null).visitEnd(); methodFields.put(mplan, name); counter++; } return methodFields; }
@Test public void testOptionalMethod() throws InvalidProxyDescriptionException { @ProxyFor class Subject extends BasicSubject { @ProxyMethod(parameterTypes = { "this.does.not.Exist" }, isOptional = true) public void methodWithMissingParameterType(Object param) { } } IProxyBuildPlan bp = ProxyBuildPlanImpl.create(Subject.class, "myname", ProxyBuildPlanImplTest.class.getClassLoader()); assertThat(bp.getMethods().size(), equalTo(0)); } }
@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")); }
@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")); }