Search in sources :

Example 46 with FieldVisitor

use of org.objectweb.asm.FieldVisitor in project Lucee by lucee.

the class SourceLastModifiedClassAdapter method execute.

/**
 * convert the Page Object to java bytecode
 * @param className name of the genrated class (only necessary when Page object has no PageSource reference)
 * @return
 * @throws TransformerException
 */
public byte[] execute(String className) throws TransformerException {
    // not exists in any case, so every usage must have a plan b for not existence
    PageSource optionalPS = sourceCode instanceof PageSourceCode ? ((PageSourceCode) sourceCode).getPageSource() : null;
    List<LitString> keys = new ArrayList<LitString>();
    ClassWriter cw = ASMUtil.getClassWriter();
    ArrayList<String> imports = new ArrayList<String>();
    getImports(imports, this);
    // look for component if necessary
    TagCIObject comp = getTagCFObject(null);
    // in case we have a sub component
    if (className == null) {
        if (optionalPS == null)
            throw new IllegalArgumentException("when Page object has no PageSource, a className is necessary");
        className = optionalPS.getClassName();
    }
    if (comp != null)
        className = createSubClass(className, comp.getName(), sourceCode.getDialect());
    className = className.replace('.', '/');
    this.className = className;
    // parent
    // "lucee/runtime/Page";
    String parent = PageImpl.class.getName();
    if (// "lucee/runtime/ComponentPage";
    isComponent(comp))
        // "lucee/runtime/ComponentPage";
        parent = ComponentPageImpl.class.getName();
    else // "lucee/runtime/InterfacePage";
    if (isInterface(comp))
        parent = InterfacePageImpl.class.getName();
    parent = parent.replace('.', '/');
    cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, className, null, parent, null);
    if (optionalPS != null) {
        // we use full path  when FD is enabled
        String path = config.allowRequestTimeout() ? optionalPS.getRealpathWithVirtual() : optionalPS.getPhyscalFile().getAbsolutePath();
        // when adding more use ; as delimiter
        cw.visitSource(path, null);
    // cw.visitSource(optionalPS.getPhyscalFile().getAbsolutePath(),
    // "rel:"+optionalPS.getRealpathWithVirtual()); // when adding more use ; as delimiter
    } else {
    // cw.visitSource("","rel:");
    }
    // static constructor
    // GeneratorAdapter statConstrAdapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC,STATIC_CONSTRUCTOR,null,null,cw);
    // StaticConstrBytecodeContext 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);
    ConstrBytecodeContext constr = new ConstrBytecodeContext(optionalPS, this, keys, cw, className, constrAdapter, CONSTRUCTOR_PS, writeLog(), suppressWSbeforeArg, output, returnValue);
    constrAdapter.loadThis();
    Type t;
    if (isComponent(comp)) {
        t = Types.COMPONENT_PAGE_IMPL;
        // extends
        // Attribute attr = comp.getAttribute("extends");
        // if(attr!=null) ExpressionUtil.writeOutSilent(attr.getValue(),constr, Expression.MODE_REF);
        // else constrAdapter.push("");
        constrAdapter.invokeConstructor(t, CONSTRUCTOR);
    } else if (isInterface(comp)) {
        t = Types.INTERFACE_PAGE_IMPL;
        constrAdapter.invokeConstructor(t, CONSTRUCTOR);
    } else {
        t = Types.PAGE_IMPL;
        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, className, "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, className, "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();
    // getSourceLength
    adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, LENGTH, null, null, cw);
    adapter.push(length);
    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();
    // getHash
    adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, HASH, null, null, cw);
    adapter.push(hash);
    adapter.returnValue();
    adapter.endMethod();
    if (comp != null) {
        writeOutStaticConstructor(constr, keys, cw, comp, className);
    }
    // newInstance/initComponent/call
    if (isComponent()) {
        writeOutNewComponent(constr, keys, cw, comp, className);
        writeOutInitComponent(constr, keys, cw, comp, className);
    } else if (isInterface()) {
        writeOutNewInterface(constr, keys, cw, comp, className);
        writeOutInitInterface(constr, keys, cw, comp, className);
    } else {
        writeOutCall(constr, keys, cw, className);
    }
    // write UDFProperties to constructor
    // writeUDFProperties(bc,funcs,pageType);
    // 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(optionalPS, constr, this, keys, cw, className, adapter, UDF_CALL, writeLog(), suppressWSbeforeArg, output, returnValue);
        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();
    } else // more than 10 functions
    {
        adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, UDF_CALL, null, new Type[] { Types.THROWABLE }, cw);
        BytecodeContext bc = new BytecodeContext(optionalPS, constr, this, keys, cw, className, adapter, UDF_CALL, writeLog(), suppressWSbeforeArg, output, returnValue);
        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, className, createFunctionName(++count), "(Llucee/runtime/PageContext;Llucee/runtime/type/UDF;I)Ljava/lang/Object;");
            // adapter.returnValue();
            adapter.visitInsn(Opcodes.ARETURN);
            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(optionalPS, constr, this, keys, cw, className, adapter, innerCall, writeLog(), suppressWSbeforeArg, output, returnValue), 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(optionalPS, constr, this, keys, cw, className, adapter, THREAD_CALL, writeLog(), suppressWSbeforeArg, output, returnValue), threads, 0, threads.length);
        // adapter.visitInsn(Opcodes.ACONST_NULL);
        adapter.returnValue();
        adapter.endMethod();
    }
    // 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(optionalPS, constr, this, keys, cw, className, adapter, UDF_DEFAULT_VALUE, writeLog(), suppressWSbeforeArg, output, returnValue), 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(optionalPS, constr, this, keys, cw, className, adapter, UDF_DEFAULT_VALUE, writeLog(), suppressWSbeforeArg, output, returnValue);
        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, className, "udfDefaultValue" + (++count), "(Llucee/runtime/PageContext;IILjava/lang/Object;)Ljava/lang/Object;");
            // adapter.returnValue();
            adapter.visitInsn(Opcodes.ARETURN);
            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(optionalPS, constr, this, keys, cw, className, adapter, innerDefaultValue, writeLog(), suppressWSbeforeArg, output, returnValue), functions, i, i + 10 > functions.length ? functions.length : i + 10);
            adapter.loadArg(DEFAULT_VALUE);
            // adapter.visitInsn(Opcodes.ACONST_NULL);
            adapter.returnValue();
            adapter.endMethod();
        }
    }
    // CONSTRUCTOR
    List<Data> udfProperties = constr.getUDFProperties();
    Iterator<Data> it = udfProperties.iterator();
    String udfpropsClassName = Types.UDF_PROPERTIES_ARRAY.toString();
    // new UDFProperties Array
    constrAdapter.visitVarInsn(Opcodes.ALOAD, 0);
    constrAdapter.push(udfProperties.size());
    constrAdapter.newArray(Types.UDF_PROPERTIES);
    constrAdapter.visitFieldInsn(Opcodes.PUTFIELD, getClassName(), "udfs", udfpropsClassName);
    // set item
    Data data;
    while (it.hasNext()) {
        data = it.next();
        constrAdapter.visitVarInsn(Opcodes.ALOAD, 0);
        constrAdapter.visitFieldInsn(Opcodes.GETFIELD, constr.getClassName(), "udfs", Types.UDF_PROPERTIES_ARRAY.toString());
        constrAdapter.push(data.arrayIndex);
        data.function.createUDFProperties(constr, data.valueIndex, data.type);
        constrAdapter.visitInsn(Opcodes.AASTORE);
    }
    // setPageSource(pageSource);
    constrAdapter.visitVarInsn(Opcodes.ALOAD, 0);
    constrAdapter.visitVarInsn(Opcodes.ALOAD, 1);
    constrAdapter.invokeVirtual(t, SET_PAGE_SOURCE);
    constrAdapter.returnValue();
    constrAdapter.endMethod();
    // INIT KEYS
    {
        GeneratorAdapter aInit = new GeneratorAdapter(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL, INIT_KEYS, null, null, cw);
        BytecodeContext bcInit = new BytecodeContext(optionalPS, constr, this, keys, cw, className, aInit, INIT_KEYS, writeLog(), suppressWSbeforeArg, output, returnValue);
        registerFields(bcInit, keys);
        aInit.returnValue();
        aInit.endMethod();
    }
    // set field subs
    FieldVisitor fv = cw.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL, "subs", "[Llucee/runtime/CIPage;", null, null);
    fv.visitEnd();
    // create sub components/interfaces
    if (comp != null && comp.isMain()) {
        List<TagCIObject> subs = getSubs(null);
        if (!ArrayUtil.isEmpty(subs)) {
            Iterator<TagCIObject> _it = subs.iterator();
            TagCIObject tc;
            while (_it.hasNext()) {
                tc = _it.next();
                tc.writeOut(this);
            }
            writeGetSubPages(cw, className, subs, sourceCode.getDialect());
        }
    }
    return cw.toByteArray();
}
Also used : InterfacePageImpl(lucee.runtime.InterfacePageImpl) PageSourceCode(lucee.transformer.util.PageSourceCode) ArrayList(java.util.ArrayList) TagThread(lucee.transformer.bytecode.statement.tag.TagThread) LitString(lucee.transformer.expression.literal.LitString) FieldVisitor(org.objectweb.asm.FieldVisitor) LitString(lucee.transformer.expression.literal.LitString) Function(lucee.transformer.bytecode.statement.udf.Function) IFunction(lucee.transformer.bytecode.statement.IFunction) ConditionVisitor(lucee.transformer.bytecode.visitor.ConditionVisitor) Iterator(java.util.Iterator) DecisionIntVisitor(lucee.transformer.bytecode.visitor.DecisionIntVisitor) TagCIObject(lucee.transformer.bytecode.statement.tag.TagCIObject) Data(lucee.transformer.bytecode.ConstrBytecodeContext.Data) Method(org.objectweb.asm.commons.Method) ClassWriter(org.objectweb.asm.ClassWriter) PageSource(lucee.runtime.PageSource) Type(org.objectweb.asm.Type) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) ArrayVisitor(lucee.transformer.bytecode.visitor.ArrayVisitor)

Example 47 with FieldVisitor

use of org.objectweb.asm.FieldVisitor in project Lucee by lucee.

the class SourceLastModifiedClassAdapter method registerFields.

public static void registerFields(BytecodeContext bc, List<LitString> keys) throws TransformerException {
    // 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();
    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());
}
Also used : LitString(lucee.transformer.expression.literal.LitString) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) FieldVisitor(org.objectweb.asm.FieldVisitor)

Example 48 with FieldVisitor

use of org.objectweb.asm.FieldVisitor in project openj9 by eclipse.

the class ClassBytes method validClassBytes_SamePackage.

public static byte[] validClassBytes_SamePackage() throws Exception {
    ClassWriter cw = new ClassWriter(0);
    FieldVisitor fv;
    MethodVisitor mv;
    cw.visit(V1_2, ACC_PUBLIC + ACC_SUPER, "com/ibm/j9/jsr292/api/SamePackageExample3", null, "java/lang/Object", null);
    cw.visitSource("SamePackageExample3.java", null);
    {
        fv = cw.visitField(ACC_STATIC + ACC_SYNTHETIC, "class$0", "Ljava/lang/Class;", null, null);
        fv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(3, l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
        mv.visitInsn(RETURN);
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "Lcom/ibm/j9/jsr292/api/SamePackageExample3;", null, l0, l1, 0);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "getClassName", "()Ljava/lang/String;", null, null);
        mv.visitCode();
        Label l0 = new Label();
        Label l1 = new Label();
        Label l2 = new Label();
        mv.visitTryCatchBlock(l0, l1, l2, "java/lang/ClassNotFoundException");
        Label l3 = new Label();
        mv.visitLabel(l3);
        mv.visitLineNumber(5, l3);
        mv.visitFieldInsn(GETSTATIC, "com/ibm/j9/jsr292/api/SamePackageExample3", "class$0", "Ljava/lang/Class;");
        mv.visitInsn(DUP);
        Label l4 = new Label();
        mv.visitJumpInsn(IFNONNULL, l4);
        mv.visitInsn(POP);
        mv.visitLabel(l0);
        mv.visitLdcInsn("com.ibm.j9.jsr292.api.SamePackageExample3");
        mv.visitMethodInsn(INVOKESTATIC, "java/lang/Class", "forName", "(Ljava/lang/String;)Ljava/lang/Class;");
        mv.visitLabel(l1);
        mv.visitInsn(DUP);
        mv.visitFieldInsn(PUTSTATIC, "com/ibm/j9/jsr292/api/SamePackageExample3", "class$0", "Ljava/lang/Class;");
        mv.visitJumpInsn(GOTO, l4);
        mv.visitLabel(l2);
        mv.visitTypeInsn(NEW, "java/lang/NoClassDefFoundError");
        mv.visitInsn(DUP_X1);
        mv.visitInsn(SWAP);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Throwable", "getMessage", "()Ljava/lang/String;");
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/NoClassDefFoundError", "<init>", "(Ljava/lang/String;)V");
        mv.visitInsn(ATHROW);
        mv.visitLabel(l4);
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getCanonicalName", "()Ljava/lang/String;");
        mv.visitInsn(ARETURN);
        Label l5 = new Label();
        mv.visitLabel(l5);
        mv.visitLocalVariable("this", "Lcom/ibm/j9/jsr292/api/SamePackageExample3;", null, l3, l5, 0);
        mv.visitMaxs(3, 1);
        mv.visitEnd();
    }
    cw.visitEnd();
    return cw.toByteArray();
}
Also used : Label(org.objectweb.asm.Label) FieldVisitor(org.objectweb.asm.FieldVisitor) ClassWriter(org.objectweb.asm.ClassWriter) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 49 with FieldVisitor

use of org.objectweb.asm.FieldVisitor in project openj9 by eclipse.

the class ClassBytes method invalidClassBytes_SamePackage.

public static byte[] invalidClassBytes_SamePackage() throws Exception {
    ClassWriter cw = new ClassWriter(0);
    FieldVisitor fv;
    MethodVisitor mv;
    cw.visit(V1_2, ACC_PUBLIC + ACC_SUPER, "com/ibm/j9/jsr292/api/SamePackageExample2", null, "java/lang/Object", null);
    cw.visitSource("SamePackageExample2.java", null);
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<Corrupted>", "()V", null, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(3, l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
        mv.visitInsn(RETURN);
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "Lcom/ibm/j9/jsr292/api/SamePackageExample2;", null, l0, l1, 0);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    cw.visitEnd();
    return cw.toByteArray();
}
Also used : Label(org.objectweb.asm.Label) FieldVisitor(org.objectweb.asm.FieldVisitor) ClassWriter(org.objectweb.asm.ClassWriter) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 50 with FieldVisitor

use of org.objectweb.asm.FieldVisitor in project openj9 by eclipse.

the class CreateTestObjectJar method main.

public static void main(String[] args) throws Throwable {
    File testDir = new File(args[0]);
    String testType = args[1];
    String jarFileName = null;
    ClassReader cr = new ClassReader("java/lang/Object");
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    FieldVisitor fv = null;
    cr.accept(cw, 0);
    if (!testDir.isDirectory()) {
        printUsage();
        throw new IllegalArgumentException("Invalid directory: " + testDir.getPath());
    }
    /* Modify the Object class according with the test type */
    if (testType.equalsIgnoreCase("d")) {
        fv = cw.visitField(ACC_PRIVATE, "d", "D", null, null);
        jarFileName = "object_d.jar";
    } else if (testType.equalsIgnoreCase("i")) {
        fv = cw.visitField(ACC_PRIVATE, "i", "I", null, null);
        jarFileName = "object_i.jar";
    } else if (testType.equalsIgnoreCase("ii")) {
        fv = cw.visitField(ACC_PRIVATE, "i", "I", null, null);
        fv = cw.visitField(ACC_PRIVATE, "ii", "I", null, null);
        jarFileName = "object_ii.jar";
    } else if (testType.equalsIgnoreCase("iii")) {
        fv = cw.visitField(ACC_PRIVATE, "i", "I", null, null);
        fv = cw.visitField(ACC_PRIVATE, "ii", "I", null, null);
        fv = cw.visitField(ACC_PRIVATE, "iii", "I", null, null);
        jarFileName = "object_iii.jar";
    } else {
        printUsage();
        throw new IllegalArgumentException("Invalid test type: " + testType);
    }
    if (null != fv) {
        fv.visitEnd();
    }
    /* Write the final jar file with the modified Object class in it */
    if (null != jarFileName) {
        ZipOutputStream outJar = new ZipOutputStream(new FileOutputStream(testDir.getPath() + "/" + jarFileName));
        outJar.putNextEntry(new ZipEntry("java/lang/Object.class"));
        ByteArrayInputStream inJar = new ByteArrayInputStream(cw.toByteArray());
        int len;
        byte[] buf = new byte[bufLen];
        while ((len = inJar.read(buf)) > 0) {
            outJar.write(buf, 0, len);
        }
        outJar.closeEntry();
        outJar.close();
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) ClassReader(org.objectweb.asm.ClassReader) File(java.io.File) FieldVisitor(org.objectweb.asm.FieldVisitor) ClassWriter(org.objectweb.asm.ClassWriter) IllegalArgumentException(java.lang.IllegalArgumentException)

Aggregations

FieldVisitor (org.objectweb.asm.FieldVisitor)90 MethodVisitor (org.objectweb.asm.MethodVisitor)59 Label (org.objectweb.asm.Label)33 ClassWriter (org.objectweb.asm.ClassWriter)28 AnnotationVisitor (org.objectweb.asm.AnnotationVisitor)9 ClassReader (org.objectweb.asm.ClassReader)8 Type (org.objectweb.asm.Type)7 Method (java.lang.reflect.Method)6 ClassVisitor (org.objectweb.asm.ClassVisitor)6 IOException (java.io.IOException)4 GeneratorAdapter (org.objectweb.asm.commons.GeneratorAdapter)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 LitString (lucee.transformer.expression.literal.LitString)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 PrintWriter (java.io.PrintWriter)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 URL (java.net.URL)2