Java Code Examples for org.springframework.expression.spel.CodeFlow#exitCompilationScope()

The following examples show how to use org.springframework.expression.spel.CodeFlow#exitCompilationScope() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OpPlus.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Walk through a possible tree of nodes that combine strings and append
 * them all to the same (on stack) StringBuilder.
 */
private void walk(MethodVisitor mv, CodeFlow cf, SpelNodeImpl operand) {
	if (operand instanceof OpPlus) {
		OpPlus plus = (OpPlus)operand;
		walk(mv, cf, plus.getLeftOperand());
		walk(mv, cf, plus.getRightOperand());
	}
	else {
		cf.enterCompilationScope();
		operand.generateCode(mv,cf);
		if (!"Ljava/lang/String".equals(cf.lastDescriptor())) {
			mv.visitTypeInsn(CHECKCAST, "java/lang/String");
		}
		cf.exitCompilationScope();
		mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
	}
}
 
Example 2
Source File: OpOr.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	// pseudo: if (leftOperandValue) { result=true; } else { result=rightOperandValue; }
	Label elseTarget = new Label();
	Label endOfIf = new Label();
	cf.enterCompilationScope();
	getLeftOperand().generateCode(mv, cf);
	cf.unboxBooleanIfNecessary(mv);
	cf.exitCompilationScope();
	mv.visitJumpInsn(IFEQ, elseTarget);
	mv.visitLdcInsn(1); // TRUE
	mv.visitJumpInsn(GOTO,endOfIf);
	mv.visitLabel(elseTarget);
	cf.enterCompilationScope();
	getRightOperand().generateCode(mv, cf);
	cf.unboxBooleanIfNecessary(mv);
	cf.exitCompilationScope();
	mv.visitLabel(endOfIf);
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example 3
Source File: OpOr.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	// pseudo: if (leftOperandValue) { result=true; } else { result=rightOperandValue; }
	Label elseTarget = new Label();
	Label endOfIf = new Label();
	cf.enterCompilationScope();
	getLeftOperand().generateCode(mv, cf);
	cf.unboxBooleanIfNecessary(mv);
	cf.exitCompilationScope();
	mv.visitJumpInsn(IFEQ, elseTarget);
	mv.visitLdcInsn(1); // TRUE
	mv.visitJumpInsn(GOTO,endOfIf);
	mv.visitLabel(elseTarget);
	cf.enterCompilationScope();
	getRightOperand().generateCode(mv, cf);
	cf.unboxBooleanIfNecessary(mv);
	cf.exitCompilationScope();
	mv.visitLabel(endOfIf);
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example 4
Source File: OpPlus.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Walk through a possible tree of nodes that combine strings and append
 * them all to the same (on stack) StringBuilder.
 */
private void walk(MethodVisitor mv, CodeFlow cf, @Nullable SpelNodeImpl operand) {
	if (operand instanceof OpPlus) {
		OpPlus plus = (OpPlus)operand;
		walk(mv, cf, plus.getLeftOperand());
		walk(mv, cf, plus.getRightOperand());
	}
	else if (operand != null) {
		cf.enterCompilationScope();
		operand.generateCode(mv,cf);
		if (!"Ljava/lang/String".equals(cf.lastDescriptor())) {
			mv.visitTypeInsn(CHECKCAST, "java/lang/String");
		}
		cf.exitCompilationScope();
		mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
	}
}
 
Example 5
Source File: OpOr.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	// pseudo: if (leftOperandValue) { result=true; } else { result=rightOperandValue; }
	Label elseTarget = new Label();
	Label endOfIf = new Label();
	cf.enterCompilationScope();
	getLeftOperand().generateCode(mv, cf);
	cf.unboxBooleanIfNecessary(mv);
	cf.exitCompilationScope();
	mv.visitJumpInsn(IFEQ, elseTarget);
	mv.visitLdcInsn(1); // TRUE
	mv.visitJumpInsn(GOTO,endOfIf);
	mv.visitLabel(elseTarget);
	cf.enterCompilationScope();
	getRightOperand().generateCode(mv, cf);
	cf.unboxBooleanIfNecessary(mv);
	cf.exitCompilationScope();
	mv.visitLabel(endOfIf);
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example 6
Source File: SpelNodeImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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();
	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();
}
 
Example 7
Source File: SpelNodeImpl.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * 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();
}
 
Example 8
Source File: Elvis.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	// exit type descriptor can be null if both components are literal expressions
	computeExitTypeDescriptor();
	cf.enterCompilationScope();
	this.children[0].generateCode(mv, cf);
	String lastDesc = cf.lastDescriptor();
	Assert.state(lastDesc != null, "No last descriptor");
	CodeFlow.insertBoxIfNecessary(mv, lastDesc.charAt(0));
	cf.exitCompilationScope();
	Label elseTarget = new Label();
	Label endOfIf = new Label();
	mv.visitInsn(DUP);
	mv.visitJumpInsn(IFNULL, elseTarget);
	// Also check if empty string, as per the code in the interpreted version
	mv.visitInsn(DUP);
	mv.visitLdcInsn("");
	mv.visitInsn(SWAP);
	mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z",false);
	mv.visitJumpInsn(IFEQ, endOfIf);  // if not empty, drop through to elseTarget
	mv.visitLabel(elseTarget);
	mv.visitInsn(POP);
	cf.enterCompilationScope();
	this.children[1].generateCode(mv, cf);
	if (!CodeFlow.isPrimitive(this.exitTypeDescriptor)) {
		lastDesc = cf.lastDescriptor();
		Assert.state(lastDesc != null, "No last descriptor");
		CodeFlow.insertBoxIfNecessary(mv, lastDesc.charAt(0));
	}
	cf.exitCompilationScope();
	mv.visitLabel(endOfIf);
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example 9
Source File: OpMultiply.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	getLeftOperand().generateCode(mv, cf);
	String leftDesc = getLeftOperand().exitTypeDescriptor;
	CodeFlow.insertNumericUnboxOrPrimitiveTypeCoercion(mv, leftDesc, this.exitTypeDescriptor.charAt(0));
	if (this.children.length > 1) {
		cf.enterCompilationScope();
		getRightOperand().generateCode(mv, cf);
		String rightDesc = getRightOperand().exitTypeDescriptor;
		cf.exitCompilationScope();
		CodeFlow.insertNumericUnboxOrPrimitiveTypeCoercion(mv, rightDesc, this.exitTypeDescriptor.charAt(0));
		switch (this.exitTypeDescriptor.charAt(0)) {
			case 'I':
				mv.visitInsn(IMUL);
				break;
			case 'J':
				mv.visitInsn(LMUL);
				break;
			case 'F': 
				mv.visitInsn(FMUL);
				break;
			case 'D':
				mv.visitInsn(DMUL);
				break;				
			default:
				throw new IllegalStateException(
						"Unrecognized exit type descriptor: '" + this.exitTypeDescriptor + "'");
		}
	}
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example 10
Source File: OpModulus.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	getLeftOperand().generateCode(mv, cf);
	String leftDesc = getLeftOperand().exitTypeDescriptor;
	String exitDesc = this.exitTypeDescriptor;
	Assert.state(exitDesc != null, "No exit type descriptor");
	char targetDesc = exitDesc.charAt(0);
	CodeFlow.insertNumericUnboxOrPrimitiveTypeCoercion(mv, leftDesc, targetDesc);
	if (this.children.length > 1) {
		cf.enterCompilationScope();
		getRightOperand().generateCode(mv, cf);
		String rightDesc = getRightOperand().exitTypeDescriptor;
		cf.exitCompilationScope();
		CodeFlow.insertNumericUnboxOrPrimitiveTypeCoercion(mv, rightDesc, targetDesc);
		switch (targetDesc) {
			case 'I':
				mv.visitInsn(IREM);
				break;
			case 'J':
				mv.visitInsn(LREM);
				break;
			case 'F':
				mv.visitInsn(FREM);
				break;
			case 'D':
				mv.visitInsn(DREM);
				break;
			default:
				throw new IllegalStateException(
						"Unrecognized exit type descriptor: '" + this.exitTypeDescriptor + "'");
		}
	}
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example 11
Source File: OpDivide.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	getLeftOperand().generateCode(mv, cf);
	String leftDesc = getLeftOperand().exitTypeDescriptor;
	String exitDesc = this.exitTypeDescriptor;
	Assert.state(exitDesc != null, "No exit type descriptor");
	char targetDesc = exitDesc.charAt(0);
	CodeFlow.insertNumericUnboxOrPrimitiveTypeCoercion(mv, leftDesc, targetDesc);
	if (this.children.length > 1) {
		cf.enterCompilationScope();
		getRightOperand().generateCode(mv, cf);
		String rightDesc = getRightOperand().exitTypeDescriptor;
		cf.exitCompilationScope();
		CodeFlow.insertNumericUnboxOrPrimitiveTypeCoercion(mv, rightDesc, targetDesc);
		switch (targetDesc) {
			case 'I':
				mv.visitInsn(IDIV);
				break;
			case 'J':
				mv.visitInsn(LDIV);
				break;
			case 'F':
				mv.visitInsn(FDIV);
				break;
			case 'D':
				mv.visitInsn(DDIV);
				break;
			default:
				throw new IllegalStateException(
						"Unrecognized exit type descriptor: '" + this.exitTypeDescriptor + "'");
		}
	}
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example 12
Source File: OpModulus.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	getLeftOperand().generateCode(mv, cf);
	String leftDesc = getLeftOperand().exitTypeDescriptor;
	CodeFlow.insertNumericUnboxOrPrimitiveTypeCoercion(mv, leftDesc, this.exitTypeDescriptor.charAt(0));
	if (this.children.length > 1) {
		cf.enterCompilationScope();
		getRightOperand().generateCode(mv, cf);
		String rightDesc = getRightOperand().exitTypeDescriptor;
		cf.exitCompilationScope();
		CodeFlow.insertNumericUnboxOrPrimitiveTypeCoercion(mv, rightDesc, this.exitTypeDescriptor.charAt(0));
		switch (this.exitTypeDescriptor.charAt(0)) {
			case 'I':
				mv.visitInsn(IREM);
				break;
			case 'J':
				mv.visitInsn(LREM);
				break;
			case 'F': 
				mv.visitInsn(FREM);
				break;
			case 'D':
				mv.visitInsn(DREM);
				break;				
			default:
				throw new IllegalStateException(
						"Unrecognized exit type descriptor: '" + this.exitTypeDescriptor + "'");
		}
	}
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example 13
Source File: OpMultiply.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	getLeftOperand().generateCode(mv, cf);
	String leftDesc = getLeftOperand().exitTypeDescriptor;
	String exitDesc = this.exitTypeDescriptor;
	Assert.state(exitDesc != null, "No exit type descriptor");
	char targetDesc = exitDesc.charAt(0);
	CodeFlow.insertNumericUnboxOrPrimitiveTypeCoercion(mv, leftDesc, targetDesc);
	if (this.children.length > 1) {
		cf.enterCompilationScope();
		getRightOperand().generateCode(mv, cf);
		String rightDesc = getRightOperand().exitTypeDescriptor;
		cf.exitCompilationScope();
		CodeFlow.insertNumericUnboxOrPrimitiveTypeCoercion(mv, rightDesc, targetDesc);
		switch (targetDesc) {
			case 'I':
				mv.visitInsn(IMUL);
				break;
			case 'J':
				mv.visitInsn(LMUL);
				break;
			case 'F':
				mv.visitInsn(FMUL);
				break;
			case 'D':
				mv.visitInsn(DMUL);
				break;
			default:
				throw new IllegalStateException(
						"Unrecognized exit type descriptor: '" + this.exitTypeDescriptor + "'");
		}
	}
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example 14
Source File: OpNE.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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);

	// Invert the boolean
	Label notZero = new Label();
	Label end = new Label();
	mv.visitJumpInsn(IFNE, notZero);
	mv.visitInsn(ICONST_1);
	mv.visitJumpInsn(GOTO, end);
	mv.visitLabel(notZero);
	mv.visitInsn(ICONST_0);
	mv.visitLabel(end);

	cf.pushDescriptor("Z");
}
 
Example 15
Source File: OpPlus.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	if (this.exitTypeDescriptor == "Ljava/lang/String") {
		mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
		mv.visitInsn(DUP);
		mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V", false);
		walk(mv,cf,getLeftOperand());
		walk(mv,cf,getRightOperand());
		mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false);
	}
	else {
		getLeftOperand().generateCode(mv, cf);
		String leftDesc = getLeftOperand().exitTypeDescriptor;
		CodeFlow.insertNumericUnboxOrPrimitiveTypeCoercion(mv, leftDesc, this.exitTypeDescriptor.charAt(0));
		if (this.children.length > 1) {
			cf.enterCompilationScope();
			getRightOperand().generateCode(mv, cf);
			String rightDesc = getRightOperand().exitTypeDescriptor;
			cf.exitCompilationScope();
			CodeFlow.insertNumericUnboxOrPrimitiveTypeCoercion(mv, rightDesc, this.exitTypeDescriptor.charAt(0));
			switch (this.exitTypeDescriptor.charAt(0)) {
				case 'I':
					mv.visitInsn(IADD);
					break;
				case 'J':
					mv.visitInsn(LADD);
					break;
				case 'F': 
					mv.visitInsn(FADD);
					break;
				case 'D':
					mv.visitInsn(DADD);
					break;				
				default:
					throw new IllegalStateException(
							"Unrecognized exit type descriptor: '" + this.exitTypeDescriptor + "'");
			}
		}
	}
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example 16
Source File: OpEQ.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	String leftDesc = getLeftOperand().exitTypeDescriptor;
	String rightDesc = getRightOperand().exitTypeDescriptor;
	Label elseTarget = new Label();
	Label endOfIf = new Label();
	boolean leftPrim = CodeFlow.isPrimitive(leftDesc);
	boolean rightPrim = CodeFlow.isPrimitive(rightDesc);

	DescriptorComparison dc = DescriptorComparison.checkNumericCompatibility(leftDesc, rightDesc,
			this.leftActualDescriptor, this.rightActualDescriptor);
	
	if (dc.areNumbers && dc.areCompatible) {
		char targetType = dc.compatibleType;
		
		getLeftOperand().generateCode(mv, cf);
		if (!leftPrim) {
			CodeFlow.insertUnboxInsns(mv, targetType, leftDesc);
		}
	
		cf.enterCompilationScope();
		getRightOperand().generateCode(mv, cf);
		cf.exitCompilationScope();
		if (!rightPrim) {
			CodeFlow.insertUnboxInsns(mv, targetType, rightDesc);
		}
		// assert: SpelCompiler.boxingCompatible(leftDesc, rightDesc)
		if (targetType=='D') {
			mv.visitInsn(DCMPL);
			mv.visitJumpInsn(IFNE, elseTarget);
		}
		else if (targetType=='F') {
			mv.visitInsn(FCMPL);		
			mv.visitJumpInsn(IFNE, elseTarget);
		}
		else if (targetType=='J') {
			mv.visitInsn(LCMP);		
			mv.visitJumpInsn(IFNE, elseTarget);
		}
		else if (targetType=='I' || targetType=='Z') {
			mv.visitJumpInsn(IF_ICMPNE, elseTarget);		
		}
		else {
			throw new IllegalStateException("Unexpected descriptor "+leftDesc);
		}
	}
	else {
		getLeftOperand().generateCode(mv, cf);
		if (leftPrim) {
			CodeFlow.insertBoxIfNecessary(mv, leftDesc.charAt(0));
		}
		getRightOperand().generateCode(mv, cf);
		if (rightPrim) {
			CodeFlow.insertBoxIfNecessary(mv, rightDesc.charAt(0));
		}
		Label leftNotNull = new Label();
		mv.visitInsn(DUP_X1); // Dup right on the top of the stack
		mv.visitJumpInsn(IFNONNULL,leftNotNull);
		// Right is null!
		mv.visitInsn(SWAP);
		mv.visitInsn(POP); // remove it
		Label rightNotNull = new Label();
		mv.visitJumpInsn(IFNONNULL, rightNotNull);
		// Left is null too
		mv.visitInsn(ICONST_1);
		mv.visitJumpInsn(GOTO, endOfIf);
		mv.visitLabel(rightNotNull);
		mv.visitInsn(ICONST_0);
		mv.visitJumpInsn(GOTO,endOfIf);
		mv.visitLabel(leftNotNull);
		mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "equals", "(Ljava/lang/Object;)Z", false);
		mv.visitLabel(endOfIf);
		cf.pushDescriptor("Z");
		return;
	}
	mv.visitInsn(ICONST_1);
	mv.visitJumpInsn(GOTO,endOfIf);
	mv.visitLabel(elseTarget);
	mv.visitInsn(ICONST_0);
	mv.visitLabel(endOfIf);
	cf.pushDescriptor("Z");
}
 
Example 17
Source File: OpPlus.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	if ("Ljava/lang/String".equals(this.exitTypeDescriptor)) {
		mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
		mv.visitInsn(DUP);
		mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V", false);
		walk(mv, cf, getLeftOperand());
		walk(mv, cf, getRightOperand());
		mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false);
	}
	else {
		this.children[0].generateCode(mv, cf);
		String leftDesc = this.children[0].exitTypeDescriptor;
		String exitDesc = this.exitTypeDescriptor;
		Assert.state(exitDesc != null, "No exit type descriptor");
		char targetDesc = exitDesc.charAt(0);
		CodeFlow.insertNumericUnboxOrPrimitiveTypeCoercion(mv, leftDesc, targetDesc);
		if (this.children.length > 1) {
			cf.enterCompilationScope();
			this.children[1].generateCode(mv, cf);
			String rightDesc = this.children[1].exitTypeDescriptor;
			cf.exitCompilationScope();
			CodeFlow.insertNumericUnboxOrPrimitiveTypeCoercion(mv, rightDesc, targetDesc);
			switch (targetDesc) {
				case 'I':
					mv.visitInsn(IADD);
					break;
				case 'J':
					mv.visitInsn(LADD);
					break;
				case 'F':
					mv.visitInsn(FADD);
					break;
				case 'D':
					mv.visitInsn(DADD);
					break;
				default:
					throw new IllegalStateException(
							"Unrecognized exit type descriptor: '" + this.exitTypeDescriptor + "'");
			}
		}
	}
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example 18
Source File: OpMinus.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	getLeftOperand().generateCode(mv, cf);
	String leftDesc = getLeftOperand().exitTypeDescriptor;
	CodeFlow.insertNumericUnboxOrPrimitiveTypeCoercion(mv, leftDesc, this.exitTypeDescriptor.charAt(0));
	if (this.children.length > 1) {
		cf.enterCompilationScope();
		getRightOperand().generateCode(mv, cf);
		String rightDesc = getRightOperand().exitTypeDescriptor;
		cf.exitCompilationScope();
		CodeFlow.insertNumericUnboxOrPrimitiveTypeCoercion(mv, rightDesc, this.exitTypeDescriptor.charAt(0));
		switch (this.exitTypeDescriptor.charAt(0)) {
			case 'I':
				mv.visitInsn(ISUB);
				break;
			case 'J':
				mv.visitInsn(LSUB);
				break;
			case 'F':
				mv.visitInsn(FSUB);
				break;
			case 'D':
				mv.visitInsn(DSUB);
				break;
			default:
				throw new IllegalStateException(
						"Unrecognized exit type descriptor: '" + this.exitTypeDescriptor + "'");
		}
	}
	else {
		switch (this.exitTypeDescriptor.charAt(0)) {
			case 'I':
				mv.visitInsn(INEG);
				break;
			case 'J':
				mv.visitInsn(LNEG);
				break;
			case 'F':
				mv.visitInsn(FNEG);
				break;
			case 'D':
				mv.visitInsn(DNEG);
				break;
			default:
				throw new IllegalStateException(
						"Unrecognized exit type descriptor: '" + this.exitTypeDescriptor + "'");
		}
	}
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example 19
Source File: OpPlus.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	if (this.exitTypeDescriptor == "Ljava/lang/String") {
		mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
		mv.visitInsn(DUP);
		mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V", false);
		walk(mv,cf,getLeftOperand());
		walk(mv,cf,getRightOperand());
		mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false);
	}
	else {
		getLeftOperand().generateCode(mv, cf);
		String leftDesc = getLeftOperand().exitTypeDescriptor;
		CodeFlow.insertNumericUnboxOrPrimitiveTypeCoercion(mv, leftDesc, this.exitTypeDescriptor.charAt(0));
		if (this.children.length > 1) {
			cf.enterCompilationScope();
			getRightOperand().generateCode(mv, cf);
			String rightDesc = getRightOperand().exitTypeDescriptor;
			cf.exitCompilationScope();
			CodeFlow.insertNumericUnboxOrPrimitiveTypeCoercion(mv, rightDesc, this.exitTypeDescriptor.charAt(0));
			switch (this.exitTypeDescriptor.charAt(0)) {
				case 'I':
					mv.visitInsn(IADD);
					break;
				case 'J':
					mv.visitInsn(LADD);
					break;
				case 'F': 
					mv.visitInsn(FADD);
					break;
				case 'D':
					mv.visitInsn(DADD);
					break;				
				default:
					throw new IllegalStateException(
							"Unrecognized exit type descriptor: '" + this.exitTypeDescriptor + "'");
			}
		}
	}
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example 20
Source File: OpNE.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	String leftDesc = getLeftOperand().exitTypeDescriptor;
	String rightDesc = getRightOperand().exitTypeDescriptor;
	Label elseTarget = new Label();
	Label endOfIf = new Label();
	boolean leftPrim = CodeFlow.isPrimitive(leftDesc);
	boolean rightPrim = CodeFlow.isPrimitive(rightDesc);

	DescriptorComparison dc = DescriptorComparison.checkNumericCompatibility(leftDesc, rightDesc, leftActualDescriptor, rightActualDescriptor);
	
	if (dc.areNumbers && dc.areCompatible) {
		char targetType = dc.compatibleType;
		
		getLeftOperand().generateCode(mv, cf);
		if (!leftPrim) {
			CodeFlow.insertUnboxInsns(mv, targetType, leftDesc);
		}
	
		cf.enterCompilationScope();
		getRightOperand().generateCode(mv, cf);
		cf.exitCompilationScope();
		if (!rightPrim) {
			CodeFlow.insertUnboxInsns(mv, targetType, rightDesc);
		}
		// assert: SpelCompiler.boxingCompatible(leftDesc, rightDesc)
		if (targetType == 'D') {
			mv.visitInsn(DCMPL);
			mv.visitJumpInsn(IFEQ, elseTarget);
		}
		else if (targetType == 'F') {
			mv.visitInsn(FCMPL);		
			mv.visitJumpInsn(IFEQ, elseTarget);
		}
		else if (targetType == 'J') {
			mv.visitInsn(LCMP);		
			mv.visitJumpInsn(IFEQ, elseTarget);
		}
		else if (targetType == 'I' || targetType == 'Z') {
			mv.visitJumpInsn(IF_ICMPEQ, elseTarget);		
		}
		else {
			throw new IllegalStateException("Unexpected descriptor "+leftDesc);
		}
	}
	else {
		getLeftOperand().generateCode(mv, cf);
		getRightOperand().generateCode(mv, cf);
		mv.visitJumpInsn(IF_ACMPEQ, elseTarget);
	}
	mv.visitInsn(ICONST_1);
	mv.visitJumpInsn(GOTO,endOfIf);
	mv.visitLabel(elseTarget);
	mv.visitInsn(ICONST_0);
	mv.visitLabel(endOfIf);
	cf.pushDescriptor("Z");
}