Example usage for org.objectweb.asm Opcodes PUTFIELD

List of usage examples for org.objectweb.asm Opcodes PUTFIELD

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes PUTFIELD.

Prototype

int PUTFIELD

To view the source code for org.objectweb.asm Opcodes PUTFIELD.

Click Source Link

Usage

From source file:org.actorsguildframework.internal.codegenerator.BeanCreator.java

License:Apache License

/**
 * Writes the accessor methods for @Prop generated properties.
 * @param bcd the class descriptor/*from   w w  w.j  a va2  s .  c  om*/
 * @param classNameInternal the internal name of this class
 * @param cw the ClassWriter to write to
 */
public static void writePropAccessors(BeanClassDescriptor bcd, String classNameInternal, ClassWriter cw) {
    String classNameDescriptor = "L" + classNameInternal + ";";

    MethodVisitor mv;
    for (int i = 0; i < bcd.getPropertyCount(); i++) {
        PropertyDescriptor pd = bcd.getProperty(i);
        if (!pd.getPropertySource().isGenerating())
            continue;

        {
            Type t = Type.getType(pd.getPropertyClass());
            Method orig = pd.getGetter();
            mv = cw.visitMethod(
                    GenerationUtils.convertAccessModifiers(orig.getModifiers())
                            + (bcd.isThreadSafe() ? Opcodes.ACC_SYNCHRONIZED : 0),
                    orig.getName(), Type.getMethodDescriptor(orig), GenericTypeHelper.getSignature(orig), null);

            mv.visitCode();
            Label l0 = new Label();
            mv.visitLabel(l0);
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitFieldInsn(Opcodes.GETFIELD, classNameInternal,
                    String.format(PROP_FIELD_NAME_TEMPLATE, pd.getName()),
                    Type.getDescriptor(pd.getPropertyClass()));
            mv.visitInsn(t.getOpcode(Opcodes.IRETURN));
            Label l1 = new Label();
            mv.visitLabel(l1);
            mv.visitLocalVariable("this", classNameDescriptor, null, l0, l1, 0);
            mv.visitMaxs(0, 0);
            mv.visitEnd();
        }

        if (pd.getAccess().isWritable()) {
            Type t = Type.getType(pd.getPropertyClass());
            Method orig = pd.getSetter();
            mv = cw.visitMethod(
                    GenerationUtils.convertAccessModifiers(orig.getModifiers())
                            + (bcd.isThreadSafe() ? Opcodes.ACC_SYNCHRONIZED : 0),
                    orig.getName(), Type.getMethodDescriptor(orig), GenericTypeHelper.getSignature(orig), null);

            mv.visitCode();
            Label l0 = new Label();
            mv.visitLabel(l0);
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitVarInsn(t.getOpcode(Opcodes.ILOAD), 1);
            mv.visitFieldInsn(Opcodes.PUTFIELD, classNameInternal,
                    String.format(PROP_FIELD_NAME_TEMPLATE, pd.getName()),
                    Type.getDescriptor(pd.getPropertyClass()));
            mv.visitInsn(Opcodes.RETURN);
            Label l1 = new Label();
            mv.visitLabel(l1);
            mv.visitLocalVariable("this", classNameDescriptor, null, l0, l1, 0);
            mv.visitLocalVariable("value", Type.getDescriptor(pd.getPropertyClass()), null, l0, l1, 1);
            mv.visitMaxs(0, 0);
            mv.visitEnd();
        }
    }
}

From source file:org.actorsguildframework.internal.codegenerator.BeanCreator.java

License:Apache License

/**
 * Writes the bean constructor to the given ClassWriter.
 * @param beanClass the original bean class to extend
 * @param bcd the descriptor of the bean
 * @param classNameInternal the internal name of the new class
 * @param cw the ClassWriter to write to
 * @param snippetWriter if not null, this will be invoked to add a snippet
 *                      after the invocation of the super constructor
 *///from   w  ww  .ja v a  2 s .com
public static void writeConstructor(Class<?> beanClass, BeanClassDescriptor bcd, String classNameInternal,
        ClassWriter cw, SnippetWriter snippetWriter) {
    String classNameDescriptor = "L" + classNameInternal + ";";

    int localPropertySize = 0;
    ArrayList<PropertyDescriptor> localVarProperties = new ArrayList<PropertyDescriptor>();
    for (int i = 0; i < bcd.getPropertyCount(); i++) {
        PropertyDescriptor pd = bcd.getProperty(i);
        if (pd.getPropertySource().isGenerating() || (pd.getDefaultValue() != null)) {
            localVarProperties.add(pd);
            localPropertySize += Type.getType(pd.getPropertyClass()).getSize();
        }
    }

    final int locVarThis = 0;
    final int locVarController = 1;
    final int locVarProps = 2;
    final int locVarPropertiesOffset = 3;
    final int locVarP = 3 + localPropertySize;
    final int locVarK = 4 + localPropertySize;
    final int locVarV = 5 + localPropertySize;

    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>",
            "(Lorg/actorsguildframework/internal/Controller;Lorg/actorsguildframework/Props;)V", null, null);
    mv.visitCode();
    Label lTry = new Label();
    Label lCatch = new Label();
    mv.visitTryCatchBlock(lTry, lCatch, lCatch, "java/lang/ClassCastException");

    Label lBegin = new Label();
    mv.visitLabel(lBegin);
    mv.visitVarInsn(Opcodes.ALOAD, locVarThis);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(beanClass), "<init>", "()V");

    if (snippetWriter != null)
        snippetWriter.write(mv);

    Label lPropertyInit = new Label();
    mv.visitLabel(lPropertyInit);
    // load default values into the local variables for each property that must be set
    int varCount = 0;
    for (PropertyDescriptor pd : localVarProperties) {
        Type pt = Type.getType(pd.getPropertyClass());
        if (pd.getDefaultValue() != null)
            mv.visitFieldInsn(Opcodes.GETSTATIC, Type.getInternalName(pd.getDefaultValue().getDeclaringClass()),
                    pd.getDefaultValue().getName(), Type.getDescriptor(pd.getDefaultValue().getType()));
        else
            GenerationUtils.generateLoadDefault(mv, pd.getPropertyClass());
        mv.visitVarInsn(pt.getOpcode(Opcodes.ISTORE), locVarPropertiesOffset + varCount);
        varCount += pt.getSize();
    }

    // loop through the props argument's list
    mv.visitVarInsn(Opcodes.ALOAD, locVarProps);
    mv.visitVarInsn(Opcodes.ASTORE, locVarP);
    Label lWhile = new Label();
    Label lEndWhile = new Label();
    Label lWhileBody = new Label();
    mv.visitLabel(lWhile);
    mv.visitJumpInsn(Opcodes.GOTO, lEndWhile);
    mv.visitLabel(lWhileBody);

    mv.visitVarInsn(Opcodes.ALOAD, locVarP);
    mv.visitInsn(Opcodes.DUP);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "org/actorsguildframework/Props", "getKey",
            "()Ljava/lang/String;");
    mv.visitVarInsn(Opcodes.ASTORE, locVarK);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "org/actorsguildframework/Props", "getValue",
            "()Ljava/lang/Object;");
    mv.visitVarInsn(Opcodes.ASTORE, locVarV);

    mv.visitLabel(lTry);
    // write an if for each property
    Label lEndIf = new Label();
    varCount = 0;
    int ifCount = 0;
    for (int i = 0; i < bcd.getPropertyCount(); i++) {
        PropertyDescriptor pd = bcd.getProperty(i);
        boolean usesLocal = pd.getPropertySource().isGenerating() || (pd.getDefaultValue() != null);
        Class<?> propClass = pd.getPropertyClass();
        Type pt = Type.getType(propClass);
        mv.visitVarInsn(Opcodes.ALOAD, locVarK);
        mv.visitLdcInsn(pd.getName());
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z");
        Label lElse = new Label();
        mv.visitJumpInsn(Opcodes.IFEQ, lElse);

        if (!usesLocal)
            mv.visitVarInsn(Opcodes.ALOAD, locVarThis); // for setter invocation, load 'this'

        if (propClass.isPrimitive()) {
            mv.visitLdcInsn(pd.getName());
            mv.visitVarInsn(Opcodes.ALOAD, locVarV);
            mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(BeanHelper.class),
                    String.format("get%s%sFromPropValue",
                            propClass.getName().substring(0, 1).toUpperCase(Locale.US),
                            propClass.getName().substring(1)),
                    "(Ljava/lang/String;Ljava/lang/Object;)" + pt.getDescriptor());
        } else if (!propClass.equals(Object.class)) {
            mv.visitVarInsn(Opcodes.ALOAD, locVarV);
            mv.visitTypeInsn(Opcodes.CHECKCAST, pt.getInternalName());
        } else
            mv.visitVarInsn(Opcodes.ALOAD, locVarV);

        if (!usesLocal)
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classNameInternal, pd.getSetter().getName(),
                    Type.getMethodDescriptor(pd.getSetter()));
        else
            mv.visitVarInsn(pt.getOpcode(Opcodes.ISTORE), varCount + locVarPropertiesOffset);

        mv.visitJumpInsn(Opcodes.GOTO, lEndIf);
        mv.visitLabel(lElse);

        ifCount++;
        if (usesLocal)
            varCount += pt.getSize();
    }

    // else (==> if not prop matched) throw IllegalArgumentException
    mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(IllegalArgumentException.class));
    mv.visitInsn(Opcodes.DUP);
    mv.visitLdcInsn("Unknown property \"%s\".");
    mv.visitInsn(Opcodes.ICONST_1);
    mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object");
    mv.visitInsn(Opcodes.DUP);
    mv.visitInsn(Opcodes.ICONST_0);
    mv.visitVarInsn(Opcodes.ALOAD, locVarK);
    mv.visitInsn(Opcodes.AASTORE);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/String", "format",
            "(Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;");
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(IllegalArgumentException.class), "<init>",
            "(Ljava/lang/String;)V");
    mv.visitInsn(Opcodes.ATHROW);

    mv.visitLabel(lCatch);
    mv.visitInsn(Opcodes.POP); // pop the exception object (not needed)
    mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(IllegalArgumentException.class));
    mv.visitInsn(Opcodes.DUP);
    mv.visitLdcInsn("Incompatible type for property \"%s\". Got %s.");
    mv.visitInsn(Opcodes.ICONST_2);
    mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object");
    mv.visitInsn(Opcodes.DUP);
    mv.visitInsn(Opcodes.ICONST_0);
    mv.visitVarInsn(Opcodes.ALOAD, locVarK);
    mv.visitInsn(Opcodes.AASTORE);
    mv.visitInsn(Opcodes.DUP);
    mv.visitInsn(Opcodes.ICONST_1);
    mv.visitVarInsn(Opcodes.ALOAD, locVarV);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;");
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Class", "getName", "()Ljava/lang/String;");
    mv.visitInsn(Opcodes.AASTORE);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/String", "format",
            "(Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;");
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(IllegalArgumentException.class), "<init>",
            "(Ljava/lang/String;)V");
    mv.visitInsn(Opcodes.ATHROW);

    mv.visitLabel(lEndIf);
    mv.visitVarInsn(Opcodes.ALOAD, locVarP);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "org/actorsguildframework/Props", "tail",
            "()Lorg/actorsguildframework/Props;");
    mv.visitVarInsn(Opcodes.ASTORE, locVarP);

    mv.visitLabel(lEndWhile);
    mv.visitVarInsn(Opcodes.ALOAD, locVarP);
    mv.visitJumpInsn(Opcodes.IFNONNULL, lWhileBody);

    // write local variables back into properties 
    varCount = 0;
    for (PropertyDescriptor pd : localVarProperties) {
        Type pt = Type.getType(pd.getPropertyClass());
        mv.visitVarInsn(Opcodes.ALOAD, locVarThis);
        if (pd.getPropertySource() == PropertySource.ABSTRACT_METHOD) {
            mv.visitVarInsn(pt.getOpcode(Opcodes.ILOAD), locVarPropertiesOffset + varCount);
            mv.visitFieldInsn(Opcodes.PUTFIELD, classNameInternal,
                    String.format(PROP_FIELD_NAME_TEMPLATE, pd.getName()), pt.getDescriptor());
        } else if (pd.getPropertySource() == PropertySource.USER_WRITTEN) {
            mv.visitVarInsn(pt.getOpcode(Opcodes.ILOAD), locVarPropertiesOffset + varCount);
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classNameInternal, pd.getSetter().getName(),
                    Type.getMethodDescriptor(pd.getSetter()));
        } else
            throw new RuntimeException("Internal error");
        varCount += pt.getSize();
    }

    // if bean is thread-safe, publish all writes now
    if (bcd.isThreadSafe()) {
        mv.visitVarInsn(Opcodes.ALOAD, locVarThis);
        mv.visitInsn(Opcodes.DUP);
        mv.visitInsn(Opcodes.MONITORENTER);
        mv.visitInsn(Opcodes.MONITOREXIT);
    }

    mv.visitInsn(Opcodes.RETURN);
    Label lEnd = new Label();
    mv.visitLabel(lEnd);

    mv.visitLocalVariable("this", classNameDescriptor, null, lBegin, lEnd, locVarThis);
    mv.visitLocalVariable("controller", "Lorg/actorsguildframework/internal/Controller;", null, lBegin, lEnd,
            locVarController);
    mv.visitLocalVariable("props", "Lorg/actorsguildframework/Props;", null, lBegin, lEnd, locVarProps);
    varCount = 0;
    for (PropertyDescriptor pd : localVarProperties) {
        Type pt = Type.getType(pd.getPropertyClass());
        mv.visitLocalVariable("__" + pd.getName(), pt.getDescriptor(),
                GenericTypeHelper.getSignature(pd.getPropertyType()), lPropertyInit, lEnd,
                locVarPropertiesOffset + varCount);
        varCount += pt.getSize();
    }
    mv.visitLocalVariable("p", "Lorg/actorsguildframework/Props;", null, lPropertyInit, lEnd, locVarP);
    mv.visitLocalVariable("k", "Ljava/lang/String;", null, lWhile, lEndWhile, locVarK);
    mv.visitLocalVariable("v", "Ljava/lang/Object;", null, lWhile, lEndWhile, locVarV);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:org.adjective.stout.operation.AssignFieldStatement.java

License:Apache License

public void getInstructions(ExecutionStack stack, InstructionCollector collector) {
    UnresolvedType owner = _owner;/*from w w  w.j ava 2s. c o  m*/
    if (owner == null) {
        owner = stack.currentClass();
    }
    UnresolvedType type = _fieldType;
    if (type == null) {
        type = owner.getFieldType(_fieldName);
        if (type == null) {
            throw new OperationException("No such field " + _fieldName + " in " + owner);
        }
    }

    if (_target == null) {
        addInstruction(collector, ThisExpression.LOAD_THIS);
    } else {
        _target.getInstructions(stack, collector);
    }
    _expression.getInstructions(stack, collector);
    addInstruction(collector,
            new FieldInstruction(Opcodes.PUTFIELD, owner.getInternalName(), _fieldName, type.getDescriptor()));
}

From source file:org.adjective.stout.tools.StackVisualiserMethodVisitor.java

License:Apache License

public void visitFieldInsn(int opcode, String owner, String name, String desc) {
    String description = owner + "." + name;
    switch (opcode) {
    case Opcodes.GETFIELD:
        pop();//from w  w  w . java 2  s. c o m
        push(description, desc);
        break;
    case Opcodes.PUTFIELD:
        pop(2);
        break;
    case Opcodes.GETSTATIC:
        push(description, desc);
        break;
    case Opcodes.PUTSTATIC:
        pop(1);
        break;
    }
    print(opcode, description);
}

From source file:org.apache.cxf.binding.corba.utils.CorbaAnyHelper.java

License:Apache License

private static void addInsertOverride(ClassWriter cw) {
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "insert_Streamable",
            "(Lorg/omg/CORBA/portable/Streamable;)V", null, null);
    mv.visitCode();/*from   w  ww.j  a  va  2s .  co m*/
    Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitLineNumber(43, l0);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "com/sun/corba/se/impl/corba/AnyImpl", "insert_Streamable",
            "(Lorg/omg/CORBA/portable/Streamable;)V");
    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitLineNumber(44, l1);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitFieldInsn(Opcodes.PUTFIELD, "org/apache/cxf/binding/corba/utils/FixedAnyImpl", "obj",
            "Lorg/omg/CORBA/portable/Streamable;");
    Label l2 = new Label();
    mv.visitLabel(l2);
    mv.visitLineNumber(45, l2);
    mv.visitInsn(Opcodes.RETURN);
    Label l3 = new Label();
    mv.visitLabel(l3);
    mv.visitLocalVariable("this", "Lorg/apache/cxf/binding/corba/utils/FixedAnyImpl;", null, l0, l3, 0);
    mv.visitLocalVariable("s", "Lorg/omg/CORBA/portable/Streamable;", null, l0, l3, 1);
    mv.visitMaxs(2, 2);
    mv.visitEnd();
}

From source file:org.apache.cxf.jaxb.JAXBUtils.java

License:Apache License

private static Class<?> createNamespaceWrapperInternal(ASMHelper helper, ClassWriter cw) {
    String className = "org.apache.cxf.jaxb.NamespaceMapperInternal";
    FieldVisitor fv;//from   w  ww  . j a v  a 2 s  .  c o  m
    MethodVisitor mv;
    cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_SUPER,
            "org/apache/cxf/jaxb/NamespaceMapperInternal", null,
            "com/sun/xml/internal/bind/marshaller/NamespacePrefixMapper", null);

    cw.visitSource("NamespaceMapper.java", null);

    fv = cw.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL, "nspref", "Ljava/util/Map;",
            "Ljava/util/Map<Ljava/lang/String;Ljava/lang/String;>;", null);
    fv.visitEnd();

    mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "(Ljava/util/Map;)V",
            "(Ljava/util/Map<Ljava/lang/String;Ljava/lang/String;>;)V", null);
    mv.visitCode();
    Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitLineNumber(30, l0);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "com/sun/xml/internal/bind/marshaller/NamespacePrefixMapper",
            "<init>", "()V");
    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitLineNumber(31, l1);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitFieldInsn(Opcodes.PUTFIELD, "org/apache/cxf/jaxb/NamespaceMapperInternal", "nspref",
            "Ljava/util/Map;");
    Label l2 = new Label();
    mv.visitLabel(l2);
    mv.visitLineNumber(32, l2);
    mv.visitInsn(Opcodes.RETURN);
    Label l3 = new Label();
    mv.visitLabel(l3);
    mv.visitLocalVariable("this", "Lorg/apache/cxf/jaxb/NamespaceMapperInternal;", null, l0, l3, 0);
    mv.visitLocalVariable("nspref", "Ljava/util/Map;", "Ljava/util/Map<Ljava/lang/String;Ljava/lang/String;>;",
            l0, l3, 1);
    mv.visitMaxs(2, 2);
    mv.visitEnd();

    mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "getPreferredPrefix",
            "(Ljava/lang/String;Ljava/lang/String;Z)Ljava/lang/String;", null, null);
    mv.visitCode();
    l0 = new Label();
    mv.visitLabel(l0);
    mv.visitLineNumber(38, l0);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, "org/apache/cxf/jaxb/NamespaceMapperInternal", "nspref",
            "Ljava/util/Map;");
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "get",
            "(Ljava/lang/Object;)Ljava/lang/Object;");
    mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/String");
    mv.visitVarInsn(Opcodes.ASTORE, 4);
    l1 = new Label();
    mv.visitLabel(l1);
    mv.visitLineNumber(39, l1);
    mv.visitVarInsn(Opcodes.ALOAD, 4);
    l2 = new Label();
    mv.visitJumpInsn(Opcodes.IFNULL, l2);
    l3 = new Label();
    mv.visitLabel(l3);
    mv.visitLineNumber(40, l3);
    mv.visitVarInsn(Opcodes.ALOAD, 4);
    mv.visitInsn(Opcodes.ARETURN);
    mv.visitLabel(l2);
    mv.visitLineNumber(42, l2);
    mv.visitVarInsn(Opcodes.ALOAD, 2);
    mv.visitInsn(Opcodes.ARETURN);
    Label l4 = new Label();
    mv.visitLabel(l4);
    mv.visitLocalVariable("this", "Lorg/apache/cxf/jaxb/NamespaceMapperInternal;", null, l0, l4, 0);
    mv.visitLocalVariable("namespaceUri", "Ljava/lang/String;", null, l0, l4, 1);
    mv.visitLocalVariable("suggestion", "Ljava/lang/String;", null, l0, l4, 2);
    mv.visitLocalVariable("requirePrefix", "Z", null, l0, l4, 3);
    mv.visitLocalVariable("prefix", "Ljava/lang/String;", null, l1, l4, 4);
    mv.visitMaxs(2, 5);
    mv.visitEnd();
    cw.visitEnd();

    byte bts[] = cw.toByteArray();
    return helper.loadClass(className, JAXBUtils.class, bts);
}

From source file:org.apache.cxf.jaxb.WrapperHelperCompiler.java

License:Apache License

private static void addConstructor(String newClassName, ClassWriter cw, Class<?> objectFactory) {

    if (objectFactory != null) {
        String ofName = "L" + periodToSlashes(objectFactory.getName()) + ";";
        FieldVisitor fv = cw.visitField(0, "factory", ofName, null, null);
        fv.visitEnd();//from   ww w  . j a  v  a 2s .  c  o  m
    }

    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();
    Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitLineNumber(102, l0);

    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
    if (objectFactory != null) {
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitTypeInsn(Opcodes.NEW, periodToSlashes(objectFactory.getName()));
        mv.visitInsn(Opcodes.DUP);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, periodToSlashes(objectFactory.getName()), "<init>", "()V");
        mv.visitFieldInsn(Opcodes.PUTFIELD, periodToSlashes(newClassName), "factory",
                "L" + periodToSlashes(objectFactory.getName()) + ";");
    }

    mv.visitInsn(Opcodes.RETURN);

    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitLineNumber(103, l0);

    mv.visitLocalVariable("this", "L" + newClassName + ";", null, l0, l1, 0);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:org.apache.cxf.jaxws.WrapperClassGenerator.java

License:Apache License

private void generateMessagePart(ClassWriter cw, MessagePartInfo mpi, Method method, String className) {
    if (Boolean.TRUE.equals(mpi.getProperty(ReflectionServiceFactoryBean.HEADER))) {
        return;/*  ww  w .  ja v a  2s.  c o m*/
    }
    String classFileName = periodToSlashes(className);
    String name = mpi.getName().getLocalPart();
    Class clz = mpi.getTypeClass();
    Object obj = mpi.getProperty(ReflectionServiceFactoryBean.RAW_CLASS);
    if (obj != null) {
        clz = (Class) obj;
    }
    Type genericType = (Type) mpi.getProperty(ReflectionServiceFactoryBean.GENERIC_TYPE);
    if (genericType instanceof ParameterizedType) {
        ParameterizedType tp = (ParameterizedType) genericType;
        if (tp.getRawType() instanceof Class && Holder.class.isAssignableFrom((Class) tp.getRawType())) {
            genericType = tp.getActualTypeArguments()[0];
        }
    }
    String classCode = getClassCode(clz);
    String fieldDescriptor = null;

    if (genericType instanceof ParameterizedType) {
        if (Collection.class.isAssignableFrom(clz) || clz.isArray()) {
            ParameterizedType ptype = (ParameterizedType) genericType;

            Type[] types = ptype.getActualTypeArguments();
            // TODO: more complex Parameterized type
            if (types.length > 0) {
                if (types[0] instanceof Class) {
                    fieldDescriptor = getClassCode(genericType);
                } else if (types[0] instanceof GenericArrayType) {
                    fieldDescriptor = getClassCode(genericType);
                } else if (types[0] instanceof ParameterizedType) {
                    classCode = getClassCode(((ParameterizedType) types[0]).getRawType());
                    fieldDescriptor = getClassCode(genericType);
                }
            }
        } else {
            classCode = getClassCode(((ParameterizedType) genericType).getRawType());
            fieldDescriptor = getClassCode(genericType);
        }
    }
    String fieldName = JavaUtils.isJavaKeyword(name) ? JavaUtils.makeNonJavaKeyword(name) : name;

    FieldVisitor fv = cw.visitField(Opcodes.ACC_PRIVATE, fieldName, classCode, fieldDescriptor, null);

    AnnotationVisitor av0 = fv.visitAnnotation("Ljavax/xml/bind/annotation/XmlElement;", true);
    av0.visit("name", name);
    if (factory.isWrapperPartQualified(mpi)) {
        av0.visit("namespace", mpi.getConcreteName().getNamespaceURI());
    }
    if (factory.isWrapperPartNillable(mpi)) {
        av0.visit("nillable", Boolean.TRUE);
    }
    if (factory.getWrapperPartMinOccurs(mpi) == 1) {
        av0.visit("required", Boolean.TRUE);
    }
    av0.visitEnd();

    List<Annotation> jaxbAnnos = getJaxbAnnos(mpi);
    addJAXBAnnotations(fv, jaxbAnnos);
    fv.visitEnd();

    String methodName = JAXBUtils.nameToIdentifier(name, JAXBUtils.IdentifierType.GETTER);
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, methodName, "()" + classCode,
            fieldDescriptor == null ? null : "()" + fieldDescriptor, null);
    mv.visitCode();

    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, classFileName, fieldName, classCode);
    mv.visitInsn(org.objectweb.asm.Type.getType(classCode).getOpcode(Opcodes.IRETURN));
    mv.visitMaxs(0, 0);
    mv.visitEnd();

    methodName = JAXBUtils.nameToIdentifier(name, JAXBUtils.IdentifierType.SETTER);
    mv = cw.visitMethod(Opcodes.ACC_PUBLIC, methodName, "(" + classCode + ")V",
            fieldDescriptor == null ? null : "(" + fieldDescriptor + ")V", null);
    mv.visitCode();
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    org.objectweb.asm.Type setType = org.objectweb.asm.Type.getType(classCode);
    mv.visitVarInsn(setType.getOpcode(Opcodes.ILOAD), 1);
    mv.visitFieldInsn(Opcodes.PUTFIELD, className, fieldName, classCode);
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();

}

From source file:org.apache.drill.exec.compile.bytecode.InstructionModifier.java

License:Apache License

@Override
public void visitFieldInsn(int opcode, String owner, String name, String desc) {
    ReplacingBasicValue v;/*from  w ww. ja  v  a  2s.co  m*/

    switch (opcode) {
    case Opcodes.PUTFIELD:
        // pop twice for put.
        v = popCurrent(true);
        if (v != null) {
            if (v.isFunctionReturn) {
                super.visitFieldInsn(opcode, owner, name, desc);
                return;
            } else {
                // we are trying to store a replaced variable in an external context, we need to generate an instance and
                // transfer it out.
                ValueHolderSub sub = oldToNew.get(v.getIndex());
                sub.transferToExternal(adder, owner, name, desc);
                return;
            }
        }

    case Opcodes.GETFIELD:
        // pop again.
        v = popCurrent();
        if (v != null) {
            // super.visitFieldInsn(opcode, owner, name, desc);
            ValueHolderSub sub = oldToNew.get(v.getIndex());
            sub.addInsn(name, this, opcode);
            return;
        }
    }

    super.visitFieldInsn(opcode, owner, name, desc);
}

From source file:org.apache.felix.scrplugin.helper.ClassModifier.java

License:Apache License

private static void createMethod(final ClassWriter cw, final String className, final String referenceName,
        final String fieldName, final String typeName, final boolean bind) {
    final org.objectweb.asm.Type type = org.objectweb.asm.Type.getType("L" + typeName.replace('.', '/') + ";");
    final String methodName = (bind ? "" : "un") + "bind" + referenceName.substring(0, 1).toUpperCase()
            + referenceName.substring(1);
    final MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PROTECTED, methodName, "(" + type.toString() + ")V",
            null, null);/*from w  w w .  jav a  2  s .co  m*/
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    if (bind) {
        mv.visitVarInsn(type.getOpcode(Opcodes.ILOAD), 1);
        mv.visitFieldInsn(Opcodes.PUTFIELD, className.replace('.', '/'), fieldName, type.toString());
    } else {
        mv.visitFieldInsn(Opcodes.GETFIELD, className.replace('.', '/'), fieldName, type.toString());
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        final Label jmpLabel = new Label();
        mv.visitJumpInsn(Opcodes.IF_ACMPNE, jmpLabel);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitInsn(Opcodes.ACONST_NULL);
        mv.visitFieldInsn(Opcodes.PUTFIELD, className.replace('.', '/'), fieldName, type.toString());
        mv.visitLabel(jmpLabel);
    }
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(2, 2);
}