Java Code Examples for org.springframework.asm.MethodVisitor#visitInsn()

The following examples show how to use org.springframework.asm.MethodVisitor#visitInsn() . 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: OperatorInstanceof.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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);
}
 
Example 2
Source File: OpMultiply.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(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 3
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 4
Source File: OperatorInstanceof.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);
	CodeFlow.insertBoxIfNecessary(mv, cf.lastDescriptor());
	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);
}
 
Example 5
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 6
Source File: ConstructorReference.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	ReflectiveConstructorExecutor executor = ((ReflectiveConstructorExecutor) this.cachedExecutor);
	Constructor<?> constructor = executor.getConstructor();		
	String classDesc = constructor.getDeclaringClass().getName().replace('.', '/');
	mv.visitTypeInsn(NEW, classDesc);
	mv.visitInsn(DUP);
	// children[0] is the type of the constructor, don't want to include that in argument processing
	SpelNodeImpl[] arguments = new SpelNodeImpl[children.length - 1];
	System.arraycopy(children, 1, arguments, 0, children.length - 1);
	generateCodeForArguments(mv, cf, constructor, arguments);	
	mv.visitMethodInsn(INVOKESPECIAL, classDesc, "<init>", CodeFlow.createSignatureDescriptor(constructor), false);
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example 7
Source File: InlineList.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
void generateClinitCode(String clazzname, String constantFieldName, MethodVisitor mv, CodeFlow codeflow, boolean nested) {
	mv.visitTypeInsn(NEW, "java/util/ArrayList");
	mv.visitInsn(DUP);
	mv.visitMethodInsn(INVOKESPECIAL, "java/util/ArrayList", "<init>", "()V", false);
	if (!nested) {
		mv.visitFieldInsn(PUTSTATIC, clazzname, constantFieldName, "Ljava/util/List;");
	}
	int childCount = getChildCount();
	for (int c = 0; c < childCount; c++) {
		if (!nested) {
			mv.visitFieldInsn(GETSTATIC, clazzname, constantFieldName, "Ljava/util/List;");
		}
		else {
			mv.visitInsn(DUP);
		}
		// The children might be further lists if they are not constants. In this
		// situation do not call back into generateCode() because it will register another clinit adder.
		// Instead, directly build the list here:
		if (children[c] instanceof InlineList) {
			((InlineList)children[c]).generateClinitCode(clazzname, constantFieldName, mv, codeflow, true);
		}
		else {
			children[c].generateCode(mv, codeflow);
			if (CodeFlow.isPrimitive(codeflow.lastDescriptor())) {
				CodeFlow.insertBoxIfNecessary(mv, codeflow.lastDescriptor().charAt(0));
			}
		}
		mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "add", "(Ljava/lang/Object;)Z", true);
		mv.visitInsn(POP);
	}
}
 
Example 8
Source File: CodeFlow.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Create the optimal instruction for loading a number on the stack.
 * @param mv where to insert the bytecode
 * @param value the value to be loaded
 */
public static void insertOptimalLoad(MethodVisitor mv, int value) {
	if (value < 6) {
		mv.visitInsn(ICONST_0+value);
	}
	else if (value < Byte.MAX_VALUE) {
		mv.visitIntInsn(BIPUSH, value);
	}
	else if (value < Short.MAX_VALUE) {
		mv.visitIntInsn(SIPUSH, value);
	}
	else {
		mv.visitLdcInsn(value);
	}
}
 
Example 9
Source File: IntLiteral.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	int intValue = (Integer) this.value.getValue();
	if (intValue == -1) {
		// Not sure we can get here because -1 is OpMinus
		mv.visitInsn(ICONST_M1);
	}
	else if (intValue >= 0 && intValue < 6) {
		mv.visitInsn(ICONST_0 + intValue);
	}
	else {
		mv.visitLdcInsn(intValue);
	}
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example 10
Source File: ConstructorReference.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	ReflectiveConstructorExecutor executor = ((ReflectiveConstructorExecutor) this.cachedExecutor);
	Constructor<?> constructor = executor.getConstructor();		
	String classDesc = constructor.getDeclaringClass().getName().replace('.', '/');
	mv.visitTypeInsn(NEW, classDesc);
	mv.visitInsn(DUP);
	// children[0] is the type of the constructor, don't want to include that in argument processing
	SpelNodeImpl[] arguments = new SpelNodeImpl[children.length - 1];
	System.arraycopy(children, 1, arguments, 0, children.length - 1);
	generateCodeForArguments(mv, cf, constructor, arguments);	
	mv.visitMethodInsn(INVOKESPECIAL, classDesc, "<init>", CodeFlow.createSignatureDescriptor(constructor), false);
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example 11
Source File: InlineList.java    From spring-analysis-note with MIT License 5 votes vote down vote up
void generateClinitCode(String clazzname, String constantFieldName, MethodVisitor mv, CodeFlow codeflow, boolean nested) {
	mv.visitTypeInsn(NEW, "java/util/ArrayList");
	mv.visitInsn(DUP);
	mv.visitMethodInsn(INVOKESPECIAL, "java/util/ArrayList", "<init>", "()V", false);
	if (!nested) {
		mv.visitFieldInsn(PUTSTATIC, clazzname, constantFieldName, "Ljava/util/List;");
	}
	int childCount = getChildCount();
	for (int c = 0; c < childCount; c++) {
		if (!nested) {
			mv.visitFieldInsn(GETSTATIC, clazzname, constantFieldName, "Ljava/util/List;");
		}
		else {
			mv.visitInsn(DUP);
		}
		// The children might be further lists if they are not constants. In this
		// situation do not call back into generateCode() because it will register another clinit adder.
		// Instead, directly build the list here:
		if (this.children[c] instanceof InlineList) {
			((InlineList)this.children[c]).generateClinitCode(clazzname, constantFieldName, mv, codeflow, true);
		}
		else {
			this.children[c].generateCode(mv, codeflow);
			String lastDesc = codeflow.lastDescriptor();
			if (CodeFlow.isPrimitive(lastDesc)) {
				CodeFlow.insertBoxIfNecessary(mv, lastDesc.charAt(0));
			}
		}
		mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "add", "(Ljava/lang/Object;)Z", true);
		mv.visitInsn(POP);
	}
}
 
Example 12
Source File: CodeFlow.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Create the optimal instruction for loading a number on the stack.
 * @param mv where to insert the bytecode
 * @param value the value to be loaded
 */
public static void insertOptimalLoad(MethodVisitor mv, int value) {
	if (value < 6) {
		mv.visitInsn(ICONST_0+value);
	}
	else if (value < Byte.MAX_VALUE) {
		mv.visitIntInsn(BIPUSH, value);
	}
	else if (value < Short.MAX_VALUE) {
		mv.visitIntInsn(SIPUSH, value);
	}
	else {
		mv.visitLdcInsn(value);
	}
}
 
Example 13
Source File: OpMinus.java    From spring-analysis-note with MIT License 4 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(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 (targetDesc) {
			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 14
Source File: Operator.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/** 
 * Numeric comparison operators share very similar generated code, only differing in 
 * two comparison instructions.
 */
protected void generateComparisonCode(MethodVisitor mv, CodeFlow cf, int compInstruction1, int compInstruction2) {
	String leftDesc = getLeftOperand().exitTypeDescriptor;
	String rightDesc = getRightOperand().exitTypeDescriptor;
	
	boolean unboxLeft = !CodeFlow.isPrimitive(leftDesc);
	boolean unboxRight = !CodeFlow.isPrimitive(rightDesc);
	DescriptorComparison dc = DescriptorComparison.checkNumericCompatibility(leftDesc, rightDesc,
			this.leftActualDescriptor, this.rightActualDescriptor);
	char targetType = dc.compatibleType;//CodeFlow.toPrimitiveTargetDesc(leftDesc);
	
	getLeftOperand().generateCode(mv, cf);
	if (unboxLeft) {
		CodeFlow.insertUnboxInsns(mv, targetType, leftDesc);
	}

	cf.enterCompilationScope();
	getRightOperand().generateCode(mv, cf);
	cf.exitCompilationScope();
	if (unboxRight) {
		CodeFlow.insertUnboxInsns(mv, targetType, rightDesc);
	}

	// assert: SpelCompiler.boxingCompatible(leftDesc, rightDesc)
	Label elseTarget = new Label();
	Label endOfIf = new Label();
	if (targetType=='D') {
		mv.visitInsn(DCMPG);
		mv.visitJumpInsn(compInstruction1, elseTarget);
	}
	else if (targetType=='F') {
		mv.visitInsn(FCMPG);		
		mv.visitJumpInsn(compInstruction1, elseTarget);
	}
	else if (targetType=='J') {
		mv.visitInsn(LCMP);		
		mv.visitJumpInsn(compInstruction1, elseTarget);
	}
	else if (targetType=='I') {
		mv.visitJumpInsn(compInstruction2, elseTarget);
	}
	else {
		throw new IllegalStateException("Unexpected descriptor "+leftDesc);
	}

	// Other numbers are not yet supported (isCompilable will not have returned true)
	mv.visitInsn(ICONST_1);
	mv.visitJumpInsn(GOTO,endOfIf);
	mv.visitLabel(elseTarget);
	mv.visitInsn(ICONST_0);
	mv.visitLabel(endOfIf);
	cf.pushDescriptor("Z");
}
 
Example 15
Source File: OpMinus.java    From java-technology-stack with MIT License 4 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(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 (targetDesc) {
			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 16
Source File: Operator.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Numeric comparison operators share very similar generated code, only differing in
 * two comparison instructions.
 */
protected void generateComparisonCode(MethodVisitor mv, CodeFlow cf, int compInstruction1, int compInstruction2) {
	SpelNodeImpl left = getLeftOperand();
	SpelNodeImpl right = getRightOperand();
	String leftDesc = left.exitTypeDescriptor;
	String rightDesc = right.exitTypeDescriptor;

	boolean unboxLeft = !CodeFlow.isPrimitive(leftDesc);
	boolean unboxRight = !CodeFlow.isPrimitive(rightDesc);
	DescriptorComparison dc = DescriptorComparison.checkNumericCompatibility(
			leftDesc, rightDesc, this.leftActualDescriptor, this.rightActualDescriptor);
	char targetType = dc.compatibleType;  // CodeFlow.toPrimitiveTargetDesc(leftDesc);

	cf.enterCompilationScope();
	left.generateCode(mv, cf);
	cf.exitCompilationScope();
	if (unboxLeft) {
		CodeFlow.insertUnboxInsns(mv, targetType, leftDesc);
	}

	cf.enterCompilationScope();
	right.generateCode(mv, cf);
	cf.exitCompilationScope();
	if (unboxRight) {
		CodeFlow.insertUnboxInsns(mv, targetType, rightDesc);
	}

	// assert: SpelCompiler.boxingCompatible(leftDesc, rightDesc)
	Label elseTarget = new Label();
	Label endOfIf = new Label();
	if (targetType == 'D') {
		mv.visitInsn(DCMPG);
		mv.visitJumpInsn(compInstruction1, elseTarget);
	}
	else if (targetType == 'F') {
		mv.visitInsn(FCMPG);
		mv.visitJumpInsn(compInstruction1, elseTarget);
	}
	else if (targetType == 'J') {
		mv.visitInsn(LCMP);
		mv.visitJumpInsn(compInstruction1, elseTarget);
	}
	else if (targetType == 'I') {
		mv.visitJumpInsn(compInstruction2, elseTarget);
	}
	else {
		throw new IllegalStateException("Unexpected descriptor " + leftDesc);
	}

	// Other numbers are not yet supported (isCompilable will not have returned true)
	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: CodeFlow.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Insert any necessary numeric conversion bytecodes based upon what is on the stack and the desired target type.
 * @param mv the method visitor into which instructions should be placed
 * @param targetDescriptor the (primitive) descriptor of the target type
 * @param stackDescriptor the descriptor of the operand on top of the stack
 */
public static void insertAnyNecessaryTypeConversionBytecodes(MethodVisitor mv, char targetDescriptor, String stackDescriptor) {
	if (CodeFlow.isPrimitive(stackDescriptor)) {
		char stackTop = stackDescriptor.charAt(0);
		if (stackTop=='I' || stackTop=='B' || stackTop=='S' || stackTop=='C') {
			if (targetDescriptor=='D') {
				mv.visitInsn(I2D);
			}
			else if (targetDescriptor=='F') {
				mv.visitInsn(I2F);
			}
			else if (targetDescriptor=='J') {
				mv.visitInsn(I2L);
			}
			else if (targetDescriptor=='I') {
				// nop
			}
			else {
				throw new IllegalStateException("cannot get from "+stackTop+" to "+targetDescriptor);
			}
		}
		else if (stackTop=='J') {
			if (targetDescriptor=='D') {
				mv.visitInsn(L2D);
			}
			else if (targetDescriptor=='F') {
				mv.visitInsn(L2F);
			}
			else if (targetDescriptor=='J') {
				// nop
			}
			else if (targetDescriptor=='I') {
				mv.visitInsn(L2I);
			}
			else {
				throw new IllegalStateException("cannot get from "+stackTop+" to "+targetDescriptor);
			}
		}
		else if (stackTop=='F') {
			if (targetDescriptor=='D') {
				mv.visitInsn(F2D);
			}
			else if (targetDescriptor=='F') {
				// nop
			}
			else if (targetDescriptor=='J') {
				mv.visitInsn(F2L);
			}
			else if (targetDescriptor=='I') {
				mv.visitInsn(F2I);
			}
			else {
				throw new IllegalStateException("cannot get from "+stackTop+" to "+targetDescriptor);
			}
		}
		else if (stackTop=='D') {
			if (targetDescriptor=='D') {
				// nop
			}
			else if (targetDescriptor=='F') {
				mv.visitInsn(D2F);
			}
			else if (targetDescriptor=='J') {
				mv.visitInsn(D2L);
			}
			else if (targetDescriptor=='I') {
				mv.visitInsn(D2I);
			}
			else {
				throw new IllegalStateException("cannot get from "+stackDescriptor+" to "+targetDescriptor);
			}
		}
	}
}
 
Example 18
Source File: SpelCompiler.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Generate the class that encapsulates the compiled expression and define it.
 * The  generated class will be a subtype of CompiledExpression.
 * @param expressionToCompile the expression to be compiled
 * @return the expression call, or {@code null} if the decision was to opt out of
 * compilation during code generation
 */
@SuppressWarnings("unchecked")
private Class<? extends CompiledExpression> createExpressionClass(SpelNodeImpl expressionToCompile) {
	// Create class outline 'spel/ExNNN extends org.springframework.expression.spel.CompiledExpression'
	String clazzName = "spel/Ex" + getNextSuffix();
	ClassWriter cw = new ExpressionClassWriter();
	cw.visit(V1_5, ACC_PUBLIC, clazzName, null, "org/springframework/expression/spel/CompiledExpression", null);

	// Create default constructor
	MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
	mv.visitCode();
	mv.visitVarInsn(ALOAD, 0);
	mv.visitMethodInsn(INVOKESPECIAL, "org/springframework/expression/spel/CompiledExpression",
			"<init>", "()V", false);
	mv.visitInsn(RETURN);
	mv.visitMaxs(1, 1);
	mv.visitEnd();

	// Create getValue() method
	mv = cw.visitMethod(ACC_PUBLIC, "getValue",
			"(Ljava/lang/Object;Lorg/springframework/expression/EvaluationContext;)Ljava/lang/Object;", null,
			new String[ ]{"org/springframework/expression/EvaluationException"});
	mv.visitCode();

	CodeFlow cf = new CodeFlow(clazzName, cw);

	// Ask the expression AST to generate the body of the method
	try {
		expressionToCompile.generateCode(mv, cf);
	}
	catch (IllegalStateException ex) {
		if (logger.isDebugEnabled()) {
			logger.debug(expressionToCompile.getClass().getSimpleName() +
					".generateCode opted out of compilation: " + ex.getMessage());
		}
		return null;
	}

	CodeFlow.insertBoxIfNecessary(mv, cf.lastDescriptor());
	if ("V".equals(cf.lastDescriptor())) {
		mv.visitInsn(ACONST_NULL);
	}
	mv.visitInsn(ARETURN);

	mv.visitMaxs(0, 0);  // not supplied due to COMPUTE_MAXS
	mv.visitEnd();
	cw.visitEnd();

	cf.finish();

	byte[] data = cw.toByteArray();
	// TODO need to make this conditionally occur based on a debug flag
	// dump(expressionToCompile.toStringAST(), clazzName, data);
	return (Class<? extends CompiledExpression>) this.ccl.defineClass(clazzName.replaceAll("/", "."), data);
}
 
Example 19
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 20
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);
}