Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to always execute a function before and after all methods of a class in Java?

Tags:

java

java-8

HI I have a class below which has functions to pull data from DataServices. I want to always call a method which prints a time at which the method was called and a method which prints time after the method was executed . Basically after every method is executed i want to know how much time it took to execute each method. Is there a generic way to do it instead of adding time at start and end of every function?

class ABC {


    void getAllProducts(){
        // some logic
    }

    void getAllClients(){
        // some logic
    }

    void getAllSymbols(){
        // some logic
    }

 }
like image 534
coder Avatar asked Feb 25 '20 03:02

coder


People also ask

Which method is always executed first in Java?

The main() method is the entry point of each and every Java program. The main() method is required because the compiler starts executing a program from this entry point. The JVM needs to instantiate the class if the main() method is allowed to be non-static.

Do methods go before or after main in Java?

Most (if not all) of the standard JDK libraries will follow it. This is IMHO a good reason to go with the methods-last approach. Regarding the placement of main among the methods, putting it first or last would work. If you find it "special" in some way, then put it dead last in the file.

How do you call multiple methods in Java?

Under this procedure, we have to write the object reference once and then call the methods by separating them with a (dot.). Method chaining in Java is a common syntax to invoke multiple methods calls in OOPs. Each method in chaining returns an object.

Can you have nested methods?

Java does not support “directly” nested methods. Many functional programming languages support method within method. But you can achieve nested method functionality in Java 7 or older version by define local classes, class within method so this does compile.


1 Answers

You can use an “Execute Around” idiom similar to this by Jon Skeet, with the timing surrounding your method call:

public class StackOverflowTest {
  public static void main(String[] args){
    StackOverflowTest test = new StackOverflowTest(); 
    test.timeMethod(test::getAllProducts);
    test.timeMethod(test::getAllClients);
    test.timeMethod(test::getAllSymbols);
  }

  void getAllProducts(){
    System.out.println("getting all Products");
  }

  void getAllClients(){
    System.out.println("getting all Clients");
  }

  void getAllSymbols(){
    System.out.println("getting all Symbols");
  }

  // The "Execute Around" method
  void timeMethod(JustDoIt justDoIt){
    long start = System.nanoTime();
    try {
      justDoIt.doIt();
    } finally {
      long end = System.nanoTime();
      System.out.println("  Time: " + (end - start) / 1000 + " microseconds"");
    }
  }  
}

@FunctionalInterface
interface JustDoIt {
  void doIt();
}

Prints:

getting all Products
  Time: 1817 microseconds
getting all Clients
  Time: 421 microseconds
getting all Symbols
  Time: 418 microseconds

If you don't want your own Interface, you can replace it with a Runnable. This example also uses System.currentTimeMillis() instead of System.nanoTime():

void timeMethod(Runnable toRun){
  long start = System.currentTimeMillis();
  try{
    toRun.run();
  } finally {
    long end = System.currentTimeMillis();
    System.out.println("  Time: " + (end - start) + " miliseconds");
  }
}  
like image 59
Scratte Avatar answered Sep 19 '22 05:09

Scratte