/** * Determine the appropriate boxing instruction for a specific type (if it needs * boxing) and insert the instruction into the supplied visitor. * @param mv the target visitor for the new instructions * @param descriptor the descriptor of a type that may or may not need boxing */ public static void insertBoxIfNecessary(MethodVisitor mv, @Nullable String descriptor) { if (descriptor != null && descriptor.length() == 1) { insertBoxIfNecessary(mv, descriptor.charAt(0)); } }
/** * Determine the appropriate boxing instruction for a specific type (if it needs * boxing) and insert the instruction into the supplied visitor. * @param mv the target visitor for the new instructions * @param descriptor the descriptor of a type that may or may not need boxing */ public static void insertBoxIfNecessary(MethodVisitor mv, @Nullable String descriptor) { if (descriptor != null && descriptor.length() == 1) { insertBoxIfNecessary(mv, descriptor.charAt(0)); } }
/** * Ask an argument to generate its bytecode and then follow it up * with any boxing/unboxing/checkcasting to ensure it matches the expected parameter descriptor. */ protected static void generateCodeForArgument(MethodVisitor mv, CodeFlow cf, SpelNodeImpl argument, String paramDesc) { cf.enterCompilationScope(); argument.generateCode(mv, cf); String lastDesc = cf.lastDescriptor(); Assert.state(lastDesc != null, "No last descriptor"); boolean primitiveOnStack = CodeFlow.isPrimitive(lastDesc); // Check if need to box it for the method reference? if (primitiveOnStack && paramDesc.charAt(0) == 'L') { CodeFlow.insertBoxIfNecessary(mv, lastDesc.charAt(0)); } else if (paramDesc.length() == 1 && !primitiveOnStack) { CodeFlow.insertUnboxInsns(mv, paramDesc.charAt(0), lastDesc); } else if (!paramDesc.equals(lastDesc)) { // This would be unnecessary in the case of subtyping (e.g. method takes Number but Integer passed in) CodeFlow.insertCheckCast(mv, paramDesc); } cf.exitCompilationScope(); }
@Override public void generateCode(MethodVisitor mv, CodeFlow cf) { cf.loadEvaluationContext(mv); String leftDesc = getLeftOperand().exitTypeDescriptor; String rightDesc = getRightOperand().exitTypeDescriptor; boolean leftPrim = CodeFlow.isPrimitive(leftDesc); boolean rightPrim = CodeFlow.isPrimitive(rightDesc); cf.enterCompilationScope(); getLeftOperand().generateCode(mv, cf); cf.exitCompilationScope(); if (leftPrim) { CodeFlow.insertBoxIfNecessary(mv, leftDesc.charAt(0)); } cf.enterCompilationScope(); getRightOperand().generateCode(mv, cf); cf.exitCompilationScope(); if (rightPrim) { CodeFlow.insertBoxIfNecessary(mv, rightDesc.charAt(0)); } String operatorClassName = Operator.class.getName().replace('.', '/'); String evaluationContextClassName = EvaluationContext.class.getName().replace('.', '/'); mv.visitMethodInsn(INVOKESTATIC, operatorClassName, "equalityCheck", "(L" + evaluationContextClassName + ";Ljava/lang/Object;Ljava/lang/Object;)Z", false); cf.pushDescriptor("Z"); }
@Override public void generateCode(MethodVisitor mv, CodeFlow cf) { getLeftOperand().generateCode(mv, cf); CodeFlow.insertBoxIfNecessary(mv, cf.lastDescriptor()); Assert.state(this.type != null, "No type available"); if (this.type.isPrimitive()) { // always false - but left operand code always driven // in case it had side effects mv.visitInsn(POP); mv.visitInsn(ICONST_0); // value of false } else { mv.visitTypeInsn(INSTANCEOF, Type.getInternalName(this.type)); } cf.pushDescriptor(this.exitTypeDescriptor); }
CodeFlow.insertBoxIfNecessary(mv, descriptor.charAt(0)); CodeFlow.insertBoxIfNecessary(mv, this.originalPrimitiveExitTypeDescriptor);
/** * Ask an argument to generate its bytecode and then follow it up * with any boxing/unboxing/checkcasting to ensure it matches the expected parameter descriptor. */ protected static void generateCodeForArgument(MethodVisitor mv, CodeFlow cf, SpelNodeImpl argument, String paramDesc) { cf.enterCompilationScope(); argument.generateCode(mv, cf); String lastDesc = cf.lastDescriptor(); Assert.state(lastDesc != null, "No last descriptor"); boolean primitiveOnStack = CodeFlow.isPrimitive(lastDesc); // Check if need to box it for the method reference? if (primitiveOnStack && paramDesc.charAt(0) == 'L') { CodeFlow.insertBoxIfNecessary(mv, lastDesc.charAt(0)); } else if (paramDesc.length() == 1 && !primitiveOnStack) { CodeFlow.insertUnboxInsns(mv, paramDesc.charAt(0), lastDesc); } else if (!paramDesc.equals(lastDesc)) { // This would be unnecessary in the case of subtyping (e.g. method takes Number but Integer passed in) CodeFlow.insertCheckCast(mv, paramDesc); } cf.exitCompilationScope(); }
cf.exitCompilationScope(); if (leftPrim) { CodeFlow.insertBoxIfNecessary(mv, leftDesc.charAt(0)); cf.exitCompilationScope(); if (rightPrim) { CodeFlow.insertBoxIfNecessary(mv, rightDesc.charAt(0));
lastDesc = cf.lastDescriptor(); Assert.state(lastDesc != null, "No last descriptor"); CodeFlow.insertBoxIfNecessary(mv, lastDesc.charAt(0)); lastDesc = cf.lastDescriptor(); Assert.state(lastDesc != null, "No last descriptor"); CodeFlow.insertBoxIfNecessary(mv, lastDesc.charAt(0));
String lastDesc = cf.lastDescriptor(); Assert.state(lastDesc != null, "No last descriptor"); CodeFlow.insertBoxIfNecessary(mv, lastDesc.charAt(0)); cf.exitCompilationScope(); Label elseTarget = new Label(); lastDesc = cf.lastDescriptor(); Assert.state(lastDesc != null, "No last descriptor"); CodeFlow.insertBoxIfNecessary(mv, lastDesc.charAt(0));
@Override public void generateCode(MethodVisitor mv, CodeFlow cf) { PropertyAccessor accessorToUse = this.cachedReadAccessor; if (!(accessorToUse instanceof CompilablePropertyAccessor)) { throw new IllegalStateException("Property accessor is not compilable: " + accessorToUse); } Label skipIfNull = null; if (this.nullSafe) { mv.visitInsn(DUP); skipIfNull = new Label(); Label continueLabel = new Label(); mv.visitJumpInsn(IFNONNULL, continueLabel); CodeFlow.insertCheckCast(mv, this.exitTypeDescriptor); mv.visitJumpInsn(GOTO, skipIfNull); mv.visitLabel(continueLabel); } ((CompilablePropertyAccessor) accessorToUse).generateCode(this.name, mv, cf); cf.pushDescriptor(this.exitTypeDescriptor); if (this.originalPrimitiveExitTypeDescriptor != null) { // The output of the accessor is a primitive but from the block above it might be null, // so to have a common stack element type at skipIfNull target it is necessary // to box the primitive CodeFlow.insertBoxIfNecessary(mv, this.originalPrimitiveExitTypeDescriptor); } if (skipIfNull != null) { mv.visitLabel(skipIfNull); } }
@Override public void generateCode(MethodVisitor mv, CodeFlow cf) { cf.loadEvaluationContext(mv); String leftDesc = getLeftOperand().exitTypeDescriptor; String rightDesc = getRightOperand().exitTypeDescriptor; boolean leftPrim = CodeFlow.isPrimitive(leftDesc); boolean rightPrim = CodeFlow.isPrimitive(rightDesc); cf.enterCompilationScope(); getLeftOperand().generateCode(mv, cf); cf.exitCompilationScope(); if (leftPrim) { CodeFlow.insertBoxIfNecessary(mv, leftDesc.charAt(0)); } cf.enterCompilationScope(); getRightOperand().generateCode(mv, cf); cf.exitCompilationScope(); if (rightPrim) { CodeFlow.insertBoxIfNecessary(mv, rightDesc.charAt(0)); } String operatorClassName = Operator.class.getName().replace('.', '/'); String evaluationContextClassName = EvaluationContext.class.getName().replace('.', '/'); mv.visitMethodInsn(INVOKESTATIC, operatorClassName, "equalityCheck", "(L" + evaluationContextClassName + ";Ljava/lang/Object;Ljava/lang/Object;)Z", false); cf.pushDescriptor("Z"); }
@Override public void generateCode(MethodVisitor mv, CodeFlow cf) { getLeftOperand().generateCode(mv, cf); CodeFlow.insertBoxIfNecessary(mv, cf.lastDescriptor()); Assert.state(this.type != null, "No type available"); if (this.type.isPrimitive()) { // always false - but left operand code always driven // in case it had side effects mv.visitInsn(POP); mv.visitInsn(ICONST_0); // value of false } else { mv.visitTypeInsn(INSTANCEOF, Type.getInternalName(this.type)); } cf.pushDescriptor(this.exitTypeDescriptor); }
CodeFlow.insertBoxIfNecessary(mv, cf.lastDescriptor()); if ("V".equals(cf.lastDescriptor())) { mv.visitInsn(ACONST_NULL);
String lastDesc = codeflow.lastDescriptor(); if (CodeFlow.isPrimitive(lastDesc)) { CodeFlow.insertBoxIfNecessary(mv, lastDesc.charAt(0));
cf.exitCompilationScope(); if (leftPrim) { CodeFlow.insertBoxIfNecessary(mv, leftDesc.charAt(0)); cf.exitCompilationScope(); if (rightPrim) { CodeFlow.insertBoxIfNecessary(mv, rightDesc.charAt(0));
lastDesc = cf.lastDescriptor(); Assert.state(lastDesc != null, "No last descriptor"); CodeFlow.insertBoxIfNecessary(mv, lastDesc.charAt(0)); lastDesc = cf.lastDescriptor(); Assert.state(lastDesc != null, "No last descriptor"); CodeFlow.insertBoxIfNecessary(mv, lastDesc.charAt(0));
String lastDesc = cf.lastDescriptor(); Assert.state(lastDesc != null, "No last descriptor"); CodeFlow.insertBoxIfNecessary(mv, lastDesc.charAt(0)); cf.exitCompilationScope(); Label elseTarget = new Label(); lastDesc = cf.lastDescriptor(); Assert.state(lastDesc != null, "No last descriptor"); CodeFlow.insertBoxIfNecessary(mv, lastDesc.charAt(0));
@Override public void generateCode(MethodVisitor mv, CodeFlow cf) { PropertyAccessor accessorToUse = this.cachedReadAccessor; if (!(accessorToUse instanceof CompilablePropertyAccessor)) { throw new IllegalStateException("Property accessor is not compilable: " + accessorToUse); } Label skipIfNull = null; if (this.nullSafe) { mv.visitInsn(DUP); skipIfNull = new Label(); Label continueLabel = new Label(); mv.visitJumpInsn(IFNONNULL, continueLabel); CodeFlow.insertCheckCast(mv, this.exitTypeDescriptor); mv.visitJumpInsn(GOTO, skipIfNull); mv.visitLabel(continueLabel); } ((CompilablePropertyAccessor) accessorToUse).generateCode(this.name, mv, cf); cf.pushDescriptor(this.exitTypeDescriptor); if (this.originalPrimitiveExitTypeDescriptor != null) { // The output of the accessor is a primitive but from the block above it might be null, // so to have a common stack element type at skipIfNull target it is necessary // to box the primitive CodeFlow.insertBoxIfNecessary(mv, this.originalPrimitiveExitTypeDescriptor); } if (skipIfNull != null) { mv.visitLabel(skipIfNull); } }
String lastDesc = codeflow.lastDescriptor(); if (CodeFlow.isPrimitive(lastDesc)) { CodeFlow.insertBoxIfNecessary(mv, lastDesc.charAt(0));