List of usage examples for org.objectweb.asm MethodVisitor visitLabel
public void visitLabel(final Label label)
From source file:org.apache.directmemory.lightning.internal.generator.BytecodeMarshallerGenerator.java
License:Apache License
private void createConstructor(ClassWriter cw, String className, List<PropertyDescriptor> propertyDescriptors) { MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", MARSHALLER_CONSTRUCTOR_SIGNATURE, null, null); mv.visitCode();/*from ww w . j ava 2 s . c o m*/ // Load this mv.visitVarInsn(ALOAD, 0); // Load first parameter type (Class) mv.visitVarInsn(ALOAD, 1); // Load second parameter type (Map) mv.visitVarInsn(ALOAD, 2); // Load third parameter type (ClassDescriptorAwaySerializer) mv.visitVarInsn(ALOAD, 3); // Load fourth parameter type (ObjenesisSerializer) mv.visitVarInsn(ALOAD, 4); // Call super(Class, Map) mv.visitMethodInsn(INVOKESPECIAL, SUPER_CLASS_INTERNAL_TYPE, "<init>", MARSHALLER_SUPER_CONSTRUCTOR_SIGNATURE); // Fill fields with marshallers for (int i = 0; i < propertyDescriptors.size(); i++) { PropertyDescriptor propertyDescriptor = propertyDescriptors.get(i); String fieldName = toFinalFieldName("marshaller", propertyDescriptor); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 0); // Load property type mv.visitVarInsn(ALOAD, 5); mv.visitIntInsn(BIPUSH, i); mv.visitMethodInsn(INVOKEINTERFACE, LIST_CLASS_INTERNAL_TYPE, "get", "(I)Ljava/lang/Object;"); // Store PropertyDescriptor mv.visitTypeInsn(CHECKCAST, PROPERTYDESCRIPTOR_CLASS_INTERNAL_TYPE); mv.visitFieldInsn(PUTFIELD, className, toFinalFieldName("descriptor", propertyDescriptor), PROPERTYDESCRIPTOR_CLASS_DESCRIPTOR); if (propertyDescriptor.getType().isArray() && !propertyDescriptor.getType().getComponentType().isPrimitive()) { Label labelNonNull = new Label(); // Get type from PropertyAccessor mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, toFinalFieldName("descriptor", propertyDescriptor), PROPERTYDESCRIPTOR_CLASS_DESCRIPTOR); mv.visitMethodInsn(INVOKEINTERFACE, PROPERTYDESCRIPTOR_CLASS_INTERNAL_TYPE, "getType", "()Ljava/lang/Class;"); // Get array component type mv.visitMethodInsn(INVOKEVIRTUAL, CLASS_CLASS_INTERNAL_TYPE, "getComponentType", CLASS_GET_COMPONENT_TYPE); mv.visitVarInsn(ASTORE, 9); // Generate cheating PropertyDescriptor for component type mv.visitVarInsn(ALOAD, 0); mv.visitTypeInsn(NEW, CHEATINGPROPERTYDESCRIPTOR_CLASS_INTERNAL_TYPE); mv.visitInsn(DUP); mv.visitLdcInsn(propertyDescriptor.getPropertyName() + "Element"); mv.visitVarInsn(ALOAD, 9); mv.visitInsn(ACONST_NULL); mv.visitMethodInsn(INVOKESPECIAL, CHEATINGPROPERTYDESCRIPTOR_CLASS_INTERNAL_TYPE, "<init>", CHEATINGPROPERTYDESCRIPTOR_CONSTRUCTOR); mv.visitFieldInsn(PUTFIELD, className, toFinalFieldName("component", propertyDescriptor), CHEATINGPROPERTYDESCRIPTOR_CLASS_DESCRIPTOR); // Search marshaller by using interal ones mv.visitVarInsn(ALOAD, 6); mv.visitVarInsn(ALOAD, 9); mv.visitInsn(ACONST_NULL); mv.visitMethodInsn(INVOKEINTERFACE, MARSHALLERSTRATEGY_CLASS_INTERNAL_TYPE, "getMarshaller", MARSHALLERSTRATEGY_GET_MARSHALLER_SIGNATURE); mv.visitVarInsn(ASTORE, 8); mv.visitVarInsn(ALOAD, 8); // Search marshaller for property type mv.visitJumpInsn(IFNONNULL, labelNonNull); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, toFinalFieldName("component", propertyDescriptor), CHEATINGPROPERTYDESCRIPTOR_CLASS_DESCRIPTOR); mv.visitMethodInsn(INVOKEVIRTUAL, SUPER_CLASS_INTERNAL_TYPE, "findMarshaller", MARSHALLER_FIND_MARSHALLER_SIGNATURE); mv.visitVarInsn(ASTORE, 8); // Save marshaller to field mv.visitLabel(labelNonNull); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 8); mv.visitFieldInsn(PUTFIELD, className, toFinalFieldName("marshaller", propertyDescriptor), MARSHALLER_CLASS_DESCRIPTOR); } else { // Check if marshaller is defined mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, toFinalFieldName("descriptor", propertyDescriptor), PROPERTYDESCRIPTOR_CLASS_DESCRIPTOR); mv.visitMethodInsn(INVOKEVIRTUAL, SUPER_CLASS_INTERNAL_TYPE, "findMarshaller", MARSHALLER_FIND_MARSHALLER_SIGNATURE); mv.visitFieldInsn(PUTFIELD, className, fieldName, MARSHALLER_CLASS_DESCRIPTOR); } // Load this to method stack mv.visitVarInsn(ALOAD, 0); mv.visitInsn(DUP); // Load property accessor mv.visitFieldInsn(GETFIELD, className, toFinalFieldName("descriptor", propertyDescriptor), PROPERTYDESCRIPTOR_CLASS_DESCRIPTOR); mv.visitMethodInsn(INVOKEINTERFACE, PROPERTYDESCRIPTOR_CLASS_INTERNAL_TYPE, "getPropertyAccessor", PROPERTY_DESCRIPTOR_GET_PROPERTYACCESSOR_SIGNATURE); // Save PropertyAccessor to field mv.visitFieldInsn(PUTFIELD, className, toFinalFieldName("accessor", propertyDescriptor), PROPERTYACCESSOR_CLASS_DESCRIPTOR); } mv.visitInsn(RETURN); mv.visitMaxs(-1, 12); mv.visitEnd(); }
From source file:org.apache.directmemory.lightning.internal.generator.BytecodeMarshallerGenerator.java
License:Apache License
private void createMarshallMethod(ClassWriter cw, String className, Class<?> type, SerializationStrategy serializationStrategy, List<PropertyDescriptor> propertyDescriptors) { MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "marshall", MARSHALLER_MARSHALL_SIGNATURE, null, MARSHALLER_EXCEPTIONS);/* w w w . j av a 2 s.c o m*/ // If element type is not reference capable or SerializationStrategy is // not SizeOptimized just prevent generation of code if (serializationStrategy == SerializationStrategy.SizeOptimized && ClassUtil.isReferenceCapable(type)) { // Load this to method stack mv.visitVarInsn(ALOAD, 0); // Load value to method stack mv.visitVarInsn(ALOAD, 1); // Load type to method stack mv.visitVarInsn(ALOAD, 2); // Load dataOutput to method stack mv.visitVarInsn(ALOAD, 3); // Load serializationContext to method stack mv.visitVarInsn(ALOAD, 4); // Call super.isAlreadyMarshalled(...); mv.visitMethodInsn(INVOKEVIRTUAL, SUPER_CLASS_INTERNAL_TYPE, "isAlreadyMarshalled", MARSHALLER_IS_ALREADY_MARSHALLED_SIGNATURE); // Label if value is not yet marshalled Label notYetMarshalled = new Label(); // Test if already marshalled mv.visitJumpInsn(IFEQ, notYetMarshalled); // If marshalled - just return mv.visitInsn(RETURN); // Visit label - if not yet marshalled - marshall it mv.visitLabel(notYetMarshalled); } for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { if (propertyDescriptor.getType().isArray() && !propertyDescriptor.getType().getComponentType().isPrimitive()) { visitObjectArrayPropertyAccessorRead(mv, className, propertyDescriptor); } else { visitValuePropertyAccessorRead(mv, className, propertyDescriptor); } } // Add Return instruction mv.visitInsn(RETURN); // End visiting mv.visitMaxs(9, 9); mv.visitEnd(); }
From source file:org.apache.directmemory.lightning.internal.generator.BytecodeMarshallerGenerator.java
License:Apache License
private void visitObjectArrayPropertyAccessorRead(MethodVisitor mv, String className, PropertyDescriptor propertyDescriptor) { Class<?> propertyType = propertyDescriptor.getType(); // Load this to method stack mv.visitVarInsn(ALOAD, 0);//from w w w. ja v a 2s. c om // Read PropertyAccessor from field mv.visitFieldInsn(GETFIELD, className, toFinalFieldName("accessor", propertyDescriptor), PROPERTYACCESSOR_CLASS_DESCRIPTOR); mv.visitTypeInsn(CHECKCAST, ARRAYPROPERTYACCESSOR_CLASS_INTERNAL_TYPE); mv.visitVarInsn(ASTORE, 8); // Load property type mv.visitVarInsn(ALOAD, 8); mv.visitInsn(DUP); mv.visitMethodInsn(INVOKEINTERFACE, PROPERTYACCESSOR_CLASS_INTERNAL_TYPE, "getType", OBJECT_GET_CLASS_SIGNATURE); mv.visitVarInsn(ASTORE, 5); // Load value to method stack mv.visitVarInsn(ALOAD, 1); // Save array to stack position 6 visitPropertyAccessorValueRead(propertyType, mv); mv.visitTypeInsn(CHECKCAST, Type.getType(propertyType).getInternalName()); mv.visitVarInsn(ASTORE, 6); // Save length to stream mv.visitVarInsn(ALOAD, 3); mv.visitVarInsn(ALOAD, 6); mv.visitInsn(ARRAYLENGTH); mv.visitMethodInsn(INVOKEINTERFACE, DATAOUTPUT_CLASS_INTERNAL_TYPE, "writeInt", "(I)V"); // Loop over every element in array Label forLoopEnd = new Label(); Label forLoopStart = new Label(); mv.visitInsn(ICONST_0); mv.visitVarInsn(ISTORE, 7); mv.visitJumpInsn(GOTO, forLoopEnd); mv.visitLabel(forLoopStart); // Load this to method stack mv.visitVarInsn(ALOAD, 0); // Load property marshaller on stack mv.visitFieldInsn(GETFIELD, className, toFinalFieldName("marshaller", propertyDescriptor), MARSHALLER_CLASS_DESCRIPTOR); // Load PropertyAccessor to method stack mv.visitVarInsn(ALOAD, 8); // Load array to method stack mv.visitVarInsn(ALOAD, 6); // Load index to method stack mv.visitVarInsn(ILOAD, 7); // Get value from array mv.visitMethodInsn(INVOKEINTERFACE, ARRAYPROPERTYACCESSOR_CLASS_INTERNAL_TYPE, "readObject", PROPERTY_ACCESSOR_ARRAY_READ_OBJECT_SIGNATURE); mv.visitTypeInsn(CHECKCAST, Type.getType(propertyType.getComponentType()).getInternalName()); // If type is primitive add some "autoboxing" magic if (propertyType.getComponentType().isPrimitive()) { visitWrapperAutoboxing(propertyType.getComponentType(), mv); } // Load type to method stack mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, toFinalFieldName("component", propertyDescriptor), CHEATINGPROPERTYDESCRIPTOR_CLASS_DESCRIPTOR); // Load DataOutput to method stack mv.visitVarInsn(ALOAD, 3); // Load SerializationContext to method stack mv.visitVarInsn(ALOAD, 4); // Call Marshaller#marshall on properties marshaller mv.visitMethodInsn(INVOKEINTERFACE, MARSHALLER_CLASS_INTERNAL_TYPE, "marshall", MARSHALLER_MARSHALL_SIGNATURE); // Test if loop ends mv.visitIincInsn(7, 1); mv.visitLabel(forLoopEnd); mv.visitVarInsn(ILOAD, 7); mv.visitVarInsn(ALOAD, 6); mv.visitInsn(ARRAYLENGTH); mv.visitJumpInsn(IF_ICMPLT, forLoopStart); }
From source file:org.apache.directmemory.lightning.internal.generator.BytecodeMarshallerGenerator.java
License:Apache License
private void visitObjectArrayPropertyAccessorWrite(MethodVisitor mv, String className, PropertyDescriptor propertyDescriptor) { Class<?> propertyType = propertyDescriptor.getType(); Class<?> componentType = propertyType.getComponentType(); // Read size// w w w .j a v a 2 s . co m mv.visitVarInsn(ALOAD, 3); mv.visitMethodInsn(INVOKEINTERFACE, DATAINPUT_CLASS_INTERNAL_TYPE, "readInt", "()I"); mv.visitInsn(DUP); mv.visitVarInsn(ISTORE, 5); // Instantiate array mv.visitTypeInsn(ANEWARRAY, Type.getType(componentType).getInternalName()); mv.visitVarInsn(ASTORE, 6); // Read PropertyAccessor from field mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, toFinalFieldName("accessor", propertyDescriptor), PROPERTYACCESSOR_CLASS_DESCRIPTOR); mv.visitVarInsn(ASTORE, 9); // Get component type mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, toFinalFieldName("component", propertyDescriptor), CHEATINGPROPERTYDESCRIPTOR_CLASS_DESCRIPTOR); mv.visitVarInsn(ASTORE, 8); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, toFinalFieldName("marshaller", propertyDescriptor), MARSHALLER_CLASS_DESCRIPTOR); mv.visitVarInsn(ASTORE, 10); // For loop Label forLoopStart = new Label(); Label forLoopEnd = new Label(); mv.visitInsn(ICONST_0); mv.visitVarInsn(ISTORE, 7); mv.visitJumpInsn(GOTO, forLoopEnd); mv.visitLabel(forLoopStart); // Write value to array mv.visitVarInsn(ALOAD, 9); mv.visitVarInsn(ALOAD, 6); mv.visitVarInsn(ILOAD, 7); // Read value from stream to 4th stack position mv.visitVarInsn(ALOAD, 10); mv.visitVarInsn(ALOAD, 8); mv.visitVarInsn(ALOAD, 3); mv.visitVarInsn(ALOAD, 4); mv.visitMethodInsn(INVOKEINTERFACE, MARSHALLER_CLASS_INTERNAL_TYPE, "unmarshall", MARSHALLER_BASE_UNMARSHALL_SIGNATURE); mv.visitMethodInsn(INVOKEINTERFACE, ARRAYPROPERTYACCESSOR_CLASS_INTERNAL_TYPE, "writeObject", PROPERTY_ACCESSOR_ARRAY_WRITE_OBJECT_SIGNATURE); // Increment counter mv.visitIincInsn(7, 1); // Test for loop end mv.visitLabel(forLoopEnd); mv.visitVarInsn(ILOAD, 7); mv.visitVarInsn(ILOAD, 5); mv.visitJumpInsn(IF_ICMPLT, forLoopStart); // Write array to object mv.visitVarInsn(ALOAD, 9); mv.visitVarInsn(ALOAD, 1); mv.visitVarInsn(ALOAD, 6); visitPropertyAccessorValueWrite(propertyType.getComponentType(), mv); }
From source file:org.apache.felix.ipojo.composite.service.provides.POJOWriter.java
License:Apache License
/** * Generate on method.// w ww . ja v a 2 s . co m * @param cw : class writer * @param className : the current class name * @param method : the method to generate * @param sign : method signature to generate * @param delegator : the field on which delegate * @param handler : the handler (used to acess the logger) */ private static void generateMethod(ClassWriter cw, String className, MethodMetadata method, Method sign, FieldMetadata delegator, Handler handler) { String desc = Type.getMethodDescriptor(sign); String name = sign.getName(); String[] exc = new String[sign.getExceptionTypes().length]; for (int i = 0; i < sign.getExceptionTypes().length; i++) { exc[i] = Type.getType(sign.getExceptionTypes()[i]).getInternalName(); } MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, name, desc, null, exc); if (delegator.isOptional()) { if (!delegator.isAggregate()) { generateOptionalCase(mv, delegator, className); } if (delegator.isAggregate() /*&& method.getPolicy() == MethodMetadata.ONE_POLICY*/) { generateOptionalAggregateCase(mv, delegator, className); } } if (delegator.isAggregate()) { if (method.getPolicy() == MethodMetadata.ONE_POLICY) { // Aggregate and One Policy mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, delegator.getName(), "[L" + delegator.getSpecification().getName().replace('.', '/') + ";"); mv.visitInsn(ICONST_0); // Use the first one mv.visitInsn(AALOAD); loadArgs(mv, ACC_PUBLIC, Type.getArgumentTypes(desc)); // Invoke mv.visitMethodInsn(INVOKEINTERFACE, delegator.getSpecification().getName().replace('.', '/'), name, desc); // Return mv.visitInsn(Type.getReturnType(desc).getOpcode(Opcodes.IRETURN)); } else { // All policy if (Type.getReturnType(desc).getSort() != Type.VOID) { handler.error("All policy cannot be used on method which does not return void"); } Type[] args = Type.getArgumentTypes(desc); int index = args.length + 1; // Init mv.visitInsn(ICONST_0); mv.visitVarInsn(ISTORE, index); Label l1b = new Label(); mv.visitLabel(l1b); Label l2b = new Label(); mv.visitJumpInsn(GOTO, l2b); // Loop Label l3b = new Label(); mv.visitLabel(l3b); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, delegator.getName(), "[L" + delegator.getSpecification().getName().replace('.', '/') + ";"); mv.visitVarInsn(ILOAD, index); mv.visitInsn(AALOAD); loadArgs(mv, ACC_PUBLIC, Type.getArgumentTypes(desc)); mv.visitMethodInsn(INVOKEINTERFACE, delegator.getSpecification().getName().replace('.', '/'), name, desc); Label l4b = new Label(); mv.visitLabel(l4b); mv.visitIincInsn(index, 1); // i++; // Condition mv.visitLabel(l2b); mv.visitVarInsn(ILOAD, index); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, delegator.getName(), "[L" + delegator.getSpecification().getName().replace('.', '/') + ";"); mv.visitInsn(ARRAYLENGTH); mv.visitJumpInsn(IF_ICMPLT, l3b); Label l5b = new Label(); mv.visitLabel(l5b); mv.visitInsn(RETURN); } } else { mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, delegator.getName(), "L" + delegator.getSpecification().getName().replace('.', '/') + ";"); loadArgs(mv, ACC_PUBLIC, Type.getArgumentTypes(desc)); // Invoke if (delegator.getSpecification().isInterface()) { mv.visitMethodInsn(INVOKEINTERFACE, delegator.getSpecification().getName().replace('.', '/'), name, desc); } else { mv.visitMethodInsn(INVOKEVIRTUAL, delegator.getSpecification().getName().replace('.', '/'), name, desc); } // Return mv.visitInsn(Type.getReturnType(desc).getOpcode(IRETURN)); } mv.visitMaxs(0, 0); mv.visitEnd(); }
From source file:org.apache.felix.ipojo.composite.service.provides.POJOWriter.java
License:Apache License
/** * Generate Optional Case for aggregate field. * @param mv : method visitor/* w w w .j a v a2s .com*/ * @param delegator : Field on which delegate * @param className : current class name */ private static void generateOptionalAggregateCase(MethodVisitor mv, FieldMetadata delegator, String className) { mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, delegator.getName(), "[L" + delegator.getSpecification().getName().replace('.', '/') + ";"); mv.visitInsn(ARRAYLENGTH); Label l1a = new Label(); mv.visitJumpInsn(IFNE, l1a); Label l2a = new Label(); mv.visitLabel(l2a); mv.visitTypeInsn(NEW, "java/lang/UnsupportedOperationException"); mv.visitInsn(DUP); mv.visitLdcInsn("Operation not supported"); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/UnsupportedOperationException", "<init>", "(Ljava/lang/String;)V"); mv.visitInsn(ATHROW); mv.visitLabel(l1a); }
From source file:org.apache.felix.ipojo.composite.service.provides.POJOWriter.java
License:Apache License
/** * Generate Optional case for non aggregate fields. * /* w w w. j a v a 2s . c o m*/ * @param mv : the method visitor * @param delegator : the field on which delegate. * @param className : the name of the current class. */ private static void generateOptionalCase(MethodVisitor mv, FieldMetadata delegator, String className) { mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, delegator.getName(), "L" + delegator.getSpecification().getName().replace('.', '/') + ";"); mv.visitTypeInsn(INSTANCEOF, "org/apache/felix/ipojo/Nullable"); Label end = new Label(); mv.visitJumpInsn(IFEQ, end); Label begin = new Label(); mv.visitLabel(begin); mv.visitTypeInsn(NEW, "java/lang/UnsupportedOperationException"); mv.visitInsn(DUP); mv.visitLdcInsn("Operation not supported"); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/UnsupportedOperationException", "<init>", "(Ljava/lang/String;)V"); mv.visitInsn(ATHROW); mv.visitLabel(end); }
From source file:org.apache.felix.ipojo.manipulation.ClassManipulator.java
License:Apache License
/** * Create the setter method for the __cm field. *//* www. j av a 2 s . com*/ private void createSetInstanceManagerMethod() { MethodVisitor mv = cv.visitMethod(ACC_PRIVATE, "_setInstanceManager", "(Lorg/apache/felix/ipojo/InstanceManager;)V", null, null); mv.visitCode(); // If the given instance manager is null, just returns. mv.visitVarInsn(ALOAD, 1); Label l1 = new Label(); mv.visitJumpInsn(IFNONNULL, l1); mv.visitInsn(RETURN); mv.visitLabel(l1); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 1); mv.visitFieldInsn(PUTFIELD, m_owner, IM_FIELD, "Lorg/apache/felix/ipojo/InstanceManager;"); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, m_owner, IM_FIELD, "Lorg/apache/felix/ipojo/InstanceManager;"); mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/felix/ipojo/InstanceManager", "getRegistredFields", "()Ljava/util/Set;", false); mv.visitVarInsn(ASTORE, 2); mv.visitVarInsn(ALOAD, 2); Label endif = new Label(); mv.visitJumpInsn(IFNULL, endif); for (String field : m_fields) { mv.visitVarInsn(ALOAD, 2); mv.visitLdcInsn(field); mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Set", "contains", "(Ljava/lang/Object;)Z", true); Label l3 = new Label(); mv.visitJumpInsn(IFEQ, l3); mv.visitVarInsn(ALOAD, 0); mv.visitInsn(ICONST_1); mv.visitFieldInsn(PUTFIELD, m_owner, FIELD_FLAG_PREFIX + field, "Z"); mv.visitLabel(l3); } mv.visitLabel(endif); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, m_owner, IM_FIELD, "Lorg/apache/felix/ipojo/InstanceManager;"); mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/felix/ipojo/InstanceManager", "getRegistredMethods", "()Ljava/util/Set;", false); mv.visitVarInsn(ASTORE, 2); mv.visitVarInsn(ALOAD, 2); Label endif2 = new Label(); mv.visitJumpInsn(IFNULL, endif2); for (String methodId : m_methods) { if (!methodId.equals("<init>")) { mv.visitVarInsn(ALOAD, 2); mv.visitLdcInsn(methodId); mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Set", "contains", "(Ljava/lang/Object;)Z", true); Label l3 = new Label(); mv.visitJumpInsn(IFEQ, l3); mv.visitVarInsn(ALOAD, 0); mv.visitInsn(ICONST_1); mv.visitFieldInsn(PUTFIELD, m_owner, METHOD_FLAG_PREFIX + methodId, "Z"); mv.visitLabel(l3); } } mv.visitLabel(endif2); mv.visitInsn(RETURN); mv.visitMaxs(0, 0); mv.visitEnd(); }
From source file:org.apache.felix.ipojo.manipulation.ClassManipulator.java
License:Apache License
/** * Create a getter method for an array./*from w w w.j av a 2 s .c o m*/ * @param name : field name * @param desc : method description */ private void createArraySetter(String name, String desc) { MethodVisitor mv = cv.visitMethod(0, "__set" + name, desc, null, null); mv.visitCode(); String internalType = desc.substring(1); internalType = internalType.substring(0, internalType.length() - 2); Label l1 = new Label(); mv.visitLabel(l1); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, m_owner, FIELD_FLAG_PREFIX + name, "Z"); Label l2 = new Label(); mv.visitJumpInsn(IFNE, l2); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 1); mv.visitFieldInsn(PUTFIELD, m_owner, name, internalType); mv.visitInsn(RETURN); mv.visitLabel(l2); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, m_owner, IM_FIELD, "Lorg/apache/felix/ipojo/InstanceManager;"); mv.visitVarInsn(ALOAD, 0); mv.visitLdcInsn(name); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/felix/ipojo/InstanceManager", SET, "(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Object;)V", false); mv.visitInsn(RETURN); // End mv.visitMaxs(0, 0); mv.visitEnd(); }
From source file:org.apache.felix.ipojo.manipulation.ClassManipulator.java
License:Apache License
/** * Create a setter method for an array./*from w ww. ja v a 2 s . c om*/ * @param name : field name * @param desc : method description */ private void createArrayGetter(String name, String desc) { String methodName = "__get" + name; MethodVisitor mv = cv.visitMethod(0, methodName, desc, null, null); mv.visitCode(); String internalType = desc.substring(2); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, m_owner, FIELD_FLAG_PREFIX + name, "Z"); Label l1 = new Label(); mv.visitJumpInsn(IFNE, l1); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, m_owner, name, internalType); mv.visitInsn(ARETURN); mv.visitLabel(l1); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, m_owner, IM_FIELD, "Lorg/apache/felix/ipojo/InstanceManager;"); mv.visitVarInsn(ALOAD, 0); mv.visitLdcInsn(name); mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/felix/ipojo/InstanceManager", GET, "(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object;", false); mv.visitTypeInsn(CHECKCAST, internalType); mv.visitInsn(ARETURN); // End mv.visitMaxs(0, 0); mv.visitEnd(); }