List of usage examples for org.objectweb.asm.commons Method Method
public Method(final String name, final Type returnType, final Type[] argumentTypes)
From source file:lucee.transformer.bytecode.cast.CastFloat.java
License:Open Source License
/** * @see lucee.transformer.bytecode.expression.Expression#_writeOut(org.objectweb.asm.commons.GeneratorAdapter, int) *//* w w w .j ava 2 s . c o m*/ public Type _writeOut(BytecodeContext bc, int mode) throws BytecodeException { GeneratorAdapter adapter = bc.getAdapter(); if (expr instanceof OpDouble) { ((OpDouble) expr).writeOutDouble(bc, MODE_VALUE); if (mode == MODE_VALUE) adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_FLOAT_VALUE_FROM_DOUBLE); else adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_FLOAT_FROM_DOUBLE); } else if (expr instanceof ExprBoolean) { expr.writeOut(bc, MODE_VALUE); if (mode == MODE_VALUE) adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_FLOAT_VALUE_FROM_BOOLEAN); else adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_FLOAT_FROM_BOOLEAN); } else if (expr instanceof ExprFloat) { expr.writeOut(bc, mode); } else if (expr instanceof ExprDouble) { expr.writeOut(bc, MODE_VALUE); if (mode == MODE_VALUE) adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_FLOAT_VALUE_FROM_DOUBLE); else adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_FLOAT_FROM_DOUBLE); } else if (expr instanceof ExprString) { expr.writeOut(bc, MODE_REF); if (mode == MODE_VALUE) adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_FLOAT_VALUE_FROM_STRING); else adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_FLOAT_FROM_STRING); } else { Type rtn = expr.writeOut(bc, mode); if (mode == MODE_VALUE) { if (!Types.isPrimitiveType(rtn)) { adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_FLOAT_VALUE); } else if (Types.DOUBLE_VALUE.equals(rtn)) { adapter.cast(Types.DOUBLE_VALUE, Types.FLOAT_VALUE); } else if (Types.FLOAT_VALUE.equals(rtn)) { } else if (Types.BOOLEAN_VALUE.equals(rtn)) { adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_FLOAT_VALUE_FROM_BOOLEAN); } else { adapter.invokeStatic(Types.CASTER, new Method("toRef", Types.toRefType(rtn), new Type[] { rtn })); adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_FLOAT_VALUE); } return Types.FLOAT_VALUE; } else if (Types.isPrimitiveType(rtn)) { if (Types.DOUBLE_VALUE.equals(rtn)) { adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_FLOAT_FROM_DOUBLE); } else if (Types.FLOAT_VALUE.equals(rtn)) { adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_FLOAT_FROM_FLOAT); } else if (Types.BOOLEAN_VALUE.equals(rtn)) { adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_FLOAT_FROM_BOOLEAN); } else { adapter.invokeStatic(Types.CASTER, new Method("toRef", Types.toRefType(rtn), new Type[] { rtn })); adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_FLOAT); } return Types.FLOAT; } //else { if (!Types.FLOAT.equals(rtn)) adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_FLOAT); return Types.FLOAT; //} } if (mode == MODE_VALUE) return Types.FLOAT_VALUE; return Types.FLOAT; }
From source file:lucee.transformer.bytecode.cast.CastInt.java
License:Open Source License
/** * @see lucee.transformer.bytecode.expression.Expression#_writeOut(org.objectweb.asm.commons.GeneratorAdapter, int) *///from w ww . j a v a 2s . c o m public Type _writeOut(BytecodeContext bc, int mode) throws BytecodeException { GeneratorAdapter adapter = bc.getAdapter(); if (expr instanceof ExprString) { expr.writeOut(bc, MODE_REF); if (mode == MODE_VALUE) adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_INT_VALUE_FROM_STRING); else adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_INTEGER_FROM_STRING); } else { Type rtn = expr.writeOut(bc, mode); if (mode == MODE_VALUE) { if (!Types.isPrimitiveType(rtn)) { adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_INT_VALUE); } else if (Types.BOOLEAN_VALUE.equals(rtn)) { adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_INT_VALUE_FROM_BOOLEAN); } else if (Types.SHORT_VALUE.equals(rtn)) { // No Cast needed } else if (Types.FLOAT_VALUE.equals(rtn)) { adapter.cast(Types.FLOAT_VALUE, Types.INT_VALUE); } else if (Types.LONG_VALUE.equals(rtn)) { adapter.cast(Types.LONG_VALUE, Types.INT_VALUE); } else if (Types.DOUBLE_VALUE.equals(rtn)) { adapter.cast(Types.DOUBLE_VALUE, Types.INT_VALUE); } else if (Types.INT_VALUE.equals(rtn)) { // No Cast needed } else { adapter.invokeStatic(Types.CASTER, new Method("toRef", Types.toRefType(rtn), new Type[] { rtn })); adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_INT_VALUE); } return Types.INT_VALUE; } else if (Types.isPrimitiveType(rtn)) { if (Types.DOUBLE_VALUE.equals(rtn)) { adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_INTEGER_FROM_DOUBLE); } else if (Types.BOOLEAN_VALUE.equals(rtn)) { adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_INTEGER_FROM_BOOLEAN); } else { adapter.invokeStatic(Types.CASTER, new Method("toRef", Types.toRefType(rtn), new Type[] { rtn })); adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_INTEGER); } return Types.INTEGER; } if (!Types.INTEGER.equals(rtn)) adapter.invokeStatic(Types.CASTER, Methods.METHOD_TO_INTEGER); return Types.INTEGER; } if (mode == MODE_VALUE) return Types.INT_VALUE; return Types.INTEGER; }
From source file:lucee.transformer.bytecode.expression.var.Variable.java
License:Open Source License
static Type _writeOutFirstBIF(BytecodeContext bc, BIF bif, int mode, boolean last, Position line) throws BytecodeException { GeneratorAdapter adapter = bc.getAdapter(); adapter.loadArg(0);/*from ww w .ja va 2s . c o m*/ // class Class bifClass = bif.getClazz(); Type bifType = Type.getType(bifClass);//Types.toType(bif.getClassName()); Type rtnType = Types.toType(bif.getReturnType()); if (rtnType == Types.VOID) rtnType = Types.STRING; // arguments Argument[] args = bif.getArguments(); Type[] argTypes; // Arg Type FIX if (bif.getArgType() == FunctionLibFunction.ARG_FIX) { if (isNamed(bif.getName(), args)) { NamedArgument[] nargs = toNamedArguments(args); String[] names = new String[nargs.length]; // get all names for (int i = 0; i < nargs.length; i++) { names[i] = getName(nargs[i].getName()); } ArrayList<FunctionLibFunctionArg> list = bif.getFlf().getArg(); Iterator<FunctionLibFunctionArg> it = list.iterator(); argTypes = new Type[list.size() + 1]; argTypes[0] = Types.PAGE_CONTEXT; FunctionLibFunctionArg flfa; int index = 0; VT vt; while (it.hasNext()) { flfa = it.next(); vt = getMatchingValueAndType(flfa, nargs, names, line); if (vt.index != -1) names[vt.index] = null; argTypes[++index] = Types.toType(vt.type); if (vt.value == null) ASMConstants.NULL(bc.getAdapter()); else vt.value.writeOut(bc, Types.isPrimitiveType(argTypes[index]) ? MODE_VALUE : MODE_REF); } for (int y = 0; y < names.length; y++) { if (names[y] != null) { BytecodeException bce = new BytecodeException("argument [" + names[y] + "] is not allowed for function [" + bif.getFlf().getName() + "]", args[y].getStart()); UDFUtil.addFunctionDoc(bce, bif.getFlf()); throw bce; } } } else { argTypes = new Type[args.length + 1]; argTypes[0] = Types.PAGE_CONTEXT; for (int y = 0; y < args.length; y++) { argTypes[y + 1] = Types.toType(args[y].getStringType()); args[y].writeOutValue(bc, Types.isPrimitiveType(argTypes[y + 1]) ? MODE_VALUE : MODE_REF); } // if no method exists for the exact match of arguments, call the method with all arguments (when exists) if (methodExists(bifClass, "call", argTypes, rtnType) == Boolean.FALSE) { ArrayList<FunctionLibFunctionArg> _args = bif.getFlf().getArg(); Type[] tmp = new Type[_args.size() + 1]; // fill the existing for (int i = 0; i < argTypes.length; i++) { tmp[i] = argTypes[i]; } // get the rest with default values FunctionLibFunctionArg flfa; for (int i = argTypes.length; i < tmp.length; i++) { flfa = _args.get(i - 1); tmp[i] = Types.toType(flfa.getTypeAsString()); getDefaultValue(flfa).value.writeOut(bc, Types.isPrimitiveType(tmp[i]) ? MODE_VALUE : MODE_REF); } argTypes = tmp; } } } // Arg Type DYN else { argTypes = new Type[2]; argTypes[0] = Types.PAGE_CONTEXT; argTypes[1] = Types.OBJECT_ARRAY; ExpressionUtil.writeOutExpressionArray(bc, Types.OBJECT, args); } adapter.invokeStatic(bifType, new Method("call", rtnType, argTypes)); if (mode == MODE_REF || !last) { if (Types.isPrimitiveType(rtnType)) { adapter.invokeStatic(Types.CASTER, new Method("toRef", Types.toRefType(rtnType), new Type[] { rtnType })); rtnType = Types.toRefType(rtnType); } } return rtnType; }
From source file:lucee.transformer.bytecode.expression.var.VariableImpl.java
License:Open Source License
static Type _writeOutFirstBIF(BytecodeContext bc, BIF bif, int mode, boolean last, Position line) throws TransformerException { GeneratorAdapter adapter = bc.getAdapter(); adapter.loadArg(0);/*www . j a v a 2 s . com*/ // class ClassDefinition bifCD = bif.getClassDefinition(); Class clazz = null; try { clazz = bifCD.getClazz(); } catch (Exception e) { e.printStackTrace(); //throw new TransformerException(e,line); } Type rtnType = Types.toType(bif.getReturnType()); if (rtnType == Types.VOID) rtnType = Types.STRING; // arguments Argument[] args = bif.getArguments(); Type[] argTypes; boolean core = bif.getFlf().isCore(); // MUST setting this to false need to work !!! if (bif.getArgType() == FunctionLibFunction.ARG_FIX && !bifCD.isBundle() && core) { if (isNamed(bif.getFlf().getName(), args)) { NamedArgument[] nargs = toNamedArguments(args); String[] names = new String[nargs.length]; // get all names for (int i = 0; i < nargs.length; i++) { names[i] = getName(nargs[i].getName()); } ArrayList<FunctionLibFunctionArg> list = bif.getFlf().getArg(); Iterator<FunctionLibFunctionArg> it = list.iterator(); argTypes = new Type[list.size() + 1]; argTypes[0] = Types.PAGE_CONTEXT; FunctionLibFunctionArg flfa; int index = 0; VT vt; while (it.hasNext()) { flfa = it.next(); vt = getMatchingValueAndType(bc.getFactory(), flfa, nargs, names, line); if (vt.index != -1) names[vt.index] = null; argTypes[++index] = Types.toType(vt.type); if (vt.value == null) ASMConstants.NULL(bc.getAdapter()); else vt.value.writeOut(bc, Types.isPrimitiveType(argTypes[index]) ? MODE_VALUE : MODE_REF); } for (int y = 0; y < names.length; y++) { if (names[y] != null) { TransformerException bce = new TransformerException("argument [" + names[y] + "] is not allowed for function [" + bif.getFlf().getName() + "]", args[y].getStart()); UDFUtil.addFunctionDoc(bce, bif.getFlf()); throw bce; } } } else { argTypes = new Type[args.length + 1]; argTypes[0] = Types.PAGE_CONTEXT; for (int y = 0; y < args.length; y++) { argTypes[y + 1] = Types.toType(args[y].getStringType()); args[y].writeOutValue(bc, Types.isPrimitiveType(argTypes[y + 1]) ? MODE_VALUE : MODE_REF); } // if no method exists for the exact match of arguments, call the method with all arguments (when exists) if (methodExists(clazz, "call", argTypes, rtnType) == Boolean.FALSE) { ArrayList<FunctionLibFunctionArg> _args = bif.getFlf().getArg(); Type[] tmp = new Type[_args.size() + 1]; // fill the existing for (int i = 0; i < argTypes.length; i++) { tmp[i] = argTypes[i]; } // get the rest with default values FunctionLibFunctionArg flfa; VT def; for (int i = argTypes.length; i < tmp.length; i++) { flfa = _args.get(i - 1); tmp[i] = Types.toType(flfa.getTypeAsString()); def = getDefaultValue(bc.getFactory(), flfa); if (def.value != null) def.value.writeOut(bc, Types.isPrimitiveType(tmp[i]) ? MODE_VALUE : MODE_REF); else ASMConstants.NULL(bc.getAdapter()); } argTypes = tmp; } } } // Arg Type DYN or bundle based else { /////////////////////////////////////////////////////////////// if (bif.getArgType() == FunctionLibFunction.ARG_FIX) { if (isNamed(bif.getFlf().getName(), args)) { NamedArgument[] nargs = toNamedArguments(args); String[] names = getNames(nargs); ArrayList<FunctionLibFunctionArg> list = bif.getFlf().getArg(); Iterator<FunctionLibFunctionArg> it = list.iterator(); LinkedList<Argument> tmpArgs = new LinkedList<Argument>(); LinkedList<Boolean> nulls = new LinkedList<Boolean>(); FunctionLibFunctionArg flfa; VT vt; while (it.hasNext()) { flfa = it.next(); vt = getMatchingValueAndType(bc.getFactory(), flfa, nargs, names, line); if (vt.index != -1) names[vt.index] = null; if (vt.value == null) tmpArgs.add(new Argument(bif.getFactory().createNull(), "any")); // has to by any otherwise a caster is set else tmpArgs.add(new Argument(vt.value, vt.type)); nulls.add(vt.value == null); } for (int y = 0; y < names.length; y++) { if (names[y] != null) { TransformerException bce = new TransformerException("argument [" + names[y] + "] is not allowed for function [" + bif.getFlf().getName() + "]", args[y].getStart()); UDFUtil.addFunctionDoc(bce, bif.getFlf()); throw bce; } } // remove null at the end Boolean tmp; while ((tmp = nulls.pollLast()) != null) { if (!tmp.booleanValue()) break; tmpArgs.pollLast(); } args = tmpArgs.toArray(new Argument[tmpArgs.size()]); } } /////////////////////////////////////////////////////////////// argTypes = new Type[2]; argTypes[0] = Types.PAGE_CONTEXT; argTypes[1] = Types.OBJECT_ARRAY; ExpressionUtil.writeOutExpressionArray(bc, Types.OBJECT, args); } // core if (core && !bifCD.isBundle()) { adapter.invokeStatic(Type.getType(clazz), new Method("call", rtnType, argTypes)); } // external else { //in that case we need 3 addional args // className if (bifCD.getClassName() != null) adapter.push(bifCD.getClassName()); else ASMConstants.NULL(adapter); if (bifCD.getName() != null) adapter.push(bifCD.getName());// bundle name else ASMConstants.NULL(adapter); if (bifCD.getVersionAsString() != null) adapter.push(bifCD.getVersionAsString());// bundle version else ASMConstants.NULL(adapter); adapter.invokeStatic(Types.FUNCTION_HANDLER_POOL, INVOKE); rtnType = Types.OBJECT; } if (mode == MODE_REF || !last) { if (Types.isPrimitiveType(rtnType)) { adapter.invokeStatic(Types.CASTER, new Method("toRef", Types.toRefType(rtnType), new Type[] { rtnType })); rtnType = Types.toRefType(rtnType); } } return rtnType; }
From source file:lucee.transformer.bytecode.Page.java
License:Open Source License
/** * result byte code as binary array/*from w w w .j av a 2 s. c o 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.statement.tag.TagHelper.java
License:Open Source License
private static void setAttributes(BytecodeContext bc, Tag tag, int currLocal, Type currType, boolean doDefault) throws BytecodeException { GeneratorAdapter adapter = bc.getAdapter(); Map<String, Attribute> attributes = tag.getAttributes(); String methodName;/*from w ww. j ava 2s. c om*/ Attribute attr; Iterator<Attribute> it = attributes.values().iterator(); while (it.hasNext()) { attr = it.next(); if (doDefault != attr.isDefaultAttribute()) continue; if (attr.isDynamicType()) { adapter.loadLocal(currLocal); adapter.visitInsn(Opcodes.ACONST_NULL); //adapter.push(attr.getName()); Variable.registerKey(bc, LitString.toExprString(attr.getName())); attr.getValue().writeOut(bc, Expression.MODE_REF); adapter.invokeVirtual(currType, SET_DYNAMIC_ATTRIBUTE); } else { Type type = CastOther.getType(attr.getType()); methodName = tag.getTagLibTag().getSetter(attr, type); adapter.loadLocal(currLocal); attr.getValue().writeOut(bc, Types.isPrimitiveType(type) ? Expression.MODE_VALUE : Expression.MODE_REF); adapter.invokeVirtual(currType, new Method(methodName, Type.VOID_TYPE, new Type[] { type })); } } }
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 ww w .ja v a 2 s.c o m 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:net.enilink.composition.asm.processors.MethodDelegationGenerator.java
License:Open Source License
@Override public void process(BehaviourClassNode classNode, ExtendedMethod method) throws Exception { // remove abstract modifier and mark as transient method.access &= ~ACC_ABSTRACT;/*from w w w .j av a2s . com*/ method.access |= ACC_TRANSIENT; MethodNodeGenerator gen = new MethodNodeGenerator(method); gen.loadThis(); gen.invokeInterface(BEHAVIOUR_TYPE, new Method(Behaviour.GET_ENTITY_METHOD, OBJECT_TYPE, new Type[0])); // if (method.getMethodDescriptor().getDeclaringClass().isInterface()) { Type faceType = Type.getType(method.getOverriddenMethod().getDeclaringClass()); gen.checkCast(faceType); gen.loadArgs(); gen.invokeInterface(faceType, new Method(method.name, method.desc)); // } else { // // reflective call of target method // gen.dup(); // gen.invokeVirtual(OBJECT_TYPE, new Method("getClass", CLASS_TYPE, // new Type[0])); // gen.push(method.name); // gen.invokeVirtual(CLASS_TYPE, new Method("getMethod", METHOD_TYPE, // new Type[] { STRING_TYPE, Type.getType(Class[].class) })); // gen.swap(); // gen.loadArgArray(); // gen.invokeVirtual(METHOD_TYPE, new Method("invoke", OBJECT_TYPE, // new Type[] { OBJECT_TYPE, Type.getType(Object[].class) })); // } gen.returnValue(); gen.endMethod(); }
From source file:net.wpm.codegen.ClassBuilder.java
License:Apache License
/** * Creates a new method for a dynamic class * * @param methodName name of method/*from w w w . j a v a2s . c o m*/ * @param returnClass type which returns this method * @param argumentTypes list of types of arguments * @param expression function which will be processed * @return changed AsmFunctionFactory */ public ClassBuilder<T> method(String methodName, Class<?> returnClass, List<? extends Class<?>> argumentTypes, Expression expression) { Type[] types = new Type[argumentTypes.size()]; for (int i = 0; i < argumentTypes.size(); i++) { types[i] = getType(argumentTypes.get(i)); } return method(new Method(methodName, getType(returnClass), types), expression); }
From source file:net.wpm.codegen.ClassBuilder.java
License:Apache License
/** * Create a new static method for a dynamic class * //from w ww . j av a 2s. c om * @param methodName name of method * @param returnClass type which returns this method * @param argumentTypes list of types of arguments * @param expression function which will be processed * @return changed AsmFunctionFactory */ public ClassBuilder<T> staticMethod(String methodName, Class<?> returnClass, List<? extends Class<?>> argumentTypes, Expression expression) { Type[] types = new Type[argumentTypes.size()]; for (int i = 0; i < argumentTypes.size(); i++) { types[i] = getType(argumentTypes.get(i)); } return staticMethod(new Method(methodName, getType(returnClass), types), expression); }