Example usage for org.objectweb.asm Opcodes ACC_PRIVATE

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

Introduction

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

Prototype

int ACC_PRIVATE

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

Click Source Link

Usage

From source file:lombok.patcher.scripts.SetSymbolDuringMethodCallScript.java

License:Open Source License

private void makeWrapperMethod(ClassVisitor cv, WrapperMethodDescriptor wmd) {
    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_SYNTHETIC | Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC,
            wmd.getWrapperName(), wmd.getWrapperDescriptor(), null, null);

    MethodLogistics logistics = new MethodLogistics(Opcodes.ACC_STATIC, wmd.getWrapperDescriptor());

    mv.visitCode();/*from w ww.j a  va 2  s .  c o  m*/
    Label start = new Label();
    Label end = new Label();
    Label handler = new Label();
    mv.visitTryCatchBlock(start, end, handler, null);
    mv.visitLabel(start);
    mv.visitLdcInsn(symbol);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "lombok/patcher/Symbols", "push", "(Ljava/lang/String;)V", false);
    for (int i = 0; i < logistics.getParamCount(); i++) {
        logistics.generateLoadOpcodeForParam(i, mv);
    }
    mv.visitMethodInsn(wmd.getOpcode(), wmd.getOwner(), wmd.getName(), wmd.getTargetDescriptor(), wmd.isItf());
    mv.visitLabel(end);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "lombok/patcher/Symbols", "pop", "()V", false);
    logistics.generateReturnOpcode(mv);
    mv.visitLabel(handler);
    mv.visitFrame(Opcodes.F_FULL, 0, null, 1, new Object[] { "java/lang/Throwable" });
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "lombok/patcher/Symbols", "pop", "()V", false);
    mv.visitInsn(Opcodes.ATHROW);
    mv.visitMaxs(Math.max(1, logistics.getParamCount()), logistics.getParamCount());
    mv.visitEnd();
}

From source file:lucee.transformer.bytecode.BodyBase.java

License:Open Source License

public static void writeOut(final BytecodeContext bc, List<Statement> statements) throws BytecodeException {
    GeneratorAdapter adapter = bc.getAdapter();
    boolean isOutsideMethod;
    GeneratorAdapter a = null;//from  www . j  ava 2 s  .  co m
    Method m;
    BytecodeContext _bc = bc;
    Iterator<Statement> it = statements.iterator();
    boolean split = bc.getPage().getSplitIfNecessary();

    //int lastLine=-1;
    while (it.hasNext()) {
        isOutsideMethod = bc.getMethod().getReturnType().equals(Types.VOID);
        Statement s = it.next();
        if (split && _bc.incCount() > MAX_STATEMENTS && bc.doSubFunctions()
                && (isOutsideMethod || !s.hasFlowController()) && s.getStart() != null) {
            if (a != null) {
                a.returnValue();
                a.endMethod();
            }
            //ExpressionUtil.visitLine(bc, s.getLine());
            String method = ASMUtil.createOverfowMethod(bc.getMethod().getName(),
                    bc.getPage().getMethodCount());
            ExpressionUtil.visitLine(bc, s.getStart());
            //ExpressionUtil.lastLine(bc);
            m = new Method(method, Types.VOID, new Type[] { Types.PAGE_CONTEXT });
            a = new GeneratorAdapter(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL, m, null,
                    new Type[] { Types.THROWABLE }, bc.getClassWriter());

            _bc = new BytecodeContext(bc.getStaticConstructor(), bc.getConstructor(), bc.getKeys(), bc, a, m);
            if (bc.getRoot() != null)
                _bc.setRoot(bc.getRoot());
            else
                _bc.setRoot(bc);

            adapter.visitVarInsn(Opcodes.ALOAD, 0);
            adapter.visitVarInsn(Opcodes.ALOAD, 1);
            adapter.visitMethodInsn(Opcodes.INVOKEVIRTUAL, bc.getClassName(), method,
                    "(Llucee/runtime/PageContext;)V");
        }
        if (_bc != bc && s.hasFlowController()) {
            if (a != null) {
                a.returnValue();
                a.endMethod();
            }
            _bc = bc;
            a = null;
        }
        ExpressionUtil.writeOut(s, _bc);
    }
    if (a != null) {
        a.returnValue();
        a.endMethod();
    }
}

From source file:lucee.transformer.bytecode.BodyBase.java

License:Open Source License

private static void addToSubMethod(BytecodeContext bc, Statement... statements) throws BytecodeException {
    if (statements == null || statements.length == 0)
        return;/*  w  w  w . ja v a 2  s.  co m*/

    GeneratorAdapter adapter = bc.getAdapter();
    String method = ASMUtil.createOverfowMethod(bc.getMethod().getName(), bc.getPage().getMethodCount());

    for (int i = 0; i < statements.length; i++) {
        if (statements[i].getStart() != null) {
            ExpressionUtil.visitLine(bc, statements[i].getStart());
            break;
        }
    }

    //ExpressionUtil.lastLine(bc);
    Method m = new Method(method, Types.VOID, new Type[] { Types.PAGE_CONTEXT });
    GeneratorAdapter a = new GeneratorAdapter(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL, m, null,
            new Type[] { Types.THROWABLE }, bc.getClassWriter());

    BytecodeContext _bc = new BytecodeContext(bc.getStaticConstructor(), bc.getConstructor(), bc.getKeys(), bc,
            a, m);
    if (bc.getRoot() != null)
        _bc.setRoot(bc.getRoot());
    else
        _bc.setRoot(bc);

    adapter.visitVarInsn(Opcodes.ALOAD, 0);
    adapter.visitVarInsn(Opcodes.ALOAD, 1);
    adapter.visitMethodInsn(Opcodes.INVOKEVIRTUAL, bc.getClassName(), method, "(Llucee/runtime/PageContext;)V");

    for (int i = 0; i < statements.length; i++) {
        ExpressionUtil.writeOut(statements[i], _bc);
    }

    a.returnValue();
    a.endMethod();
}

From source file:lucee.transformer.bytecode.Page.java

License:Open Source License

/**
 * result byte code as binary array/* w  w  w  . jav  a 2s  .  co  m*/
 * @param classFile 
 * @return byte code
 * @throws IOException 
 * @throws TemplateException 
 */
public byte[] execute(PageSource source, Resource classFile) throws BytecodeException {
    /*
    // this is done that the Page can be executed more than once
    if(initFunctions==null)
       initFunctions=(ArrayList<IFunction>) functions.clone();
    else
       functions=initFunctions;
    if(initThreads==null)
       initThreads=(ArrayList<TagThread>) threads.clone();
    else
       threads=initThreads;
    methodCount=0;
    off=0;
    staticTextLocation=null;
            
            
    print.e(this.functions);
    print.e(this.threads);*/

    Resource p = classFile.getParentResource().getRealResource(classFile.getName() + ".txt");

    List<LitString> keys = new ArrayList<LitString>();
    ClassWriter cw = ASMUtil.getClassWriter();

    ArrayList<String> imports = new ArrayList<String>();
    getImports(imports, this);

    // parent
    String parent = "lucee/runtime/PagePlus";
    if (isComponent())
        parent = "lucee/runtime/ComponentPage";
    else if (isInterface())
        parent = "lucee/runtime/InterfacePage";

    cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, name, null, parent, null);
    cw.visitSource(this.pageSource.getPhyscalFile().getAbsolutePath(),
            "rel:" + this.pageSource.getFullRealpath()); // when adding more use ; as delimiter
    //

    // static constructor
    //GeneratorAdapter statConstrAdapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC,STATIC_CONSTRUCTOR,null,null,cw);
    BytecodeContext statConstr = null;//new BytecodeContext(null,null,this,externalizer,keys,cw,name,statConstrAdapter,STATIC_CONSTRUCTOR,writeLog(),suppressWSbeforeArg);

    // constructor
    GeneratorAdapter constrAdapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC, CONSTRUCTOR_PS, null, null, cw);
    BytecodeContext constr = new BytecodeContext(source, null, null, this, keys, cw, name, constrAdapter,
            CONSTRUCTOR_PS, writeLog(), suppressWSbeforeArg, output);
    constrAdapter.loadThis();
    Type t = Types.PAGE_PLUS;
    if (isComponent())
        t = Types.COMPONENT_PAGE;
    else if (isInterface())
        t = Types.INTERFACE_PAGE;

    constrAdapter.invokeConstructor(t, CONSTRUCTOR);

    // call _init()
    constrAdapter.visitVarInsn(Opcodes.ALOAD, 0);
    constrAdapter.visitMethodInsn(Opcodes.INVOKEVIRTUAL, constr.getClassName(), "initKeys", "()V");

    // private static  ImportDefintion[] test=new ImportDefintion[]{...};
    {
        FieldVisitor fv = cw.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL, "imports",
                "[Llucee/runtime/component/ImportDefintion;", null, null);
        fv.visitEnd();

        constrAdapter.visitVarInsn(Opcodes.ALOAD, 0);
        ArrayVisitor av = new ArrayVisitor();
        av.visitBegin(constrAdapter, Types.IMPORT_DEFINITIONS, imports.size());
        int index = 0;
        Iterator<String> it = imports.iterator();
        while (it.hasNext()) {
            av.visitBeginItem(constrAdapter, index++);
            constrAdapter.push(it.next());
            ASMConstants.NULL(constrAdapter);
            constrAdapter.invokeStatic(Types.IMPORT_DEFINITIONS_IMPL, ID_GET_INSTANCE);
            av.visitEndItem(constrAdapter);
        }
        av.visitEnd();
        constrAdapter.visitFieldInsn(Opcodes.PUTFIELD, name, "imports",
                "[Llucee/runtime/component/ImportDefintion;");

    }

    // getVersion
    GeneratorAdapter adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, VERSION, null, null,
            cw);
    adapter.push(version);
    adapter.returnValue();
    adapter.endMethod();

    // public ImportDefintion[] getImportDefintions()
    if (imports.size() > 0) {
        adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, GET_IMPORT_DEFINITIONS, null,
                null, cw);
        adapter.visitVarInsn(Opcodes.ALOAD, 0);
        adapter.visitFieldInsn(Opcodes.GETFIELD, name, "imports", "[Llucee/runtime/component/ImportDefintion;");
        adapter.returnValue();
        adapter.endMethod();
    } else {
        adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, GET_IMPORT_DEFINITIONS, null,
                null, cw);
        adapter.visitInsn(Opcodes.ICONST_0);
        adapter.visitTypeInsn(Opcodes.ANEWARRAY, "lucee/runtime/component/ImportDefintion");
        adapter.returnValue();
        adapter.endMethod();
    }

    // getSourceLastModified
    adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, LAST_MOD, null, null, cw);
    adapter.push(lastModifed);
    adapter.returnValue();
    adapter.endMethod();

    // getCompileTime
    adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, COMPILE_TIME, null, null, cw);
    adapter.push(System.currentTimeMillis());
    adapter.returnValue();
    adapter.endMethod();

    // newInstance/initComponent/call
    if (isComponent()) {
        Tag component = getComponent();
        writeOutNewComponent(statConstr, constr, keys, cw, component);
        writeOutInitComponent(statConstr, constr, keys, cw, component);
    } else if (isInterface()) {
        Tag interf = getInterface();
        writeOutNewInterface(statConstr, constr, keys, cw, interf);
        writeOutInitInterface(statConstr, constr, keys, cw, interf);
    } else {
        writeOutCall(statConstr, constr, keys, cw);
    }

    // udfCall     
    Function[] functions = getFunctions();
    ConditionVisitor cv;
    DecisionIntVisitor div;
    // less/equal than 10 functions
    if (isInterface()) {
    } else if (functions.length <= 10) {
        adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, UDF_CALL, null,
                new Type[] { Types.THROWABLE }, cw);
        BytecodeContext bc = new BytecodeContext(source, statConstr, constr, this, keys, cw, name, adapter,
                UDF_CALL, writeLog(), suppressWSbeforeArg, output);
        if (functions.length == 0) {
        } else if (functions.length == 1) {
            ExpressionUtil.visitLine(bc, functions[0].getStart());
            functions[0].getBody().writeOut(bc);
            ExpressionUtil.visitLine(bc, functions[0].getEnd());
        } else
            writeOutUdfCallInner(bc, functions, 0, functions.length);
        adapter.visitInsn(Opcodes.ACONST_NULL);
        adapter.returnValue();
        adapter.endMethod();
    }
    // more than 10 functions
    else {
        adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, UDF_CALL, null,
                new Type[] { Types.THROWABLE }, cw);
        BytecodeContext bc = new BytecodeContext(source, statConstr, constr, this, keys, cw, name, adapter,
                UDF_CALL, writeLog(), suppressWSbeforeArg, output);
        cv = new ConditionVisitor();
        cv.visitBefore();
        int count = 0;
        for (int i = 0; i < functions.length; i += 10) {
            cv.visitWhenBeforeExpr();
            div = new DecisionIntVisitor();
            div.visitBegin();
            adapter.loadArg(2);
            div.visitLT();
            adapter.push(i + 10);
            div.visitEnd(bc);
            cv.visitWhenAfterExprBeforeBody(bc);

            adapter.visitVarInsn(Opcodes.ALOAD, 0);
            adapter.visitVarInsn(Opcodes.ALOAD, 1);
            adapter.visitVarInsn(Opcodes.ALOAD, 2);
            adapter.visitVarInsn(Opcodes.ILOAD, 3);
            adapter.visitMethodInsn(Opcodes.INVOKEVIRTUAL, name, createFunctionName(++count),
                    "(Llucee/runtime/PageContext;Llucee/runtime/type/UDF;I)Ljava/lang/Object;");
            adapter.visitInsn(Opcodes.ARETURN);//adapter.returnValue();
            cv.visitWhenAfterBody(bc);
        }
        cv.visitAfter(bc);

        adapter.visitInsn(Opcodes.ACONST_NULL);
        adapter.returnValue();
        adapter.endMethod();

        count = 0;
        Method innerCall;
        for (int i = 0; i < functions.length; i += 10) {
            innerCall = new Method(createFunctionName(++count), Types.OBJECT,
                    new Type[] { Types.PAGE_CONTEXT, USER_DEFINED_FUNCTION, Types.INT_VALUE });

            adapter = new GeneratorAdapter(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL, innerCall, null,
                    new Type[] { Types.THROWABLE }, cw);
            writeOutUdfCallInner(
                    new BytecodeContext(source, statConstr, constr, this, keys, cw, name, adapter, innerCall,
                            writeLog(), suppressWSbeforeArg, output),
                    functions, i, i + 10 > functions.length ? functions.length : i + 10);

            adapter.visitInsn(Opcodes.ACONST_NULL);
            adapter.returnValue();
            adapter.endMethod();
        }
    }

    // threadCall
    TagThread[] threads = getThreads();
    if (true) {
        adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, THREAD_CALL, null,
                new Type[] { Types.THROWABLE }, cw);
        if (threads.length > 0)
            writeOutThreadCallInner(new BytecodeContext(source, statConstr, constr, this, keys, cw, name,
                    adapter, THREAD_CALL, writeLog(), suppressWSbeforeArg, output), threads, 0, threads.length);
        //adapter.visitInsn(Opcodes.ACONST_NULL);
        adapter.returnValue();
        adapter.endMethod();
    }

    // udfDefaultValue
    // less/equal than 10 functions
    if (isInterface()) {
    } else if (functions.length <= 10) {
        adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, UDF_DEFAULT_VALUE, null,
                new Type[] { Types.PAGE_EXCEPTION }, cw);
        if (functions.length > 0)
            writeUdfDefaultValueInner(
                    new BytecodeContext(source, statConstr, constr, this, keys, cw, name, adapter,
                            UDF_DEFAULT_VALUE, writeLog(), suppressWSbeforeArg, output),
                    functions, 0, functions.length);

        adapter.loadArg(DEFAULT_VALUE);
        adapter.returnValue();
        adapter.endMethod();
    } else {
        adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, UDF_DEFAULT_VALUE, null,
                new Type[] { Types.PAGE_EXCEPTION }, cw);
        BytecodeContext bc = new BytecodeContext(source, statConstr, constr, this, keys, cw, name, adapter,
                UDF_DEFAULT_VALUE, writeLog(), suppressWSbeforeArg, output);
        cv = new ConditionVisitor();
        cv.visitBefore();
        int count = 0;
        for (int i = 0; i < functions.length; i += 10) {
            cv.visitWhenBeforeExpr();
            div = new DecisionIntVisitor();
            div.visitBegin();
            adapter.loadArg(1);
            div.visitLT();
            adapter.push(i + 10);
            div.visitEnd(bc);
            cv.visitWhenAfterExprBeforeBody(bc);

            adapter.visitVarInsn(Opcodes.ALOAD, 0);
            adapter.visitVarInsn(Opcodes.ALOAD, 1);
            adapter.visitVarInsn(Opcodes.ILOAD, 2);
            adapter.visitVarInsn(Opcodes.ILOAD, 3);
            adapter.visitVarInsn(Opcodes.ALOAD, 4);

            adapter.visitMethodInsn(Opcodes.INVOKEVIRTUAL, name, "udfDefaultValue" + (++count),
                    "(Llucee/runtime/PageContext;IILjava/lang/Object;)Ljava/lang/Object;");
            adapter.visitInsn(Opcodes.ARETURN);//adapter.returnValue();

            cv.visitWhenAfterBody(bc);
        }
        cv.visitAfter(bc);

        adapter.visitInsn(Opcodes.ACONST_NULL);
        adapter.returnValue();
        adapter.endMethod();

        count = 0;
        Method innerDefaultValue;
        for (int i = 0; i < functions.length; i += 10) {
            innerDefaultValue = new Method("udfDefaultValue" + (++count), Types.OBJECT,
                    new Type[] { Types.PAGE_CONTEXT, Types.INT_VALUE, Types.INT_VALUE, Types.OBJECT });
            adapter = new GeneratorAdapter(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL, innerDefaultValue, null,
                    new Type[] { Types.PAGE_EXCEPTION }, cw);
            writeUdfDefaultValueInner(
                    new BytecodeContext(source, statConstr, constr, this, keys, cw, name, adapter,
                            innerDefaultValue, writeLog(), suppressWSbeforeArg, output),
                    functions, i, i + 10 > functions.length ? functions.length : i + 10);

            adapter.loadArg(DEFAULT_VALUE);
            //adapter.visitInsn(Opcodes.ACONST_NULL);
            adapter.returnValue();
            adapter.endMethod();
        }

    }

    // register fields
    {
        GeneratorAdapter aInit = new GeneratorAdapter(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL, _INIT, null,
                null, cw);
        BytecodeContext bcInit = new BytecodeContext(source, statConstr, constr, this, keys, cw, name, aInit,
                _INIT, writeLog(), suppressWSbeforeArg, output);
        registerFields(bcInit, keys);
        aInit.returnValue();
        aInit.endMethod();
    }

    //setPageSource(pageSource);
    constrAdapter.visitVarInsn(Opcodes.ALOAD, 0);
    constrAdapter.visitVarInsn(Opcodes.ALOAD, 1);
    constrAdapter.invokeVirtual(t, SET_PAGE_SOURCE);

    constrAdapter.returnValue();
    constrAdapter.endMethod();

    return cw.toByteArray();

}

From source file:lucee.transformer.bytecode.Page.java

License:Open Source License

public static void registerFields(BytecodeContext bc, List<LitString> keys) throws BytecodeException {
    //if(keys.size()==0) return;
    GeneratorAdapter ga = bc.getAdapter();

    FieldVisitor fv = bc.getClassWriter().visitField(Opcodes.ACC_PRIVATE, "keys",
            Types.COLLECTION_KEY_ARRAY.toString(), null, null);
    fv.visitEnd();/*w  w  w .  ja  v a2  s  .  c o m*/

    int index = 0;
    LitString value;
    Iterator<LitString> it = keys.iterator();
    ga.visitVarInsn(Opcodes.ALOAD, 0);
    ga.push(keys.size());
    ga.newArray(Types.COLLECTION_KEY);
    while (it.hasNext()) {
        value = it.next();
        ga.dup();
        ga.push(index++);
        //value.setExternalize(false);
        ExpressionUtil.writeOutSilent(value, bc, Expression.MODE_REF);
        ga.invokeStatic(KEY_IMPL, KEY_INTERN);
        ga.visitInsn(Opcodes.AASTORE);
    }
    ga.visitFieldInsn(Opcodes.PUTFIELD, bc.getClassName(), "keys", Types.COLLECTION_KEY_ARRAY.toString());
}

From source file:lucee.transformer.bytecode.util.ASMUtil.java

License:Open Source License

private static void createProperty(ClassWriter cw, String classType, ASMProperty property)
        throws PageException {
    String name = property.getName();
    Type type = property.getASMType();
    Class clazz = property.getClazz();

    cw.visitField(Opcodes.ACC_PRIVATE, name, type.toString(), null, null).visitEnd();

    int load = loadFor(type);
    //int sizeOf=sizeOf(type);

    // get<PropertyName>():<type>
    Type[] types = new Type[0];
    Method method = new Method((clazz == boolean.class ? "get" : "get") + StringUtil.ucFirst(name), type,
            types);/*w  ww. jav a 2  s.c  om*/
    GeneratorAdapter adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC, method, null, null, cw);

    Label start = new Label();
    adapter.visitLabel(start);

    adapter.visitVarInsn(Opcodes.ALOAD, 0);
    adapter.visitFieldInsn(Opcodes.GETFIELD, classType, name, type.toString());
    adapter.returnValue();

    Label end = new Label();
    adapter.visitLabel(end);
    adapter.visitLocalVariable("this", "L" + classType + ";", null, start, end, 0);
    adapter.visitEnd();

    adapter.endMethod();

    // set<PropertyName>(object):void
    types = new Type[] { type };
    method = new Method("set" + StringUtil.ucFirst(name), Types.VOID, types);
    adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC, method, null, null, cw);

    start = new Label();
    adapter.visitLabel(start);
    adapter.visitVarInsn(Opcodes.ALOAD, 0);
    adapter.visitVarInsn(load, 1);
    adapter.visitFieldInsn(Opcodes.PUTFIELD, classType, name, type.toString());

    adapter.visitInsn(Opcodes.RETURN);
    end = new Label();
    adapter.visitLabel(end);
    adapter.visitLocalVariable("this", "L" + classType + ";", null, start, end, 0);
    adapter.visitLocalVariable(name, type.toString(), null, start, end, 1);
    //adapter.visitMaxs(0, 0);//.visitMaxs(sizeOf+1, sizeOf+1);// hansx
    adapter.visitEnd();

    adapter.endMethod();

}

From source file:lucee.transformer.bytecode.util.JavaProxyFactory.java

License:Open Source License

public static Object createProxy(PageContext pc, Component cfc, Class extendz, Class... interfaces)
        throws PageException, IOException {
    PageContextImpl pci = (PageContextImpl) pc;
    if (extendz == null)
        extendz = Object.class;
    if (interfaces == null)
        interfaces = new Class[0];
    else {/*from   w ww . j a v  a2  s  .  co m*/
        for (int i = 0; i < interfaces.length; i++) {
            if (!interfaces[i].isInterface())
                throw new IOException(
                        "definition [" + interfaces[i].getName() + "] is a class and not a interface");
        }
    }

    Type typeExtends = Type.getType(extendz);
    Type[] typeInterfaces = ASMUtil.toTypes(interfaces);
    String[] strInterfaces = new String[typeInterfaces.length];
    for (int i = 0; i < strInterfaces.length; i++) {
        strInterfaces[i] = typeInterfaces[i].getInternalName();
    }

    String className = createClassName(extendz, interfaces);
    //Mapping mapping = cfc.getPageSource().getMapping();

    // get ClassLoader
    PhysicalClassLoader cl = null;
    try {
        cl = (PhysicalClassLoader) pci.getRPCClassLoader(false);// mapping.getConfig().getRPCClassLoader(false)
    } catch (IOException e) {
        throw Caster.toPageException(e);
    }
    Resource classFile = cl.getDirectory().getRealResource(className.concat(".class"));

    // check if already exists, if yes return
    if (classFile.exists()) {
        //Object obj=newInstance(cl,className,cfc);
        // if(obj!=null) return obj;
    }

    /*
    String classNameOriginal=component.getPageSource().getFullClassName();
     String realOriginal=classNameOriginal.replace('.','/');
    Resource classFileOriginal = mapping.getClassRootDirectory().getRealResource(realOriginal.concat(".class"));
    */
    ClassWriter cw = ASMUtil.getClassWriter();

    cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC, className, null, typeExtends.getInternalName(), strInterfaces);
    //BytecodeContext statConstr = null;//new BytecodeContext(null,null,null,cw,real,ga,Page.STATIC_CONSTRUCTOR);
    //BytecodeContext constr = null;//new BytecodeContext(null,null,null,cw,real,ga,Page.CONSTRUCTOR);

    // field Component
    FieldVisitor _fv = cw.visitField(Opcodes.ACC_PRIVATE, "cfc", COMPONENT_NAME, null, null);
    _fv.visitEnd();
    _fv = cw.visitField(Opcodes.ACC_PRIVATE, "config", CONFIG_WEB_NAME, null, null);
    _fv.visitEnd();

    // Constructor
    GeneratorAdapter adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC, CONSTRUCTOR, null, null, cw);
    Label begin = new Label();
    adapter.visitLabel(begin);
    adapter.loadThis();
    adapter.invokeConstructor(Types.OBJECT, SUPER_CONSTRUCTOR);

    //adapter.putField(JAVA_PROXY, arg1, arg2)

    adapter.visitVarInsn(Opcodes.ALOAD, 0);
    adapter.visitVarInsn(Opcodes.ALOAD, 1);
    adapter.visitFieldInsn(Opcodes.PUTFIELD, className, "config", CONFIG_WEB_NAME);

    adapter.visitVarInsn(Opcodes.ALOAD, 0);
    adapter.visitVarInsn(Opcodes.ALOAD, 2);
    adapter.visitFieldInsn(Opcodes.PUTFIELD, className, "cfc", COMPONENT_NAME);

    adapter.visitInsn(Opcodes.RETURN);
    Label end = new Label();
    adapter.visitLabel(end);
    adapter.visitLocalVariable("config", CONFIG_WEB_NAME, null, begin, end, 1);
    adapter.visitLocalVariable("cfc", COMPONENT_NAME, null, begin, end, 2);

    //adapter.returnValue();
    adapter.endMethod();

    // create methods
    Set<Class> cDone = new HashSet<Class>();
    Map<String, Class> mDone = new HashMap<String, Class>();
    for (int i = 0; i < interfaces.length; i++) {
        _createProxy(cw, cDone, mDone, cfc, interfaces[i], className);
    }
    cw.visitEnd();

    // create class file
    byte[] barr = cw.toByteArray();

    try {
        ResourceUtil.touch(classFile);
        IOUtil.copy(new ByteArrayInputStream(barr), classFile, true);

        cl = (PhysicalClassLoader) pci.getRPCClassLoader(true);
        Class<?> clazz = cl.loadClass(className, barr);
        return newInstance(clazz, pc.getConfig(), cfc);
    } catch (Throwable t) {
        throw Caster.toPageException(t);
    }
}

From source file:net.enilink.composition.asm.CompositeClassNode.java

License:Open Source License

@SuppressWarnings("unchecked")
public FieldNode addStaticMethodField(Type declaringClass, String name, Type returnType, Type[] paramTypes) {
    List<?> key = Arrays.asList(declaringClass, name, Arrays.asList(paramTypes));
    FieldNode field = methodFields.get(key);
    if (field == null) {
        field = new FieldNode(Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC, "_$method" + ++varCount,
                Type.getDescriptor(Method.class), null, null);
        fields.add(field);/*from   w w  w .j  a va  2s .co m*/
        methodFields.put(key, field);

        MethodNodeGenerator gen = getClassInitGen();

        gen.push(declaringClass);
        gen.push(name);
        gen.loadArray(paramTypes);

        gen.invokeVirtual(Type.getType(Class.class),
                new org.objectweb.asm.commons.Method("getDeclaredMethod", Type.getType(Method.class),
                        new Type[] { Type.getType(String.class), Type.getType(Class[].class) }));

        gen.putStatic(getType(), field.name, Type.getType(field.desc));
    }
    return field;
}

From source file:net.enilink.composition.asm.ExtendedClassNode.java

License:Open Source License

@SuppressWarnings("unchecked")
public void addInjectorField() {
    FieldNode injectorField = new FieldNode(Opcodes.ACC_PRIVATE, INJECTOR_FIELD,
            Type.getDescriptor(Injector.class), null, null);
    injectorField.visitAnnotation(Type.getDescriptor(Inject.class), true);
    fields.add(injectorField);/*from  w  ww. j av a 2s . c  o m*/
}

From source file:net.enilink.composition.cache.behaviours.CacheBehaviourMethodProcessor.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*from w  ww. j av a2s.  c o  m*/
public void initialize(BehaviourClassNode classNode) throws Exception {
    FieldNode cacheField = new FieldNode(Opcodes.ACC_PRIVATE, "cache", Type.getDescriptor(IPropertyCache.class),
            null, null);
    cacheField.visitAnnotation(Type.getDescriptor(Inject.class), true);
    classNode.fields.add(cacheField);
}