Search in sources :

Example 1 with PUTFIELD

use of org.apache.xbean.asm6.Opcodes.PUTFIELD in project component-runtime by Talend.

the class ProxyGenerator method createConstructor.

private String createConstructor(final ClassWriter cw, final Class<?> classToProxy, final String classFileName, final String proxyClassFileName, final Constructor<?> constructor, final boolean withInterceptors) {
    try {
        Constructor superDefaultCt;
        String parentClassFileName;
        String[] exceptions = null;
        if (classToProxy.isInterface()) {
            parentClassFileName = Type.getInternalName(Object.class);
            superDefaultCt = Object.class.getConstructor();
        } else {
            parentClassFileName = classFileName;
            if (constructor == null) {
                superDefaultCt = classToProxy.getConstructor();
            } else {
                superDefaultCt = constructor;
                Class<?>[] exceptionTypes = constructor.getExceptionTypes();
                exceptions = exceptionTypes.length == 0 ? null : new String[exceptionTypes.length];
                for (int i = 0; i < exceptionTypes.length; i++) {
                    exceptions[i] = Type.getInternalName(exceptionTypes[i]);
                }
            }
        }
        final String descriptor = Type.getConstructorDescriptor(superDefaultCt);
        final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", descriptor, null, exceptions);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        if (constructor != null) {
            for (int i = 1; i <= constructor.getParameterTypes().length; i++) {
                mv.visitVarInsn(ALOAD, i);
            }
        }
        mv.visitMethodInsn(INVOKESPECIAL, parentClassFileName, "<init>", descriptor, false);
        if (withInterceptors) {
            mv.visitVarInsn(ALOAD, 0);
            mv.visitInsn(ACONST_NULL);
            mv.visitFieldInsn(PUTFIELD, proxyClassFileName, FIELD_INTERCEPTOR_HANDLER, Type.getDescriptor(InterceptorHandler.class));
        }
        mv.visitInsn(RETURN);
        mv.visitMaxs(-1, -1);
        mv.visitEnd();
        return parentClassFileName;
    } catch (final NoSuchMethodException e) {
        throw new IllegalStateException(e);
    }
}
Also used : InterceptorHandler(org.talend.sdk.component.api.service.interceptor.InterceptorHandler) Constructor(java.lang.reflect.Constructor) AllArgsConstructor(lombok.AllArgsConstructor) MethodVisitor(org.apache.xbean.asm6.MethodVisitor)

Aggregations

Constructor (java.lang.reflect.Constructor)1 AllArgsConstructor (lombok.AllArgsConstructor)1 MethodVisitor (org.apache.xbean.asm6.MethodVisitor)1 InterceptorHandler (org.talend.sdk.component.api.service.interceptor.InterceptorHandler)1