Package org.cojen.classfile

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


        } 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

            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

            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

    }

    private static ThrowUnchecked generateImpl() {
        RuntimeClassFile cf = new RuntimeClassFile(null, ThrowUnchecked.class.getName());
        cf.addDefaultConstructor();
        CodeBuilder b = new CodeBuilder
            (cf.addMethod(Modifiers.PROTECTED, "doFire",
                          null, new TypeDesc[] {TypeDesc.forClass(Throwable.class)}));
        b.loadLocal(b.getParameter(0));
        b.throwObject();
        try {
            return (ThrowUnchecked) cf.defineClass().newInstance();
        } catch (Exception e) {
            throw new Error(e);
        }
View Full Code Here

TOP

Related Classes of org.cojen.classfile.CodeBuilder

Copyright © 2018 www.massapicom. 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.