org.springframework.expression.spel.CompiledExpression Java Examples

The following examples show how to use org.springframework.expression.spel.CompiledExpression. 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: SpelCompiler.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Attempt compilation of the supplied expression. A check is made to see
 * if it is compilable before compilation proceeds. The check involves
 * visiting all the nodes in the expression Ast and ensuring enough state
 * is known about them that bytecode can be generated for them.
 * @param expression the expression to compile
 * @return an instance of the class implementing the compiled expression,
 * or {@code null} if compilation is not possible
 */
@Nullable
public CompiledExpression compile(SpelNodeImpl expression) {
	if (expression.isCompilable()) {
		if (logger.isDebugEnabled()) {
			logger.debug("SpEL: compiling " + expression.toStringAST());
		}
		Class<? extends CompiledExpression> clazz = createExpressionClass(expression);
		if (clazz != null) {
			try {
				return ReflectionUtils.accessibleConstructor(clazz).newInstance();
			}
			catch (Throwable ex) {
				throw new IllegalStateException("Failed to instantiate CompiledExpression", ex);
			}
		}
	}

	if (logger.isDebugEnabled()) {
		logger.debug("SpEL: unable to compile " + expression.toStringAST());
	}
	return null;
}
 
Example #2
Source File: SpelCompiler.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Attempt compilation of the supplied expression. A check is made to see
 * if it is compilable before compilation proceeds. The check involves
 * visiting all the nodes in the expression Ast and ensuring enough state
 * is known about them that bytecode can be generated for them.
 * @param expression the expression to compile
 * @return an instance of the class implementing the compiled expression,
 * or {@code null} if compilation is not possible
 */
@Nullable
public CompiledExpression compile(SpelNodeImpl expression) {
	if (expression.isCompilable()) {
		if (logger.isDebugEnabled()) {
			logger.debug("SpEL: compiling " + expression.toStringAST());
		}
		Class<? extends CompiledExpression> clazz = createExpressionClass(expression);
		if (clazz != null) {
			try {
				return ReflectionUtils.accessibleConstructor(clazz).newInstance();
			}
			catch (Throwable ex) {
				throw new IllegalStateException("Failed to instantiate CompiledExpression", ex);
			}
		}
	}

	if (logger.isDebugEnabled()) {
		logger.debug("SpEL: unable to compile " + expression.toStringAST());
	}
	return null;
}
 
Example #3
Source File: SpelCompiler.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Attempt compilation of the supplied expression. A check is made to see
 * if it is compilable before compilation proceeds. The check involves
 * visiting all the nodes in the expression Ast and ensuring enough state
 * is known about them that bytecode can be generated for them.
 * @param expression the expression to compile
 * @return an instance of the class implementing the compiled expression,
 * or {@code null} if compilation is not possible
 */
public CompiledExpression compile(SpelNodeImpl expression) {
	if (expression.isCompilable()) {
		if (logger.isDebugEnabled()) {
			logger.debug("SpEL: compiling " + expression.toStringAST());
		}
		Class<? extends CompiledExpression> clazz = createExpressionClass(expression);
		if (clazz != null) {
			try {
				return clazz.newInstance();
			}
			catch (Throwable ex) {
				throw new IllegalStateException("Failed to instantiate CompiledExpression", ex);
			}
		}
	}

	if (logger.isDebugEnabled()) {
		logger.debug("SpEL: unable to compile " + expression.toStringAST());
	}
	return null;
}
 
Example #4
Source File: SpelCompiler.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Attempt compilation of the supplied expression. A check is
 * made to see if it is compilable before compilation proceeds. The
 * check involves visiting all the nodes in the expression Ast and
 * ensuring enough state is known about them that bytecode can
 * be generated for them.
 * @param expression the expression to compile
 * @return an instance of the class implementing the compiled expression, or null
 * if compilation is not possible
 */
public CompiledExpression compile(SpelNodeImpl expression) {
	if (expression.isCompilable()) {
		if (logger.isDebugEnabled()) {
			logger.debug("SpEL: compiling " + expression.toStringAST());
		}
		Class<? extends CompiledExpression> clazz = createExpressionClass(expression);
		if (clazz != null) {
			try {
				return clazz.newInstance();
			}
			catch (Throwable ex) {
				throw new IllegalStateException("Failed to instantiate CompiledExpression", ex);
			}
		}
	}

	if (logger.isDebugEnabled()) {
		logger.debug("SpEL: unable to compile " + expression.toStringAST());
	}
	return null;
}
 
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);
}
 
Example #9
Source File: SpelCompiler.java    From spring-analysis-note with MIT License 3 votes vote down vote up
/**
 * Load a compiled expression class. Makes sure the classloaders aren't used too much
 * because they anchor compiled classes in memory and prevent GC.  If you have expressions
 * continually recompiling over time then by replacing the classloader periodically
 * at least some of the older variants can be garbage collected.
 * @param name name of the class
 * @param bytes bytecode for the class
 * @return the Class object for the compiled expression
 */
@SuppressWarnings("unchecked")
private Class<? extends CompiledExpression> loadClass(String name, byte[] bytes) {
	if (this.ccl.getClassesDefinedCount() > CLASSES_DEFINED_LIMIT) {
		this.ccl = new ChildClassLoader(this.ccl.getParent());
	}
	return (Class<? extends CompiledExpression>) this.ccl.defineClass(name, bytes);
}
 
Example #10
Source File: SpelCompiler.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * Load a compiled expression class. Makes sure the classloaders aren't used too much
 * because they anchor compiled classes in memory and prevent GC.  If you have expressions
 * continually recompiling over time then by replacing the classloader periodically
 * at least some of the older variants can be garbage collected.
 * @param name name of the class
 * @param bytes bytecode for the class
 * @return the Class object for the compiled expression
 */
@SuppressWarnings("unchecked")
private Class<? extends CompiledExpression> loadClass(String name, byte[] bytes) {
	if (this.ccl.getClassesDefinedCount() > CLASSES_DEFINED_LIMIT) {
		this.ccl = new ChildClassLoader(this.ccl.getParent());
	}
	return (Class<? extends CompiledExpression>) this.ccl.defineClass(name, bytes);
}