Running Java code inside a Java program

I would like to create an application that, for example, measures the execution time of a specific block of code. In this case, it should have the following structure:

public static long measureExecution(String code){ long start = System.nanoTime(); executeCode(code); // <---- long time = System.nanoTime() - start; return time; } 

I am interested in the method indicated by the arrow, I need some kind of placeholder. How should this method be implemented? Is it possible to execute native Java code inside an executable Java application?

I thought it could be done with some kind of redefinition of the body of other methods, but I cannot figure out how to do it.

Thank you for your opinion!

+6
source share
4 answers

You can pass Runnable :

 public static long measureExecution(Runnable code) { long start = System.nanoTime(); code.run(); long time = System.nanoTime() - start; return time; } 

In the place where you call the method, use an anonymous inner class to port the code you want to measure:

 long time = measureExecution(new Runnable() { @Override public void run() { System.out.println("Do something"); } }); 

(If you are using Java 8, you can use the lambda expression instead of the anonymous inner class, which will make the code shorter and easier to read).

+4
source

You can use OpenHFT / Java-Runtime-Compiler:

https://github.com/OpenHFT/Java-Runtime-Compiler

Alternatively, you can use the ToolProvider class (compiler API) since java 1.6:

 private Path compileJavaFile(Path javaFile, String className) { JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); compiler.run(null, null, null, javaFile.toFile().getAbsolutePath()); return javaFile.getParent().resolve(className); } 
+1
source

You can use Dynamic Proxy to wrap a method call, here is an example:

First you need to create the InvocationHandler class:

 public class MyInvocationHandler implements InvocationHandler { private Object target; public MyInvocationHandler(Object target) { this.target = target; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("About to invoke " + method + "\n with argument " + args); Object rv = method.invoke(target, args); System.out.println(" Call returned " + rv);// here you could print the time instead return rv; } } 

Then create a factory to get the object and wrap it with the previous proxy created.

 public class MyFactory { public static MyInterface getMyInterface() { MyInterface mc = new MyClass(); InvocationHandler h = new MyInvocationHandler(mc); MyInterface mi = (MyInterface) Proxy.newProxyInstance(MyInterface.class.getClassLoader(), new Class[] { MyInterface.class }, h); return mi; } } 

Hope that helps you.

0
source

Is this considered a thought?

 public static void main(String[] args) { long start1=System.nanoTime(); long t=(1+100000)*10000/2; System.out.println(t); long start2=System.nanoTime(); long time=start2-start2; System.out.println(time); } 
-2
source

All Articles