Search in sources :

Example 81 with FieldVisitor

use of org.objectweb.asm.FieldVisitor in project LogisticsPipes by RS485.

the class LogisticsWrapperHandler method getWrappedSubProxy.

@SuppressWarnings("unchecked")
public static <T> T getWrappedSubProxy(AbstractWrapper wrapper, Class<T> interfaze, T proxy, T dummyProxy) throws SecurityException, IllegalArgumentException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException {
    if (proxy == null) {
        return null;
    }
    String proxyName = interfaze.getSimpleName().substring(1);
    String className = "logisticspipes/asm/wrapper/generated/" + proxyName + "ProxyWrapper";
    Class<?> clazz;
    synchronized (lookupMap) {
        clazz = LogisticsWrapperHandler.lookupMap.get(className);
        if (clazz == null) {
            String fieldName = interfaze.getName().replace('.', '/');
            // String classFile = interfaze.getSimpleName().substring(1) + "Wrapper.java";
            ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
            cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, className, null, "logisticspipes/asm/wrapper/AbstractSubWrapper", new String[] { fieldName });
            cw.visitSource(".LP|ASM.dynamic", null);
            {
                FieldVisitor fv = cw.visitField(Opcodes.ACC_PRIVATE, "proxy", "L" + fieldName + ";", null, null);
                fv.visitEnd();
            }
            {
                FieldVisitor fv = cw.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL, "dummyProxy", "L" + fieldName + ";", null, null);
                fv.visitEnd();
            }
            {
                MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "(Llogisticspipes/asm/wrapper/AbstractWrapper;L" + fieldName + ";L" + fieldName + ";)V", null, null);
                mv.visitCode();
                Label l0 = new Label();
                mv.visitLabel(l0);
                mv.visitLineNumber(11, l0);
                mv.visitVarInsn(Opcodes.ALOAD, 0);
                mv.visitVarInsn(Opcodes.ALOAD, 1);
                mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "logisticspipes/asm/wrapper/AbstractSubWrapper", "<init>", "(Llogisticspipes/asm/wrapper/AbstractWrapper;)V");
                Label l1 = new Label();
                mv.visitLabel(l1);
                mv.visitLineNumber(12, l1);
                mv.visitVarInsn(Opcodes.ALOAD, 0);
                mv.visitVarInsn(Opcodes.ALOAD, 2);
                mv.visitFieldInsn(Opcodes.PUTFIELD, className, "dummyProxy", "L" + fieldName + ";");
                Label l2 = new Label();
                mv.visitLabel(l2);
                mv.visitLineNumber(13, l2);
                mv.visitVarInsn(Opcodes.ALOAD, 0);
                mv.visitVarInsn(Opcodes.ALOAD, 3);
                mv.visitFieldInsn(Opcodes.PUTFIELD, className, "proxy", "L" + fieldName + ";");
                Label l3 = new Label();
                mv.visitLabel(l3);
                mv.visitLineNumber(14, l3);
                mv.visitInsn(Opcodes.RETURN);
                Label l4 = new Label();
                mv.visitLabel(l4);
                mv.visitLocalVariable("this", "L" + className + ";", null, l0, l4, 0);
                mv.visitLocalVariable("wrapper", "Llogisticspipes/asm/wrapper/AbstractWrapper;", null, l0, l4, 1);
                mv.visitLocalVariable("dProxy", "L" + fieldName + ";", null, l0, l4, 2);
                mv.visitLocalVariable("iProxy", "L" + fieldName + ";", null, l0, l4, 3);
                mv.visitMaxs(2, 3);
                mv.visitEnd();
            }
            int lineAddition = 100;
            for (Method method : interfaze.getMethods()) {
                LogisticsWrapperHandler.addProxyMethod(cw, method, fieldName, className, lineAddition, !wrapper.getWrapperInterfaces().contains(method.getReturnType()));
                lineAddition += 10;
            }
            cw.visitEnd();
            String lookfor = className.replace('/', '.');
            byte[] bytes = cw.toByteArray();
            if (LogisticsPipes.isDEBUG()) {
                if (LogisticsWrapperHandler.DUMP) {
                    LogisticsWrapperHandler.saveGeneratedClass(bytes, lookfor, "LP_WRAPPER_CLASSES");
                }
                ClassReader cr = new ClassReader(bytes);
                org.objectweb.asm.util.CheckClassAdapter.verify(cr, Launch.classLoader, false, new PrintWriter(System.err));
            }
            try {
                clazz = LogisticsWrapperHandler.loadClass(bytes, lookfor);
            } catch (LinkageError e) {
                try {
                    if (e.getMessage().contains("attempted") && e.getMessage().contains("duplicate class definition")) {
                        Class<?> prev = Class.forName(className);
                        System.err.println(e.getMessage());
                        System.err.printf("Already loaded: %s%n", prev);
                        String resourcePath = className.replace('.', '/').concat(".class");
                        URL classResource = Launch.classLoader.findResource(resourcePath);
                        if (classResource != null) {
                            String path = classResource.getPath();
                            System.err.println("Class source: " + path);
                        } else {
                            System.err.println("Class source: Null");
                        }
                    }
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
                throw e;
            }
            LogisticsWrapperHandler.lookupMap.put(className, clazz);
        }
    }
    T instance = (T) clazz.getConstructor(new Class<?>[] { AbstractWrapper.class, interfaze, interfaze }).newInstance(wrapper, dummyProxy, proxy);
    ((AbstractWrapper) instance).setWrapperInterfaces(wrapper.getWrapperInterfaces());
    return instance;
}
Also used : Label(org.objectweb.asm.Label) Method(java.lang.reflect.Method) FieldVisitor(org.objectweb.asm.FieldVisitor) ClassWriter(org.objectweb.asm.ClassWriter) URL(java.net.URL) VersionNotSupportedException(logisticspipes.proxy.VersionNotSupportedException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MethodVisitor(org.objectweb.asm.MethodVisitor) ClassReader(org.objectweb.asm.ClassReader) PrintWriter(java.io.PrintWriter)

Example 82 with FieldVisitor

use of org.objectweb.asm.FieldVisitor in project LogisticsPipes by RS485.

the class ClassTravelingItemHandler method handleTravelingItemClass.

public static byte[] handleTravelingItemClass(byte[] bytes) {
    final ClassReader reader = new ClassReader(bytes);
    final ClassNode node = new ClassNode();
    reader.accept(node, 0);
    boolean noChecksumMatch = false;
    String sumHandleEvent1 = ASMHelper.getCheckSumForMethod(reader, "toNBT", "(Lnet/minecraft/nbt/NBTTagCompound;)V");
    String sumHandleEvent2 = ASMHelper.getCheckSumForMethod(reader, "<init>", "(Lnet/minecraft/nbt/NBTTagCompound;)V");
    String sumHandleEvent3 = ASMHelper.getCheckSumForMethod(reader, "writePacket", "(Lcofh/core/network/PacketBase;)V");
    if (!"512A30E22A9C24032AAE7CE51271339A4F68D344".equals(sumHandleEvent1) && !"2166AB8FF647A90701787CCCFB4CD1C065BE640D".equals(sumHandleEvent1)) {
        noChecksumMatch = true;
    }
    if (!"E8DD6BEB9676D079E6C97FF8206283E766147E45".equals(sumHandleEvent2) && !"BB4178915C3A10EBAC5158B8742592388E47B181".equals(sumHandleEvent2) && !"0151577E2B8CF9BF2D528A309D5AE919308ABE03".equals(sumHandleEvent2) && !"768681BA4851C581B15713ED3FF9CA6530C24931".equals(sumHandleEvent2)) {
        noChecksumMatch = true;
    }
    if (!"98A853547FA1771D6D7D63E0458960405DFEE092".equals(sumHandleEvent3)) {
        noChecksumMatch = true;
    }
    if (noChecksumMatch) {
        System.out.println("toNBT: " + sumHandleEvent1);
        System.out.println("<init>: " + sumHandleEvent2);
        System.out.println("writePacket: " + sumHandleEvent3);
        new UnsupportedOperationException("This LP version isn't compatible with the installed TD version.").printStackTrace();
        FMLCommonHandler.instance().exitJava(1, true);
    }
    node.interfaces.add("logisticspipes/asm/td/ILPTravelingItemInfo");
    {
        FieldVisitor fv = node.visitField(Opcodes.ACC_PUBLIC, "lpRoutingInformation", "Ljava/lang/Object;", null, null);
        fv.visitEnd();
    }
    {
        MethodVisitor mv;
        mv = node.visitMethod(Opcodes.ACC_PUBLIC, "getLPRoutingInfoAddition", "()Ljava/lang/Object;", null, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(23, l0);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitFieldInsn(Opcodes.GETFIELD, "cofh/thermaldynamics/duct/item/TravelingItem", "lpRoutingInformation", "Ljava/lang/Object;");
        mv.visitInsn(Opcodes.ARETURN);
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "Lcofh/thermaldynamics/duct/item/TravelingItem;", null, l0, l1, 0);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        MethodVisitor mv;
        mv = node.visitMethod(Opcodes.ACC_PUBLIC, "setLPRoutingInfoAddition", "(Ljava/lang/Object;)V", null, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(28, l0);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitFieldInsn(Opcodes.PUTFIELD, "cofh/thermaldynamics/duct/item/TravelingItem", "lpRoutingInformation", "Ljava/lang/Object;");
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLineNumber(29, l1);
        mv.visitInsn(Opcodes.RETURN);
        Label l2 = new Label();
        mv.visitLabel(l2);
        mv.visitLocalVariable("this", "Lcofh/thermaldynamics/duct/item/TravelingItem;", null, l0, l2, 0);
        mv.visitLocalVariable("info", "Ljava/lang/Object;", null, l0, l2, 1);
        mv.visitMaxs(2, 2);
        mv.visitEnd();
    }
    for (MethodNode m : node.methods) {
        if (m.name.equals("toNBT") && m.desc.equals("(Lnet/minecraft/nbt/NBTTagCompound;)V")) {
            MethodNode mv = new MethodNode(Opcodes.ASM4, m.access, m.name, m.desc, m.signature, m.exceptions.toArray(new String[0])) {

                @Override
                public void visitCode() {
                    super.visitCode();
                    Label l = new Label();
                    visitLabel(l);
                    visitVarInsn(Opcodes.ALOAD, 0);
                    visitVarInsn(Opcodes.ALOAD, 1);
                    this.visitMethodInsn(Opcodes.INVOKESTATIC, "logisticspipes/asm/td/ThermalDynamicsHooks", "travelingItemToNBT", "(Lcofh/thermaldynamics/duct/item/TravelingItem;Lnet/minecraft/nbt/NBTTagCompound;)V", false);
                }
            };
            m.accept(mv);
            node.methods.set(node.methods.indexOf(m), mv);
        }
        if (m.name.equals("<init>") && m.desc.equals("(Lnet/minecraft/nbt/NBTTagCompound;)V")) {
            MethodNode mv = new MethodNode(Opcodes.ASM4, m.access, m.name, m.desc, m.signature, m.exceptions.toArray(new String[0])) {

                @Override
                public void visitInsn(int opcode) {
                    if (opcode == Opcodes.RETURN) {
                        visitVarInsn(Opcodes.ALOAD, 0);
                        visitVarInsn(Opcodes.ALOAD, 1);
                        this.visitMethodInsn(Opcodes.INVOKESTATIC, "logisticspipes/asm/td/ThermalDynamicsHooks", "travelingItemNBTContructor", "(Lcofh/thermaldynamics/duct/item/TravelingItem;Lnet/minecraft/nbt/NBTTagCompound;)V", false);
                        Label l = new Label();
                        visitLabel(l);
                    }
                    super.visitInsn(opcode);
                }
            };
            m.accept(mv);
            node.methods.set(node.methods.indexOf(m), mv);
        }
        if (m.name.equals("writePacket") && m.desc.equals("(Lcofh/core/network/PacketBase;)V")) {
            MethodNode mv = new MethodNode(Opcodes.ASM4, m.access, m.name, m.desc, m.signature, m.exceptions.toArray(new String[0])) {

                @Override
                public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) {
                    if (opcode == Opcodes.INVOKEVIRTUAL && "cofh/core/network/PacketBase".equals(owner) && "addItemStack".equals(name) && "(Lnet/minecraft/item/ItemStack;)Lcofh/core/network/PacketBase;".equals(desc)) {
                        visitVarInsn(Opcodes.ALOAD, 0);
                        this.visitMethodInsn(Opcodes.INVOKESTATIC, "logisticspipes/asm/td/ThermalDynamicsHooks", "handleItemSendPacket", "(Lnet/minecraft/item/ItemStack;Lcofh/thermaldynamics/duct/item/TravelingItem;)Lnet/minecraft/item/ItemStack;", false);
                    }
                    super.visitMethodInsn(opcode, owner, name, desc, itf);
                }
            };
            m.accept(mv);
            node.methods.set(node.methods.indexOf(m), mv);
        }
    }
    ClassWriter writer = new ClassWriter(0);
    node.accept(writer);
    return writer.toByteArray();
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) MethodNode(org.objectweb.asm.tree.MethodNode) Label(org.objectweb.asm.Label) ClassReader(org.objectweb.asm.ClassReader) FieldVisitor(org.objectweb.asm.FieldVisitor) ClassWriter(org.objectweb.asm.ClassWriter) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 83 with FieldVisitor

use of org.objectweb.asm.FieldVisitor in project drill by apache.

the class TestIcebergTableSchema method testAllTypes.

@Test
public void testAllTypes() {
    Class<?> clazz = new ClassGenerator(getClass().getSimpleName() + "AllTypes") {

        @Override
        void addFields(ClassWriter classWriter) {
            FieldVisitor stringField = addField(classWriter, Opcodes.ACC_PRIVATE, MetastoreColumn.STORAGE_PLUGIN, String.class);
            annotate(stringField, MetastoreColumn.STORAGE_PLUGIN, MetadataType.ALL);
            FieldVisitor intField = addField(classWriter, Opcodes.ACC_PRIVATE, MetastoreColumn.COLUMN, int.class);
            annotate(intField, MetastoreColumn.COLUMN, MetadataType.TABLE);
            FieldVisitor integerField = addField(classWriter, Opcodes.ACC_PRIVATE, MetastoreColumn.ROW_GROUP_INDEX, Integer.class);
            annotate(integerField, MetastoreColumn.ROW_GROUP_INDEX, MetadataType.SEGMENT);
            FieldVisitor longField = addField(classWriter, Opcodes.ACC_PRIVATE, MetastoreColumn.LAST_MODIFIED_TIME, Long.class);
            annotate(longField, MetastoreColumn.LAST_MODIFIED_TIME, MetadataType.FILE);
            FieldVisitor floatField = addField(classWriter, Opcodes.ACC_PRIVATE, MetastoreColumn.COLUMNS_STATISTICS, Float.class);
            annotate(floatField, MetastoreColumn.COLUMNS_STATISTICS, MetadataType.ALL);
            FieldVisitor doubleField = addField(classWriter, Opcodes.ACC_PRIVATE, MetastoreColumn.ADDITIONAL_METADATA, Double.class);
            annotate(doubleField, MetastoreColumn.ADDITIONAL_METADATA, MetadataType.TABLE);
            FieldVisitor booleanField = addField(classWriter, Opcodes.ACC_PRIVATE, MetastoreColumn.INTERESTING_COLUMNS, Boolean.class);
            annotate(booleanField, MetastoreColumn.INTERESTING_COLUMNS, MetadataType.SEGMENT);
            FieldVisitor listField = addField(classWriter, Opcodes.ACC_PRIVATE, MetastoreColumn.LOCATIONS, List.class, String.class);
            annotate(listField, MetastoreColumn.LOCATIONS, MetadataType.PARTITION);
            FieldVisitor mapField = addField(classWriter, Opcodes.ACC_PRIVATE, MetastoreColumn.HOST_AFFINITY, Map.class, String.class, Float.class);
            annotate(mapField, MetastoreColumn.HOST_AFFINITY, MetadataType.ROW_GROUP);
        }
    }.generate();
    IcebergTableSchema schema = IcebergTableSchema.of(clazz, Collections.emptyList());
    int schemaIndex = IcebergTableSchema.STARTING_SCHEMA_INDEX;
    int complexTypesIndex = IcebergTableSchema.STARTING_COMPLEX_TYPES_INDEX;
    Schema expectedSchema = new Schema(Types.NestedField.optional(schemaIndex++, MetastoreColumn.STORAGE_PLUGIN.columnName(), Types.StringType.get()), Types.NestedField.optional(schemaIndex++, MetastoreColumn.COLUMN.columnName(), Types.IntegerType.get()), Types.NestedField.optional(schemaIndex++, MetastoreColumn.ROW_GROUP_INDEX.columnName(), Types.IntegerType.get()), Types.NestedField.optional(schemaIndex++, MetastoreColumn.LAST_MODIFIED_TIME.columnName(), Types.LongType.get()), Types.NestedField.optional(schemaIndex++, MetastoreColumn.COLUMNS_STATISTICS.columnName(), Types.FloatType.get()), Types.NestedField.optional(schemaIndex++, MetastoreColumn.ADDITIONAL_METADATA.columnName(), Types.DoubleType.get()), Types.NestedField.optional(schemaIndex++, MetastoreColumn.INTERESTING_COLUMNS.columnName(), Types.BooleanType.get()), Types.NestedField.optional(schemaIndex++, MetastoreColumn.LOCATIONS.columnName(), Types.ListType.ofOptional(complexTypesIndex++, Types.StringType.get())), Types.NestedField.optional(schemaIndex, MetastoreColumn.HOST_AFFINITY.columnName(), Types.MapType.ofOptional(complexTypesIndex++, complexTypesIndex, Types.StringType.get(), Types.FloatType.get())));
    assertEquals(expectedSchema.asStruct(), schema.tableSchema().asStruct());
}
Also used : Schema(org.apache.iceberg.Schema) FieldVisitor(org.objectweb.asm.FieldVisitor) ClassWriter(org.objectweb.asm.ClassWriter) List(java.util.List) Map(java.util.Map) IcebergBaseTest(org.apache.drill.metastore.iceberg.IcebergBaseTest) Test(org.junit.Test)

Example 84 with FieldVisitor

use of org.objectweb.asm.FieldVisitor in project drill by apache.

the class CheckClassVisitorFsm method visitField.

@Override
public FieldVisitor visitField(final int access, final String name, final String desc, final String signature, final Object value) {
    fsmCursor.transition("visitField");
    final FieldVisitor fieldVisitor = super.visitField(access, name, desc, signature, value);
    // TODO: add CheckFieldVisitorFsm
    return fieldVisitor;
}
Also used : FieldVisitor(org.objectweb.asm.FieldVisitor)

Example 85 with FieldVisitor

use of org.objectweb.asm.FieldVisitor in project groovy by apache.

the class AsmClassGenerator method visitTypeAnnotations.

private void visitTypeAnnotations(final ClassNode sourceNode, final Object visitor, final TypeReference typeRef, final String typePathStr, boolean typeUse) {
    for (AnnotationNode an : sourceNode.getTypeAnnotations()) {
        if (an.isBuiltIn() || an.hasSourceRetention())
            continue;
        if (typeUse && !an.isTargetAllowed(AnnotationNode.TYPE_USE_TARGET))
            continue;
        AnnotationVisitor av = null;
        final TypePath typePath;
        try {
            typePath = TypePath.fromString(typePathStr);
        } catch (IllegalArgumentException ex) {
            throw new GroovyBugError("Illegal type path for " + sourceNode.getText() + ", typeRef = " + typeRef + ", typePath = " + typePathStr);
        }
        final int typeRefInt = typeRef.getValue();
        final String annotationDescriptor = BytecodeHelper.getTypeDescription(an.getClassNode());
        if (visitor instanceof ClassVisitor) {
            av = ((ClassVisitor) visitor).visitTypeAnnotation(typeRefInt, typePath, annotationDescriptor, an.hasRuntimeRetention());
        } else if (visitor instanceof MethodVisitor) {
            av = ((MethodVisitor) visitor).visitTypeAnnotation(typeRefInt, typePath, annotationDescriptor, an.hasRuntimeRetention());
        } else if (visitor instanceof FieldVisitor) {
            av = ((FieldVisitor) visitor).visitTypeAnnotation(typeRefInt, typePath, annotationDescriptor, an.hasRuntimeRetention());
        } else if (visitor instanceof RecordComponentVisitor) {
            av = ((RecordComponentVisitor) visitor).visitTypeAnnotation(typeRefInt, typePath, annotationDescriptor, an.hasRuntimeRetention());
        } else {
            throwException("Cannot create an AnnotationVisitor. Please report Groovy bug");
        }
        visitAnnotationAttributes(an, av);
        av.visitEnd();
    }
}
Also used : AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) TypePath(org.objectweb.asm.TypePath) AnnotationVisitor(org.objectweb.asm.AnnotationVisitor) GroovyBugError(org.codehaus.groovy.GroovyBugError) RecordComponentVisitor(org.objectweb.asm.RecordComponentVisitor) ClassVisitor(org.objectweb.asm.ClassVisitor) FieldVisitor(org.objectweb.asm.FieldVisitor) TraceMethodVisitor(org.objectweb.asm.util.TraceMethodVisitor) MethodVisitor(org.objectweb.asm.MethodVisitor)

Aggregations

FieldVisitor (org.objectweb.asm.FieldVisitor)90 MethodVisitor (org.objectweb.asm.MethodVisitor)59 Label (org.objectweb.asm.Label)33 ClassWriter (org.objectweb.asm.ClassWriter)28 AnnotationVisitor (org.objectweb.asm.AnnotationVisitor)9 ClassReader (org.objectweb.asm.ClassReader)8 Type (org.objectweb.asm.Type)7 Method (java.lang.reflect.Method)6 ClassVisitor (org.objectweb.asm.ClassVisitor)6 IOException (java.io.IOException)4 GeneratorAdapter (org.objectweb.asm.commons.GeneratorAdapter)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 LitString (lucee.transformer.expression.literal.LitString)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 PrintWriter (java.io.PrintWriter)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 URL (java.net.URL)2