@Test public void testDefaultSuperClass() throws InvalidProxyDescriptionException { @ProxyFor(implementedInterfaces = { "java.io.Closeable" }) class Subject extends BasicSubject { @ProxyMethod public void close() { } } IProxyBuildPlan bp = ProxyBuildPlanImpl.create(Subject.class, "myname", ProxyBuildPlanImplTest.class.getClassLoader()); assertThat(bp.getSuperClass().getName(), equalTo(Object.class.getName())); }
@Test public void testCorrectSuperClassAndInterfaces() throws InvalidProxyDescriptionException { @ProxyFor(superClass = "java.io.OutputStream", implementedInterfaces = { "java.lang.Runnable", "java.lang.Readable" }) class Subject extends BasicSubject { @ProxyMethod public void run() { } @ProxyMethod public int read(CharBuffer buf) { return 0; } @ProxyMethod public void write(int value) { } } IProxyBuildPlan bp = ProxyBuildPlanImpl.create(Subject.class, "myname", ProxyBuildPlanImplTest.class.getClassLoader()); assertThat(bp.getSuperClass().getName(), equalTo("java.io.OutputStream")); assertThat(bp.getImplementedInterfaces(), containsInAnyOrder(new Class<?>[] { java.lang.Runnable.class, java.lang.Readable.class })); }
mg.invokeConstructor(Type.getType(plan.getSuperClass()), superConstructor);
/** * {@inheritDoc} */ @Override public IProxyClassInfo createProxyClass(IProxyBuildPlan plan) { String internalName = AsmUtil.getAsmInternalName(plan.getProxyClassName()); Type proxyType = Type.getObjectType(internalName); String[] interfaces = getInternalNames(plan.getImplementedInterfaces()); String superClass = Type.getInternalName(plan.getSuperClass()); // generate a new class for the proxy with the defined interfaces and superclass ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); cw.visit(V1_5, ACC_PUBLIC, internalName, null, superClass, interfaces); // create the subject field cw.visitField(ACC_PUBLIC, SUBJECT_FIELD, IInstrumenterConstant.OBJECT_TYPE.getDescriptor(), null, null).visitEnd(); // create the fields for the delegation-target methods // The result map maps the proxied methods to their generated field's names Map<IMethodBuildPlan, String> methodFields = generateDelegationMethodTargetFields(plan, cw); generateConstructor(plan, proxyType, cw); // create the proxied methods for (Entry<IMethodBuildPlan, String> entry : methodFields.entrySet()) { createDelegationMethod(proxyType, entry.getKey(), entry.getValue(), cw); } cw.visitEnd(); byte[] bytecode = cw.toByteArray(); Class<?> cl = loadClass(bytecode, plan.getProxyClassName(), plan.getTargetClassLoader()); // inject the method fields injectDelegationTargetFields(plan, methodFields, cl); return new ASMProxyClassInfo(cl); }