List of usage examples for org.objectweb.asm Opcodes RETURN
int RETURN
To view the source code for org.objectweb.asm Opcodes RETURN.
Click Source Link
From source file:pt.minha.kernel.instrument.SyncToMonitorClassVisitor.java
License:Open Source License
public void makeStub(int access, String name, String desc, String signature, String[] exceptions) { Method m = new Method(name, desc); MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions); mv.visitCode();//from w w w . j a v a 2s .co m Label begin = new Label(); Label pre_invoke = new Label(); Label pos_leave = new Label(); Label in_catch = new Label(); Label pre_rethrow = new Label(); Label end = new Label(); mv.visitTryCatchBlock(pre_invoke, pos_leave, in_catch, null); mv.visitTryCatchBlock(in_catch, pre_rethrow, in_catch, null); mv.visitLabel(begin); int offset; if ((access & Opcodes.ACC_STATIC) == 0) { mv.visitVarInsn(Opcodes.ALOAD, 0); offset = 1; } else { mv.visitFieldInsn(Opcodes.GETSTATIC, clz, "_fake_class", "L" + ClassConfig.fake_prefix + "java/lang/Object;"); offset = 0; } int length = 0; for (Type t : m.getArgumentTypes()) length += t.getSize(); mv.visitInsn(Opcodes.DUP); mv.visitVarInsn(Opcodes.ASTORE, offset + length); mv.visitInsn(Opcodes.MONITORENTER); mv.visitLabel(pre_invoke); if ((access & Opcodes.ACC_STATIC) == 0) mv.visitVarInsn(Opcodes.ALOAD, 0); int i = offset; for (Type t : m.getArgumentTypes()) { // t.getOpcode() should work for long and double too... :-( if (t.getClassName().equals("long")) mv.visitVarInsn(Opcodes.LLOAD, i); else if (t.getClassName().equals("double")) mv.visitVarInsn(Opcodes.DLOAD, i); else mv.visitVarInsn(t.getOpcode(Opcodes.ILOAD), i); i += t.getSize(); } boolean itf = (access & Opcodes.ACC_INTERFACE) != 0; if ((access & Opcodes.ACC_STATIC) == 0) mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, clz, "_" + name, desc, itf); else mv.visitMethodInsn(Opcodes.INVOKESTATIC, clz, "_" + name, desc, itf); mv.visitVarInsn(Opcodes.ALOAD, offset + length); mv.visitInsn(Opcodes.MONITOREXIT); mv.visitLabel(pos_leave); if (m.getReturnType().equals(Type.VOID_TYPE)) mv.visitInsn(Opcodes.RETURN); else mv.visitInsn(m.getReturnType().getOpcode(Opcodes.IRETURN)); mv.visitLabel(in_catch); mv.visitVarInsn(Opcodes.ALOAD, offset + length); mv.visitInsn(Opcodes.MONITOREXIT); mv.visitLabel(pre_rethrow); mv.visitInsn(Opcodes.ATHROW); mv.visitLabel(end); i = 0; if ((access & Opcodes.ACC_STATIC) == 0) mv.visitLocalVariable("this", "L" + clz + ";", null, begin, end, i++); for (Type t : m.getArgumentTypes()) mv.visitLocalVariable("arg" + i, t.getDescriptor(), null, begin, end, i++); mv.visitMaxs(0, 0); mv.visitEnd(); }
From source file:scouter.agent.asm.HttpServiceASM.java
License:Apache License
@Override public void visitCode() { mv.visitVarInsn(Opcodes.ALOAD, 1);// w w w . j ava 2 s .c om mv.visitVarInsn(Opcodes.ALOAD, 2); if (httpservlet) { mv.visitMethodInsn(Opcodes.INVOKESTATIC, TRACEMAIN, START_SERVICE, START_SIGNATURE, false); } else { mv.visitMethodInsn(Opcodes.INVOKESTATIC, TRACEMAIN, START_FILTER, START_SIGNATURE, false); } statIdx = newLocal(Type.getType(Object.class)); mv.visitVarInsn(Opcodes.ASTORE, statIdx); mv.visitLabel(startFinally); mv.visitVarInsn(Opcodes.ALOAD, statIdx); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitVarInsn(Opcodes.ALOAD, 2); mv.visitMethodInsn(Opcodes.INVOKESTATIC, TRACEMAIN, REJECT, REJECT_SIGNATURE, false); Label end = new Label(); mv.visitJumpInsn(IFNULL, end); mv.visitInsn(Opcodes.RETURN); mv.visitLabel(end); mv.visitCode(); }
From source file:serianalyzer.JVMImpl.java
License:Open Source License
/** * @param opcode//ww w . j av a 2 s. co m * @param s */ static void handleJVMInsn(int opcode, JVMStackState s) { BaseType o1; BaseType o2; BaseType o3; List<BaseType> l1; List<BaseType> l2; switch (opcode) { case Opcodes.NOP: break; case Opcodes.ARRAYLENGTH: o1 = s.pop(); s.push(new BasicConstant(Type.INT_TYPE, 0, !(o1 != null && !o1.isTainted()))); break; case Opcodes.ACONST_NULL: s.push(new BasicConstant(Type.VOID_TYPE, "<null>")); //$NON-NLS-1$ break; case Opcodes.ICONST_M1: case Opcodes.ICONST_0: case Opcodes.ICONST_1: case Opcodes.ICONST_2: case Opcodes.ICONST_3: case Opcodes.ICONST_4: case Opcodes.ICONST_5: s.push(new BasicConstant(Type.INT_TYPE, opcode - 3)); break; case Opcodes.LCONST_0: case Opcodes.LCONST_1: s.push(new BasicConstant(Type.LONG_TYPE, opcode - 9L)); break; case Opcodes.FCONST_0: case Opcodes.FCONST_1: case Opcodes.FCONST_2: s.push(new BasicConstant(Type.FLOAT_TYPE, opcode - 11f)); break; case Opcodes.DCONST_0: case Opcodes.DCONST_1: s.push(new BasicConstant(Type.DOUBLE_TYPE, opcode - 14d)); break; case Opcodes.IALOAD: case Opcodes.LALOAD: case Opcodes.FALOAD: case Opcodes.DALOAD: case Opcodes.BALOAD: case Opcodes.CALOAD: case Opcodes.SALOAD: o1 = s.pop(); o2 = s.pop(); s.push(new BasicVariable(toType(opcode), "primitive array elem", //$NON-NLS-1$ (o1 == null || o1.isTainted()) | (o2 == null || o2.isTainted()))); break; case Opcodes.AALOAD: o1 = s.pop(); o2 = s.pop(); if (o1 != null && o2 instanceof SimpleType && ((SimpleType) o2).getType().toString().startsWith("[")) { //$NON-NLS-1$ Type atype = Type.getType(((SimpleType) o2).getType().toString().substring(1)); if (o2.getAlternativeTypes() != null && !o2.getAlternativeTypes().isEmpty()) { s.clear(); break; } s.push(new BasicVariable(atype, "array elem " + atype, o1.isTainted() | o2.isTainted())); //$NON-NLS-1$ } else { s.clear(); } break; case Opcodes.IASTORE: case Opcodes.LASTORE: case Opcodes.FASTORE: case Opcodes.DASTORE: case Opcodes.AASTORE: case Opcodes.BASTORE: case Opcodes.CASTORE: case Opcodes.SASTORE: s.pop(3); break; case Opcodes.POP2: s.pop(); case Opcodes.MONITORENTER: case Opcodes.MONITOREXIT: case Opcodes.POP: s.pop(); break; case Opcodes.DUP: if (!s.isEmpty()) { o1 = s.pop(); s.push(o1); s.push(o1); } break; case Opcodes.DUP_X1: o1 = s.pop(); o2 = s.pop(); s.push(o1); s.push(o2); s.push(o1); break; case Opcodes.DUP_X2: o1 = s.pop(); o2 = s.pop(); o3 = s.pop(); s.push(o1); s.push(o3); s.push(o2); s.push(o1); break; case Opcodes.DUP2: l1 = s.popWord(); if (l1.isEmpty()) { log.trace("DUP2 with unknown operand"); //$NON-NLS-1$ s.clear(); } else { s.pushWord(l1); s.pushWord(l1); } break; case Opcodes.DUP2_X1: l1 = s.popWord(); o1 = s.pop(); if (l1.isEmpty()) { log.trace("DUP2 with unknown operand"); //$NON-NLS-1$ s.clear(); } else { s.pushWord(l1); s.push(o1); s.pushWord(l1); } break; case Opcodes.DUP2_X2: l1 = s.popWord(); l2 = s.popWord(); if (l1.isEmpty() || l2.isEmpty()) { log.trace("DUP2 with unknown operand"); //$NON-NLS-1$ s.clear(); } else { s.pushWord(l1); s.pushWord(l2); s.pushWord(l1); } break; case Opcodes.SWAP: o1 = s.pop(); o2 = s.pop(); s.push(o1); s.push(o2); break; case Opcodes.IADD: case Opcodes.LADD: case Opcodes.FADD: case Opcodes.DADD: case Opcodes.ISUB: case Opcodes.LSUB: case Opcodes.FSUB: case Opcodes.DSUB: case Opcodes.IMUL: case Opcodes.LMUL: case Opcodes.FMUL: case Opcodes.DMUL: case Opcodes.IDIV: case Opcodes.LDIV: case Opcodes.FDIV: case Opcodes.DDIV: case Opcodes.IREM: case Opcodes.LREM: case Opcodes.FREM: case Opcodes.DREM: case Opcodes.IAND: case Opcodes.LAND: case Opcodes.IOR: case Opcodes.LOR: case Opcodes.IXOR: case Opcodes.LXOR: case Opcodes.LCMP: case Opcodes.FCMPL: case Opcodes.FCMPG: case Opcodes.DCMPL: case Opcodes.DCMPG: s.merge(2); break; case Opcodes.ISHL: case Opcodes.LSHL: case Opcodes.ISHR: case Opcodes.LSHR: case Opcodes.IUSHR: case Opcodes.LUSHR: s.pop(); // amount // ignore value break; case Opcodes.INEG: case Opcodes.F2I: case Opcodes.D2I: case Opcodes.L2I: s.push(cast(s.pop(), Type.INT_TYPE)); break; case Opcodes.LNEG: case Opcodes.I2L: case Opcodes.F2L: case Opcodes.D2L: s.push(cast(s.pop(), Type.LONG_TYPE)); break; case Opcodes.FNEG: case Opcodes.I2F: case Opcodes.L2F: case Opcodes.D2F: s.push(cast(s.pop(), Type.FLOAT_TYPE)); case Opcodes.DNEG: case Opcodes.I2D: case Opcodes.L2D: case Opcodes.F2D: s.push(cast(s.pop(), Type.DOUBLE_TYPE)); case Opcodes.I2B: s.push(cast(s.pop(), Type.BYTE_TYPE)); break; case Opcodes.I2C: s.push(cast(s.pop(), Type.CHAR_TYPE)); break; case Opcodes.I2S: s.push(cast(s.pop(), Type.SHORT_TYPE)); break; case Opcodes.ARETURN: s.clear(); break; case Opcodes.IRETURN: case Opcodes.LRETURN: case Opcodes.FRETURN: case Opcodes.DRETURN: case Opcodes.RETURN: if (log.isTraceEnabled()) { log.trace("Found return " + s.pop()); //$NON-NLS-1$ } s.clear(); break; case Opcodes.ATHROW: Object thrw = s.pop(); log.trace("Found throw " + thrw); //$NON-NLS-1$ s.clear(); break; default: log.warn("Unsupported instruction code " + opcode); //$NON-NLS-1$ } }
From source file:serianalyzer.SerianalyzerMethodVisitor.java
License:Open Source License
/** * {@inheritDoc}/* w w w . j a va 2s. com*/ * * @see org.objectweb.asm.MethodVisitor#visitInsn(int) */ @Override public void visitInsn(int opcode) { switch (opcode) { case Opcodes.ARETURN: Object ret = this.stack.pop(); Type sigType = Type.getReturnType(this.ref.getSignature()); Type retType = null; Set<Type> altTypes = null; if (ret != null) { if (ret instanceof SimpleType) { retType = ((SimpleType) ret).getType(); altTypes = ((SimpleType) ret).getAlternativeTypes(); } else if (ret instanceof MultiAlternatives) { retType = ((MultiAlternatives) ret).getCommonType(); } } if (retType != null) { this.returnTypes.add(retType); if (altTypes != null) { this.returnTypes.addAll(altTypes); } } else { this.returnTypes.add(sigType); } this.stack.clear(); break; case Opcodes.IRETURN: case Opcodes.LRETURN: case Opcodes.FRETURN: case Opcodes.DRETURN: case Opcodes.RETURN: if (this.log.isTraceEnabled()) { this.log.trace("Found return " + this.stack.pop()); //$NON-NLS-1$ } this.stack.clear(); break; case Opcodes.ATHROW: Object thrw = this.stack.pop(); this.log.trace("Found throw " + thrw); //$NON-NLS-1$ this.stack.clear(); break; default: JVMImpl.handleJVMInsn(opcode, this.stack); } super.visitInsn(opcode); }
From source file:sg.atom.core.actor.internal.codegenerator.ActorProxyCreator.java
License:Apache License
/** * Create or get a MessageCaller implementation for the given method. * * @param ownerClass the class that owns the message * @param method the method to invoke/* w ww . j a v a 2 s . co m*/ * @return the message caller * @throws NoSuchMethodException * @throws SecurityException */ @SuppressWarnings("unchecked") public static Class<MessageCaller<?>> createMessageCaller(Class<?> ownerClass, Method method) throws SecurityException, NoSuchMethodException { String className = String.format("%s_%s_%d__MESSAGECALLER", ownerClass.getName(), method.getName(), getMethodNumber(method)); String classNameInternal = className.replace('.', '/'); java.lang.reflect.Type fullReturnType = method.getGenericReturnType(); if ((!(fullReturnType instanceof ParameterizedType)) && AsyncResult.class .isAssignableFrom(((Class) ((ParameterizedType) fullReturnType).getRawType()))) { throw new RuntimeException("Something's wrong here: should not be called for such a method"); } String returnSignature = GenericTypeHelper .getSignature(((ParameterizedType) fullReturnType).getActualTypeArguments()[0]); 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, "L" + classNameInternal + "<" + returnSignature + ">;", "org/actorsguildframework/internal/MessageCaller", null); 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, "org/actorsguildframework/internal/MessageCaller", "<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, "invoke", "(Lorg/actorsguildframework/Actor;[Ljava/lang/Object;)Lorg/actorsguildframework/AsyncResult;", "(Lorg/actorsguildframework/Actor;[Ljava/lang/Object;)Lorg/actorsguildframework/AsyncResult<" + returnSignature + ">;", null); mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitTypeInsn(Opcodes.CHECKCAST, Type.getInternalName(method.getDeclaringClass()) + "__ACTORPROXY"); int idx = 0; for (Class<?> t : method.getParameterTypes()) { mv.visitVarInsn(Opcodes.ALOAD, 2); mv.visitIntInsn(Opcodes.BIPUSH, idx); mv.visitInsn(Opcodes.AALOAD); if (t.isPrimitive()) { String wrapperDescr = GenerationUtils.getWrapperInternalName(t); mv.visitTypeInsn(Opcodes.CHECKCAST, wrapperDescr); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, wrapperDescr, t.getName() + "Value", "()" + Type.getDescriptor(t)); } else { if (isArgumentFreezingRequired(method, idx, t)) { mv.visitTypeInsn(Opcodes.CHECKCAST, Type.getInternalName(SerializableFreezer.class)); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(SerializableFreezer.class), "get", Type.getMethodDescriptor(SerializableFreezer.class.getMethod("get"))); } if (!t.equals(Object.class)) { mv.visitTypeInsn(Opcodes.CHECKCAST, Type.getInternalName(t)); } } idx++; } mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(method.getDeclaringClass()) + "__ACTORPROXY", String.format(SUPER_CALLER_NAME_FORMAT, method.getName()), Type.getMethodDescriptor(method)); mv.visitInsn(Opcodes.ARETURN); Label l2 = new Label(); mv.visitLabel(l2); mv.visitLocalVariable("this", "L" + classNameInternal + ";", null, l0, l2, 0); mv.visitLocalVariable("instance", "Lorg/actorsguildframework/Actor;", null, l0, l2, 1); mv.visitLocalVariable("arguments", "[Ljava/lang/Object;", null, l0, l2, 2); mv.visitMaxs(0, 0); mv.visitEnd(); } { mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "getMessageName", "()Ljava/lang/String;", null, null); mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitLdcInsn(method.getName()); mv.visitInsn(Opcodes.ARETURN); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", "L" + classNameInternal + ";", null, l0, l1, 0); mv.visitMaxs(0, 0); mv.visitEnd(); } cw.visitEnd(); return (Class<MessageCaller<?>>) GenerationUtils.loadClass(className, cw.toByteArray()); }
From source file:sg.atom.core.actor.internal.codegenerator.ActorProxyCreator.java
License:Apache License
/** * Creates and loads the actor's proxy class. * * @param actorClass the Actor class/*from w w w. j av a 2s . c om*/ * @param acd the actor's class descriptor * @throws ConfigurationException if the agent is not configured correctly */ @SuppressWarnings("unchecked") private static Class<?> generateProxyClass(Class<?> actorClass, final ActorClassDescriptor acd) throws NoSuchMethodException { BeanClassDescriptor bcd = acd.getBeanClassDescriptor(); String className = String.format("%s__ACTORPROXY", actorClass.getName()); final String classNameInternal = className.replace('.', '/'); String classNameDescriptor = "L" + classNameInternal + ";"; final Type actorState = Type .getType(acd.getConcurrencyModel().isMultiThreadingCapable() ? MultiThreadedActorState.class : SingleThreadedActorState.class); 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, Type.getInternalName(actorClass), new String[] { "org/actorsguildframework/internal/ActorProxy" }); cw.visitSource(null, null); { for (int i = 0; i < acd.getMessageCount(); i++) { cw.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL + Opcodes.ACC_STATIC, String.format(MESSAGE_CALLER_NAME_FORMAT, i), "Lorg/actorsguildframework/internal/MessageCaller;", "Lorg/actorsguildframework/internal/MessageCaller<*>;", null).visitEnd(); } cw.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL, "actorState__ACTORPROXY", actorState.getDescriptor(), null, null).visitEnd(); } BeanCreator.writePropFields(bcd, cw); { mv = cw.visitMethod(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null); mv.visitCode(); for (int i = 0; i < acd.getMessageCount(); i++) { Class<?> caller = createMessageCaller(acd.getMessage(i).getOwnerClass(), acd.getMessage(i).getMethod()); String mcName = Type.getInternalName(caller); mv.visitTypeInsn(Opcodes.NEW, mcName); mv.visitInsn(Opcodes.DUP); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, mcName, "<init>", "()V"); mv.visitFieldInsn(Opcodes.PUTSTATIC, classNameInternal, String.format(MESSAGE_CALLER_NAME_FORMAT, i), "Lorg/actorsguildframework/internal/MessageCaller;"); } mv.visitInsn(Opcodes.RETURN); mv.visitMaxs(0, 0); mv.visitEnd(); } BeanCreator.writeConstructor(actorClass, bcd, classNameInternal, cw, new BeanCreator.SnippetWriter() { @Override public void write(MethodVisitor mv) { mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitTypeInsn(Opcodes.NEW, actorState.getInternalName()); mv.visitInsn(Opcodes.DUP); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, actorState.getInternalName(), "<init>", "(Lorg/actorsguildframework/internal/Controller;Lorg/actorsguildframework/Actor;)V"); mv.visitFieldInsn(Opcodes.PUTFIELD, classNameInternal, "actorState__ACTORPROXY", actorState.getDescriptor()); } }); BeanCreator.writePropAccessors(bcd, classNameInternal, cw); { mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "getState__ACTORPROXYMETHOD", "()Lorg/actorsguildframework/internal/ActorState;", null, null); mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, classNameInternal, "actorState__ACTORPROXY", actorState.getDescriptor()); mv.visitInsn(Opcodes.ARETURN); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", classNameDescriptor, null, l0, l1, 0); mv.visitMaxs(0, 0); mv.visitEnd(); } for (int i = 0; i < acd.getMessageCount(); i++) { MessageImplDescriptor mid = acd.getMessage(i); Method method = mid.getMethod(); String simpleDescriptor = Type.getMethodDescriptor(method); String genericSignature = GenericTypeHelper.getSignature(method); writeProxyMethod(classNameInternal, classNameDescriptor, cw, i, actorState, acd.getConcurrencyModel(), mid, method, simpleDescriptor, genericSignature); writeSuperProxyMethod(actorClass, classNameDescriptor, cw, method, simpleDescriptor, genericSignature, !acd.getConcurrencyModel().isMultiThreadingCapable()); } { mv = cw.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_SYNCHRONIZED, "toString", "()Ljava/lang/String;", null, null); mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "toString", "()Ljava/lang/String;"); mv.visitInsn(Opcodes.ARETURN); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", classNameDescriptor, null, l0, l1, 0); mv.visitMaxs(0, 0); mv.visitEnd(); } cw.visitEnd(); try { return (Class<? extends ActorProxy>) GenerationUtils.loadClass(className, cw.toByteArray()); } catch (Exception e) { throw new ConfigurationException("Failure loading ActorProxy", e); } }
From source file:sg.atom.core.actor.internal.codegenerator.BeanCreator.java
License:Apache License
/** * Writes the accessor methods for//from w w w . j av a2s . c o m * * @Prop generated properties. * @param bcd the class descriptor * @param classNameInternal the internal name of this class * @param cw the ClassWriter to write to */ public static void writePropAccessors(BeanClassDescriptor bcd, String classNameInternal, ClassWriter cw) { String classNameDescriptor = "L" + classNameInternal + ";"; MethodVisitor mv; for (int i = 0; i < bcd.getPropertyCount(); i++) { PropertyDescriptor pd = bcd.getProperty(i); if (!pd.getPropertySource().isGenerating()) { continue; } { Type t = Type.getType(pd.getPropertyClass()); Method orig = pd.getGetter(); mv = cw.visitMethod( GenerationUtils.convertAccessModifiers(orig.getModifiers()) + (bcd.isThreadSafe() ? Opcodes.ACC_SYNCHRONIZED : 0), orig.getName(), Type.getMethodDescriptor(orig), GenericTypeHelper.getSignature(orig), null); mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, classNameInternal, String.format(PROP_FIELD_NAME_TEMPLATE, pd.getName()), Type.getDescriptor(pd.getPropertyClass())); mv.visitInsn(t.getOpcode(Opcodes.IRETURN)); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", classNameDescriptor, null, l0, l1, 0); mv.visitMaxs(0, 0); mv.visitEnd(); } if (pd.getAccess().isWritable()) { Type t = Type.getType(pd.getPropertyClass()); Method orig = pd.getSetter(); mv = cw.visitMethod( GenerationUtils.convertAccessModifiers(orig.getModifiers()) + (bcd.isThreadSafe() ? Opcodes.ACC_SYNCHRONIZED : 0), orig.getName(), Type.getMethodDescriptor(orig), GenericTypeHelper.getSignature(orig), null); mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitVarInsn(t.getOpcode(Opcodes.ILOAD), 1); mv.visitFieldInsn(Opcodes.PUTFIELD, classNameInternal, String.format(PROP_FIELD_NAME_TEMPLATE, pd.getName()), Type.getDescriptor(pd.getPropertyClass())); mv.visitInsn(Opcodes.RETURN); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", classNameDescriptor, null, l0, l1, 0); mv.visitLocalVariable("value", Type.getDescriptor(pd.getPropertyClass()), null, l0, l1, 1); mv.visitMaxs(0, 0); mv.visitEnd(); } } }
From source file:sg.atom.core.actor.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 2s. com*/ 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:test.GenSourceDebugExtension.java
License:Open Source License
public static byte[] dump() { ClassWriter cw = new ClassWriter(0); MethodVisitor mv;/* ww w . j av a 2s . c o m*/ cw.visit(52, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, "sourcedebugextension/Test", null, "java/lang/Object", null); // This is the important part for the test - it's an example SourceDebugExtension found // in Clojure. cw.visitSource("dispatch.clj", "SMAP\ndispatch.java\nClojure\n*S Clojure\n*F\n+ 1 dispatch.clj\nclojure/pprint/dispatch.clj\n*L\n144#1,8:144\n*E"); { mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); mv.visitInsn(Opcodes.RETURN); mv.visitMaxs(1, 1); mv.visitEnd(); } { mv = cw.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null); mv.visitCode(); mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "err", "Ljava/io/PrintStream;"); mv.visitLdcInsn("Hello"); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false); mv.visitInsn(Opcodes.RETURN); mv.visitMaxs(2, 1); mv.visitEnd(); } cw.visitEnd(); return cw.toByteArray(); }
From source file:test.GenZipWithEntries.java
License:Open Source License
public static byte[] dump(String name) { ClassWriter cw = new ClassWriter(0); cw.visit(52, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, name, null, "java/lang/Object", null); {/*ww w .j av a 2 s . co m*/ MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); mv.visitInsn(Opcodes.RETURN); mv.visitMaxs(1, 1); mv.visitEnd(); } cw.visitEnd(); return cw.toByteArray(); }