Package serp.bytecode

Examples of serp.bytecode.Code


        }
        c = p.loadClass(new ByteArrayInputStream(b));
        c.getDeclaredFields();
        BCMethod[] methods = c.getDeclaredMethods();
        for (int i = 0; i < methods.length; ++i) {
            Code code = methods[i].getCode(false);
            if (code != null) {
                while (code.hasNext()) {
                    code.next();
                }
                if (compute) {
                    code.calculateMaxStack();
                    code.calculateMaxLocals();
                }
            }
        }
        c.toByteArray();
        return c;
View Full Code Here


     */
    private void delegateConstructors(BCClass bc, Class type) {
        Constructor[] cons = type.getConstructors();
        Class[] params;
        BCMethod m;
        Code code;
        for (int i = 0; i < cons.length; i++) {
            params = cons[i].getParameterTypes();
            m = bc.declareMethod("<init>", void.class, params);
            m.makePublic();

            code = m.getCode(true);
            code.aload().setThis();
            for (int j = 0; j < params.length; j++)
                code.xload().setParam(j).setType(params[j]);
            code.invokespecial().setMethod(cons[i]);
            code.vreturn();
            code.calculateMaxStack();
            code.calculateMaxLocals();
        }
    }
View Full Code Here

        field.setTransient(true);

        BCMethod m = bc.declareMethod("setOwner", void.class, new Class[] {
            OpenJPAStateManager.class, int.class });
        m.makePublic();
        Code code = m.getCode(true);
        code.aload().setThis();
        code.aload().setParam(0);
        code.putfield().setField(sm);
        code.aload().setThis();
        code.iload().setParam(1);
        code.putfield().setField(field);
        code.vreturn();
        code.calculateMaxStack();
        code.calculateMaxLocals();

        m = bc.declareMethod("getOwner", OpenJPAStateManager.class, null);
        m.makePublic();
        code = m.getCode(true);
        code.aload().setThis();
        code.getfield().setField(sm);
        code.areturn();
        code.calculateMaxStack();
        code.calculateMaxLocals();

        m = bc.declareMethod("getOwnerField", int.class, null);
        m.makePublic();
        code = m.getCode(true);
        code.aload().setThis();
        code.getfield().setField(field);
        code.ireturn();
        code.calculateMaxStack();
        code.calculateMaxLocals();

        if (changeTracker) {
            m = bc.declareMethod("getChangeTracker", ChangeTracker.class, null);
            m.makePublic();
            code = m.getCode(true);
            code.constant().setNull();
            code.areturn();
            code.calculateMaxStack();
            code.calculateMaxLocals();
        }
    }
View Full Code Here

            CollectionChangeTracker.class);
        changeTracker.setTransient(true);
        BCMethod m = bc.declareMethod("getChangeTracker", ChangeTracker.class,
            null);
        m.makePublic();
        Code code = m.getCode(true);
        code.aload().setThis();
        code.getfield().setField(changeTracker);
        code.areturn();
        code.calculateMaxStack();
        code.calculateMaxLocals();

        // collection copy
        Constructor cons = findCopyConstructor(type);
        if (cons == null && SortedSet.class.isAssignableFrom(type))
            cons = findComparatorConstructor(type);
        Class[] params = (cons == null) ? new Class[0]
            : cons.getParameterTypes();

        m = bc.declareMethod("copy", Object.class, new Class[] {Object.class});
        m.makePublic();
        code = m.getCode(true);

        code.anew().setType(type);
        code.dup();
        if (params.length == 1) {
            code.aload().setParam(0);
            if (params[0] == Comparator.class) {
                code.checkcast().setType(SortedSet.class);
                code.invokeinterface().setMethod(SortedSet.class, "comparator",
                    Comparator.class, null);
            } else
                code.checkcast().setType(params[0]);
        }
        code.invokespecial().setMethod(type, "<init>", void.class, params);
        if (params.length == 0 || params[0] == Comparator.class) {
            code.dup();
            code.aload().setParam(0);
            code.checkcast().setType(Collection.class);
            code.invokevirtual().setMethod(type, "addAll", boolean.class,
                new Class[] { Collection.class });
            code.pop();
        }
        code.areturn();
        code.calculateMaxStack();
        code.calculateMaxLocals();

        // element type
        BCField elementType = bc.declareField("elementType", Class.class);
        elementType.setTransient(true);
        m = bc.declareMethod("getElementType", Class.class, null);
        m.makePublic();
        code = m.getCode(true);
        code.aload().setThis();
        code.getfield().setField(elementType);
        code.areturn();
        code.calculateMaxStack();
        code.calculateMaxLocals();

        // new instance factory
        m = bc.declareMethod("newInstance", ProxyCollection.class,
            new Class[] { Class.class, Comparator.class, boolean.class });
        m.makePublic();
        code = m.getCode(true);

        code.anew().setType(bc);
        code.dup();
        cons = findComparatorConstructor(type);
        params = (cons == null) ? new Class[0] : cons.getParameterTypes();
        if (params.length == 1)
            code.aload().setParam(1);
        code.invokespecial().setMethod("<init>", void.class, params);
        int ret = code.getNextLocalsIndex();
        code.astore().setLocal(ret);

        // set element type
        code.aload().setLocal(ret);
        code.aload().setParam(0);
        code.putfield().setField(elementType);

        // create change tracker and set it
        code.iload().setParam(2);
        JumpInstruction ifins = code.ifeq();
        code.aload().setLocal(ret);
        code.anew().setType(CollectionChangeTrackerImpl.class);
        code.dup();
        code.aload().setLocal(ret);
        code.constant().setValue(allowsDuplicates(type));
        code.constant().setValue(isOrdered(type));
        code.invokespecial().setMethod(CollectionChangeTrackerImpl.class,
            "<init>", void.class, new Class[] { Collection.class,
            boolean.class, boolean.class });
        code.putfield().setField(changeTracker);

        ifins.setTarget(code.aload().setLocal(ret));
        code.areturn();
        code.calculateMaxStack();
        code.calculateMaxLocals();
    }
View Full Code Here

    /**
     * Enhance constructor to initialize fields
     */
    private void enhanceConstructor(BCClass bc) {
        BCMethod cons = bc.getDeclaredMethod("<init>", (String[]) null);
        Code code = cons.getCode(false);
        code.afterLast();
        code.previous();

        // private BitSet loaded = new BitSet();
        BCField loaded = addBeanField(bc, "loaded", BitSet.class);
        loaded.setFinal(true);
        code.aload().setThis();
        code.anew().setType(BitSet.class);
        code.dup();
        code.constant().setValue(bc.getFields().length);
        code.invokespecial().setMethod(BitSet.class, "<init>", void.class,
            new Class[]{ int.class });
        code.putfield().setField(loaded);

        code.calculateMaxStack();
        code.calculateMaxLocals();
    }
View Full Code Here

        type.setStatic(true);
        type.makePrivate();
        // public Class getType() {
        BCMethod getter = bc.declareMethod("getType", Class.class, null);
        getter.makePublic();
        Code code = getter.getCode(true);
        // if (type == null) {
        //     try {
        //       type = Class.forName
        //         (meta.getDescribedType().getName(), true,
        //         Thread.currentThread().getContextClassLoader());
        //     } catch (ClassNotFoundException cnfe) {
        //       throw new InternalException();
        //     }
        // }
        code.getstatic().setField(type);

        Collection jumps = new LinkedList();
        jumps.add(code.ifnonnull());
        ExceptionHandler handler = code.addExceptionHandler();

        handler.setTryStart(code.constant().setValue
            (meta.getDescribedType().getName()));
        code.constant().setValue(true);
        code.invokestatic().setMethod(Thread.class, "currentThread",
            Thread.class, null);
        code.invokevirtual().setMethod(Thread.class, "getContextClassLoader",
            ClassLoader.class, null);
        code.invokestatic().setMethod(Class.class, "forName", Class.class,
            new Class[]{ String.class, boolean.class, ClassLoader.class });
        code.putstatic().setField(type);
        Instruction go2 = code.go2();
        jumps.add(go2);
        handler.setTryEnd(go2);
        handler.setCatch(ClassNotFoundException.class);
        handler.setHandlerStart(throwException
            (code, InternalException.class));
        setTarget(code.getstatic().setField(type), jumps);
        code.areturn();

        code.calculateMaxStack();
        code.calculateMaxLocals();
    }
View Full Code Here

     */
    private void addImplDataMethods(BCClass bc, ClassMetaData meta) {
        // void storeImplData(OpenJPAStateManager);
        BCMethod meth = bc.declareMethod("storeImplData", void.class,
            new Class[]{ OpenJPAStateManager.class });
        Code code = meth.getCode(true);

        BCField impl = null;
        if (!usesImplData(meta))
            code.vreturn();
        else {
            // if (sm.isImplDataCacheable())
            //     setImplData(sm.getImplData());
            impl = addBeanField(bc, "implData", Object.class);
            code.aload().setParam(0);
            code.invokeinterface().setMethod(OpenJPAStateManager.class,
                "isImplDataCacheable", boolean.class, null);
            JumpInstruction ifins = code.ifeq();
            code.aload().setThis();
            code.aload().setParam(0);
            code.invokeinterface().setMethod(OpenJPAStateManager.class,
                "getImplData", Object.class, null);
            code.invokevirtual().setMethod("setImplData", void.class,
                new Class[]{ Object.class });
            ifins.setTarget(code.vreturn());
        }
        code.calculateMaxStack();
        code.calculateMaxLocals();

        // void loadImplData(OpenJPAStateManager);
        meth = bc.declareMethod("loadImplData", void.class,
            new Class[]{ OpenJPAStateManager.class });
        code = meth.getCode(true);
        if (!usesImplData(meta))
            code.vreturn();
        else {
            // if (sm.getImplData() == null && implData != null)
            //     sm.setImplData(impl, true);
            code.aload().setParam(0);
            code.invokeinterface().setMethod(OpenJPAStateManager.class,
                "getImplData", Object.class, null);
            JumpInstruction ifins = code.ifnonnull();
            code.aload().setThis();
            code.getfield().setField(impl);
            JumpInstruction ifins2 = code.ifnull();
            code.aload().setParam(0);
            code.aload().setThis();
            code.getfield().setField(impl);
            code.constant().setValue(true);
            code.invokeinterface().setMethod(OpenJPAStateManager.class,
                "setImplData", void.class,
                new Class[]{ Object.class, boolean.class });
            Instruction ins = code.vreturn();
            ifins.setTarget(ins);
            ifins2.setTarget(ins);
        }
        code.calculateMaxStack();
        code.calculateMaxLocals();
    }
View Full Code Here

            MapChangeTracker.class);
        changeTracker.setTransient(true);
        BCMethod m = bc.declareMethod("getChangeTracker", ChangeTracker.class,
            null);
        m.makePublic();
        Code code = m.getCode(true);
        code.aload().setThis();
        code.getfield().setField(changeTracker);
        code.areturn();
        code.calculateMaxStack();
        code.calculateMaxLocals();

        // map copy
        Constructor cons = findCopyConstructor(type);
        if (cons == null && SortedMap.class.isAssignableFrom(type))
            cons = findComparatorConstructor(type);
        Class[] params = (cons == null) ? new Class[0]
            : cons.getParameterTypes();

        m = bc.declareMethod("copy", Object.class, new Class[] {Object.class});
        m.makePublic();
        code = m.getCode(true);

        code.anew().setType(type);
        code.dup();
        if (params.length == 1) {
            code.aload().setParam(0);
            if (params[0] == Comparator.class) {
                code.checkcast().setType(SortedMap.class);
                code.invokeinterface().setMethod(SortedMap.class, "comparator",
                    Comparator.class, null);
            } else
                code.checkcast().setType(params[0]);
        }
        code.invokespecial().setMethod(type, "<init>", void.class, params);
        if (params.length == 0 || params[0] == Comparator.class) {
            code.dup();
            code.aload().setParam(0);
            code.checkcast().setType(Map.class);
            code.invokevirtual().setMethod(type, "putAll", void.class,
                new Class[] { Map.class });
        }
        code.areturn();
        code.calculateMaxStack();
        code.calculateMaxLocals();

        // key type
        BCField keyType = bc.declareField("keyType", Class.class);
        keyType.setTransient(true);
        m = bc.declareMethod("getKeyType", Class.class, null);
        m.makePublic();
        code = m.getCode(true);
        code.aload().setThis();
        code.getfield().setField(keyType);
        code.areturn();
        code.calculateMaxStack();
        code.calculateMaxLocals();

        // value type
        BCField valueType = bc.declareField("valueType", Class.class);
        valueType.setTransient(true);
        m = bc.declareMethod("getValueType", Class.class, null);
        m.makePublic();
        code = m.getCode(true);
        code.aload().setThis();
        code.getfield().setField(valueType);
        code.areturn();
        code.calculateMaxStack();
        code.calculateMaxLocals();

        // new instance factory
        m = bc.declareMethod("newInstance", ProxyMap.class,
            new Class[] { Class.class, Class.class, Comparator.class,
            boolean.class });
        m.makePublic();
        code = m.getCode(true);

        code.anew().setType(bc);
        code.dup();
        cons = findComparatorConstructor(type);
        params = (cons == null) ? new Class[0] : cons.getParameterTypes();
        if (params.length == 1)
            code.aload().setParam(2);
        code.invokespecial().setMethod("<init>", void.class, params);
        int ret = code.getNextLocalsIndex();
        code.astore().setLocal(ret);

        // set key and value types
        code.aload().setLocal(ret);
        code.aload().setParam(0);
        code.putfield().setField(keyType);
        code.aload().setLocal(ret);
        code.aload().setParam(1);
        code.putfield().setField(valueType);

        // create change tracker and set it
        code.iload().setParam(3);
        JumpInstruction ifins = code.ifeq();
        code.aload().setLocal(ret);
        code.anew().setType(MapChangeTrackerImpl.class);
        code.dup();
        code.aload().setLocal(ret);
        code.invokespecial().setMethod(MapChangeTrackerImpl.class,
            "<init>", void.class, new Class[] { Map.class });
        code.putfield().setField(changeTracker);

        ifins.setTarget(code.aload().setLocal(ret));
        code.areturn();
        code.calculateMaxStack();
        code.calculateMaxLocals();
    }
View Full Code Here

     * @return serp.bytecode.Code
     */
    public static final PrivilegedAction newCodeAction() {
        return new PrivilegedAction() {
            public Object run() {
                return new Code();
            }
        };
    }
View Full Code Here

    private void addFieldImplDataMethods(BCClass bc, ClassMetaData meta) {
        // public void loadImplData(OpenJPAStateManager sm, int i)
        BCMethod meth = bc.declareMethod("loadImplData", void.class,
            new Class[]{ OpenJPAStateManager.class, int.class });
        meth.makePrivate();
        Code code = meth.getCode(true);

        int count = countImplDataFields(meta);
        BCField impl = null;
        if (count == 0)
            code.vreturn();
        else {
            // Object[] fieldImpl
            impl = bc.declareField("fieldImpl", Object[].class);
            impl.makePrivate();

            // if (fieldImpl != null)
            code.aload().setThis();
            code.getfield().setField(impl);
            JumpInstruction ifins = code.ifnonnull();
            code.vreturn();

            // Object obj = null;
            int obj = code.getNextLocalsIndex();
            ifins.setTarget(code.constant().setNull());
            code.astore().setLocal(obj);

            // establish switch target, then move before it
            Instruction target = code.aload().setLocal(obj);
            code.previous();

            // switch(i)
            code.iload().setParam(1);
            LookupSwitchInstruction lswitch = code.lookupswitch();
            FieldMetaData[] fields = meta.getFields();
            int cacheable = 0;
            for (int i = 0; i < fields.length; i++) {
                if (!usesImplData(fields[i]))
                    continue;
                // case x: obj = fieldImpl[y]; break;
                lswitch.addCase(i, code.aload().setThis());
                code.getfield().setField(impl);
                code.constant().setValue(cacheable++);
                code.aaload();
                code.astore().setLocal(obj);
                code.go2().setTarget(target);
            }
            lswitch.setDefaultTarget(target);

            // if (obj != null)
            code.next();    // jump back over target
            ifins = code.ifnonnull();
            code.vreturn();

            // sm.setImplData(index, impl);
            ifins.setTarget(code.aload().setParam(0));
            code.iload().setParam(1);
            code.aload().setLocal(obj);
            code.invokeinterface().setMethod(OpenJPAStateManager.class,
                "setImplData", void.class,
                new Class[]{ int.class, Object.class });
            code.vreturn();
        }
        code.calculateMaxLocals();
        code.calculateMaxStack();

        // void storeImplData(OpenJPAStateManager sm, int index, boolean loaded)
        meth = bc.declareMethod("storeImplData", void.class,
            new Class[]{ OpenJPAStateManager.class, int.class, boolean.class });
        code = meth.getCode(true);
        if (count == 0)
            code.vreturn();
        else {
            // int arrIdx = -1;
            // switch(index)
            int arrIdx = code.getNextLocalsIndex();
            code.constant().setValue(-1);
            code.istore().setLocal(arrIdx);
            code.iload().setParam(1);
            LookupSwitchInstruction lswitch = code.lookupswitch();

            // establish switch target, then move before it
            Instruction switchTarget = code.iload().setLocal(arrIdx);
            code.previous();

            FieldMetaData[] fields = meta.getFields();
            int cacheable = 0;
            for (int i = 0; i < fields.length; i++) {
                if (!usesImplData(fields[i]))
                    continue;
                // case x: arrIdx = y; break;
                lswitch.addCase(i, code.constant().setValue(cacheable++));
                code.istore().setLocal(arrIdx);
                code.go2().setTarget(switchTarget);
            }
            lswitch.setDefaultTarget(switchTarget);
            code.next();    // step over switch target

            // if (arrIdx != -1)
            code.constant().setValue(-1);
            JumpInstruction ifins = code.ificmpne();
            code.vreturn();

            // create null target, then move before it
            Instruction nullTarget = code.aload().setThis();
            code.previous();

            // if (loaded)
            ifins.setTarget(code.iload().setParam(2));
            code.ifeq().setTarget(nullTarget);

            // Object obj = sm.getImplData(index)
            int obj = code.getNextLocalsIndex();
            code.aload().setParam(0);
            code.iload().setParam(1);
            code.invokeinterface().setMethod(OpenJPAStateManager.class,
                "getImplData", Object.class, new Class[]{ int.class });
            code.astore().setLocal(obj);

            // if (obj != null)
            code.aload().setLocal(obj);
            code.ifnull().setTarget(nullTarget);

            // if (fieldImpl == null)
            //     fieldImpl = new Object[fields];
            code.aload().setThis();
            code.getfield().setField(impl);
            ifins = code.ifnonnull();
            code.aload().setThis();
            code.constant().setValue(count);
            code.anewarray().setType(Object.class);
            code.putfield().setField(impl);

            // fieldImpl[arrIdx] = obj;
            // return;
            ifins.setTarget(code.aload().setThis());
            code.getfield().setField(impl);
            code.iload().setLocal(arrIdx);
            code.aload().setLocal(obj);
            code.aastore();
            code.vreturn();

            // if (fieldImpl != null)
            //     fieldImpl[index] = null;
            code.next(); // step over nullTarget
            code.getfield().setField(impl);
            ifins = code.ifnonnull();
            code.vreturn();
            ifins.setTarget(code.aload().setThis());
            code.getfield().setField(impl);
            code.iload().setLocal(arrIdx);
            code.constant().setNull();
            code.aastore();
            code.vreturn();
        }
        code.calculateMaxStack();
        code.calculateMaxLocals();
    }
View Full Code Here

TOP

Related Classes of serp.bytecode.Code

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.