Search in sources :

Example 1 with Label

use of org.simpleflatmapper.ow2asm.Label in project SimpleFlatMapper by arnaudroger.

the class CsvMapperCellHandlerBuilder method appendPeekDelayedCellSetterValue.

private static <T> void appendPeekDelayedCellSetterValue(final DelayedCellSetterFactory<T, ?>[] delayedCellSetters, final ClassWriter cw, final String classType, final int maxMethodSize) {
    ShardingHelper.shard(delayedCellSetters.length, maxMethodSize, new AbstractMethodDispatchShardCallBack<T>(cw, classType, maxMethodSize) {

        @Override
        protected void appendLeafSwitch(MethodVisitor mv, int start, int end) {
            Label defaultLabel = new Label();
            Label[] labels = newLabels(end - start);
            mv.visitTableSwitchInsn(start, end - 1, defaultLabel, labels);
            for (int i = start; i < end; i++) {
                mv.visitLabel(labels[i - start]);
                mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
                if (delayedCellSetters[i] != null) {
                    mv.visitVarInsn(Opcodes.ALOAD, 0);
                    mv.visitFieldInsn(Opcodes.GETFIELD, classType, "delayedCellSetter" + i, "L" + DELAYED_CELL_SETTER_TYPE + ";");
                    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, DELAYED_CELL_SETTER_TYPE, "peekValue", "()Ljava/lang/Object;", true);
                    mv.visitInsn(Opcodes.ARETURN);
                } else if (i < (delayedCellSetters.length - 1)) {
                    mv.visitInsn(Opcodes.ACONST_NULL);
                    mv.visitInsn(Opcodes.ARETURN);
                }
            }
            mv.visitLabel(defaultLabel);
            mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
        }

        @Override
        protected int maxArgIndex() {
            return 1;
        }

        @Override
        protected void loadArguments(MethodVisitor mv) {
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitVarInsn(Opcodes.ILOAD, 1);
        }

        @Override
        protected int argIndex() {
            return 1;
        }

        @Override
        protected String name() {
            return "_peekDelayedCellSetterValue";
        }

        @Override
        protected String signature() {
            return "(I)Ljava/lang/Object;";
        }
    });
    MethodVisitor mv;
    mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "peekDelayedCellSetterValue", "(L" + AsmUtils.toAsmType(CsvColumnKey.class) + ";)Ljava/lang/Object;", null, null);
    mv.visitCode();
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, AsmUtils.toAsmType(CsvColumnKey.class), "getIndex", "()I", false);
    mv.visitVarInsn(Opcodes.ISTORE, 2);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ILOAD, 2);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, classType, "_peekDelayedCellSetterValue", "(I)Ljava/lang/Object;", false);
    mv.visitInsn(Opcodes.ARETURN);
    mv.visitMaxs(1, 2);
    mv.visitEnd();
}
Also used : Label(org.simpleflatmapper.ow2asm.Label) CsvColumnKey(org.simpleflatmapper.csv.CsvColumnKey) MethodVisitor(org.simpleflatmapper.ow2asm.MethodVisitor)

Example 2 with Label

use of org.simpleflatmapper.ow2asm.Label in project SimpleFlatMapper by arnaudroger.

the class CsvMapperCellHandlerBuilder method appendGetDelayedCellSetterSwitch.

private static <T> void appendGetDelayedCellSetterSwitch(DelayedCellSetterFactory<T, ?>[] delayedCellSetters, String classType, MethodVisitor mv, int switchStart, int switchEnd) {
    mv.visitVarInsn(Opcodes.ILOAD, 1);
    Label defaultLabel = new Label();
    Label[] labels = newLabels(switchEnd - switchStart);
    mv.visitTableSwitchInsn(switchStart, switchEnd - 1, defaultLabel, labels);
    for (int i = switchStart; i < switchEnd; i++) {
        mv.visitLabel(labels[i - switchStart]);
        mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
        if (delayedCellSetters != null) {
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitFieldInsn(Opcodes.GETFIELD, classType, "delayedCellSetter" + i, "L" + DELAYED_CELL_SETTER_TYPE + ";");
        } else {
            mv.visitInsn(Opcodes.ACONST_NULL);
        }
        mv.visitInsn(Opcodes.ARETURN);
    }
    mv.visitLabel(defaultLabel);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
}
Also used : Label(org.simpleflatmapper.ow2asm.Label)

Example 3 with Label

use of org.simpleflatmapper.ow2asm.Label in project SimpleFlatMapper by arnaudroger.

the class AsmInstantiatorDefinitionFactory method extractDefinitions.

public static List<InstantiatorDefinition> extractDefinitions(final Type target) throws IOException {
    final List<InstantiatorDefinition> constructors = new ArrayList<InstantiatorDefinition>();
    final Class<?> targetClass = TypeHelper.toClass(target);
    ClassLoader cl = targetClass.getClassLoader();
    if (cl == null) {
        cl = ClassLoader.getSystemClassLoader();
    }
    final String fileName = targetClass.getName().replace('.', '/') + ".class";
    final InputStream is = cl.getResourceAsStream(fileName);
    try {
        if (is == null) {
            throw new IOException("Cannot find file " + fileName + " in " + cl);
        }
        ClassReader classReader = new ClassReader(is);
        classReader.accept(new ClassVisitor(Opcodes.ASM5) {

            List<String> genericTypeNames;

            @Override
            public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
                if (signature != null) {
                    genericTypeNames = AsmUtils.extractGenericTypeNames(signature);
                } else {
                    genericTypeNames = Collections.emptyList();
                }
                super.visit(version, access, name, signature, superName, interfaces);
            }

            @Override
            public MethodVisitor visitMethod(int access, final String methodName, String desc, String signature, String[] exceptions) {
                final boolean isConstructor = "<init>".equals(methodName);
                if ((Opcodes.ACC_PUBLIC & access) == Opcodes.ACC_PUBLIC && (isConstructor || ((Opcodes.ACC_STATIC & access) == Opcodes.ACC_STATIC && !desc.endsWith("V")))) {
                    final List<String> descTypes = AsmUtils.extractTypeNamesFromSignature(desc);
                    final List<String> genericTypes;
                    final List<String> names = new ArrayList<String>();
                    if (signature != null) {
                        genericTypes = AsmUtils.extractTypeNamesFromSignature(signature);
                    } else {
                        genericTypes = descTypes;
                    }
                    if (!isConstructor) {
                        if (descTypes.size() > 0) {
                            try {
                                final Type genericType = AsmUtils.toGenericType(descTypes.get(descTypes.size() - 1), genericTypeNames, target);
                                if (!targetClass.isAssignableFrom(TypeHelper.toClass(genericType))) {
                                    return null;
                                }
                            } catch (ClassNotFoundException e) {
                                return null;
                            }
                        } else
                            return null;
                    }
                    return new MethodVisitor(Opcodes.ASM5) {

                        Label firstLabel;

                        Label lastLabel;

                        @Override
                        public void visitLabel(Label label) {
                            if (firstLabel == null) {
                                firstLabel = label;
                            }
                            lastLabel = label;
                        }

                        @Override
                        public void visitLocalVariable(String name, String desc, String signature, Label start, Label end, int index) {
                            if (start.equals(firstLabel) && end.equals(lastLabel) && !"this".equals(name)) {
                                names.add(name);
                            }
                        }

                        @Override
                        public void visitEnd() {
                            try {
                                final List<Parameter> parameters = new ArrayList<Parameter>();
                                int l = descTypes.size() - (isConstructor ? 0 : 1);
                                for (int i = 0; i < l; i++) {
                                    String name = "arg" + i;
                                    if (i < names.size()) {
                                        name = names.get(i);
                                    }
                                    parameters.add(createParameter(i, name, descTypes.get(i), genericTypes.get(i)));
                                }
                                final Member executable;
                                if (isConstructor) {
                                    executable = targetClass.getDeclaredConstructor(toTypeArray(parameters));
                                } else {
                                    executable = targetClass.getDeclaredMethod(methodName, toTypeArray(parameters));
                                }
                                constructors.add(new ExecutableInstantiatorDefinition(executable, parameters.toArray(new Parameter[0])));
                            } catch (Exception e) {
                                ErrorHelper.rethrow(e);
                            }
                        }

                        private Class<?>[] toTypeArray(List<Parameter> parameters) {
                            Class<?>[] types = new Class<?>[parameters.size()];
                            for (int i = 0; i < types.length; i++) {
                                types[i] = parameters.get(i).getType();
                            }
                            return types;
                        }

                        private Parameter createParameter(int index, String name, String desc, String signature) {
                            try {
                                Type basicType = AsmUtils.toGenericType(desc, genericTypeNames, target);
                                Type genericType = basicType;
                                if (signature != null) {
                                    Type type = AsmUtils.toGenericType(signature, genericTypeNames, target);
                                    if (type != null) {
                                        genericType = type;
                                    }
                                }
                                return new Parameter(index, name, TypeHelper.toClass(basicType), genericType);
                            } catch (ClassNotFoundException e) {
                                throw new Error("Unexpected error " + e, e);
                            }
                        }
                    };
                } else {
                    return null;
                }
            }
        }, 0);
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (Exception e) {
            }
        }
    }
    return constructors;
}
Also used : ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition) ArrayList(java.util.ArrayList) Label(org.simpleflatmapper.ow2asm.Label) ClassVisitor(org.simpleflatmapper.ow2asm.ClassVisitor) MethodVisitor(org.simpleflatmapper.ow2asm.MethodVisitor) ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition) InstantiatorDefinition(org.simpleflatmapper.reflect.InstantiatorDefinition) ArrayList(java.util.ArrayList) List(java.util.List) Member(java.lang.reflect.Member) InputStream(java.io.InputStream) IOException(java.io.IOException) IOException(java.io.IOException) Type(java.lang.reflect.Type) ClassReader(org.simpleflatmapper.ow2asm.ClassReader) Parameter(org.simpleflatmapper.reflect.Parameter)

Example 4 with Label

use of org.simpleflatmapper.ow2asm.Label in project SimpleFlatMapper by arnaudroger.

the class CsvMapperCellHandlerBuilder method appendCellValue.

private static <T> void appendCellValue(CellSetter<T>[] setters, boolean ignoreException, ClassWriter cw, String classType) {
    MethodVisitor mv;
    mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "cellValue", "([CIII)V", null, null);
    mv.visitCode();
    if (setters.length != 0) {
        if (ignoreException) {
            callCellValue(mv, classType);
        } else {
            Label l0 = new Label();
            Label l1 = new Label();
            Label l2 = new Label();
            mv.visitTryCatchBlock(l0, l1, l2, "java/lang/Exception");
            mv.visitLabel(l0);
            callCellValue(mv, classType);
            mv.visitLabel(l1);
            Label l3 = new Label();
            mv.visitJumpInsn(Opcodes.GOTO, l3);
            mv.visitLabel(l2);
            mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Exception" });
            mv.visitVarInsn(Opcodes.ASTORE, 5);
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitVarInsn(Opcodes.ILOAD, 4);
            mv.visitVarInsn(Opcodes.ALOAD, 5);
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classType, "fieldError", "(ILjava/lang/Exception;)V", false);
            mv.visitLabel(l3);
            mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
        }
    }
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(5, 6);
    mv.visitEnd();
}
Also used : Label(org.simpleflatmapper.ow2asm.Label) MethodVisitor(org.simpleflatmapper.ow2asm.MethodVisitor)

Example 5 with Label

use of org.simpleflatmapper.ow2asm.Label in project SimpleFlatMapper by arnaudroger.

the class CsvMapperCellHandlerBuilder method appendDelayedCellValue.

private static <T> void appendDelayedCellValue(DelayedCellSetterFactory<T, ?>[] delayedCellSetters, boolean ignoreException, ClassWriter cw, String classType) {
    MethodVisitor mv;
    mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "delayedCellValue", "([CIII)V", null, null);
    mv.visitCode();
    if (delayedCellSetters.length != 0) {
        if (ignoreException) {
            callDelayedCellValue(mv, classType);
        } else {
            Label l0 = new Label();
            Label l1 = new Label();
            Label l2 = new Label();
            mv.visitTryCatchBlock(l0, l1, l2, "java/lang/Exception");
            mv.visitLabel(l0);
            callDelayedCellValue(mv, classType);
            mv.visitLabel(l1);
            Label l3 = new Label();
            mv.visitJumpInsn(Opcodes.GOTO, l3);
            mv.visitLabel(l2);
            mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Exception" });
            mv.visitVarInsn(Opcodes.ASTORE, 5);
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitVarInsn(Opcodes.ILOAD, 4);
            mv.visitVarInsn(Opcodes.ALOAD, 5);
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classType, "fieldError", "(ILjava/lang/Exception;)V", false);
            mv.visitLabel(l3);
            mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
        }
    }
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(5, 6);
    mv.visitEnd();
}
Also used : Label(org.simpleflatmapper.ow2asm.Label) MethodVisitor(org.simpleflatmapper.ow2asm.MethodVisitor)

Aggregations

Label (org.simpleflatmapper.ow2asm.Label)7 MethodVisitor (org.simpleflatmapper.ow2asm.MethodVisitor)4 Method (java.lang.reflect.Method)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Member (java.lang.reflect.Member)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 CsvColumnKey (org.simpleflatmapper.csv.CsvColumnKey)1 ClassReader (org.simpleflatmapper.ow2asm.ClassReader)1 ClassVisitor (org.simpleflatmapper.ow2asm.ClassVisitor)1 InstantiatorDefinition (org.simpleflatmapper.reflect.InstantiatorDefinition)1 Parameter (org.simpleflatmapper.reflect.Parameter)1 ExecutableInstantiatorDefinition (org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition)1