List of usage examples for org.objectweb.asm Opcodes CHECKCAST
int CHECKCAST
To view the source code for org.objectweb.asm Opcodes CHECKCAST.
Click Source Link
From source file:com.nginious.http.serialize.XmlSerializerCreator.java
License:Apache License
/** * Creates bytecode which implements the {@link XmlSerializer#serializeProperties(javax.xml.transform.sax.TransformerHandler, Object)} * method for the serializer class being created. * /* w ww.j ava 2 s .c o m*/ * @param writer class byte code writer * @param intBeanClazzName binary name of serializer class being generated * @return a method visitor for writing bytecode inside the generated method */ private MethodVisitor createSerializeMethod(ClassWriter writer, String intBeanClazzName) { String[] exceptions = { "com/nginious/serialize/SerializerException" }; MethodVisitor visitor = writer.visitMethod(Opcodes.ACC_PUBLIC, "serializeProperties", "(Ljavax/xml/transform/sax/TransformerHandler;Ljava/lang/Object;)V", null, exceptions); visitor.visitCode(); Label label = new Label(); visitor.visitVarInsn(Opcodes.ALOAD, 2); visitor.visitJumpInsn(Opcodes.IFNONNULL, label); visitor.visitInsn(Opcodes.RETURN); visitor.visitLabel(label); visitor.visitVarInsn(Opcodes.ALOAD, 0); visitor.visitVarInsn(Opcodes.ALOAD, 2); visitor.visitTypeInsn(Opcodes.CHECKCAST, intBeanClazzName); visitor.visitIntInsn(Opcodes.ASTORE, 3); return visitor; }
From source file:com.nginious.http.xsp.ForEachTagPart.java
License:Apache License
/** * Creates bytecode in a separate method for evaluating this for each tag part. * /* w w w . j av a2 s . c om*/ * @param intClassName the binary class name of the class being created * @param writer the class writer * @throws XspException if unable to create bytecode */ void compileMethod(String intClassName, ClassWriter writer) throws XspException { String[] exceptions = { "com/nginious/http/xsp/XspException" }; MethodVisitor visitor = writer.visitMethod(Opcodes.ACC_PRIVATE, this.methodName, "(Lcom/nginious/http/HttpRequest;Lcom/nginious/http/HttpResponse;Ljava/lang/StringBuffer;)V", null, exceptions); visitor.visitCode(); Label tryLabel = new Label(); Label startCatchLabel = new Label(); // Start try block visitor.visitTryCatchBlock(tryLabel, startCatchLabel, startCatchLabel, "java/lang/Exception"); visitor.visitLabel(tryLabel); try { String expression = setValue.getExpressionContent(); ExpressionParser parser = new ExpressionParser(); TreeExpression expr = parser.parse(expression); if (expr.getType() != Type.ANY) { throw new XspException("Expression in attribute set in tag " + getName() + " is not an attribute or bean property " + " at line " + getLocationDescriptor()); } expr.compile(visitor, Type.ANY); visitor.visitTypeInsn(Opcodes.CHECKCAST, "java/util/Collection"); visitor.visitVarInsn(Opcodes.ASTORE, 4); } catch (ExpressionException e) { throw new XspException("Invalid expression in attribute set in tag " + getName() + " at line " + getLocationDescriptor(), e); } Label labelOut = new Label(); visitor.visitVarInsn(Opcodes.ALOAD, 4); visitor.visitJumpInsn(Opcodes.IFNULL, labelOut); // Start if (this.start != null) { start.compile(visitor, Type.INT); } else { visitor.visitLdcInsn((int) 0); } visitor.visitVarInsn(Opcodes.ISTORE, 5); // End if (this.end != null) { end.compile(visitor, Type.INT); } else { visitor.visitVarInsn(Opcodes.ALOAD, 4); visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Collection", "size", "()I"); } visitor.visitVarInsn(Opcodes.ISTORE, 6); // Step if (this.step != null) { step.compile(visitor, Type.INT); } else { visitor.visitLdcInsn((int) 1); } visitor.visitVarInsn(Opcodes.ISTORE, 7); // Current pos visitor.visitLdcInsn(0); visitor.visitVarInsn(Opcodes.ISTORE, 8); visitor.visitVarInsn(Opcodes.ALOAD, 4); visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Collection", "iterator", "()Ljava/util/Iterator;"); visitor.visitVarInsn(Opcodes.ASTORE, 9); Label labelStart = new Label(); // Start of loop visitor.visitLabel(labelStart); // iterator.hasNext(); visitor.visitVarInsn(Opcodes.ALOAD, 9); visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Iterator", "hasNext", "()Z"); visitor.visitJumpInsn(Opcodes.IFEQ, labelOut); // iterator.next(); visitor.visitVarInsn(Opcodes.ALOAD, 9); visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Iterator", "next", "()Ljava/lang/Object;"); visitor.visitVarInsn(Opcodes.ASTORE, 10); // pos >= start && pos <= end && (pos - start) % step == 0 Label labelIncr = new Label(); visitor.visitVarInsn(Opcodes.ILOAD, 8); visitor.visitVarInsn(Opcodes.ILOAD, 5); visitor.visitJumpInsn(Opcodes.IF_ICMPLT, labelIncr); visitor.visitVarInsn(Opcodes.ILOAD, 8); visitor.visitVarInsn(Opcodes.ILOAD, 6); visitor.visitJumpInsn(Opcodes.IF_ICMPGT, labelIncr); visitor.visitVarInsn(Opcodes.ILOAD, 8); visitor.visitVarInsn(Opcodes.ILOAD, 5); visitor.visitInsn(Opcodes.ISUB); visitor.visitVarInsn(Opcodes.ILOAD, 7); visitor.visitInsn(Opcodes.IREM); visitor.visitJumpInsn(Opcodes.IFNE, labelIncr); visitor.visitVarInsn(Opcodes.ALOAD, 1); varName.compile(visitor, Type.STRING); visitor.visitVarInsn(Opcodes.ALOAD, 10); visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, "com/nginious/http/HttpRequest", "setAttribute", "(Ljava/lang/String;Ljava/lang/Object;)V"); // Call sub parts for (XspPart part : this.contentParts) { part.compile(intClassName, writer, visitor); } // pos++ visitor.visitLabel(labelIncr); visitor.visitIincInsn(8, 1); visitor.visitJumpInsn(Opcodes.GOTO, labelStart); visitor.visitLabel(labelOut); visitor.visitInsn(Opcodes.RETURN); visitor.visitLabel(startCatchLabel); visitor.visitVarInsn(Opcodes.ASTORE, 3); visitor.visitTypeInsn(Opcodes.NEW, "com/nginious/http/xsp/XspException"); visitor.visitInsn(Opcodes.DUP); visitor.visitLdcInsn("Attribute set contains an invalid collection for tag " + getName() + " at " + getLocationDescriptor()); visitor.visitVarInsn(Opcodes.ALOAD, 3); visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "com/nginious/http/xsp/XspException", "<init>", "(Ljava/lang/String;Ljava/lang/Throwable;)V"); visitor.visitInsn(Opcodes.ATHROW); visitor.visitMaxs(11, 11); visitor.visitEnd(); }
From source file:com.nginious.http.xsp.FormatDateTagPart.java
License:Apache License
/** * Creates bytecode in a separate method for evaluating this format date tag part. * /*from ww w .j ava 2 s . c o m*/ * @param intClassName the binary class name of the class being created * @param writer the class writer * @throws XspException if unable to create bytecode */ void compileMethod(String intClassName, ClassWriter writer) throws XspException { String[] exceptions = { "com/nginious/http/xsp/XspException" }; MethodVisitor visitor = writer.visitMethod(Opcodes.ACC_PRIVATE, this.methodName, "(Lcom/nginious/http/HttpRequest;Lcom/nginious/http/HttpResponse;Ljava/lang/StringBuffer;)V", null, exceptions); visitor.visitCode(); Label tryLabel = new Label(); Label startCatchLabel = new Label(); // Start try block visitor.visitTryCatchBlock(tryLabel, startCatchLabel, startCatchLabel, "java/lang/Exception"); visitor.visitLabel(tryLabel); visitor.visitTypeInsn(Opcodes.NEW, "java/text/SimpleDateFormat"); visitor.visitInsn(Opcodes.DUP); pattern.compile(visitor, Type.STRING); visitor.visitVarInsn(Opcodes.ALOAD, 2); visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, "com/nginious/http/HttpResponse", "getLocale", "()Ljava/util/Locale;"); visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/text/SimpleDateFormat", "<init>", "(Ljava/lang/String;Ljava/util/Locale;)V"); visitor.visitVarInsn(Opcodes.ASTORE, 4); if (this.timeZone != null) { visitor.visitVarInsn(Opcodes.ALOAD, 4); String timeZoneDesc = timeZone.getStringContent(); visitor.visitLdcInsn(timeZoneDesc); visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/util/TimeZone", "getTimeZone", "(Ljava/lang/String;)Ljava/util/TimeZone;"); visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/text/SimpleDateFormat", "setTimeZone", "(Ljava/util/TimeZone;)V"); } try { String expression = value.getExpressionContent(); ExpressionParser parser = new ExpressionParser(); TreeExpression expr = parser.parse(expression); if (expr.getType() != Type.ANY) { throw new XspException("Expression in attribute value in tag " + getName() + " is not an attribute or bean property " + " at line " + getLocationDescriptor()); } expr.compile(visitor, Type.ANY); visitor.visitTypeInsn(Opcodes.CHECKCAST, "java/util/Date"); visitor.visitVarInsn(Opcodes.ASTORE, 5); } catch (ExpressionException e) { throw new XspException("Invalid expression in attribute value in tag " + getName() + " at line " + getLocationDescriptor(), e); } Label nullLabel = new Label(); visitor.visitVarInsn(Opcodes.ALOAD, 5); visitor.visitJumpInsn(Opcodes.IFNULL, nullLabel); visitor.visitVarInsn(Opcodes.ALOAD, 4); visitor.visitVarInsn(Opcodes.ALOAD, 5); visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/text/SimpleDateFormat", "format", "(Ljava/util/Date;)Ljava/lang/String;"); visitor.visitVarInsn(Opcodes.ASTORE, 5); if (this.var != null) { visitor.visitVarInsn(Opcodes.ALOAD, 1); var.compile(visitor, Type.STRING); visitor.visitVarInsn(Opcodes.ALOAD, 5); visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, "com/nginious/http/HttpRequest", "setAttribute", "(Ljava/lang/String;Ljava/lang/Object;)V"); } else { visitor.visitVarInsn(Opcodes.ALOAD, 3); visitor.visitVarInsn(Opcodes.ALOAD, 5); visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuffer", "append", "(Ljava/lang/String;)Ljava/lang/StringBuffer;"); visitor.visitInsn(Opcodes.POP); } visitor.visitLabel(nullLabel); visitor.visitInsn(Opcodes.RETURN); visitor.visitLabel(startCatchLabel); visitor.visitVarInsn(Opcodes.ASTORE, 3); visitor.visitTypeInsn(Opcodes.NEW, "com/nginious/http/xsp/XspException"); visitor.visitInsn(Opcodes.DUP); visitor.visitLdcInsn( "Attribute value contains an invalid date for tag " + getName() + " at " + getLocationDescriptor()); visitor.visitVarInsn(Opcodes.ALOAD, 3); visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "com/nginious/http/xsp/XspException", "<init>", "(Ljava/lang/String;Ljava/lang/Throwable;)V"); visitor.visitInsn(Opcodes.ATHROW); visitor.visitMaxs(6, 6); visitor.visitEnd(); super.compileMethod(intClassName, writer); }
From source file:com.nginious.http.xsp.MessageTagPart.java
License:Apache License
/** * Creates bytecode in a separate method for evaluating this message tag part. * /*w ww . j a v a 2 s . c o m*/ * @param intClassName the binary class name of the class being created * @param writer the class writer * @throws XspException if unable to create bytecode */ void compileMethod(String intClassName, ClassWriter writer) throws XspException { MethodVisitor visitor = writer.visitMethod(Opcodes.ACC_PRIVATE, this.methodName, "(Lcom/nginious/http/HttpRequest;Lcom/nginious/http/HttpResponse;Ljava/lang/StringBuffer;)V", null, null); visitor.visitCode(); if (this.var != null) { visitor.visitVarInsn(Opcodes.ALOAD, 1); var.compile(visitor, Type.STRING); } else { visitor.visitVarInsn(Opcodes.ALOAD, 3); } bundle.compile(visitor, Type.STRING); visitor.visitVarInsn(Opcodes.ALOAD, 2); visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, "com/nginious/http/HttpResponse", "getLocale", "()Ljava/util/Locale;"); visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/util/ResourceBundle", "getBundle", "(Ljava/lang/String;Ljava/util/Locale;)Ljava/util/ResourceBundle;"); key.compile(visitor, Type.STRING); visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/ResourceBundle", "getString", "(Ljava/lang/String;)Ljava/lang/String;"); if (this.args != null) { try { String expression = args.getExpressionContent(); ExpressionParser parser = new ExpressionParser(); TreeExpression expr = parser.parse(expression); if (expr.getType() != Type.ANY) { throw new XspException("Expression in attribute value in args " + getName() + " is not an attribute or bean property " + " at line " + getLocationDescriptor()); } expr.compile(visitor, Type.ANY); visitor.visitTypeInsn(Opcodes.CHECKCAST, "[Ljava/lang/Object;"); visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/text/MessageFormat", "format", "(Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;"); } catch (ExpressionException e) { throw new XspException("Invalid expression in attribute value in tag " + getName() + " at line " + getLocationDescriptor(), e); } } if (this.var != null) { visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, "com/nginious/http/HttpRequest", "setAttribute", "(Ljava/lang/String;Ljava/lang/Object;)V"); } else { visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuffer", "append", "(Ljava/lang/String;)Ljava/lang/StringBuffer;"); visitor.visitInsn(Opcodes.POP); } visitor.visitInsn(Opcodes.RETURN); visitor.visitMaxs(5, 5); visitor.visitEnd(); super.compileMethod(intClassName, writer); }
From source file:com.nway.spring.jdbc.bean.AsmBeanProcessor.java
License:Apache License
private void visitMethodCast(MethodVisitor mv, int index, String beanSignature, int beanType, String beanTypeDesc, String writeMethod) { mv.visitVarInsn(Opcodes.ALOAD, 3);// w ww . jav a 2 s . c o m mv.visitVarInsn(Opcodes.ALOAD, 1); visitInsn(mv, index); switch (beanType) { case PROPERTY_TYPE_DATE: mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/sql/ResultSet", "getTimestamp", "(I)Ljava/sql/Timestamp;", true); break; default: mv.visitMethodInsn(Opcodes.INVOKESTATIC, "org/springframework/jdbc/support/JdbcUtils", "getResultSetValue", "(Ljava/sql/ResultSet;I)Ljava/lang/Object;", false); mv.visitTypeInsn(Opcodes.CHECKCAST, beanTypeDesc); break; } mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, beanSignature, writeMethod, "(L" + beanTypeDesc + ";)V", false); }
From source file:com.offbynull.coroutines.instrumenter.asm.InstructionUtils.java
License:Open Source License
/** * Copies a local variable on to the stack. * @param variable variable within the local variable table to load from * @return instructions to load a local variable on to the stack * @throws NullPointerException if any argument is {@code null} * @throws IllegalArgumentException if {@code variable} has been released *//* ww w . j a va 2 s . c om*/ public static InsnList loadVar(Variable variable) { Validate.notNull(variable); InsnList ret = new InsnList(); switch (variable.getType().getSort()) { case Type.BOOLEAN: case Type.BYTE: case Type.CHAR: case Type.SHORT: case Type.INT: ret.add(new VarInsnNode(Opcodes.ILOAD, variable.getIndex())); break; case Type.LONG: ret.add(new VarInsnNode(Opcodes.LLOAD, variable.getIndex())); break; case Type.FLOAT: ret.add(new VarInsnNode(Opcodes.FLOAD, variable.getIndex())); break; case Type.DOUBLE: ret.add(new VarInsnNode(Opcodes.DLOAD, variable.getIndex())); break; case Type.OBJECT: case Type.ARRAY: ret.add(new VarInsnNode(Opcodes.ALOAD, variable.getIndex())); ret.add(new TypeInsnNode(Opcodes.CHECKCAST, variable.getType().getInternalName())); break; default: throw new IllegalStateException(); // should never happen, there is code in Variable/VariableTable to make sure invalid // types aren't set } return ret; }
From source file:com.offbynull.coroutines.instrumenter.asm.InstructionUtils.java
License:Open Source License
private static InsnList loadOperandStack(Variable arrayStackVar, Variable tempObjectVar, Frame<BasicValue> frame, int start, int end) { Validate.notNull(arrayStackVar);//from www .j av a 2 s . c o m Validate.notNull(tempObjectVar); Validate.notNull(frame); Validate.isTrue(arrayStackVar.getType().equals(Type.getType(Object[].class))); Validate.isTrue(tempObjectVar.getType().equals(Type.getType(Object.class))); validateLocalIndicies(arrayStackVar.getIndex(), tempObjectVar.getIndex()); Validate.isTrue(start >= 0); Validate.isTrue(end >= start); // end is exclusive Validate.isTrue(end <= frame.getStackSize()); InsnList ret = new InsnList(); // Restore the stack for (int i = start; i < end; i++) { BasicValue basicValue = frame.getStack(i); Type type = basicValue.getType(); // If type is 'Lnull;', this means that the slot has been assigned null and that "there has been no merge yet that would 'raise' // the type toward some class or interface type" (from ASM mailing list). We know this slot will always contain null at this // point in the code so there's no specific value to load up from the array. Instead we push a null in to that slot, thereby // keeping the same 'Lnull;' type originally assigned to that slot (it doesn't make sense to do a CHECKCAST because 'null' is // not a real class and can never be a real class -- null is a reserved word in Java). if (type.getSort() == Type.OBJECT && "Lnull;".equals(type.getDescriptor())) { ret.add(new InsnNode(Opcodes.ACONST_NULL)); continue; } // Load item from stack storage array ret.add(new VarInsnNode(Opcodes.ALOAD, arrayStackVar.getIndex())); ret.add(new LdcInsnNode(i)); ret.add(new InsnNode(Opcodes.AALOAD)); // Convert the item to an object (if not already an object) and stores it in local vars table. Item removed from stack. switch (type.getSort()) { case Type.BOOLEAN: ret.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Boolean")); ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Boolean", "booleanValue", "()Z", false)); break; case Type.BYTE: ret.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Byte")); ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Byte", "byteValue", "()B", false)); break; case Type.SHORT: ret.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Short")); ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Short", "shortValue", "()S", false)); break; case Type.CHAR: ret.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Character")); ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Character", "charValue", "()C", false)); break; case Type.INT: ret.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Integer")); ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Integer", "intValue", "()I", false)); break; case Type.FLOAT: ret.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Float")); ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Float", "floatValue", "()F", false)); break; case Type.LONG: ret.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Long")); ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Long", "longValue", "()J", false)); break; case Type.DOUBLE: ret.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Double")); ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Double", "doubleValue", "()D", false)); break; case Type.ARRAY: case Type.OBJECT: ret.add(new TypeInsnNode(Opcodes.CHECKCAST, basicValue.getType().getInternalName())); break; case Type.METHOD: case Type.VOID: default: throw new IllegalArgumentException(); } } return ret; }
From source file:com.offbynull.coroutines.instrumenter.asm.InstructionUtils.java
License:Open Source License
/** * Generates instructions to save the operand stack to an object array. * @param arrayStackVar variable that the object array containing operand stack is stored * @param tempObjectVar variable to use for temporary objects * @param frame execution frame at the instruction where the operand stack is to be saved * @return instructions to save the operand stack in to an array and save it to the local variables table * @throws NullPointerException if any argument is {@code null} * @throws IllegalArgumentException if variables have the same index, or if variables have been released, or if variables are of wrong * type//from w w w .j a v a 2 s .c o m */ public static InsnList saveOperandStack(Variable arrayStackVar, Variable tempObjectVar, Frame<BasicValue> frame) { Validate.notNull(arrayStackVar); Validate.notNull(tempObjectVar); Validate.notNull(frame); Validate.isTrue(arrayStackVar.getType().equals(Type.getType(Object[].class))); Validate.isTrue(tempObjectVar.getType().equals(Type.getType(Object.class))); validateLocalIndicies(arrayStackVar.getIndex(), tempObjectVar.getIndex()); InsnList ret = new InsnList(); // Create stack storage array and save it in local vars table ret.add(new LdcInsnNode(frame.getStackSize())); ret.add(new TypeInsnNode(Opcodes.ANEWARRAY, "java/lang/Object")); ret.add(new VarInsnNode(Opcodes.ASTORE, arrayStackVar.getIndex())); // Save the stack for (int i = frame.getStackSize() - 1; i >= 0; i--) { BasicValue basicValue = frame.getStack(i); Type type = basicValue.getType(); // If type is 'Lnull;', this means that the slot has been assigned null and that "there has been no merge yet that would 'raise' // the type toward some class or interface type" (from ASM mailing list). We know this slot will always contain null at this // point in the code so we can avoid saving it (but we still need to do a POP to get rid of it). When we load it back up, we can // simply push a null in to that slot, thereby keeping the same 'Lnull;' type. if ("Lnull;".equals(type.getDescriptor())) { ret.add(new InsnNode(Opcodes.POP)); continue; } // Convert the item to an object (if not already an object) and stores it in local vars table. Item removed from stack. switch (type.getSort()) { case Type.BOOLEAN: ret.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;")); ret.add(new VarInsnNode(Opcodes.ASTORE, tempObjectVar.getIndex())); break; case Type.BYTE: ret.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Byte", "valueOf", "(B)Ljava/lang/Byte;", false)); ret.add(new VarInsnNode(Opcodes.ASTORE, tempObjectVar.getIndex())); break; case Type.SHORT: ret.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Short", "valueOf", "(S)Ljava/lang/Short;", false)); ret.add(new VarInsnNode(Opcodes.ASTORE, tempObjectVar.getIndex())); break; case Type.CHAR: ret.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Character", "valueOf", "(C)Ljava/lang/Character;", false)); ret.add(new VarInsnNode(Opcodes.ASTORE, tempObjectVar.getIndex())); break; case Type.INT: ret.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;", false)); ret.add(new VarInsnNode(Opcodes.ASTORE, tempObjectVar.getIndex())); break; case Type.FLOAT: ret.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Float", "valueOf", "(F)Ljava/lang/Float;", false)); ret.add(new VarInsnNode(Opcodes.ASTORE, tempObjectVar.getIndex())); break; case Type.LONG: ret.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Long", "valueOf", "(J)Ljava/lang/Long;", false)); ret.add(new VarInsnNode(Opcodes.ASTORE, tempObjectVar.getIndex())); break; case Type.DOUBLE: ret.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Double", "valueOf", "(D)Ljava/lang/Double;", false)); ret.add(new VarInsnNode(Opcodes.ASTORE, tempObjectVar.getIndex())); break; case Type.ARRAY: case Type.OBJECT: ret.add(new VarInsnNode(Opcodes.ASTORE, tempObjectVar.getIndex())); break; case Type.METHOD: case Type.VOID: default: throw new IllegalArgumentException(); } // Store item in to stack storage array ret.add(new VarInsnNode(Opcodes.ALOAD, arrayStackVar.getIndex())); ret.add(new LdcInsnNode(i)); ret.add(new VarInsnNode(Opcodes.ALOAD, tempObjectVar.getIndex())); ret.add(new InsnNode(Opcodes.AASTORE)); } // Restore the stack for (int i = 0; i < frame.getStackSize(); i++) { BasicValue basicValue = frame.getStack(i); Type type = basicValue.getType(); // If type is 'Lnull;', this means that the slot has been assigned null and that "there has been no merge yet that would 'raise' // the type toward some class or interface type" (from ASM mailing list). We know this slot will always contain null at this // point in the code so there's no specific value to load up from the array. Instead we push a null in to that slot, thereby // keeping the same 'Lnull;' type originally assigned to that slot (it doesn't make sense to do a CHECKCAST because 'null' is // not a real class and can never be a real class -- null is a reserved word in Java). if (type.getSort() == Type.OBJECT && "Lnull;".equals(type.getDescriptor())) { ret.add(new InsnNode(Opcodes.ACONST_NULL)); continue; } // Load item from stack storage array ret.add(new VarInsnNode(Opcodes.ALOAD, arrayStackVar.getIndex())); ret.add(new LdcInsnNode(i)); ret.add(new InsnNode(Opcodes.AALOAD)); // Convert the item to an object (if not already an object) and stores it in local vars table. Item removed from stack. switch (type.getSort()) { case Type.BOOLEAN: ret.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Boolean")); ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Boolean", "booleanValue", "()Z", false)); break; case Type.BYTE: ret.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Byte")); ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Byte", "byteValue", "()B", false)); break; case Type.SHORT: ret.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Short")); ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Short", "shortValue", "()S", false)); break; case Type.CHAR: ret.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Character")); ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Character", "charValue", "()C", false)); break; case Type.INT: ret.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Integer")); ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Integer", "intValue", "()I", false)); break; case Type.FLOAT: ret.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Float")); ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Float", "floatValue", "()F", false)); break; case Type.LONG: ret.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Long")); ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Long", "longValue", "()J", false)); break; case Type.DOUBLE: ret.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Double")); ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Double", "doubleValue", "()D", false)); break; case Type.ARRAY: case Type.OBJECT: ret.add(new TypeInsnNode(Opcodes.CHECKCAST, basicValue.getType().getInternalName())); break; case Type.METHOD: case Type.VOID: default: throw new IllegalArgumentException(); } } return ret; }
From source file:com.offbynull.coroutines.instrumenter.asm.InstructionUtils.java
License:Open Source License
/** * Generates instructions to load the local variables table from an object array. * * @param arrayLocalsVar variable that the object array containing local variables table is stored * @param tempObjectVar variable to use for temporary objects * @param frame execution frame at the instruction for which the local variables table is to be restored * @return instructions to load the local variables table from an array * @throws NullPointerException if any argument is {@code null} * @throws IllegalArgumentException if variables have the same index, or if variables have been released, or if variables are of wrong * type//from w w w. ja va 2 s.c o m */ public static InsnList loadLocalVariableTable(Variable arrayLocalsVar, Variable tempObjectVar, Frame<BasicValue> frame) { Validate.notNull(arrayLocalsVar); Validate.notNull(tempObjectVar); Validate.notNull(frame); Validate.isTrue(arrayLocalsVar.getType().equals(Type.getType(Object[].class))); Validate.isTrue(tempObjectVar.getType().equals(Type.getType(Object.class))); validateLocalIndicies(arrayLocalsVar.getIndex(), tempObjectVar.getIndex()); InsnList ret = new InsnList(); // Load the locals for (int i = 0; i < frame.getLocals(); i++) { BasicValue basicValue = frame.getLocal(i); Type type = basicValue.getType(); // If type == null, basicValue is pointing to uninitialized var -- basicValue.toString() will return ".". This means that this // slot contains nothing to load. So, skip this slot if we encounter it (such that it will remain uninitialized). if (type == null) { continue; } // If type is 'Lnull;', this means that the slot has been assigned null and that "there has been no merge yet that would 'raise' // the type toward some class or interface type" (from ASM mailing list). We know this slot will always contain null at this // point in the code so there's no specific value to load up from the array. Instead we push a null in to that slot, thereby // keeping the same 'Lnull;' type originally assigned to that slot (it doesn't make sense to do a CHECKCAST because 'null' is // not a real class and can never be a real class -- null is a reserved word in Java). if (type.getSort() == Type.OBJECT && "Lnull;".equals(type.getDescriptor())) { ret.add(new InsnNode(Opcodes.ACONST_NULL)); ret.add(new VarInsnNode(Opcodes.ASTORE, i)); continue; } // Load item from locals storage array ret.add(new VarInsnNode(Opcodes.ALOAD, arrayLocalsVar.getIndex())); ret.add(new LdcInsnNode(i)); ret.add(new InsnNode(Opcodes.AALOAD)); // Convert the item from an object stores it in local vars table. switch (type.getSort()) { case Type.BOOLEAN: ret.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Boolean")); ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Boolean", "booleanValue", "()Z", false)); ret.add(new VarInsnNode(Opcodes.ISTORE, i)); break; case Type.BYTE: ret.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Byte")); ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Byte", "byteValue", "()B", false)); ret.add(new VarInsnNode(Opcodes.ISTORE, i)); break; case Type.SHORT: ret.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Short")); ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Short", "shortValue", "()S", false)); ret.add(new VarInsnNode(Opcodes.ISTORE, i)); break; case Type.CHAR: ret.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Character")); ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Character", "charValue", "()C", false)); ret.add(new VarInsnNode(Opcodes.ISTORE, i)); break; case Type.INT: ret.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Integer")); ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Integer", "intValue", "()I", false)); ret.add(new VarInsnNode(Opcodes.ISTORE, i)); break; case Type.FLOAT: ret.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Float")); ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Float", "floatValue", "()F", false)); ret.add(new VarInsnNode(Opcodes.FSTORE, i)); break; case Type.LONG: ret.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Long")); ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Long", "longValue", "()J", false)); ret.add(new VarInsnNode(Opcodes.LSTORE, i)); break; case Type.DOUBLE: ret.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Double")); ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Double", "doubleValue", "()D", false)); ret.add(new VarInsnNode(Opcodes.DSTORE, i)); break; case Type.ARRAY: case Type.OBJECT: ret.add(new TypeInsnNode(Opcodes.CHECKCAST, basicValue.getType().getInternalName())); ret.add(new VarInsnNode(Opcodes.ASTORE, i)); break; case Type.METHOD: case Type.VOID: default: throw new IllegalStateException(); } } return ret; }
From source file:com.offbynull.coroutines.instrumenter.ContinuationPointInstructionUtils.java
License:Open Source License
static InsnList loadAndCastToOriginal(Type originalType, Variable variable) { Validate.notNull(originalType);/*from www . ja v a 2 s . c om*/ Validate.notNull(variable); Validate.isTrue(variable.getType().equals(Type.getType(Object.class))); InsnList ret = new InsnList(); switch (originalType.getSort()) { case Type.BOOLEAN: ret.add(loadVar(variable)); // load it in to the returnValObj ret.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Boolean")); ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Boolean", "booleanValue", "()Z", false)); break; case Type.BYTE: ret.add(loadVar(variable)); // load it in to the returnValObj ret.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Byte")); ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Byte", "byteValue", "()B", false)); break; case Type.SHORT: ret.add(loadVar(variable)); // load it in to the returnValObj ret.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Short")); ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Short", "shortValue", "()S", false)); break; case Type.CHAR: ret.add(loadVar(variable)); // load it in to the returnValObj ret.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Character")); ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Character", "charValue", "()C", false)); break; case Type.INT: ret.add(loadVar(variable)); // load it in to the returnValObj ret.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Integer")); ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Integer", "intValue", "()I", false)); break; case Type.FLOAT: ret.add(loadVar(variable)); // load it in to the returnValObj ret.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Float")); ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Float", "floatValue", "()F", false)); break; case Type.LONG: ret.add(loadVar(variable)); // load it in to the returnValObj ret.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Long")); ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Long", "longValue", "()J", false)); break; case Type.DOUBLE: ret.add(loadVar(variable)); // load it in to the returnValObj ret.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Double")); ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Double", "doubleValue", "()D", false)); break; case Type.ARRAY: case Type.OBJECT: ret.add(loadVar(variable)); // load it in to the returnValObj ret.add(new TypeInsnNode(Opcodes.CHECKCAST, originalType.getInternalName())); break; case Type.VOID: break; case Type.METHOD: default: throw new IllegalArgumentException(); } return ret; }