Code evaluation as a debugging tool

Gentlemen, developers of java applications. Today, you are presented with a simple way of using code evaluation, the implementation of which will allow you to execute arbitrary code in a running application, which in turn will save a lot of time on CI / CD.





Why do I need it?

If you have to develop in a microservice architecture, especially in a large company, then most likely you are familiar with the situation when you can see how an application works "in combat" only at stands where there are integrations with other microservices. And since not all things can be checked locally on stubs, in order to test a particular hypothesis, you have to push new code (not the fact that it works correctly) into the repository ... run it through CI / CD ... and only then through logs to understand that somewhere something went wrong. And it's good if the logs immediately show what you were wrong about, because otherwise this process of pushing and running through pipelines can become your cycle of samsara.





The execution of dynamically entered code will help solve this problem.





How it works?

As we all know, groovy is a fully Java-compatible programming language with dynamic compilation . These two features of groovy will help us to implement code evaluation for java applications. You can easily find out how to add groovy support to a java project yourself. And I will give an example of how to implement code evaluation (in a sense, similar to what you can see during debugging in your IDE).





1) Create a groovy class, and in it a template string in which we will place the class and placeholder for the dynamically entered code. An example of such a line:





def EXPRESSION_CLASS_TEMPLATE = """
package dev.toliyansky.eval.service
class ExpressionClass implements java.util.function.Supplier<Object> {
	def get() {
		%s
	}
}
"""
      
      



Note: package must be the same as the class in which you will write this code.





Why we are implementing Supplier will be described below.





2) Dynamically compile and load this class.





REST , code .





def finalClassCode = String.format(EXPRESSION_CLASS_TEMPLATE, code)
def supplier = groovyClassLoader.parseClass(finalClassCode)
                                .getDeclaredConstructor()
                                .newInstance() as Supplier<Object>
def result = supplier.get()
      
      



%s .





, . Supplier , . Supplier<Object> .





.





code evaluation

web kubernetes. - , , , CI/CD POD , . , , - NullPointerException.





HTTP , applicationContext, . , CI/CD. . .





web spring boot

groovy java , , , , - evaluator-spring-boot-starter





, , spring boot starter. web http://host:port/eval WEB-UI, , . , . , , curl wget POD, http://host:port/eval/groovy GET POST .





, - readme .





Screenshot of WEB-UI evaluator-spring-boot-starter
WEB-UI evaluator-spring-boot-starter

  • Demonstrated how code evaluation can save time debugging an application





  • Demonstrated how to implement code evaluation in a java project





  • Demonstrated a ready-made solution in the form of a spring boot starter.








All Articles