List of usage examples for org.objectweb.asm Opcodes V1_6
int V1_6
To view the source code for org.objectweb.asm Opcodes V1_6.
Click Source Link
From source file:lucee.transformer.bytecode.Page.java
License:Open Source License
/** * result byte code as binary array/*from w w w . j a v 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.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);//from w w w .j ava 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.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 ww.java 2s . c o 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.yrom.tools.WriteStyleablesProcessor.java
License:Apache License
@Override public void proceed() { ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS); writer.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC | Opcodes.ACC_SUPER, RSymbols.R_STYLEABLES_CLASS_NAME, null, "java/lang/Object", null); for (String name : symbols.getStyleables().keySet()) { writer.visitField(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL, name, "[I", null, null); }//from ww w . j ava 2 s . c o m writeClinit(writer); writer.visitEnd(); byte[] bytes = writer.toByteArray(); try { if (!dir.isDirectory() && !dir.mkdirs()) { throw new RuntimeException("Cannot mkdir " + dir); } Files.write(dir.toPath().resolve(RSymbols.R_STYLEABLES_CLASS_NAME + ".class"), bytes); } catch (IOException e) { throw new UncheckedIOException(e); } }
From source file:org.apache.aries.proxy.impl.ProxyUtils.java
License:Apache License
/** * Get the java version to be woven at./*from w ww. j a va2 s . c o m*/ * @return */ public static int getWeavingJavaVersion() { if (weavingJavaVersion == -1) { //In order to avoid an inconsistent stack error the version of the woven byte code needs to match //the level of byte codes in the original class switch (JAVA_CLASS_VERSION) { case Opcodes.V1_8: LOGGER.debug("Weaving to Java 8"); weavingJavaVersion = Opcodes.V1_8; break; case Opcodes.V1_7: LOGGER.debug("Weaving to Java 7"); weavingJavaVersion = Opcodes.V1_7; break; case Opcodes.V1_6: LOGGER.debug("Weaving to Java 6"); weavingJavaVersion = Opcodes.V1_6; break; case Opcodes.V1_5: LOGGER.debug("Weaving to Java 5"); weavingJavaVersion = Opcodes.V1_5; break; default: //aries should work with Java 5 or above - also will highlight when a higher level (and unsupported) level of Java is released throw new IllegalArgumentException("Invalid Java version " + JAVA_CLASS_VERSION); } } return weavingJavaVersion; }
From source file:org.apache.deltaspike.partialbean.impl.proxy.AsmProxyClassGenerator.java
License:Apache License
private static byte[] generateProxyClassBytes(Class<?> targetClass, Class<? extends InvocationHandler> invocationHandlerClass, String proxyName, java.lang.reflect.Method[] redirectMethods, java.lang.reflect.Method[] interceptionMethods) { Class<?> superClass = targetClass; String[] interfaces = new String[] {}; if (targetClass.isInterface()) { superClass = Object.class; interfaces = new String[] { Type.getInternalName(targetClass) }; }/*from w w w.ja va 2s. c o m*/ // add PartialBeanProxy as interface interfaces = Arrays.copyOf(interfaces, interfaces.length + 1); interfaces[interfaces.length - 1] = Type.getInternalName(PartialBeanProxy.class); Type superType = Type.getType(superClass); Type proxyType = Type.getObjectType(proxyName); Type invocationHandlerType = Type.getType(invocationHandlerClass); ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, proxyType.getInternalName(), null, superType.getInternalName(), interfaces); // copy annotations for (Annotation annotation : targetClass.getDeclaredAnnotations()) { cw.visitAnnotation(Type.getDescriptor(annotation.annotationType()), true).visitEnd(); } defineInvocationHandlerField(cw, invocationHandlerType); defineConstructor(cw, proxyType, superType); definePartialBeanProxyMethods(cw, proxyType, invocationHandlerType); for (java.lang.reflect.Method method : redirectMethods) { defineMethod(cw, method, proxyType, invocationHandlerType, superType, true); } for (java.lang.reflect.Method method : interceptionMethods) { defineMethod(cw, method, proxyType, invocationHandlerType, superType, false); } return cw.toByteArray(); }
From source file:org.apache.deltaspike.proxy.impl.AsmDeltaSpikeProxyClassGenerator.java
License:Apache License
private static byte[] generateProxyClassBytes(Class<?> targetClass, String proxyName, String superAccessorMethodSuffix, Class<?>[] additionalInterfaces, java.lang.reflect.Method[] delegateMethods, java.lang.reflect.Method[] interceptMethods) { Class<?> superClass = targetClass; String[] interfaces = new String[] {}; if (targetClass.isInterface()) { superClass = Object.class; interfaces = new String[] { Type.getInternalName(targetClass) }; }//from w w w . j av a 2s. com // add DeltaSpikeProxy as interface interfaces = Arrays.copyOf(interfaces, interfaces.length + 1); interfaces[interfaces.length - 1] = Type.getInternalName(DeltaSpikeProxy.class); if (additionalInterfaces != null && additionalInterfaces.length > 0) { interfaces = Arrays.copyOf(interfaces, interfaces.length + additionalInterfaces.length); for (int i = 0; i < additionalInterfaces.length; i++) { interfaces[(interfaces.length - 1) + i] = Type.getInternalName(additionalInterfaces[i]); } } Type superType = Type.getType(superClass); Type proxyType = Type.getObjectType(proxyName); final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, proxyType.getInternalName(), null, superType.getInternalName(), interfaces); defineDefaultConstructor(cw, proxyType, superType); defineDeltaSpikeProxyFields(cw); defineDeltaSpikeProxyMethods(cw, proxyType); if (delegateMethods != null) { for (java.lang.reflect.Method method : delegateMethods) { defineMethod(cw, method, proxyType); } } if (interceptMethods != null) { for (java.lang.reflect.Method method : interceptMethods) { defineSuperAccessorMethod(cw, method, superType, superAccessorMethodSuffix); defineMethod(cw, method, proxyType); } } // copy all annotations from the source class try { // ClassVisitor to intercept all annotation visits on the class ClassVisitor cv = new ClassVisitor(Opcodes.ASM5) { @Override public AnnotationVisitor visitAnnotation(String desc, boolean visible) { return new CopyAnnotationVisitorAdapter(super.visitAnnotation(desc, visible), cw.visitAnnotation(desc, visible)); } }; // visit class to proxy with our visitor to copy all annotations from the source class to our ClassWriter String sourceClassFilename = targetClass.getName().replace('.', '/') + ".class"; ClassReader cr = new ClassReader(targetClass.getClassLoader().getResourceAsStream(sourceClassFilename)); cr.accept(cv, 0); } catch (Exception e) { throw new RuntimeException(e); } return cw.toByteArray(); }
From source file:org.apache.deltaspike.proxy.impl.AsmProxyClassGenerator.java
License:Apache License
private static byte[] generateProxyClassBytes(Class<?> targetClass, String proxyName, String superAccessorMethodSuffix, Class<?>[] additionalInterfaces, java.lang.reflect.Method[] delegateMethods, java.lang.reflect.Method[] interceptMethods) { Class<?> superClass = targetClass; String[] interfaces = new String[] {}; if (targetClass.isInterface()) { superClass = Object.class; interfaces = new String[] { Type.getInternalName(targetClass) }; }/*from w w w . j av a 2 s . co m*/ // add DeltaSpikeProxy as interface interfaces = Arrays.copyOf(interfaces, interfaces.length + 1); interfaces[interfaces.length - 1] = Type.getInternalName(DeltaSpikeProxy.class); if (additionalInterfaces != null && additionalInterfaces.length > 0) { interfaces = Arrays.copyOf(interfaces, interfaces.length + additionalInterfaces.length); for (int i = 0; i < additionalInterfaces.length; i++) { interfaces[(interfaces.length - 1) + i] = Type.getInternalName(additionalInterfaces[i]); } } Type superType = Type.getType(superClass); Type proxyType = Type.getObjectType(proxyName); Type delegateInvocationHandlerType = Type.getType(InvocationHandler.class); final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, proxyType.getInternalName(), null, superType.getInternalName(), interfaces); defineInvocationHandlerField(cw, delegateInvocationHandlerType); defineDefaultConstructor(cw, proxyType, superType); defineDelegateInvocationHandlerConstructor(cw, proxyType, superType, delegateInvocationHandlerType); defineDeltaSpikeProxyMethods(cw, proxyType, delegateInvocationHandlerType); if (delegateMethods != null) { for (java.lang.reflect.Method method : delegateMethods) { defineMethod(cw, method, DelegateManualInvocationHandler.class); } } if (interceptMethods != null) { for (java.lang.reflect.Method method : interceptMethods) { defineSuperAccessorMethod(cw, method, superType, superAccessorMethodSuffix); defineMethod(cw, method, InterceptManualInvocationHandler.class); } } // copy all annotations from the source class try { // ClassVisitor to intercept all annotation visits on the class ClassVisitor cv = new ClassVisitor(Opcodes.ASM5) { @Override public AnnotationVisitor visitAnnotation(String desc, boolean visible) { return new CopyAnnotationVisitorAdapter(super.visitAnnotation(desc, visible), cw.visitAnnotation(desc, visible)); } }; // visit class to proxy with our visitor to copy all annotations from the source class to our ClassWriter String sourceClassFilename = targetClass.getName().replace('.', '/') + ".class"; ClassReader cr = new ClassReader(targetClass.getClassLoader().getResourceAsStream(sourceClassFilename)); cr.accept(cv, 0); } catch (Exception e) { throw new RuntimeException(e); } return cw.toByteArray(); }
From source file:org.apache.deltaspike.proxy.util.AsmProxyClassGenerator.java
License:Apache License
private static byte[] generateProxyClassBytes(Class<?> targetClass, Class<? extends InvocationHandler> invocationHandlerClass, String proxyName, String superAccessorMethodSuffix, Class<?>[] additionalInterfaces, java.lang.reflect.Method[] delegateMethods, java.lang.reflect.Method[] interceptMethods) { Class<?> superClass = targetClass; String[] interfaces = new String[] {}; if (targetClass.isInterface()) { superClass = Object.class; interfaces = new String[] { Type.getInternalName(targetClass) }; }/*ww w . ja va 2s. com*/ // add DeltaSpikeProxy as interface interfaces = Arrays.copyOf(interfaces, interfaces.length + 1); interfaces[interfaces.length - 1] = Type.getInternalName(DeltaSpikeProxy.class); if (additionalInterfaces != null && additionalInterfaces.length > 0) { interfaces = Arrays.copyOf(interfaces, interfaces.length + additionalInterfaces.length); for (int i = 0; i < additionalInterfaces.length; i++) { interfaces[(interfaces.length - 1) + i] = Type.getInternalName(additionalInterfaces[i]); } } Type superType = Type.getType(superClass); Type proxyType = Type.getObjectType(proxyName); Type invocationHandlerType = Type.getType(invocationHandlerClass); ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, proxyType.getInternalName(), null, superType.getInternalName(), interfaces); // copy annotations for (Annotation annotation : targetClass.getDeclaredAnnotations()) { cw.visitAnnotation(Type.getDescriptor(annotation.annotationType()), true).visitEnd(); } defineInvocationHandlerField(cw, invocationHandlerType); defineDefaultConstructor(cw, proxyType, superType); defineDelegateInvocationHandlerConstructor(cw, proxyType, superType, invocationHandlerType); defineDeltaSpikeProxyMethods(cw, proxyType, invocationHandlerType); for (java.lang.reflect.Method method : delegateMethods) { defineMethod(cw, method, DelegateManualInvocationHandler.class); } for (java.lang.reflect.Method method : interceptMethods) { defineSuperAccessorMethod(cw, method, superType, superAccessorMethodSuffix); defineMethod(cw, method, InterceptManualInvocationHandler.class); } return cw.toByteArray(); }