List of usage examples for org.objectweb.asm MethodVisitor visitTryCatchBlock
public void visitTryCatchBlock(final Label start, final Label end, final Label handler, final String type)
From source file:lombok.patcher.scripts.SetSymbolDuringMethodCallScript.java
License:Open Source License
private void makeWrapperMethod(ClassVisitor cv, WrapperMethodDescriptor wmd) { MethodVisitor mv = cv.visitMethod(Opcodes.ACC_SYNTHETIC | Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC, wmd.getWrapperName(), wmd.getWrapperDescriptor(), null, null); MethodLogistics logistics = new MethodLogistics(Opcodes.ACC_STATIC, wmd.getWrapperDescriptor()); mv.visitCode();/*from w ww . j av a 2s . co m*/ Label start = new Label(); Label end = new Label(); Label handler = new Label(); mv.visitTryCatchBlock(start, end, handler, null); mv.visitLabel(start); mv.visitLdcInsn(symbol); mv.visitMethodInsn(Opcodes.INVOKESTATIC, "lombok/patcher/Symbols", "push", "(Ljava/lang/String;)V", false); for (int i = 0; i < logistics.getParamCount(); i++) { logistics.generateLoadOpcodeForParam(i, mv); } mv.visitMethodInsn(wmd.getOpcode(), wmd.getOwner(), wmd.getName(), wmd.getTargetDescriptor(), wmd.isItf()); mv.visitLabel(end); mv.visitMethodInsn(Opcodes.INVOKESTATIC, "lombok/patcher/Symbols", "pop", "()V", false); logistics.generateReturnOpcode(mv); mv.visitLabel(handler); mv.visitFrame(Opcodes.F_FULL, 0, null, 1, new Object[] { "java/lang/Throwable" }); mv.visitMethodInsn(Opcodes.INVOKESTATIC, "lombok/patcher/Symbols", "pop", "()V", false); mv.visitInsn(Opcodes.ATHROW); mv.visitMaxs(Math.max(1, logistics.getParamCount()), logistics.getParamCount()); mv.visitEnd(); }
From source file:me.rojo8399.placeholderapi.impl.placeholder.gen.ClassPlaceholderFactory.java
License:Open Source License
private static void tryCatch(MethodVisitor mv, Consumer<MethodVisitor> t, Consumer<MethodVisitor> c) { Label st = new Label(), et = new Label(), sc = new Label(), ec = new Label(); mv.visitTryCatchBlock(st, et, sc, "java/lang/Exception"); mv.visitLabel(st);// w w w. j av a2s. c om t.accept(mv); mv.visitLabel(et); mv.visitJumpInsn(GOTO, ec); mv.visitLabel(sc); c.accept(mv); mv.visitLabel(ec); }
From source file:org.actorsguildframework.internal.codegenerator.BeanCreator.java
License:Apache License
/** * Creates and loads the bean's factory class. * @param beanClass the Bean class/*from w w w.ja v a2 s .c om*/ * @param generatedBeanClassName the name of the class that this factory will produce * @param bcd the bean class descriptor * @param synchronizeInitializers true to synchronize the initializer invocations (actors * do this), false otherwise * @return the new factory */ public static BeanFactory generateFactoryClass(Class<?> beanClass, String generatedBeanClassName, BeanClassDescriptor bcd, boolean synchronizeInitializers) { String className = String.format("%s__BEANFACTORY", beanClass.getName()); String classNameInternal = className.replace('.', '/'); String generatedBeanClassNameInternal = generatedBeanClassName.replace('.', '/'); ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES); MethodVisitor mv; cw.visit(codeVersion, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_SUPER + Opcodes.ACC_SYNTHETIC, classNameInternal, null, "java/lang/Object", new String[] { Type.getInternalName(BeanFactory.class) }); cw.visitSource(null, null); { mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V"); mv.visitInsn(Opcodes.RETURN); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", "L" + classNameInternal + ";", null, l0, l1, 0); mv.visitMaxs(1, 1); mv.visitEnd(); } { mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "createNewInstance", "(Lorg/actorsguildframework/internal/Controller;Lorg/actorsguildframework/Props;)Ljava/lang/Object;", null, null); mv.visitCode(); final int initCount = bcd.getInitializerCount(); Label tryStart = new Label(); Label tryEnd = new Label(); Label tryFinally = new Label(); Label tryFinallyEnd = new Label(); if (synchronizeInitializers && (initCount > 0)) { mv.visitTryCatchBlock(tryStart, tryEnd, tryFinally, null); mv.visitTryCatchBlock(tryFinally, tryFinallyEnd, tryFinally, null); } Label l0 = new Label(); mv.visitLabel(l0); mv.visitTypeInsn(Opcodes.NEW, generatedBeanClassNameInternal); mv.visitInsn(Opcodes.DUP); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitVarInsn(Opcodes.ALOAD, 2); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, generatedBeanClassNameInternal, "<init>", "(Lorg/actorsguildframework/internal/Controller;Lorg/actorsguildframework/Props;)V"); if (synchronizeInitializers) { mv.visitInsn(Opcodes.DUP); mv.visitInsn(Opcodes.MONITORENTER); mv.visitLabel(tryStart); } for (int i = 0; i < initCount; i++) { Method m = bcd.getInitializers(i); mv.visitInsn(Opcodes.DUP); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, generatedBeanClassNameInternal, m.getName(), Type.getMethodDescriptor(m)); } if (synchronizeInitializers) { if (initCount > 0) { mv.visitInsn(Opcodes.DUP); mv.visitInsn(Opcodes.MONITOREXIT); mv.visitLabel(tryEnd); mv.visitJumpInsn(Opcodes.GOTO, tryFinallyEnd); } mv.visitLabel(tryFinally); mv.visitInsn(Opcodes.DUP); mv.visitInsn(Opcodes.MONITOREXIT); mv.visitLabel(tryFinallyEnd); } mv.visitInsn(Opcodes.ARETURN); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", "L" + classNameInternal + ";", null, l0, l1, 0); mv.visitLocalVariable("controller", "Lorg/actorsguildframework/internal/Controller;", null, l0, l1, 1); mv.visitLocalVariable("props", "Lorg/actorsguildframework/Props;", null, l0, l1, 2); mv.visitLocalVariable("synchronizeInitializer", "Z", null, l0, l1, 3); mv.visitMaxs(4, 3); mv.visitEnd(); } cw.visitEnd(); Class<?> newClass = GenerationUtils.loadClass(className, cw.toByteArray()); try { return (BeanFactory) newClass.newInstance(); } catch (Exception e) { throw new ConfigurationException("Failure loading ActorProxyFactory", e); } }
From source file:org.actorsguildframework.internal.codegenerator.BeanCreator.java
License:Apache License
/** * Writes the bean constructor to the given ClassWriter. * @param beanClass the original bean class to extend * @param bcd the descriptor of the bean * @param classNameInternal the internal name of the new class * @param cw the ClassWriter to write to * @param snippetWriter if not null, this will be invoked to add a snippet * after the invocation of the super constructor *///from w w w .j av a 2 s.c om public static void writeConstructor(Class<?> beanClass, BeanClassDescriptor bcd, String classNameInternal, ClassWriter cw, SnippetWriter snippetWriter) { String classNameDescriptor = "L" + classNameInternal + ";"; int localPropertySize = 0; ArrayList<PropertyDescriptor> localVarProperties = new ArrayList<PropertyDescriptor>(); for (int i = 0; i < bcd.getPropertyCount(); i++) { PropertyDescriptor pd = bcd.getProperty(i); if (pd.getPropertySource().isGenerating() || (pd.getDefaultValue() != null)) { localVarProperties.add(pd); localPropertySize += Type.getType(pd.getPropertyClass()).getSize(); } } final int locVarThis = 0; final int locVarController = 1; final int locVarProps = 2; final int locVarPropertiesOffset = 3; final int locVarP = 3 + localPropertySize; final int locVarK = 4 + localPropertySize; final int locVarV = 5 + localPropertySize; MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "(Lorg/actorsguildframework/internal/Controller;Lorg/actorsguildframework/Props;)V", null, null); mv.visitCode(); Label lTry = new Label(); Label lCatch = new Label(); mv.visitTryCatchBlock(lTry, lCatch, lCatch, "java/lang/ClassCastException"); Label lBegin = new Label(); mv.visitLabel(lBegin); mv.visitVarInsn(Opcodes.ALOAD, locVarThis); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(beanClass), "<init>", "()V"); if (snippetWriter != null) snippetWriter.write(mv); Label lPropertyInit = new Label(); mv.visitLabel(lPropertyInit); // load default values into the local variables for each property that must be set int varCount = 0; for (PropertyDescriptor pd : localVarProperties) { Type pt = Type.getType(pd.getPropertyClass()); if (pd.getDefaultValue() != null) mv.visitFieldInsn(Opcodes.GETSTATIC, Type.getInternalName(pd.getDefaultValue().getDeclaringClass()), pd.getDefaultValue().getName(), Type.getDescriptor(pd.getDefaultValue().getType())); else GenerationUtils.generateLoadDefault(mv, pd.getPropertyClass()); mv.visitVarInsn(pt.getOpcode(Opcodes.ISTORE), locVarPropertiesOffset + varCount); varCount += pt.getSize(); } // loop through the props argument's list mv.visitVarInsn(Opcodes.ALOAD, locVarProps); mv.visitVarInsn(Opcodes.ASTORE, locVarP); Label lWhile = new Label(); Label lEndWhile = new Label(); Label lWhileBody = new Label(); mv.visitLabel(lWhile); mv.visitJumpInsn(Opcodes.GOTO, lEndWhile); mv.visitLabel(lWhileBody); mv.visitVarInsn(Opcodes.ALOAD, locVarP); mv.visitInsn(Opcodes.DUP); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "org/actorsguildframework/Props", "getKey", "()Ljava/lang/String;"); mv.visitVarInsn(Opcodes.ASTORE, locVarK); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "org/actorsguildframework/Props", "getValue", "()Ljava/lang/Object;"); mv.visitVarInsn(Opcodes.ASTORE, locVarV); mv.visitLabel(lTry); // write an if for each property Label lEndIf = new Label(); varCount = 0; int ifCount = 0; for (int i = 0; i < bcd.getPropertyCount(); i++) { PropertyDescriptor pd = bcd.getProperty(i); boolean usesLocal = pd.getPropertySource().isGenerating() || (pd.getDefaultValue() != null); Class<?> propClass = pd.getPropertyClass(); Type pt = Type.getType(propClass); mv.visitVarInsn(Opcodes.ALOAD, locVarK); mv.visitLdcInsn(pd.getName()); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z"); Label lElse = new Label(); mv.visitJumpInsn(Opcodes.IFEQ, lElse); if (!usesLocal) mv.visitVarInsn(Opcodes.ALOAD, locVarThis); // for setter invocation, load 'this' if (propClass.isPrimitive()) { mv.visitLdcInsn(pd.getName()); mv.visitVarInsn(Opcodes.ALOAD, locVarV); mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(BeanHelper.class), String.format("get%s%sFromPropValue", propClass.getName().substring(0, 1).toUpperCase(Locale.US), propClass.getName().substring(1)), "(Ljava/lang/String;Ljava/lang/Object;)" + pt.getDescriptor()); } else if (!propClass.equals(Object.class)) { mv.visitVarInsn(Opcodes.ALOAD, locVarV); mv.visitTypeInsn(Opcodes.CHECKCAST, pt.getInternalName()); } else mv.visitVarInsn(Opcodes.ALOAD, locVarV); if (!usesLocal) mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classNameInternal, pd.getSetter().getName(), Type.getMethodDescriptor(pd.getSetter())); else mv.visitVarInsn(pt.getOpcode(Opcodes.ISTORE), varCount + locVarPropertiesOffset); mv.visitJumpInsn(Opcodes.GOTO, lEndIf); mv.visitLabel(lElse); ifCount++; if (usesLocal) varCount += pt.getSize(); } // else (==> if not prop matched) throw IllegalArgumentException mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(IllegalArgumentException.class)); mv.visitInsn(Opcodes.DUP); mv.visitLdcInsn("Unknown property \"%s\"."); mv.visitInsn(Opcodes.ICONST_1); mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object"); mv.visitInsn(Opcodes.DUP); mv.visitInsn(Opcodes.ICONST_0); mv.visitVarInsn(Opcodes.ALOAD, locVarK); mv.visitInsn(Opcodes.AASTORE); mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/String", "format", "(Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;"); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(IllegalArgumentException.class), "<init>", "(Ljava/lang/String;)V"); mv.visitInsn(Opcodes.ATHROW); mv.visitLabel(lCatch); mv.visitInsn(Opcodes.POP); // pop the exception object (not needed) mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(IllegalArgumentException.class)); mv.visitInsn(Opcodes.DUP); mv.visitLdcInsn("Incompatible type for property \"%s\". Got %s."); mv.visitInsn(Opcodes.ICONST_2); mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object"); mv.visitInsn(Opcodes.DUP); mv.visitInsn(Opcodes.ICONST_0); mv.visitVarInsn(Opcodes.ALOAD, locVarK); mv.visitInsn(Opcodes.AASTORE); mv.visitInsn(Opcodes.DUP); mv.visitInsn(Opcodes.ICONST_1); mv.visitVarInsn(Opcodes.ALOAD, locVarV); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;"); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Class", "getName", "()Ljava/lang/String;"); mv.visitInsn(Opcodes.AASTORE); mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/String", "format", "(Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;"); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(IllegalArgumentException.class), "<init>", "(Ljava/lang/String;)V"); mv.visitInsn(Opcodes.ATHROW); mv.visitLabel(lEndIf); mv.visitVarInsn(Opcodes.ALOAD, locVarP); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "org/actorsguildframework/Props", "tail", "()Lorg/actorsguildframework/Props;"); mv.visitVarInsn(Opcodes.ASTORE, locVarP); mv.visitLabel(lEndWhile); mv.visitVarInsn(Opcodes.ALOAD, locVarP); mv.visitJumpInsn(Opcodes.IFNONNULL, lWhileBody); // write local variables back into properties varCount = 0; for (PropertyDescriptor pd : localVarProperties) { Type pt = Type.getType(pd.getPropertyClass()); mv.visitVarInsn(Opcodes.ALOAD, locVarThis); if (pd.getPropertySource() == PropertySource.ABSTRACT_METHOD) { mv.visitVarInsn(pt.getOpcode(Opcodes.ILOAD), locVarPropertiesOffset + varCount); mv.visitFieldInsn(Opcodes.PUTFIELD, classNameInternal, String.format(PROP_FIELD_NAME_TEMPLATE, pd.getName()), pt.getDescriptor()); } else if (pd.getPropertySource() == PropertySource.USER_WRITTEN) { mv.visitVarInsn(pt.getOpcode(Opcodes.ILOAD), locVarPropertiesOffset + varCount); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classNameInternal, pd.getSetter().getName(), Type.getMethodDescriptor(pd.getSetter())); } else throw new RuntimeException("Internal error"); varCount += pt.getSize(); } // if bean is thread-safe, publish all writes now if (bcd.isThreadSafe()) { mv.visitVarInsn(Opcodes.ALOAD, locVarThis); mv.visitInsn(Opcodes.DUP); mv.visitInsn(Opcodes.MONITORENTER); mv.visitInsn(Opcodes.MONITOREXIT); } mv.visitInsn(Opcodes.RETURN); Label lEnd = new Label(); mv.visitLabel(lEnd); mv.visitLocalVariable("this", classNameDescriptor, null, lBegin, lEnd, locVarThis); mv.visitLocalVariable("controller", "Lorg/actorsguildframework/internal/Controller;", null, lBegin, lEnd, locVarController); mv.visitLocalVariable("props", "Lorg/actorsguildframework/Props;", null, lBegin, lEnd, locVarProps); varCount = 0; for (PropertyDescriptor pd : localVarProperties) { Type pt = Type.getType(pd.getPropertyClass()); mv.visitLocalVariable("__" + pd.getName(), pt.getDescriptor(), GenericTypeHelper.getSignature(pd.getPropertyType()), lPropertyInit, lEnd, locVarPropertiesOffset + varCount); varCount += pt.getSize(); } mv.visitLocalVariable("p", "Lorg/actorsguildframework/Props;", null, lPropertyInit, lEnd, locVarP); mv.visitLocalVariable("k", "Ljava/lang/String;", null, lWhile, lEndWhile, locVarK); mv.visitLocalVariable("v", "Ljava/lang/Object;", null, lWhile, lEndWhile, locVarV); mv.visitMaxs(0, 0); mv.visitEnd(); }
From source file:org.adjective.stout.instruction.TryCatchInstruction.java
License:Apache License
public void accept(MethodVisitor visitor) { visitor.visitTryCatchBlock(_start, _end, _handler, _type.getInternalName()); }
From source file:org.ballerinalang.nativeimpl.jvm.methodvisitor.VisitTryCatchBlock.java
License:Open Source License
public static void visitTryCatchBlock(Strand strand, ObjectValue oMv, ObjectValue oStartLabel, ObjectValue oEndLabel, ObjectValue oHandlerLabel, Object exceptionType) { MethodVisitor mv = ASMUtil.getRefArgumentNativeData(oMv); Label startLabel = ASMUtil.getRefArgumentNativeData(oStartLabel); Label endLabel = ASMUtil.getRefArgumentNativeData(oEndLabel); Label handlerLabel = ASMUtil.getRefArgumentNativeData(oHandlerLabel); mv.visitTryCatchBlock(startLabel, endLabel, handlerLabel, (String) exceptionType); }
From source file:org.cacheonix.impl.transformer.CacheonixMethodGenerator.java
License:LGPL
/** * Generates a new method body for implementing DataSource with the old name to look into the Cacheonix cache first * before calling the original method/* www. j a v a 2 s. c o m*/ * * @param cv ClassVisitor that this class delegates the calls to * @param className Name of the class for which the method is being generated * @param access Method level access * @param desc Method descriptor * @param signature Method signature * @param exceptions Any exceptions that the method can throw * @param name original name of the method * @param newName the original method renamed to the format:orig$Cacheonix$methodName * @param metaData Annotation information for the method * @param cacheonixCacheFieldValue cacheName specified at the class level */ public static void generateCacheAddBody(final ClassVisitor cv, final String className, final int access, final String desc, final String signature, final String[] exceptions, final String name, final String newName, final MethodMetaData metaData, final String cacheonixCacheFieldValue) { final Type[] args = Type.getArgumentTypes(desc); final LocalStackUtil stackFrame = new LocalStackUtil(args); int expirationTime = CacheonixAnnotation.CACHEDATASOURCE_EXPIRATION_TIME_MILLIS_DEFAULT_VALUE; if (metaData.isAnnotationsPresent()) { final Object expTime = metaData.getAnnotationParameterValue( CacheonixAnnotation.CACHE_DATA_SOURCE_DESCRIPTOR, CacheonixAnnotation.CACHEDATASOURCE_EXPIRATION_TIME_MILLIS); if (expTime != null) { expirationTime = Integer.parseInt(expTime.toString()); } } // Start final MethodVisitor mv = cv.visitMethod(access, name, desc, signature, exceptions); mv.visitCode(); final Label l0 = new Label(); final Label l1 = new Label(); final Label l2 = new Label(); mv.visitTryCatchBlock(l0, l1, l2, "java/lang/Exception"); final String classCan = 'L' + className + ';'; mv.visitLdcInsn(Type.getType(classCan)); mv.visitMethodInsn(INVOKESTATIC, "org/cacheonix/impl/util/logging/Logger", "getLogger", "(Ljava/lang/Class;)Lorg/cacheonix/impl/util/logging/Logger;"); mv.visitVarInsn(ASTORE, stackFrame.getLogLocalStackPos()); mv.visitInsn(ACONST_NULL); mv.visitVarInsn(ASTORE, stackFrame.getValObjLocalStackPos()); // val generateKeyAggregationSequence(mv, args, stackFrame, cacheonixCacheFieldValue, metaData.getMethodParamAnnotationInfo()); mv.visitInsn(ACONST_NULL); mv.visitVarInsn(ASTORE, stackFrame.getCacheRefLocalStackPos()); // cache // printingToSysout(mv, "!!!!! G E N E R A T E D !!!!!! '" + name + "' is Called"); mv.visitLabel(l0); // Config file from annotation on Class level mv.visitFieldInsn(GETSTATIC, className, CacheonixClassAdapter.CACHEONIX_CONFIG_FILE_FIELD, "Ljava/lang/String;"); mv.visitMethodInsn(INVOKESTATIC, "cacheonix/cache/CacheManager", "getInstance", "(Ljava/lang/String;)Lcacheonix/cache/CacheManager;"); mv.visitVarInsn(ASTORE, stackFrame.getCacheManagerLocalStackPos()); // inst // CATCH Block mv.visitLabel(l1); final Label l3 = new Label(); mv.visitJumpInsn(GOTO, l3); mv.visitLabel(l2); mv.visitVarInsn(ASTORE, stackFrame.getExceptionLocalStackPos()); // exception mv.visitInsn(ACONST_NULL); mv.visitVarInsn(ASTORE, stackFrame.getCacheManagerLocalStackPos()); // inst mv.visitVarInsn(ALOAD, stackFrame.getLogLocalStackPos()); // log mv.visitLdcInsn(">>>>> Exception getting CacheManager "); mv.visitVarInsn(ALOAD, stackFrame.getExceptionLocalStackPos()); // exception mv.visitMethodInsn(INVOKEVIRTUAL, "org/cacheonix/impl/util/logging/Logger", "e", "(Ljava/lang/Object;Ljava/lang/Throwable;)V"); // END OF TRY CACTCH mv.visitLabel(l3); // printingToSysout(mv, "!!!!! INST is NOT NULL !!!!!! '" + name + "' is Called"); mv.visitVarInsn(ALOAD, stackFrame.getCacheManagerLocalStackPos()); // inst final Label l4 = new Label(); mv.visitJumpInsn(IFNULL, l4); mv.visitVarInsn(ALOAD, stackFrame.getCacheManagerLocalStackPos()); // inst mv.visitFieldInsn(GETSTATIC, className, CacheonixClassAdapter.CACHE_NAME_FIELD, "Ljava/lang/String;"); mv.visitMethodInsn(INVOKEVIRTUAL, "cacheonix/cache/CacheManager", "getCache", "(Ljava/lang/String;)Lcacheonix/cache/Cache;"); mv.visitVarInsn(ASTORE, stackFrame.getCacheRefLocalStackPos()); // cache mv.visitVarInsn(ALOAD, stackFrame.getCacheRefLocalStackPos()); // cache mv.visitVarInsn(ALOAD, stackFrame.getKeyGenLocalStackPos()); // key mv.visitMethodInsn(INVOKEINTERFACE, "cacheonix/cache/Cache", "get", "(Ljava/lang/Object;)Ljava/lang/Object;"); mv.visitVarInsn(ASTORE, stackFrame.getValObjLocalStackPos()); // val mv.visitVarInsn(ALOAD, stackFrame.getValObjLocalStackPos()); // val mv.visitJumpInsn(IFNONNULL, l4); // printingToSysout(mv, "!!!!! VALUE IN CACHE IS NULL !!!!!! '" + name + "' is Called"); generateMethodParameterLoadingSequence(mv, args); mv.visitMethodInsn(INVOKESPECIAL, className, newName, desc); mv.visitVarInsn(ASTORE, stackFrame.getValObjLocalStackPos()); // val mv.visitVarInsn(ALOAD, stackFrame.getCacheRefLocalStackPos()); // cache mv.visitVarInsn(ALOAD, stackFrame.getKeyGenLocalStackPos()); // key mv.visitVarInsn(ALOAD, stackFrame.getValObjLocalStackPos()); // val if (expirationTime == -1) { mv.visitMethodInsn(INVOKEINTERFACE, "cacheonix/cache/Cache", "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); } else { mv.visitLdcInsn(Long.valueOf(expirationTime)); mv.visitMethodInsn(INVOKEINTERFACE, "cacheonix/cache/Cache", "put", "(Ljava/lang/Object;Ljava/lang/Object;J)Ljava/lang/Object;"); } mv.visitInsn(POP); mv.visitLabel(l4); mv.visitVarInsn(ALOAD, stackFrame.getValObjLocalStackPos()); // val // Return type final String retType = Type.getReturnType(desc).getInternalName(); mv.visitTypeInsn(CHECKCAST, retType); mv.visitInsn(ARETURN); mv.visitMaxs(6, 10); mv.visitEnd(); }
From source file:org.cacheonix.impl.transformer.CacheonixMethodGenerator.java
License:LGPL
/** * Generates a new method body for implementing CacheInvalidate with the old name to look into the Cacheonix cache * first before calling the original method * * @param cv ClassVisitor that this class delegates the calls to * @param className Name of the class for which the method is being generated * @param access Method level access * @param desc Method descriptor * @param signature Method signature * @param exceptions Any exceptions that the method can throw * @param name original name of the method * @param newName the original method renamed to the format:orig$Cacheonix$methodName * @param metaData Annotation information for the method * @param cacheonixCacheFieldValue cacheName specified at the class level *//* ww w . j a v a 2 s . co m*/ public static void generateCacheRemoveBody(final ClassVisitor cv, final String className, final int access, final String desc, final String signature, final String[] exceptions, final String name, final String newName, final MethodMetaData metaData, final String cacheonixCacheFieldValue) { final Type[] args = Type.getArgumentTypes(desc); final LocalStackUtil stackFrame = new LocalStackUtil(args); // Start final MethodVisitor mv = cv.visitMethod(access, name, desc, signature, exceptions); mv.visitCode(); final Label l0 = new Label(); final Label l1 = new Label(); final Label l2 = new Label(); mv.visitTryCatchBlock(l0, l1, l2, "java/lang/Exception"); final Label l3 = new Label(); final Label l4 = new Label(); final Label l5 = new Label(); mv.visitTryCatchBlock(l3, l4, l5, "java/lang/Exception"); final String classCan = 'L' + className + ';'; mv.visitLdcInsn(Type.getType(classCan)); mv.visitMethodInsn(INVOKESTATIC, "org/cacheonix/impl/util/logging/Logger", "getLogger", "(Ljava/lang/Class;)Lorg/cacheonix/impl/util/logging/Logger;"); mv.visitVarInsn(ASTORE, stackFrame.getCILogLocalStackPos()); // Log mv.visitInsn(ACONST_NULL); mv.visitVarInsn(ASTORE, stackFrame.getCICacheManagerLocalStackPos()); // inst // try mv.visitLabel(l0); mv.visitFieldInsn(GETSTATIC, className, CacheonixClassAdapter.CACHEONIX_CONFIG_FILE_FIELD, "Ljava/lang/String;"); mv.visitMethodInsn(INVOKESTATIC, "cacheonix/cache/CacheManager", "getInstance", "(Ljava/lang/String;)Lcacheonix/cache/CacheManager;"); mv.visitVarInsn(ASTORE, stackFrame.getCICacheManagerLocalStackPos()); // inst // catch in mv.visitLabel(l1); mv.visitJumpInsn(GOTO, l3); // catch out mv.visitLabel(l2); mv.visitVarInsn(ASTORE, stackFrame.getCIExceptionLocalStackPos()); // Exception1 mv.visitInsn(ACONST_NULL); mv.visitVarInsn(ASTORE, stackFrame.getCIExceptionLocalStackPos()); // inst <- null mv.visitVarInsn(ALOAD, stackFrame.getCILogLocalStackPos()); // Log mv.visitLdcInsn(">>>>> Exception getting CacheManager "); mv.visitVarInsn(ALOAD, stackFrame.getCIExceptionLocalStackPos()); // Exception mv.visitMethodInsn(INVOKEVIRTUAL, "org/cacheonix/impl/util/logging/Logger", "e", "(Ljava/lang/Object;Ljava/lang/Throwable;)V"); // END OF TRY CACTCH // try mv.visitLabel(l3); // printingToSysout(mv, "!!!!! INVALIDATE | INST is NOT NULL !!!!!! '" + name + "' is Called"); mv.visitVarInsn(ALOAD, stackFrame.getCICacheManagerLocalStackPos()); // inst final Label l6 = new Label(); mv.visitJumpInsn(IFNULL, l6); // Key Loop generateKeyAggregationSequence(mv, args, stackFrame, cacheonixCacheFieldValue, metaData.getMethodParamAnnotationInfo()); mv.visitVarInsn(ALOAD, stackFrame.getCICacheManagerLocalStackPos()); // inst mv.visitFieldInsn(GETSTATIC, className, CacheonixClassAdapter.CACHE_NAME_FIELD, "Ljava/lang/String;"); mv.visitMethodInsn(INVOKEVIRTUAL, "cacheonix/cache/CacheManager", "getCache", "(Ljava/lang/String;)Lcacheonix/cache/Cache;"); mv.visitVarInsn(ASTORE, stackFrame.getCICacheRefLocalStackPos()); // cache mv.visitVarInsn(ALOAD, stackFrame.getCICacheRefLocalStackPos()); // cache mv.visitJumpInsn(IFNULL, l6); mv.visitVarInsn(ALOAD, stackFrame.getCICacheRefLocalStackPos()); // cache mv.visitVarInsn(ALOAD, stackFrame.getCIKeyGenLocalStackPos()); // Key mv.visitMethodInsn(INVOKEINTERFACE, "cacheonix/cache/Cache", "remove", "(Ljava/lang/Object;)Ljava/lang/Object;"); mv.visitInsn(POP); // Ignore result mv.visitLabel(l4); mv.visitJumpInsn(GOTO, l6); mv.visitLabel(l5); mv.visitVarInsn(ASTORE, stackFrame.getCIEExceptionLocalStackPos()); // EException mv.visitVarInsn(ALOAD, stackFrame.getCILogLocalStackPos()); // Log mv.visitLdcInsn(">>>>> Exception while removing key "); mv.visitVarInsn(ALOAD, stackFrame.getCIEExceptionLocalStackPos()); // EException mv.visitMethodInsn(INVOKEVIRTUAL, "org/cacheonix/impl/util/logging/Logger", "e", "(Ljava/lang/Object;Ljava/lang/Throwable;)V"); mv.visitLabel(l6); // generateMethodParameterLoadingSequence(mv, args); mv.visitMethodInsn(INVOKESPECIAL, className, newName, desc); // final int op = ByteInstruction.getReturnCode(desc); mv.visitInsn(op); mv.visitMaxs(6, 9); mv.visitEnd(); }
From source file:org.codehaus.aspectwerkz.transform.inlining.compiler.AbstractJoinPointCompiler.java
License:Open Source License
/** * Creates the static initialization method (not clinit) for the join point. *//* w w w.j a va2 s . c om*/ private void createStaticInitializer() { MethodVisitor cv = m_cw.visitMethod(ACC_STATIC | ACC_PUBLIC, STATIC_INITIALIZATION_METHOD_NAME, NO_PARAM_RETURN_VOID_SIGNATURE, null, null); Label tryLabel = new Label(); cv.visitLabel(tryLabel); cv.visitLdcInsn(m_calleeClassName.replace('/', '.')); cv.visitMethodInsn(INVOKESTATIC, CLASS_CLASS, FOR_NAME_METHOD_NAME, FOR_NAME_METHOD_SIGNATURE); cv.visitFieldInsn(PUTSTATIC, m_joinPointClassName, TARGET_CLASS_FIELD_NAME_IN_JP, CLASS_CLASS_SIGNATURE); cv.visitLdcInsn(m_callerClassName.replace('/', '.')); cv.visitMethodInsn(INVOKESTATIC, CLASS_CLASS, FOR_NAME_METHOD_NAME, FOR_NAME_METHOD_SIGNATURE); cv.visitFieldInsn(PUTSTATIC, m_joinPointClassName, THIS_CLASS_FIELD_NAME_IN_JP, CLASS_CLASS_SIGNATURE); Label finallyLabel = new Label(); cv.visitLabel(finallyLabel); Label gotoFinallyLabel = new Label(); cv.visitJumpInsn(GOTO, gotoFinallyLabel); Label catchLabel = new Label(); cv.visitLabel(catchLabel); cv.visitVarInsn(ASTORE, 0); cv.visitVarInsn(ALOAD, 0); cv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Throwable", "printStackTrace", "()V"); cv.visitTypeInsn(NEW, RUNTIME_EXCEPTION_CLASS_NAME); cv.visitInsn(DUP); cv.visitLdcInsn("could not load target class using Class.forName() in generated join point base class " + m_joinPointClassName); cv.visitMethodInsn(INVOKESPECIAL, RUNTIME_EXCEPTION_CLASS_NAME, INIT_METHOD_NAME, RUNTIME_EXCEPTION_INIT_METHOD_SIGNATURE); cv.visitInsn(ATHROW); cv.visitLabel(gotoFinallyLabel); // create the enclosing static joinpoint createEnclosingStaticJoinPoint(cv); // create the metadata map cv.visitTypeInsn(NEW, HASH_MAP_CLASS_NAME); cv.visitInsn(DUP); cv.visitMethodInsn(INVOKESPECIAL, HASH_MAP_CLASS_NAME, INIT_METHOD_NAME, NO_PARAM_RETURN_VOID_SIGNATURE); cv.visitFieldInsn(PUTSTATIC, m_joinPointClassName, META_DATA_FIELD_NAME, MAP_CLASS_SIGNATURE); // create the Signature instance createSignature(cv); // create the static JoinPoint instance cv.visitTypeInsn(NEW, m_joinPointClassName); cv.visitInsn(DUP); cv.visitMethodInsn(INVOKESPECIAL, m_joinPointClassName, INIT_METHOD_NAME, NO_PARAM_RETURN_VOID_SIGNATURE); cv.visitFieldInsn(PUTSTATIC, m_joinPointClassName, OPTIMIZED_JOIN_POINT_INSTANCE_FIELD_NAME, L + m_joinPointClassName + SEMICOLON); // ensure aspect factories are all loaded for (int i = 0; i < m_aspectInfos.length; i++) { AspectInfo m_aspectInfo = m_aspectInfos[i]; cv.visitLdcInsn(m_aspectInfo.getAspectFactoryClassName()); cv.visitLdcInsn(m_aspectInfo.getAspectDefinition().getSystemDefinition().getUuid()); cv.visitLdcInsn(m_aspectInfo.getAspectClassName()); cv.visitLdcInsn(m_aspectInfo.getAspectQualifiedName()); AsmHelper.loadStringConstant(cv, m_aspectInfo.getAspectDefinition().getContainerClassName()); //TODO AVF do it once per aspect def StringBuffer sb = new StringBuffer(); boolean hasOne = false; boolean isFirst = true; for (Iterator iterator = m_aspectInfo.getAspectDefinition().getParameters().entrySet() .iterator(); iterator.hasNext();) { Map.Entry entry = (Map.Entry) iterator.next(); if (!isFirst) { sb.append(DELIMITER); } isFirst = false; hasOne = true; sb.append(entry.getKey()).append(DELIMITER).append(entry.getValue()); } if (hasOne) { cv.visitLdcInsn(sb.toString()); } else { cv.visitInsn(ACONST_NULL); } cv.visitFieldInsn(GETSTATIC, m_joinPointClassName, THIS_CLASS_FIELD_NAME_IN_JP, CLASS_CLASS_SIGNATURE); cv.visitMethodInsn(INVOKEVIRTUAL, CLASS_CLASS, GETCLASSLOADER_METHOD_NAME, CLASS_CLASS_GETCLASSLOADER_METHOD_SIGNATURE); cv.visitLdcInsn(m_aspectInfo.getDeploymentModel().toString()); cv.visitMethodInsn(INVOKESTATIC, Type.getInternalName(AspectFactoryManager.class), "loadAspectFactory", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/ClassLoader;Ljava/lang/String;)V"); } // create and initialize the aspect fields for (int i = 0; i < m_aspectInfos.length; i++) { m_aspectInfos[i].getAspectModel().createAndStoreStaticAspectInstantiation(m_cw, cv, m_aspectInfos[i], m_joinPointClassName); } cv.visitInsn(RETURN); cv.visitTryCatchBlock(tryLabel, finallyLabel, catchLabel, CLASS_NOT_FOUND_EXCEPTION_CLASS_NAME); cv.visitMaxs(0, 0); }
From source file:org.codehaus.aspectwerkz.transform.inlining.compiler.AbstractJoinPointCompiler.java
License:Open Source License
/** * @param cv// w w w . j a va 2 s .c om * @param input */ private void createPartOfInvokeMethodWithAllAdviceTypes(final MethodVisitor cv, final CompilerInput input) { final int returnValueIndex = (input.joinPointInstanceIndex != INDEX_NOTAVAILABLE) ? (input.joinPointInstanceIndex + 1) : input.callerIndex + 1; final int exceptionIndex1 = returnValueIndex + 1; final int exceptionIndex2 = returnValueIndex + 2; cv.visitInsn(ACONST_NULL); cv.visitVarInsn(ASTORE, returnValueIndex); Label tryLabel = new Label(); cv.visitLabel(tryLabel); if (!m_requiresProceedMethod) { // if no around advice then optimize by invoking the target JP directly and no call to proceed() createInlinedJoinPointInvocation(cv, input); int stackIndex = returnValueIndex;//use another int since storeType will update it AsmHelper.storeType(cv, stackIndex, m_returnType); addReturnedValueToJoinPoint(cv, input, returnValueIndex, false); } else { createInvocationToProceedMethod(cv, input.joinPointInstanceIndex, returnValueIndex); } createAfterReturningAdviceInvocations(cv, input); Label finallyLabel1 = new Label(); cv.visitLabel(finallyLabel1); if (m_isThisAdvisable) { final int registerDepth = input.callerIndex + 2; // caller is using last register + possible return value createAfterInterceptorInvocations(cv, input.joinPointInstanceIndex, registerDepth); } createAfterFinallyAdviceInvocations(cv, input); Label gotoFinallyLabel = new Label(); cv.visitJumpInsn(GOTO, gotoFinallyLabel); Label catchLabel = new Label(); cv.visitLabel(catchLabel); // store the exception cv.visitVarInsn(ASTORE, exceptionIndex1); if (m_isThisAdvisable) { createAfterThrowingInterceptorInvocations(cv, input.joinPointInstanceIndex, exceptionIndex1); } // loop over the after throwing advices for (int i = m_afterThrowingAdviceMethodInfos.length - 1; i >= 0; i--) { AdviceMethodInfo advice = m_afterThrowingAdviceMethodInfos[i]; // set the exception argument index advice.setSpecialArgumentIndex(exceptionIndex1); // if (e instanceof TYPE) {...} cv.visitVarInsn(ALOAD, exceptionIndex1); final String specialArgTypeName = advice.getSpecialArgumentTypeName(); if (specialArgTypeName != null) { // after throwing <TYPE> cv.visitTypeInsn(INSTANCEOF, specialArgTypeName); Label ifInstanceOfLabel = new Label(); cv.visitJumpInsn(IFEQ, ifInstanceOfLabel); // after throwing advice invocation createAfterAdviceInvocation(cv, input, advice, exceptionIndex1); cv.visitLabel(ifInstanceOfLabel); } else { // after throwing createAfterAdviceInvocation(cv, input, advice, INDEX_NOTAVAILABLE); } } // rethrow exception cv.visitVarInsn(ALOAD, exceptionIndex1); cv.visitInsn(ATHROW); // store exception Label exceptionLabel = new Label(); cv.visitLabel(exceptionLabel); cv.visitVarInsn(ASTORE, exceptionIndex2); // after finally advice invocation Label finallyLabel2 = new Label(); cv.visitLabel(finallyLabel2); if (m_isThisAdvisable) { final int registerDepth = input.callerIndex + 2; // caller is using last register + possible return value createAfterInterceptorInvocations(cv, input.joinPointInstanceIndex, registerDepth); } createAfterFinallyAdviceInvocations(cv, input); // rethrow exception cv.visitVarInsn(ALOAD, exceptionIndex2); cv.visitInsn(ATHROW); cv.visitLabel(gotoFinallyLabel); // unwrap if around advice and return in all cases if (m_returnType.getSort() != Type.VOID) { if (m_requiresProceedMethod) { cv.visitVarInsn(ALOAD, returnValueIndex); AsmHelper.unwrapType(cv, m_returnType); } else { AsmHelper.loadType(cv, returnValueIndex, m_returnType); } } AsmHelper.addReturnStatement(cv, m_returnType); // build up the exception table cv.visitTryCatchBlock(tryLabel, finallyLabel1, catchLabel, THROWABLE_CLASS_NAME); cv.visitTryCatchBlock(tryLabel, finallyLabel1, exceptionLabel, null); cv.visitTryCatchBlock(catchLabel, finallyLabel2, exceptionLabel, null); }