Examples of CodeBuilder


Examples of com.alibaba.citrus.codegen.CodeBuilder

    /**
     * ����Ĭ�Ϲ��캯����
     */
    public static void addDefaultConstructor(ClassBuilder cb) {
        MethodBuilder mb = cb.addConstructor(null, null);
        CodeBuilder code = mb.startCode();

        code.loadThis();
        code.invokeConstructor(cb.getSuperType(), new com.alibaba.citrus.asm.commons.Method(CONSTRUCTOR_NAME, "()V"));
        code.returnValue();
    }
View Full Code Here

Examples of com.alibaba.citrus.codegen.CodeBuilder

    /**
     * ����һ�����س�����<code>toString()</code>������
     */
    public static void addToString(ClassBuilder cb, String constantToString) {
        MethodBuilder mb = cb.addMethod(String.class, "toString", null, null);
        CodeBuilder code = mb.startCode();

        code.push(constantToString);
        code.returnValue();
    }
View Full Code Here

Examples of com.claritysys.jvm.builder.CodeBuilder

                "java/lang/Object",
                "HelloWorld.java");
        ConstantPool cp = cf.getConstantPool();

        CfMethod method = cf.addMethod(JVM.ACC_PUBLIC, "<init>", "()V");
        CodeBuilder code = new CodeBuilder(method);
        code.add(JVM.ALOAD_0);
        code.add(JVM.INVOKESPECIAL, cp.addMethodRef(false,
                "java/lang/Object",
                "<init>",
                "()V"));
        code.add(JVM.RETURN);
        code.flush();

        method = cf.addMethod(JVM.ACC_PUBLIC + JVM.ACC_STATIC,
                "main",
                "([Ljava/lang/String;)V");
        code = new CodeBuilder(method);
        code.add(JVM.GETSTATIC, cp.addFieldRef("java/lang/System",
                "out",
                "Ljava/io/PrintStream;"));
        code.add(JVM.LDC, "Hello world!");
        code.add(JVM.INVOKEVIRTUAL, cp.addMethodRef(false,
                "java/io/PrintStream",
                "println",
                "(Ljava/lang/String;)V"));
        code.add(JVM.RETURN);
        code.flush();

        return cf.writeToArray();
    }
View Full Code Here

Examples of gov.nasa.jpf.jvm.CodeBuilder

   * See JVM spec for Java 6 at 3.11.8 , Table 4.2
   * and chapter 6
   */
  public static MethodInfo implement(MethodInfo method) {
    String returnType = method.getReturnType();
    CodeBuilder cb = method.createCodeBuilder();
    // TODO don't know if it's safe to pass a null class file.
    cb.initialize(null, method);
 
    if ("V".equals(returnType)) {         // void
      cb.return_();
    } else if ("B".equals(returnType) ||  // byte
               "C".equals(returnType) ||  // char
               "I".equals(returnType) ||  // int
               "S".equals(returnType) ||  // short
               "Z".equals(returnType) ) { // boolean
      cb.iconst_0();
      cb.ireturn();
    } else if ("J".equals(returnType)) {  // long
      cb.lconst_0();
      cb.lreturn();
    } else if ("F".equals(returnType)) {  // float
      cb.fconst_0();
      cb.freturn();
    } else if ("D".equals(returnType)) {  // double
      cb.dconst_0();
      cb.dreturn();
    } else if ('L' == returnType.charAt(0) ||
               '[' == returnType.charAt(0)) {  // references
      cb.aconst_null();
      cb.areturn();
    }
    cb.installCode();
    logger.finer("Default implementation for method: "
        + method.getReturnTypeName() + " " + method.getLongName() + "\n\t"
        + Arrays.toString(method.getInstructions()).replace(", ", "\n\t ") + "\n");
    return method;   
  }
View Full Code Here

Examples of org.cojen.classfile.CodeBuilder

        } catch (Exception e) {
        }

        MethodInfo ctor = cf.addConstructor(Modifiers.PUBLIC, null);
        ctor.markSynthetic();
        CodeBuilder b = new CodeBuilder(ctor);

        b.loadThis();
        b.invokeSuperConstructor(null);
        b.returnVoid();

        generateAccessMethod(cf, beanType, props[0], READ_METHOD);
        generateAccessMethod(cf, beanType, props[0], TRY_READ_METHOD);
        generateAccessMethod(cf, beanType, props[0], HAS_READ_METHOD);
        generateAccessMethod(cf, beanType, props[1], WRITE_METHOD);
View Full Code Here

Examples of org.cojen.classfile.CodeBuilder

            break;
        }
        }

        mi.markSynthetic();
        CodeBuilder b = new CodeBuilder(mi);

        LocalVariable beanVar, propertyVar, valueVar;

        switch (methodType) {
        case READ_METHOD: case TRY_READ_METHOD: default:
            beanVar = b.getParameter(0);
            propertyVar = b.getParameter(1);
            valueVar = null;
            break;
        case WRITE_METHOD: case TRY_WRITE_METHOD:
            beanVar = b.getParameter(0);
            propertyVar = b.getParameter(1);
            valueVar = b.getParameter(2);
            break;
        case HAS_READ_METHOD: case HAS_WRITE_METHOD:
            beanVar = null;
            propertyVar = b.getParameter(0);
            valueVar = null;
            break;
        }

        if (beanVar != null) {
            b.loadLocal(beanVar);
            b.checkCast(TypeDesc.forClass(beanType));
            b.storeLocal(beanVar);
        }

        if (properties.length > 0) {
            int[] cases = new int[hashCapacity(properties.length)];
            int caseCount = cases.length;
            for (int i=0; i<caseCount; i++) {
                cases[i] = i;
            }

            Label[] switchLabels = new Label[caseCount];
            Label noMatch = b.createLabel();
            List[] caseMethods = caseMethods(caseCount, properties);
           
            for (int i=0; i<caseCount; i++) {
                List matches = caseMethods[i];
                if (matches == null || matches.size() == 0) {
                    switchLabels[i] = noMatch;
                } else {
                    switchLabels[i] = b.createLabel();
                }
            }

            if (properties.length > 1) {
                b.loadLocal(propertyVar);
                b.invokeVirtual(String.class.getName(), "hashCode", TypeDesc.INT, null);
                b.loadConstant(0x7fffffff);
                b.math(Opcode.IAND);
                b.loadConstant(caseCount);
                b.math(Opcode.IREM);
           
                b.switchBranch(cases, switchLabels, noMatch);
            }
           
            // Params to invoke String.equals.
            TypeDesc[] params = {TypeDesc.OBJECT};
           
            for (int i=0; i<caseCount; i++) {
                List matches = caseMethods[i];
                if (matches == null || matches.size() == 0) {
                    continue;
                }
               
                switchLabels[i].setLocation();
               
                int matchCount = matches.size();
                for (int j=0; j<matchCount; j++) {
                    BeanProperty bp = (BeanProperty)matches.get(j);
                   
                    // Test against name to find exact match.
                   
                    b.loadConstant(bp.getName());
                    b.loadLocal(propertyVar);
                    b.invokeVirtual(String.class.getName(), "equals", TypeDesc.BOOLEAN, params);
                   
                    Label notEqual;
                   
                    if (j == matchCount - 1) {
                        notEqual = null;
                        b.ifZeroComparisonBranch(noMatch, "==");
                    } else {
                        notEqual = b.createLabel();
                        b.ifZeroComparisonBranch(notEqual, "==");
                    }
                   
                    switch (methodType) {
                    case READ_METHOD: case TRY_READ_METHOD: default: {
                        b.loadLocal(beanVar);
                        b.invoke(bp.getReadMethod());
                        TypeDesc type = TypeDesc.forClass(bp.getType());
                        b.convert(type, type.toObjectType());
                        b.returnValue(TypeDesc.OBJECT);
                        break;
                    }
                    case WRITE_METHOD: case TRY_WRITE_METHOD: {
                        b.loadLocal(beanVar);
                        b.loadLocal(valueVar);
                        TypeDesc type = TypeDesc.forClass(bp.getType());
                        b.checkCast(type.toObjectType());
                        b.convert(type.toObjectType(), type);
                        b.invoke(bp.getWriteMethod());
                        if (methodType == WRITE_METHOD) {
                            b.returnVoid();
                        } else {
                            b.loadConstant(true);
                            b.returnValue(TypeDesc.BOOLEAN);
                        }
                        break;
                    }
                    case HAS_READ_METHOD: case HAS_WRITE_METHOD: {
                        b.loadConstant(true);
                        b.returnValue(TypeDesc.BOOLEAN);
                        break;
                    }
                    }
                   
                    if (notEqual != null) {
                        notEqual.setLocation();
                    }
                }
            }
           
            noMatch.setLocation();
        }

        if (methodType == HAS_READ_METHOD || methodType == HAS_WRITE_METHOD
            || methodType == TRY_WRITE_METHOD)
        {
            b.loadConstant(false);
            b.returnValue(TypeDesc.BOOLEAN);
        } else if (methodType == TRY_READ_METHOD) {
            b.loadNull();
            b.returnValue(TypeDesc.OBJECT);
        } else {
            b.newObject(TypeDesc.forClass(NoSuchPropertyException.class));
            b.dup();
            b.loadLocal(propertyVar);
            b.loadConstant(methodType == READ_METHOD);

            // Params to invoke NoSuchPropertyException.<init>.
            TypeDesc[] params = {TypeDesc.STRING, TypeDesc.BOOLEAN};

            b.invokeConstructor(NoSuchPropertyException.class.getName(), params);
            b.throwObject();
        }
    }
View Full Code Here

Examples of org.cojen.classfile.CodeBuilder

            TypeDesc[] params = {TypeDesc.OBJECT, TypeDesc.OBJECT};
            mi = cf.addMethod(Modifiers.PUBLIC, "hasPropertyValue", TypeDesc.BOOLEAN, params);
        }

        mi.markSynthetic();
        CodeBuilder b = new CodeBuilder(mi);

        LocalVariable beanVar = b.getParameter(0);
        b.loadLocal(beanVar);
        b.checkCast(TypeDesc.forClass(beanType));
        b.storeLocal(beanVar);

        LocalVariable valueVar = b.getParameter(1);

        // If search value is null, only check properties which might be null.
        b.loadLocal(valueVar);
        Label searchNotNull = b.createLabel();
        b.ifNullBranch(searchNotNull, false);

        for (BeanProperty bp : properties) {
            if (bp.getType().isPrimitive()) {
                continue;
            }

            b.loadLocal(beanVar);
            b.invoke(bp.getReadMethod());

            Label noMatch = b.createLabel();
            b.ifNullBranch(noMatch, false);
            b.loadConstant(true);
            b.returnValue(TypeDesc.BOOLEAN);

            noMatch.setLocation();
        }

        b.loadConstant(false);
        b.returnValue(TypeDesc.BOOLEAN);

        searchNotNull.setLocation();

        // Handle search for non-null value. Search non-primitive properties
        // first, to avoid object conversion.

        // Params to invoke Object.equals.
        TypeDesc[] params = {TypeDesc.OBJECT};

        for (int pass = 1; pass <= 2; pass++) {
            for (BeanProperty bp : properties) {
                boolean primitive = bp.getType().isPrimitive();
                if (pass == 1 && primitive) {
                    continue;
                } else if (pass == 2 && !primitive) {
                    continue;
                }

                b.loadLocal(valueVar);
                b.loadLocal(beanVar);
                b.invoke(bp.getReadMethod());
                b.convert(TypeDesc.forClass(bp.getType()), TypeDesc.OBJECT);
                b.invokeVirtual(Object.class.getName(), "equals", TypeDesc.BOOLEAN, params);

                Label noMatch = b.createLabel();
                b.ifZeroComparisonBranch(noMatch, "==");
                b.loadConstant(true);
                b.returnValue(TypeDesc.BOOLEAN);

                noMatch.setLocation();
            }
        }

        b.loadConstant(false);
        b.returnValue(TypeDesc.BOOLEAN);
    }
View Full Code Here

Examples of org.cojen.classfile.CodeBuilder

                cf.markSynthetic();
                cf.addDefaultConstructor();
            }

            // Now define the method that constructs the object.
            CodeBuilder b = new CodeBuilder(cf.addMethod(method));
            b.newObject(TypeDesc.forClass(objectType));
            b.dup();
            int count = b.getParameterCount();
            for (int i=0; i<count; i++) {
                b.loadLocal(b.getParameter(i));
            }
            b.invoke(ctor);
            b.returnValue(TypeDesc.OBJECT);
        }

        if (cf == null) {
            // No methods found to implement.
            throw new IllegalArgumentException("No methods in factory to implement");
View Full Code Here

Examples of org.cojen.classfile.CodeBuilder

        TypeDesc[] paramTypes = {
            comparatorArrayType, comparatorArrayType
        };
        MethodInfo ctor = cf.addConstructor(Modifiers.PUBLIC, paramTypes);
        ctor.markSynthetic();
        CodeBuilder builder = new CodeBuilder(ctor);

        builder.loadThis();
        builder.invokeSuperConstructor(null);
        builder.loadThis();
        builder.loadLocal(builder.getParameter(0));
        builder.storeField("mCollators", comparatorArrayType);
        builder.loadThis();
        builder.loadLocal(builder.getParameter(1));
        builder.storeField("mUsingComparators", comparatorArrayType);
        builder.returnVoid();

        // Create the all-important compare method.
        Method compareMethod, compareToMethod;
        try {
            compareMethod = Comparator.class.getMethod
                ("compare", new Class[] {Object.class, Object.class});
            compareToMethod = Comparable.class.getMethod
                ("compareTo", new Class[] {Object.class});
        } catch (NoSuchMethodException e) {
            throw new InternalError(e.toString());
        }

        MethodInfo mi = cf.addMethod(compareMethod);
        mi.markSynthetic();
        builder = new CodeBuilder(mi);

        Label endLabel = builder.createLabel();
        LocalVariable obj1 = builder.getParameter(0);
        LocalVariable obj2 = builder.getParameter(1);

        // The first rule always applies to the beans directly. All others
        // apply to properties.

        BeanComparator[] ruleParts = rules.getRuleParts();
        BeanComparator bc = ruleParts[0];

        if ((bc.mFlags & 0x01) != 0) {
            // Reverse beans.
            LocalVariable temp = obj1;
            obj1 = obj2;
            obj2 = temp;
        }

        // Handle the case when obj1 and obj2 are the same (or both null)
        builder.loadLocal(obj1);
        builder.loadLocal(obj2);
        builder.ifEqualBranch(endLabel, true);

        // Do null order checks for beans.
        boolean nullHigh = (bc.mFlags & 0x02) == 0;
        Label label = builder.createLabel();
        builder.loadLocal(obj1);
        builder.ifNullBranch(label, false);
        builder.loadConstant(nullHigh ? 1 : -1);
        builder.returnValue(TypeDesc.INT);
        label.setLocation();
        label = builder.createLabel();
        builder.loadLocal(obj2);
        builder.ifNullBranch(label, false);
        builder.loadConstant(nullHigh ? -1 : 1);
        builder.returnValue(TypeDesc.INT);
        label.setLocation();

        // Call 'using' Comparator if one is provided.
        LocalVariable result =
            builder.createLocalVariable("result", TypeDesc.INT);
        if (bc.mUsingComparator != null) {
            builder.loadThis();
            builder.loadField("mUsingComparators", comparatorArrayType);
            builder.loadConstant(0);
            builder.loadFromArray(TypeDesc.forClass(Comparator.class));
            builder.loadLocal(obj1);
            builder.loadLocal(obj2);
            builder.invoke(compareMethod);
            builder.storeLocal(result);
            builder.loadLocal(result);
            label = builder.createLabel();
            builder.ifZeroComparisonBranch(label, "==");
            builder.loadLocal(result);
            builder.returnValue(TypeDesc.INT);
            label.setLocation();
        }

        // Cast bean parameters to correct types so that properties may be
        // accessed.
        TypeDesc type = TypeDesc.forClass(bc.mBeanClass);
        builder.loadLocal(obj1);
        builder.checkCast(type);
        builder.storeLocal(obj1);
        builder.loadLocal(obj2);
        builder.checkCast(type);
        builder.storeLocal(obj2);

        // Generate code to perform comparisons against each property.
        for (int i=1; i<ruleParts.length; i++) {
            bc = ruleParts[i];

            BeanProperty prop =
                (BeanProperty)bc.getProperties().get(bc.mOrderByName);
            Class propertyClass = prop.getType();
            TypeDesc propertyType = TypeDesc.forClass(propertyClass);

            // Create local variable to hold property values.
            LocalVariable p1 = builder.createLocalVariable("p1", propertyType);
            LocalVariable p2 = builder.createLocalVariable("p2", propertyType);

            // Access properties and store in local variables.
            builder.loadLocal(obj1);
            builder.invoke(prop.getReadMethod());
            builder.storeLocal(p1);
            builder.loadLocal(obj2);
            builder.invoke(prop.getReadMethod());
            builder.storeLocal(p2);

            if ((bc.mFlags & 0x01) != 0) {
                // Reverse properties.
                LocalVariable temp = p1;
                p1 = p2;
                p2 = temp;
            }

            Label nextLabel = builder.createLabel();

            // Handle the case when p1 and p2 are the same (or both null)
            if (!propertyClass.isPrimitive()) {
                builder.loadLocal(p1);
                builder.loadLocal(p2);
                builder.ifEqualBranch(nextLabel, true);

                // Do null order checks for properties.
                nullHigh = (bc.mFlags & 0x02) == 0;
                label = builder.createLabel();
                builder.loadLocal(p1);
                builder.ifNullBranch(label, false);
                builder.loadConstant(nullHigh ? 1 : -1);
                builder.returnValue(TypeDesc.INT);
                label.setLocation();
                label = builder.createLabel();
                builder.loadLocal(p2);
                builder.ifNullBranch(label, false);
                builder.loadConstant(nullHigh ? -1 : 1);
                builder.returnValue(TypeDesc.INT);
                label.setLocation();
            }

            // Call 'using' Comparator if one is provided, else assume
            // Comparable.
            if (bc.mUsingComparator != null) {
                builder.loadThis();
                builder.loadField("mUsingComparators", comparatorArrayType);
                builder.loadConstant(i);
                builder.loadFromArray(TypeDesc.forClass(Comparator.class));
                builder.loadLocal(p1);
                builder.convert(propertyType, propertyType.toObjectType());
                builder.loadLocal(p2);
                builder.convert(propertyType, propertyType.toObjectType());
                builder.invoke(compareMethod);
            } else {
                // If case-sensitive is off and a collator is provided and
                // property could be a String, apply collator.
                if ((bc.mFlags & 0x04) == 0 && bc.mCollator != null &&
                    propertyClass.isAssignableFrom(String.class)) {

                    Label resultLabel = builder.createLabel();

                    if (!String.class.isAssignableFrom(propertyClass)) {
                        // Check if both property values are strings at
                        // runtime. If they aren't, cast to Comparable and call
                        // compareTo.

                        TypeDesc stringType = TypeDesc.STRING;

                        builder.loadLocal(p1);
                        builder.instanceOf(stringType);
                        Label notString = builder.createLabel();
                        builder.ifZeroComparisonBranch(notString, "==");
                        builder.loadLocal(p2);
                        builder.instanceOf(stringType);
                        Label isString = builder.createLabel();
                        builder.ifZeroComparisonBranch(isString, "!=");

                        notString.setLocation();
                        generateComparableCompareTo
                            (builder, propertyClass, compareToMethod,
                             resultLabel, nextLabel, p1, p2);

                        isString.setLocation();
                    }

                    builder.loadThis();
                    builder.loadField("mCollators", comparatorArrayType);
                    builder.loadConstant(i);
                    builder.loadFromArray(TypeDesc.forClass(Comparator.class));
                    builder.loadLocal(p1);
                    builder.loadLocal(p2);
                    builder.invoke(compareMethod);

                    resultLabel.setLocation();
                } else if (propertyClass.isPrimitive()) {
                    generatePrimitiveComparison(builder, propertyClass, p1,p2);
                } else {
                    // Assume properties are instances of Comparable.
                    generateComparableCompareTo
                        (builder, propertyClass, compareToMethod,
                         null, nextLabel, p1, p2);
                }
            }

            if (i < (ruleParts.length - 1)) {
                builder.storeLocal(result);
                builder.loadLocal(result);
                builder.ifZeroComparisonBranch(nextLabel, "==");
                builder.loadLocal(result);
            }
            builder.returnValue(TypeDesc.INT);

            // The next property comparison will start here.
            nextLabel.setLocation();
        }

        endLabel.setLocation();
        builder.loadConstant(0);
        builder.returnValue(TypeDesc.INT);

        return cf.defineClass();
    }
View Full Code Here

Examples of org.cojen.classfile.CodeBuilder

           
            // constructor
            TypeDesc objectArrayType = TypeDesc.OBJECT.toArrayType();
            TypeDesc[] params = {objectArrayType};
            MethodInfo mi = cf.addConstructor(Modifiers.PUBLIC, params);
            mBuilder = new CodeBuilder(mi);
            mBuilder.loadThis();
            mBuilder.loadLocal(mBuilder.getParameter(0));
            mBuilder.invokeSuperConstructor(params);
            mBuilder.returnVoid();

            mIntType = TypeDesc.INT;
            mBooleanType = TypeDesc.BOOLEAN;
            mListType = TypeDesc.forClass(List.class);
            mStringType = TypeDesc.STRING;
            mObjectType = TypeDesc.OBJECT;
            mIntArrayType = TypeDesc.INT.toArrayType();

            // fillMatchResults method
            TypeDesc charArrayType = TypeDesc.CHAR.toArrayType();
            params = new TypeDesc[]{charArrayType, mIntType, mListType};
            mi = cf.addMethod(Modifiers.PUBLIC, "fillMatchResults", null, params);
            mBuilder = new CodeBuilder(mi);

            mLookupLocal = mBuilder.getParameter(0);
            mLimitLocal = mBuilder.getParameter(1);
            mResultsLocal = mBuilder.getParameter(2);
            mPositionsLocal = mBuilder.createLocalVariable("positions", mIntArrayType);
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.