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

The following examples show how to use org.springframework.asm.MethodVisitor#visitCode() . 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: CodeFlow.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Called after the main expression evaluation method has been generated, this
 * method will callback any registered FieldAdders or ClinitAdders to add any
 * extra information to the class representing the compiled expression.
 */
public void finish() {
	if (this.fieldAdders != null) {
		for (FieldAdder fieldAdder : this.fieldAdders) {
			fieldAdder.generateField(this.classWriter, this);
		}
	}
	if (this.clinitAdders != null) {
		MethodVisitor mv = this.classWriter.visitMethod(ACC_PUBLIC | ACC_STATIC, "<clinit>", "()V", null, null);
		mv.visitCode();
		this.nextFreeVariableId = 0;  // to 0 because there is no 'this' in a clinit
		for (ClinitAdder clinitAdder : this.clinitAdders) {
			clinitAdder.generateCode(mv, this);
		}
		mv.visitInsn(RETURN);
		mv.visitMaxs(0,0);  // not supplied due to COMPUTE_MAXS
		mv.visitEnd();
	}
}
 
Example 2
Source File: CodeFlow.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Called after the main expression evaluation method has been generated, this
 * method will callback any registered FieldAdders or ClinitAdders to add any
 * extra information to the class representing the compiled expression.
 */
public void finish() {
	if (this.fieldAdders != null) {
		for (FieldAdder fieldAdder : this.fieldAdders) {
			fieldAdder.generateField(this.classWriter, this);
		}
	}
	if (this.clinitAdders != null) {
		MethodVisitor mv = this.classWriter.visitMethod(ACC_PUBLIC | ACC_STATIC, "<clinit>", "()V", null, null);
		mv.visitCode();
		this.nextFreeVariableId = 0;  // to 0 because there is no 'this' in a clinit
		for (ClinitAdder clinitAdder : this.clinitAdders) {
			clinitAdder.generateCode(mv, this);
		}
		mv.visitInsn(RETURN);
		mv.visitMaxs(0,0);  // not supplied due to COMPUTE_MAXS
		mv.visitEnd();
	}
}
 
Example 3
Source File: CodeFlow.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Called after the main expression evaluation method has been generated, this
 * method will callback any registered FieldAdders or ClinitAdders to add any
 * extra information to the class representing the compiled expression.
 */
public void finish() {
	if (this.fieldAdders != null) {
		for (FieldAdder fieldAdder : this.fieldAdders) {
			fieldAdder.generateField(this.classWriter, this);
		}
	}
	if (this.clinitAdders != null) {
		MethodVisitor mv = this.classWriter.visitMethod(ACC_PUBLIC | ACC_STATIC, "<clinit>", "()V", null, null);
		mv.visitCode();
		this.nextFreeVariableId = 0;  // to 0 because there is no 'this' in a clinit
		for (ClinitAdder clinitAdder : this.clinitAdders) {
			clinitAdder.generateCode(mv, this);
		}
		mv.visitInsn(RETURN);
		mv.visitMaxs(0,0);  // not supplied due to COMPUTE_MAXS
		mv.visitEnd();
	}
}
 
Example 4
Source File: CodeFlow.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Called after the main expression evaluation method has been generated, this
 * method will callback any registered FieldAdders or ClinitAdders to add any
 * extra information to the class representing the compiled expression.
 */
public void finish() {
	if (fieldAdders != null) {
		for (FieldAdder fieldAdder: fieldAdders) {
			fieldAdder.generateField(cw,this);
		}
	}
	if (clinitAdders != null) {
		MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "<clinit>", "()V", null, null);
		mv.visitCode();
		nextFreeVariableId = 0; // To 0 because there is no 'this' in a clinit
		for (ClinitAdder clinitAdder: clinitAdders) {
			clinitAdder.generateCode(mv, this);
		}
		mv.visitInsn(RETURN);
		mv.visitMaxs(0,0); // not supplied due to COMPUTE_MAXS
		mv.visitEnd();
	}
}
 
Example 5
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 6
Source File: SpelCompiler.java    From java-technology-stack 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 7
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 8
Source File: SpelCompiler.java    From spring4-understanding with Apache License 2.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);
}