简体   繁体   中英

Calling Java method with void arguments

I'm trying to implement stopwatch class that will wrap all the required methods that i'm trying to measure

The main purpose is to create an framwork that when ever you want to measure the execution time of a method you will change the exiting code from:

<Classname> x = A();

or

A();

To:

<Classname> x = PSW.startWatch(MethodToWatch.B).invoke("123123123", A());

or

PSW.startWatch(MethodToWatch.B).invoke("123123123", A());

This is the code:

public class PerformanceStopWatch {
    private long start;
    private long end;
    private String uniqueID;
    private MethodToWatch methodName;

    public enum MethodToWatch {
        A,
        B
    }

    public PerformanceStopWatch startWatch(MethodToWatch methodName) {
        this.methodName = methodName;
        start();
        return this;
    }

    public void invoke(String uniqeID) {
        this.uniqueID = uniqeID;
        stop();
    }

    public void invoke(UUID machineId, void a) {
       invoke(machineId);
    }

    private void start() {
        start = System.nanoTime();
    }
    private void stop() {
        end = System.nanoTime();
        System.out.println("[PerformanceStopWatch|"+methodName.name()+"] - Unique ID: "+ uniqueID + " Took: <"+(end-start)+"> Nanosec");
    }
}

public void a() {
PerformanceStopWatch PSW = new PerformanceStopWatch();
..
..
..
PSW.startWatch(MethodToWatch.B).invoke("123123123", b());

}

public void b() {
...
...
..

}

The problem is that the compiler dose not allow me to use

public void invoke(UUID machineId, void a) {
    }

void is illegle type.

any idea?

You can try Void insteed of void (Capital V) as the second one is not a type in strict sense. Void on the other hand is. But in general if you want to invoke with "void" you simply ommit arguments (talking about reflection here)

EDIT: to your comment

public void invoke(UUID machineId, Void a) { //note the capital V
   invoke(machineId);
}

Will compile just fine, but I dont see any usable usecase for such method signature.

It looks like you want to pass a method to the invoke() method. Since the method you are passing returns nothing and has no arguments, you should use Runnable interface :

public void invoke(UUID machineId, Runnable a) {
   invoke(machineId);
   a.run();
}

and

PSW.startWatch(MethodToWatch.B).invoke("123123123", () -> {b();});

or (if you are not using Java 8) :

PSW.startWatch(MethodToWatch.B).invoke("123123123",
                                       new Runnable() {
                                             public void run() {
                                               b();
                                             }
                                       });

What exactly do you want to do? There are no objects or values of type void , so if it would have been allowed, you would not be able to call the method.

Maybe what you want is to be able to pass an object of any type. In this case you can use Object :

public void invoke(UUID machineId, Object a) {
    invoke(machineId);
}

Since every class is a subclass of Object , now you can pass any object as the second argument to this method, including arrays. You can also pass primitive types (they will be wrapped to their corresponding wrapper class).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM