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

The following examples show how to use org.springframework.asm.MethodVisitor#visitMethodInsn() . 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: Elvis.java    From lams with GNU General Public License v2.0 6 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();
	this.children[0].generateCode(mv, cf);
	CodeFlow.insertBoxIfNecessary(mv, cf.lastDescriptor().charAt(0));
	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);
	this.children[1].generateCode(mv, cf);
	if (!CodeFlow.isPrimitive(this.exitTypeDescriptor)) {
		CodeFlow.insertBoxIfNecessary(mv, cf.lastDescriptor().charAt(0));
	}
	mv.visitLabel(endOfIf);
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example 2
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 3
Source File: SpelCompilationCoverageTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void generateCode(String propertyName, MethodVisitor mv, CodeFlow cf) {
	String descriptor = cf.lastDescriptor();
	if (descriptor == null) {
		cf.loadTarget(mv);
	}
	mv.visitLdcInsn(propertyName);
	mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "get","(Ljava/lang/Object;)Ljava/lang/Object;",true);
}
 
Example 4
Source File: CodeFlow.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * For numbers, use the appropriate method on the number to convert it to the primitive type requested.
 * @param mv the method visitor into which instructions should be inserted
 * @param targetDescriptor the primitive type desired as output
 * @param stackDescriptor the descriptor of the type on top of the stack
 */
public static void insertUnboxNumberInsns(MethodVisitor mv, char targetDescriptor, String stackDescriptor) {
	switch (targetDescriptor) {
		case 'D':
			if (stackDescriptor.equals("Ljava/lang/Object")) {
				mv.visitTypeInsn(CHECKCAST, "java/lang/Number");
			}
			mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Number", "doubleValue", "()D", false);
			break;
		case 'F':
			if (stackDescriptor.equals("Ljava/lang/Object")) {
				mv.visitTypeInsn(CHECKCAST, "java/lang/Number");
			}
			mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Number", "floatValue", "()F", false);
			break;
		case 'J':
			if (stackDescriptor.equals("Ljava/lang/Object")) {
				mv.visitTypeInsn(CHECKCAST, "java/lang/Number");
			}
			mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Number", "longValue", "()J", false);
			break;
		case 'I':
			if (stackDescriptor.equals("Ljava/lang/Object")) {
				mv.visitTypeInsn(CHECKCAST, "java/lang/Number");
			}
			mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Number", "intValue", "()I", false);
			break;
		// does not handle Z, B, C, S
		default:
			throw new IllegalArgumentException("Unboxing should not be attempted for descriptor '" + targetDescriptor + "'");
	}
}
 
Example 5
Source File: FunctionReference.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override 
public void generateCode(MethodVisitor mv,CodeFlow cf) {
	String classDesc = this.method.getDeclaringClass().getName().replace('.', '/');
	generateCodeForArguments(mv, cf, this.method, this.children);
	mv.visitMethodInsn(INVOKESTATIC, classDesc, this.method.getName(),
			CodeFlow.createSignatureDescriptor(this.method), false);
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example 6
Source File: MapAccessor.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void generateCode(String propertyName, MethodVisitor mv, CodeFlow cf) {
	String descriptor = cf.lastDescriptor();
	if (descriptor == null || !descriptor.equals("Ljava/util/Map")) {
		if (descriptor == null) {
			cf.loadTarget(mv);
		}
		CodeFlow.insertCheckCast(mv, "Ljava/util/Map");
	}
	mv.visitLdcInsn(propertyName);
	mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "get","(Ljava/lang/Object;)Ljava/lang/Object;",true);
}
 
Example 7
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 8
Source File: MapAccessor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void generateCode(String propertyName, MethodVisitor mv, CodeFlow cf) {
	String descriptor = cf.lastDescriptor();
	if (descriptor == null || !descriptor.equals("Ljava/util/Map")) {
		if (descriptor == null) {
			cf.loadTarget(mv);
		}
		CodeFlow.insertCheckCast(mv, "Ljava/util/Map");
	}
	mv.visitLdcInsn(propertyName);
	mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "get","(Ljava/lang/Object;)Ljava/lang/Object;",true);
}
 
Example 9
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 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: 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 13
Source File: CodeFlow.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * 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 ch the descriptor of the type that might need boxing
 */
public static void insertBoxIfNecessary(MethodVisitor mv, char ch) {
	switch (ch) {
		case 'Z':
			mv.visitMethodInsn(INVOKESTATIC, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;", false);
			break;
		case 'B':
			mv.visitMethodInsn(INVOKESTATIC, "java/lang/Byte", "valueOf", "(B)Ljava/lang/Byte;", false);
			break;
		case 'C':
			mv.visitMethodInsn(INVOKESTATIC, "java/lang/Character", "valueOf", "(C)Ljava/lang/Character;", false);
			break;
		case 'D':
			mv.visitMethodInsn(INVOKESTATIC, "java/lang/Double", "valueOf", "(D)Ljava/lang/Double;", false);
			break;
		case 'F':
			mv.visitMethodInsn(INVOKESTATIC, "java/lang/Float", "valueOf", "(F)Ljava/lang/Float;", false);
			break;
		case 'I':
			mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;", false);
			break;
		case 'J':
			mv.visitMethodInsn(INVOKESTATIC, "java/lang/Long", "valueOf", "(J)Ljava/lang/Long;", false);
			break;
		case 'S':
			mv.visitMethodInsn(INVOKESTATIC, "java/lang/Short", "valueOf", "(S)Ljava/lang/Short;", false);
			break;
		case 'L':
		case 'V':
		case '[':
			// no box needed
			break;
		default:
			throw new IllegalArgumentException("Boxing should not be attempted for descriptor '" + ch + "'");
	}
}
 
Example 14
Source File: ReadMapAccessor.java    From poi-tl with Apache License 2.0 5 votes vote down vote up
@Override
public void generateCode(String propertyName, MethodVisitor mv, CodeFlow cf) {
    String descriptor = cf.lastDescriptor();
    if (descriptor == null || !descriptor.equals("Ljava/util/Map")) {
        if (descriptor == null) {
            cf.loadTarget(mv);
        }
        CodeFlow.insertCheckCast(mv, "Ljava/util/Map");
    }
    mv.visitLdcInsn(propertyName);
    mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "get", "(Ljava/lang/Object;)Ljava/lang/Object;", true);
}
 
Example 15
Source File: CodeFlow.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * For numbers, use the appropriate method on the number to convert it to the primitive type requested.
 * @param mv the method visitor into which instructions should be inserted
 * @param targetDescriptor the primitive type desired as output
 * @param stackDescriptor the descriptor of the type on top of the stack
 */
public static void insertUnboxNumberInsns(MethodVisitor mv, char targetDescriptor, String stackDescriptor) {
	switch (targetDescriptor) {
		case 'D':
			if (stackDescriptor.equals("Ljava/lang/Object")) {
				mv.visitTypeInsn(CHECKCAST, "java/lang/Number");
			}
			mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Number", "doubleValue", "()D", false);
			break;
		case 'F':
			if (stackDescriptor.equals("Ljava/lang/Object")) {
				mv.visitTypeInsn(CHECKCAST, "java/lang/Number");
			}
			mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Number", "floatValue", "()F", false);
			break;
		case 'J':
			if (stackDescriptor.equals("Ljava/lang/Object")) {
				mv.visitTypeInsn(CHECKCAST, "java/lang/Number");
			}
			mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Number", "longValue", "()J", false);
			break;
		case 'I':
			if (stackDescriptor.equals("Ljava/lang/Object")) {
				mv.visitTypeInsn(CHECKCAST, "java/lang/Number");
			}
			mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Number", "intValue", "()I", false);
			break;
		// does not handle Z, B, C, S
		default:
			throw new IllegalArgumentException("Unboxing should not be attempted for descriptor '" + targetDescriptor + "'");
	}
}
 
Example 16
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 17
Source File: CodeFlow.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * If the codeflow shows the last expression evaluated to java.lang.Boolean then
 * insert the necessary instructions to unbox that to a boolean primitive.
 * @param mv the visitor into which new instructions should be inserted
 */
public void unboxBooleanIfNecessary(MethodVisitor mv) {
	if (lastDescriptor().equals("Ljava/lang/Boolean")) {
		mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Boolean", "booleanValue", "()Z", false);
	}
}
 
Example 18
Source File: CodeFlow.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Insert any necessary cast and value call to convert from a boxed type to a
 * primitive value.
 * @param mv the method visitor into which instructions should be inserted
 * @param ch the primitive type desired as output
 * @param stackDescriptor the descriptor of the type on top of the stack
 */
public static void insertUnboxInsns(MethodVisitor mv, char ch, String stackDescriptor) {
	switch (ch) {
		case 'Z':
			if (!stackDescriptor.equals("Ljava/lang/Boolean")) {
				mv.visitTypeInsn(CHECKCAST, "java/lang/Boolean");
			}
			mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Boolean", "booleanValue", "()Z", false);
			break;
		case 'B':
			if (!stackDescriptor.equals("Ljava/lang/Byte")) {
				mv.visitTypeInsn(CHECKCAST, "java/lang/Byte");
			}
			mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Byte", "byteValue", "()B", false);
			break;
		case 'C':
			if (!stackDescriptor.equals("Ljava/lang/Character")) {
				mv.visitTypeInsn(CHECKCAST, "java/lang/Character");
			}
			mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Character", "charValue", "()C", false);
			break;
		case 'D':
			if (!stackDescriptor.equals("Ljava/lang/Double")) {
				mv.visitTypeInsn(CHECKCAST, "java/lang/Double");
			}
			mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Double", "doubleValue", "()D", false);
			break;
		case 'F':
			if (!stackDescriptor.equals("Ljava/lang/Float")) {
				mv.visitTypeInsn(CHECKCAST, "java/lang/Float");
			}
			mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Float", "floatValue", "()F", false);
			break;
		case 'I':
			if (!stackDescriptor.equals("Ljava/lang/Integer")) {
				mv.visitTypeInsn(CHECKCAST, "java/lang/Integer");
			}
			mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Integer", "intValue", "()I", false);
			break;
		case 'J':
			if (!stackDescriptor.equals("Ljava/lang/Long")) {
				mv.visitTypeInsn(CHECKCAST, "java/lang/Long");
			}
			mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Long", "longValue", "()J", false);
			break;
		case 'S':
			if (!stackDescriptor.equals("Ljava/lang/Short")) {
				mv.visitTypeInsn(CHECKCAST, "java/lang/Short");
			}
			mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Short", "shortValue", "()S", false);
			break;
		default:
			throw new IllegalArgumentException("Unboxing should not be attempted for descriptor '" + ch + "'");
	}
}
 
Example 19
Source File: SpelCompiler.java    From spring-analysis-note with MIT License 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
 */
@Nullable
private Class<? extends CompiledExpression> createExpressionClass(SpelNodeImpl expressionToCompile) {
	// Create class outline 'spel/ExNNN extends org.springframework.expression.spel.CompiledExpression'
	String className = "spel/Ex" + getNextSuffix();
	ClassWriter cw = new ExpressionClassWriter();
	cw.visit(V1_5, ACC_PUBLIC, className, 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(className, 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 loadClass(StringUtils.replace(className, "/", "."), data);
}
 
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);
}