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

The following examples show how to use org.springframework.expression.spel.CodeFlow#pushDescriptor() . 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: IntLiteral.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	Integer intValue = (Integer) this.value.getValue();
	Assert.state(intValue != null, "No int value");
	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 2
Source File: OperatorInstanceof.java    From java-technology-stack 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 3
Source File: OpAnd.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=false; } else { result=rightOperandValue; }
	Label elseTarget = new Label();
	Label endOfIf = new Label();
	cf.enterCompilationScope();
	getLeftOperand().generateCode(mv, cf);
	cf.unboxBooleanIfNecessary(mv);
	cf.exitCompilationScope();
	mv.visitJumpInsn(IFNE, elseTarget);
	mv.visitLdcInsn(0); // FALSE
	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: OpAnd.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=false; } else { result=rightOperandValue; }
	Label elseTarget = new Label();
	Label endOfIf = new Label();
	cf.enterCompilationScope();
	getLeftOperand().generateCode(mv, cf);
	cf.unboxBooleanIfNecessary(mv);
	cf.exitCompilationScope();
	mv.visitJumpInsn(IFNE, elseTarget);
	mv.visitLdcInsn(0); // FALSE
	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 5
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 6
Source File: ConstructorReference.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	ReflectiveConstructorExecutor executor = ((ReflectiveConstructorExecutor) this.cachedExecutor);
	Assert.state(executor != null, "No cached executor");

	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[this.children.length - 1];
	System.arraycopy(this.children, 1, arguments, 0, this.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: VariableReference.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	if (this.name.equals(ROOT)) {
		mv.visitVarInsn(ALOAD,1);
	}
	else {
		mv.visitVarInsn(ALOAD, 2);
		mv.visitLdcInsn(name);
		mv.visitMethodInsn(INVOKEINTERFACE, "org/springframework/expression/EvaluationContext", "lookupVariable", "(Ljava/lang/String;)Ljava/lang/Object;",true);
	}
	CodeFlow.insertCheckCast(mv,this.exitTypeDescriptor);
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example 8
Source File: OpEQ.java    From java-technology-stack 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);
	cf.pushDescriptor("Z");
}
 
Example 9
Source File: Ternary.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	// May reach here without it computed if all elements are literals
	computeExitTypeDescriptor();
	cf.enterCompilationScope();
	this.children[0].generateCode(mv, cf);
	if (!CodeFlow.isPrimitive(cf.lastDescriptor())) {
		CodeFlow.insertUnboxInsns(mv, 'Z', cf.lastDescriptor());
	}
	cf.exitCompilationScope();
	Label elseTarget = new Label();
	Label endOfIf = new Label();
	mv.visitJumpInsn(IFEQ, elseTarget);
	cf.enterCompilationScope();
	this.children[1].generateCode(mv, cf);
	if (!CodeFlow.isPrimitive(this.exitTypeDescriptor)) {
		CodeFlow.insertBoxIfNecessary(mv, cf.lastDescriptor().charAt(0));
	}
	cf.exitCompilationScope();
	mv.visitJumpInsn(GOTO, endOfIf);
	mv.visitLabel(elseTarget);
	cf.enterCompilationScope();
	this.children[2].generateCode(mv, cf);
	if (!CodeFlow.isPrimitive(this.exitTypeDescriptor)) {
		CodeFlow.insertBoxIfNecessary(mv, cf.lastDescriptor().charAt(0));
	}
	cf.exitCompilationScope();
	mv.visitLabel(endOfIf);
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example 10
Source File: IntLiteral.java    From spring4-understanding with Apache License 2.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 11
Source File: FunctionReference.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	Method method = this.method;
	Assert.state(method != null, "No method handle");
	String classDesc = method.getDeclaringClass().getName().replace('.', '/');
	generateCodeForArguments(mv, cf, method, this.children);
	mv.visitMethodInsn(INVOKESTATIC, classDesc, method.getName(),
			CodeFlow.createSignatureDescriptor(method), false);
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example 12
Source File: BooleanLiteral.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	if (this.value == BooleanTypedValue.TRUE) {
		mv.visitLdcInsn(1);
	}
	else {
		mv.visitLdcInsn(0);
	}
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example 13
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 14
Source File: VariableReference.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	if (this.name.equals(ROOT)) {
		mv.visitVarInsn(ALOAD,1);
	}
	else {
		mv.visitVarInsn(ALOAD, 2);
		mv.visitLdcInsn(this.name);
		mv.visitMethodInsn(INVOKEINTERFACE, "org/springframework/expression/EvaluationContext", "lookupVariable", "(Ljava/lang/String;)Ljava/lang/Object;",true);
	}
	CodeFlow.insertCheckCast(mv, this.exitTypeDescriptor);
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example 15
Source File: FunctionReference.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	Method method = this.method;
	Assert.state(method != null, "No method handle");
	String classDesc = method.getDeclaringClass().getName().replace('.', '/');
	generateCodeForArguments(mv, cf, method, this.children);
	mv.visitMethodInsn(INVOKESTATIC, classDesc, method.getName(),
			CodeFlow.createSignatureDescriptor(method), false);
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example 16
Source File: NullLiteral.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	mv.visitInsn(ACONST_NULL);
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example 17
Source File: FloatLiteral.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	mv.visitLdcInsn(this.value.getValue());
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example 18
Source File: MethodReference.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	CachedMethodExecutor executorToCheck = this.cachedExecutor;
	if (executorToCheck == null || !(executorToCheck.get() instanceof ReflectiveMethodExecutor)) {
		throw new IllegalStateException("No applicable cached executor found: " + executorToCheck);
	}

	ReflectiveMethodExecutor methodExecutor = (ReflectiveMethodExecutor) executorToCheck.get();
	Method method = methodExecutor.getMethod();
	boolean isStaticMethod = Modifier.isStatic(method.getModifiers());
	String descriptor = cf.lastDescriptor();

	if (descriptor == null) {
		if (!isStaticMethod) {
			// Nothing on the stack but something is needed
			cf.loadTarget(mv);
		}
	}
	else {
		if (isStaticMethod) {
			// Something on the stack when nothing is needed
			mv.visitInsn(POP);
		}
	}
	
	if (CodeFlow.isPrimitive(descriptor)) {
		CodeFlow.insertBoxIfNecessary(mv, descriptor.charAt(0));
	}

	String classDesc = (Modifier.isPublic(method.getDeclaringClass().getModifiers()) ?
			method.getDeclaringClass().getName().replace('.', '/') :
			methodExecutor.getPublicDeclaringClass().getName().replace('.', '/'));
	if (!isStaticMethod) {
		if (descriptor == null || !descriptor.substring(1).equals(classDesc)) {
			CodeFlow.insertCheckCast(mv, "L" + classDesc);
		}
	}

	generateCodeForArguments(mv, cf, method, this.children);
	mv.visitMethodInsn((isStaticMethod ? INVOKESTATIC : INVOKEVIRTUAL), classDesc, method.getName(),
			CodeFlow.createSignatureDescriptor(method), method.getDeclaringClass().isInterface());
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example 19
Source File: FloatLiteral.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	mv.visitLdcInsn(this.value.getValue());
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example 20
Source File: StringLiteral.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	mv.visitLdcInsn(this.value.getValue());
	cf.pushDescriptor(this.exitTypeDescriptor);
}