Example usage for org.objectweb.asm Opcodes ACC_PUBLIC

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

Introduction

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

Prototype

int ACC_PUBLIC

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

Click Source Link

Usage

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

License:Open Source License

private void writeOutInitComponent(BytecodeContext statConstr, BytecodeContext constr, List<LitString> keys,
        ClassWriter cw, Tag component) throws BytecodeException {
    final GeneratorAdapter adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL,
            INIT_COMPONENT, null, new Type[] { Types.PAGE_EXCEPTION }, cw);
    BytecodeContext bc = new BytecodeContext(null, statConstr, constr, this, keys, cw, name, adapter,
            INIT_COMPONENT, writeLog(), suppressWSbeforeArg, output);
    Label methodBegin = new Label();
    Label methodEnd = new Label();

    adapter.visitLocalVariable("this", "L" + name + ";", null, methodBegin, methodEnd, 0);
    adapter.visitLabel(methodBegin);/* ww  w  .j av a 2  s.c om*/

    // Scope oldData=null;
    final int oldData = adapter.newLocal(Types.VARIABLES);
    ASMConstants.NULL(adapter);
    adapter.storeLocal(oldData);

    int localBC = adapter.newLocal(Types.BODY_CONTENT);
    ConditionVisitor cv = new ConditionVisitor();
    cv.visitBefore();
    cv.visitWhenBeforeExpr();
    adapter.loadArg(1);
    adapter.invokeVirtual(Types.COMPONENT_IMPL, GET_OUTPUT);
    cv.visitWhenAfterExprBeforeBody(bc);
    ASMConstants.NULL(adapter);
    cv.visitWhenAfterBody(bc);

    cv.visitOtherviseBeforeBody();
    adapter.loadArg(0);
    adapter.invokeVirtual(Types.PAGE_CONTEXT, PUSH_BODY);
    cv.visitOtherviseAfterBody();
    cv.visitAfter(bc);
    adapter.storeLocal(localBC);

    // c.init(pc,this);
    adapter.loadArg(1);
    adapter.loadArg(0);
    adapter.visitVarInsn(Opcodes.ALOAD, 0);
    adapter.invokeVirtual(Types.COMPONENT_IMPL, INIT);

    //int oldCheckArgs=   pc.undefinedScope().setMode(Undefined.MODE_NO_LOCAL_AND_ARGUMENTS);
    final int oldCheckArgs = adapter.newLocal(Types.INT_VALUE);
    adapter.loadArg(0);
    adapter.invokeVirtual(Types.PAGE_CONTEXT, UNDEFINED_SCOPE);
    adapter.push(Undefined.MODE_NO_LOCAL_AND_ARGUMENTS);
    adapter.invokeInterface(Types.UNDEFINED, SET_MODE);
    adapter.storeLocal(oldCheckArgs);

    TryCatchFinallyVisitor tcf = new TryCatchFinallyVisitor(new OnFinally() {

        public void _writeOut(BytecodeContext bc) {

            // undefined.setMode(oldMode);
            adapter.loadArg(0);
            adapter.invokeVirtual(Types.PAGE_CONTEXT, UNDEFINED_SCOPE);
            adapter.loadLocal(oldCheckArgs, Types.INT_VALUE);
            adapter.invokeInterface(Types.UNDEFINED, SET_MODE);
            adapter.pop();

            // c.afterCall(pc,_oldData);
            adapter.loadArg(1);
            adapter.loadArg(0);
            adapter.loadLocal(oldData);
            adapter.invokeVirtual(Types.COMPONENT_IMPL, AFTER_CALL);

        }
    }, null);
    tcf.visitTryBegin(bc);
    // oldData=c.beforeCall(pc);
    adapter.loadArg(1);
    adapter.loadArg(0);
    adapter.invokeVirtual(Types.COMPONENT_IMPL, BEFORE_CALL);
    adapter.storeLocal(oldData);
    ExpressionUtil.visitLine(bc, component.getStart());
    writeOutCallBody(bc, component.getBody(), IFunction.PAGE_TYPE_COMPONENT);
    ExpressionUtil.visitLine(bc, component.getEnd());
    int t = tcf.visitTryEndCatchBeging(bc);
    // BodyContentUtil.flushAndPop(pc,bc);
    adapter.loadArg(0);
    adapter.loadLocal(localBC);
    adapter.invokeStatic(Types.BODY_CONTENT_UTIL, FLUSH_AND_POP);

    // throw Caster.toPageException(t);
    adapter.loadLocal(t);
    adapter.invokeStatic(Types.CASTER, TO_PAGE_EXCEPTION);
    adapter.throwException();
    tcf.visitCatchEnd(bc);

    adapter.loadArg(0);
    adapter.loadLocal(localBC);
    adapter.invokeStatic(Types.BODY_CONTENT_UTIL, CLEAR_AND_POP);

    adapter.returnValue();
    adapter.visitLabel(methodEnd);

    adapter.endMethod();

}

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

License:Open Source License

private void writeOutInitInterface(BytecodeContext statConstr, BytecodeContext constr, List<LitString> keys,
        ClassWriter cw, Tag interf) throws BytecodeException {
    GeneratorAdapter adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, INIT_INTERFACE,
            null, new Type[] { Types.PAGE_EXCEPTION }, cw);
    BytecodeContext bc = new BytecodeContext(null, statConstr, constr, this, keys, cw, name, adapter,
            INIT_INTERFACE, writeLog(), suppressWSbeforeArg, output);
    Label methodBegin = new Label();
    Label methodEnd = new Label();

    adapter.visitLocalVariable("this", "L" + name + ";", null, methodBegin, methodEnd, 0);
    adapter.visitLabel(methodBegin);/*from  ww  w  .j  a  v a2s  .co m*/

    ExpressionUtil.visitLine(bc, interf.getStart());
    writeOutCallBody(bc, interf.getBody(), IFunction.PAGE_TYPE_INTERFACE);
    ExpressionUtil.visitLine(bc, interf.getEnd());

    adapter.returnValue();
    adapter.visitLabel(methodEnd);

    adapter.endMethod();

}

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

License:Open Source License

private void writeOutNewComponent(BytecodeContext statConstr, BytecodeContext constr, List<LitString> keys,
        ClassWriter cw, Tag component) throws BytecodeException {

    GeneratorAdapter adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL,
            NEW_COMPONENT_IMPL_INSTANCE, null, new Type[] { Types.PAGE_EXCEPTION }, cw);
    BytecodeContext bc = new BytecodeContext(null, statConstr, constr, this, keys, cw, name, adapter,
            NEW_COMPONENT_IMPL_INSTANCE, writeLog(), suppressWSbeforeArg, output);
    Label methodBegin = new Label();
    Label methodEnd = new Label();

    adapter.visitLocalVariable("this", "L" + name + ";", null, methodBegin, methodEnd, 0);
    ExpressionUtil.visitLine(bc, component.getStart());
    adapter.visitLabel(methodBegin);/* www. j  ava 2 s .  co  m*/

    int comp = adapter.newLocal(Types.COMPONENT_IMPL);
    adapter.newInstance(Types.COMPONENT_IMPL);
    adapter.dup();

    Attribute attr;
    // ComponentPage
    adapter.visitVarInsn(Opcodes.ALOAD, 0);
    adapter.checkCast(Types.COMPONENT_PAGE);

    // !!! also check CFMLScriptTransformer.addMetaData if you do any change here !!!

    // Output
    attr = component.removeAttribute("output");
    if (attr != null) {
        ExpressionUtil.writeOutSilent(attr.getValue(), bc, Expression.MODE_REF);
    } else
        ASMConstants.NULL(adapter);

    // synchronized 
    attr = component.removeAttribute("synchronized");
    if (attr != null)
        ExpressionUtil.writeOutSilent(attr.getValue(), bc, Expression.MODE_VALUE);
    else
        adapter.push(false);

    // extends
    attr = component.removeAttribute("extends");
    if (attr != null)
        ExpressionUtil.writeOutSilent(attr.getValue(), bc, Expression.MODE_REF);
    else
        adapter.push("");

    // implements
    attr = component.removeAttribute("implements");
    if (attr != null)
        ExpressionUtil.writeOutSilent(attr.getValue(), bc, Expression.MODE_REF);
    else
        adapter.push("");

    // hint
    attr = component.removeAttribute("hint");
    if (attr != null) {
        Expression value = attr.getValue();
        if (!(value instanceof Literal)) {
            value = LitString.toExprString("[runtime expression]");
        }
        ExpressionUtil.writeOutSilent(value, bc, Expression.MODE_REF);
    } else
        adapter.push("");

    // dspName
    attr = component.removeAttribute("displayname");
    if (attr == null)
        attr = component.getAttribute("display");
    if (attr != null)
        ExpressionUtil.writeOutSilent(attr.getValue(), bc, Expression.MODE_REF);
    else
        adapter.push("");

    // callpath
    adapter.visitVarInsn(Opcodes.ALOAD, 2);
    // relpath
    adapter.visitVarInsn(Opcodes.ILOAD, 3);

    // style
    attr = component.removeAttribute("style");
    if (attr != null)
        ExpressionUtil.writeOutSilent(attr.getValue(), bc, Expression.MODE_REF);
    else
        adapter.push("");

    // persistent
    attr = component.removeAttribute("persistent");
    boolean persistent = false;
    if (attr != null) {
        persistent = ASMUtil.toBoolean(attr, component.getStart()).booleanValue();
    }

    // persistent
    attr = component.removeAttribute("accessors");
    boolean accessors = false;
    if (attr != null) {
        accessors = ASMUtil.toBoolean(attr, component.getStart()).booleanValue();
    }

    adapter.push(persistent);
    adapter.push(accessors);
    //ExpressionUtil.writeOutSilent(attr.getValue(),bc, Expression.MODE_VALUE);

    //adapter.visitVarInsn(Opcodes.ALOAD, 4);
    createMetaDataStruct(bc, component.getAttributes(), component.getMetaData());

    adapter.invokeConstructor(Types.COMPONENT_IMPL, CONSTR_COMPONENT_IMPL);

    adapter.storeLocal(comp);

    //Component Impl(ComponentPage componentPage,boolean output, String extend, String hint, String dspName)

    // initComponent(pc,c);
    adapter.visitVarInsn(Opcodes.ALOAD, 0);
    adapter.loadArg(0);
    adapter.loadLocal(comp);
    adapter.invokeVirtual(Types.COMPONENT_PAGE, INIT_COMPONENT);

    adapter.visitLabel(methodEnd);

    // return component;
    adapter.loadLocal(comp);

    adapter.returnValue();
    //ExpressionUtil.visitLine(adapter, component.getEndLine());
    adapter.endMethod();

}

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

License:Open Source License

private void writeOutNewInterface(BytecodeContext statConstr, BytecodeContext constr, List<LitString> keys,
        ClassWriter cw, Tag interf) throws BytecodeException {
    GeneratorAdapter adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL,
            NEW_INTERFACE_IMPL_INSTANCE, null, new Type[] { Types.PAGE_EXCEPTION }, cw);
    BytecodeContext bc = new BytecodeContext(null, statConstr, constr, this, keys, cw, name, adapter,
            NEW_INTERFACE_IMPL_INSTANCE, writeLog(), suppressWSbeforeArg, output);
    Label methodBegin = new Label();
    Label methodEnd = new Label();

    adapter.visitLocalVariable("this", "L" + name + ";", null, methodBegin, methodEnd, 0);
    ExpressionUtil.visitLine(bc, interf.getStart());
    adapter.visitLabel(methodBegin);/*from  ww w.  j  a  va2  s  .  c om*/

    //ExpressionUtil.visitLine(adapter, interf.getStartLine());

    int comp = adapter.newLocal(Types.INTERFACE_IMPL);

    adapter.newInstance(Types.INTERFACE_IMPL);
    adapter.dup();

    Attribute attr;
    // Interface Page
    adapter.visitVarInsn(Opcodes.ALOAD, 0);
    adapter.checkCast(Types.INTERFACE_PAGE);

    // extened
    attr = interf.removeAttribute("extends");
    if (attr != null)
        ExpressionUtil.writeOutSilent(attr.getValue(), bc, Expression.MODE_REF);
    else
        adapter.push("");

    // hint
    attr = interf.removeAttribute("hint");
    if (attr != null)
        ExpressionUtil.writeOutSilent(attr.getValue(), bc, Expression.MODE_REF);
    else
        adapter.push("");

    // dspName
    attr = interf.removeAttribute("displayname");
    if (attr == null)
        attr = interf.getAttribute("display");
    if (attr != null)
        ExpressionUtil.writeOutSilent(attr.getValue(), bc, Expression.MODE_REF);
    else
        adapter.push("");

    // callpath
    adapter.visitVarInsn(Opcodes.ALOAD, 1);
    // relpath
    adapter.visitVarInsn(Opcodes.ILOAD, 2);

    // interface udfs
    adapter.visitVarInsn(Opcodes.ALOAD, 3);

    createMetaDataStruct(bc, interf.getAttributes(), interf.getMetaData());

    adapter.invokeConstructor(Types.INTERFACE_IMPL, CONSTR_INTERFACE_IMPL);

    adapter.storeLocal(comp);

    // initInterface(pc,c);
    adapter.visitVarInsn(Opcodes.ALOAD, 0);
    //adapter.loadArg(0);
    adapter.loadLocal(comp);
    adapter.invokeVirtual(Types.INTERFACE_PAGE, INIT_INTERFACE);

    adapter.visitLabel(methodEnd);

    // return interface;
    adapter.loadLocal(comp);

    adapter.returnValue();
    //ExpressionUtil.visitLine(adapter, interf.getEndLine());
    adapter.endMethod();

}

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

License:Open Source License

private void writeOutCall(BytecodeContext statConstr, BytecodeContext constr, List<LitString> keys,
        ClassWriter cw) throws BytecodeException {
    //GeneratorAdapter adapter = bc.getAdapter();
    GeneratorAdapter adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, CALL, null,
            new Type[] { Types.THROWABLE }, cw);
    Label methodBegin = new Label();
    Label methodEnd = new Label();

    adapter.visitLocalVariable("this", "L" + name + ";", null, methodBegin, methodEnd, 0);
    adapter.visitLabel(methodBegin);/* w w w  .java  2s.  co m*/

    writeOutCallBody(new BytecodeContext(null, statConstr, constr, this, keys, cw, name, adapter, CALL,
            writeLog(), suppressWSbeforeArg, output), this, IFunction.PAGE_TYPE_REGULAR);

    adapter.visitLabel(methodEnd);
    adapter.returnValue();
    adapter.endMethod();
}

From source file:lucee.transformer.bytecode.reflection.ASMProxyFactory.java

License:Open Source License

private static byte[] _createMethod(Type type, Class clazz, Method method, Resource classRoot, String className)
        throws IOException {
    Class<?> rtn = method.getReturnType();
    Type rtnType = Type.getType(rtn);

    className = className.replace('.', File.separatorChar);
    ClassWriter cw = ASMUtil.getClassWriter();
    cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC, className, null, ASM_METHOD.getInternalName(), null);

    // CONSTRUCTOR

    GeneratorAdapter adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC, CONSTRUCTOR, null, null, cw);

    Label begin = new Label();
    adapter.visitLabel(begin);// w  ww  .  jav a 2s . co m
    adapter.loadThis();

    adapter.visitVarInsn(Opcodes.ALOAD, 1);
    adapter.visitVarInsn(Opcodes.ALOAD, 2);

    adapter.invokeConstructor(ASM_METHOD, CONSTRUCTOR);
    adapter.visitInsn(Opcodes.RETURN);

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

    adapter.endMethod();

    /*
             
        GeneratorAdapter adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC,CONSTRUCTOR,null,null,cw);
                
        Label begin = new Label();
         adapter.visitLabel(begin);
       adapter.loadThis();
                 
        // clazz
         adapter.visitVarInsn(Opcodes.ALOAD, 2);
                 
         // parameterTypes 
         Class<?>[] params = method.getParameterTypes();
         Type[] paramTypes = new Type[params.length];
        ArrayVisitor av=new ArrayVisitor();
        av.visitBegin(adapter, Types.CLASS, params.length);
        for(int i=0;i<params.length;i++){
           paramTypes[i]=Type.getType(params[i]);
           av.visitBeginItem(adapter, i);
     loadClass(adapter,params[i]);
           av.visitEndItem(adapter);
        }
        av.visitEnd();
                
       adapter.invokeConstructor(ASM_METHOD, ASM_METHOD_CONSTRUCTOR);
       adapter.visitInsn(Opcodes.RETURN);
               
       Label end = new Label();
       adapter.visitLabel(end);
               
       adapter.endMethod();
     */

    // METHOD getName();
    adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC, GET_NAME, null, null, cw);
    adapter.push(method.getName());
    adapter.visitInsn(Opcodes.ARETURN);
    adapter.endMethod();

    // METHOD getModifiers();
    adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC, GET_MODIFIERS, null, null, cw);
    adapter.push(method.getModifiers());
    adapter.visitInsn(Opcodes.IRETURN);
    adapter.endMethod();

    // METHOD getReturnType();
    adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC, GET_RETURN_TYPE_AS_STRING, null, null, cw);

    adapter.push(method.getReturnType().getName());
    adapter.visitInsn(Opcodes.ARETURN);

    adapter.endMethod();

    // METHOD INVOKE
    boolean isStatic = Modifier.isStatic(method.getModifiers());
    adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC, INVOKE, null, null, cw);
    Label start = adapter.newLabel();
    adapter.visitLabel(start);

    // load Object
    if (!isStatic) {
        adapter.visitVarInsn(Opcodes.ALOAD, 1);
        adapter.checkCast(type);
    }

    // load params
    Class<?>[] params = method.getParameterTypes();

    Type[] paramTypes = new Type[params.length];
    for (int i = 0; i < params.length; i++) {
        paramTypes[i] = Type.getType(params[i]);
    }

    for (int i = 0; i < params.length; i++) {
        adapter.visitVarInsn(Opcodes.ALOAD, 2);
        adapter.push(i);
        //adapter.visitInsn(Opcodes.ICONST_0);
        adapter.visitInsn(Opcodes.AALOAD);

        adapter.checkCast(toReferenceType(params[i], paramTypes[i]));

        // cast
        if (params[i] == boolean.class)
            adapter.invokeVirtual(Types.BOOLEAN, BOOL_VALUE);
        else if (params[i] == short.class)
            adapter.invokeVirtual(Types.SHORT, SHORT_VALUE);
        else if (params[i] == int.class)
            adapter.invokeVirtual(Types.INTEGER, INT_VALUE);
        else if (params[i] == float.class)
            adapter.invokeVirtual(Types.FLOAT, FLT_VALUE);
        else if (params[i] == long.class)
            adapter.invokeVirtual(Types.LONG, LONG_VALUE);
        else if (params[i] == double.class)
            adapter.invokeVirtual(Types.DOUBLE, DBL_VALUE);
        else if (params[i] == char.class)
            adapter.invokeVirtual(Types.CHARACTER, CHR_VALUE);
        else if (params[i] == byte.class)
            adapter.invokeVirtual(Types.BYTE, BYT_VALUE);
        //else adapter.checkCast(paramTypes[i]);

    }

    // call method
    final org.objectweb.asm.commons.Method m = new org.objectweb.asm.commons.Method(method.getName(), rtnType,
            paramTypes);
    if (isStatic)
        adapter.invokeStatic(type, m);
    else
        adapter.invokeVirtual(type, m);

    // return
    if (rtn == void.class)
        ASMConstants.NULL(adapter);

    // cast result to object
    if (rtn == boolean.class)
        adapter.invokeStatic(Types.BOOLEAN, BOOL_VALUE_OF);
    else if (rtn == short.class)
        adapter.invokeStatic(Types.SHORT, SHORT_VALUE_OF);
    else if (rtn == int.class)
        adapter.invokeStatic(Types.INTEGER, INT_VALUE_OF);
    else if (rtn == long.class)
        adapter.invokeStatic(Types.LONG, LONG_VALUE_OF);
    else if (rtn == float.class)
        adapter.invokeStatic(Types.FLOAT, FLT_VALUE_OF);
    else if (rtn == double.class)
        adapter.invokeStatic(Types.DOUBLE, DBL_VALUE_OF);
    else if (rtn == char.class)
        adapter.invokeStatic(Types.CHARACTER, CHR_VALUE_OF);
    else if (rtn == byte.class)
        adapter.invokeStatic(Types.BYTE, BYT_VALUE_OF);

    adapter.visitInsn(Opcodes.ARETURN);

    adapter.endMethod();

    if (classRoot != null) {
        Resource classFile = classRoot.getRealResource(className + ".class");
        return store(cw.toByteArray(), classFile);
    }
    return cw.toByteArray();
}

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

License:Open Source License

public static byte[] createPojo(String className, ASMProperty[] properties, Class parent, Class[] interfaces,
        String srcName) throws PageException {
    className = className.replace('.', '/');
    className = className.replace('\\', '/');
    className = ListUtil.trim(className, "/");
    String[] inter = null;/* w w w.j a  v a 2  s  .c  o m*/
    if (interfaces != null) {
        inter = new String[interfaces.length];
        for (int i = 0; i < inter.length; i++) {
            inter[i] = interfaces[i].getName().replace('.', '/');
        }
    }
    // CREATE CLASS   
    ClassWriter cw = ASMUtil.getClassWriter();
    cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC, className, null, parent.getName().replace('.', '/'), inter);
    String md5;
    try {
        md5 = createMD5(properties);
    } catch (Throwable t) {
        md5 = "";
        t.printStackTrace();
    }

    FieldVisitor fv = cw.visitField(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_STATIC, "_md5_",
            "Ljava/lang/String;", null, md5);
    fv.visitEnd();

    // Constructor
    GeneratorAdapter adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC, CONSTRUCTOR_OBJECT, null, null, cw);
    adapter.loadThis();
    adapter.invokeConstructor(toType(parent, true), CONSTRUCTOR_OBJECT);
    adapter.returnValue();
    adapter.endMethod();

    // properties
    for (int i = 0; i < properties.length; i++) {
        createProperty(cw, className, properties[i]);
    }

    // complexType src
    if (!StringUtil.isEmpty(srcName)) {
        GeneratorAdapter _adapter = new GeneratorAdapter(
                Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_STATIC, _SRC_NAME, null, null, cw);
        _adapter.push(srcName);
        _adapter.returnValue();
        _adapter.endMethod();
    }

    cw.visitEnd();
    return cw.toByteArray();
}

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);//from   www  .  j a v  a 2  s  . com
    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 {// w  w  w . j av  a  2  s  .c om
        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:lucee.transformer.bytecode.util.JavaProxyFactory.java

License:Open Source License

private static void _createMethod(ClassWriter cw, Map<String, Class> mDone, Method src, String className)
        throws IOException {
    Class<?>[] classArgs = src.getParameterTypes();
    Class<?> classRtn = src.getReturnType();

    String str = src.getName() + "(" + Reflector.getDspMethods(classArgs) + ")";
    Class rtnClass = mDone.get(str);
    if (rtnClass != null) {
        if (rtnClass != classRtn)
            throw new IOException("there is a conflict with method [" + str
                    + "], this method is declared more than once with different return types.");
        return;//ww w . ja  v  a 2 s.c  o m
    }
    mDone.put(str, classRtn);

    Type[] typeArgs = ASMUtil.toTypes(classArgs);
    Type typeRtn = Type.getType(classRtn);

    org.objectweb.asm.commons.Method method = new org.objectweb.asm.commons.Method(src.getName(), typeRtn,
            typeArgs);
    GeneratorAdapter adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, method, null, null,
            cw);
    //BytecodeContext bc = new BytecodeContext(statConstr,constr,null,null,keys,cw,className,adapter,method,writeLog);
    Label start = adapter.newLabel();
    adapter.visitLabel(start);

    //JavaProxy.call(cfc,"add",new Object[]{arg0})
    // config
    adapter.visitVarInsn(Opcodes.ALOAD, 0);
    adapter.visitFieldInsn(Opcodes.GETFIELD, className, "config", CONFIG_WEB_NAME);

    // cfc
    adapter.visitVarInsn(Opcodes.ALOAD, 0);
    adapter.visitFieldInsn(Opcodes.GETFIELD, className, "cfc", COMPONENT_NAME);

    // name
    adapter.push(src.getName());

    // arguments
    ArrayVisitor av = new ArrayVisitor();
    av.visitBegin(adapter, Types.OBJECT, typeArgs.length);
    for (int y = 0; y < typeArgs.length; y++) {
        av.visitBeginItem(adapter, y);
        adapter.loadArg(y);
        if (classArgs[y] == boolean.class)
            adapter.invokeStatic(JAVA_PROXY, _BOOLEAN);
        else if (classArgs[y] == byte.class)
            adapter.invokeStatic(JAVA_PROXY, _BYTE);
        else if (classArgs[y] == char.class)
            adapter.invokeStatic(JAVA_PROXY, _CHAR);
        else if (classArgs[y] == double.class)
            adapter.invokeStatic(JAVA_PROXY, _DOUBLE);
        else if (classArgs[y] == float.class)
            adapter.invokeStatic(JAVA_PROXY, _FLOAT);
        else if (classArgs[y] == int.class)
            adapter.invokeStatic(JAVA_PROXY, _INT);
        else if (classArgs[y] == long.class)
            adapter.invokeStatic(JAVA_PROXY, _LONG);
        else if (classArgs[y] == short.class)
            adapter.invokeStatic(JAVA_PROXY, _SHORT);
        else {
            adapter.invokeStatic(JAVA_PROXY, _OBJECT);
        }

        av.visitEndItem(adapter);
    }
    av.visitEnd();
    adapter.invokeStatic(JAVA_PROXY, CALL);

    //JavaProxy.to...(...);
    int rtn = Opcodes.IRETURN;
    if (classRtn == boolean.class)
        adapter.invokeStatic(JAVA_PROXY, TO_BOOLEAN);
    else if (classRtn == byte.class)
        adapter.invokeStatic(JAVA_PROXY, TO_BYTE);
    else if (classRtn == char.class)
        adapter.invokeStatic(JAVA_PROXY, TO_CHAR);
    else if (classRtn == double.class) {
        rtn = Opcodes.DRETURN;
        adapter.invokeStatic(JAVA_PROXY, TO_DOUBLE);
    } else if (classRtn == float.class) {
        rtn = Opcodes.FRETURN;
        adapter.invokeStatic(JAVA_PROXY, TO_FLOAT);
    } else if (classRtn == int.class)
        adapter.invokeStatic(JAVA_PROXY, TO_INT);
    else if (classRtn == long.class) {
        rtn = Opcodes.LRETURN;
        adapter.invokeStatic(JAVA_PROXY, TO_LONG);
    } else if (classRtn == short.class)
        adapter.invokeStatic(JAVA_PROXY, TO_SHORT);
    else if (classRtn == void.class) {
        rtn = Opcodes.RETURN;
        adapter.pop();
    } else {
        rtn = Opcodes.ARETURN;
        adapter.checkCast(typeRtn);
    }

    /*mv = cw.visitMethod(ACC_PUBLIC, "add", "(Ljava/lang/Object;)Z", null, null);
    mv.visitCode();
    Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitLineNumber(20, l0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, "Test", "cfc", "Ljava/lang/Object;");
    mv.visitLdcInsn("add");
    mv.visitInsn(ICONST_1);
    mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");
    mv.visitInsn(DUP);
    mv.visitInsn(ICONST_0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitInsn(AASTORE);
    mv.visitMethodInsn(INVOKESTATIC, "JavaProxy", "call", "(Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/Object;");
    mv.visitMethodInsn(INVOKESTATIC, "JavaProxy", "toBoolean", "(Ljava/lang/Object;)Z");
    mv.visitInsn(IRETURN);
    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitLocalVariable("this", "LTest;", null, l0, l1, 0);
    mv.visitLocalVariable("arg0", "Ljava/lang/Object;", null, l0, l1, 1);
    mv.visitMaxs(6, 2);
    mv.visitEnd();*/

    adapter.visitInsn(rtn);
    adapter.endMethod();

}