Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Check if a given method name exist in compile time

I'm developing a utility class to handle Actions from Java Swing components; I would like to know if there is a way of checking if a given method name (that will be accessed by reflections) exists in compile time, and show a compiler error if not?

--update

Ok, looks like I was not clear, lets talk about the details:

I have a class called TomAction that I use to simplify simple actions declarations in my project. Instead of write something like:

class MyClass {

    public MyClass() {
    Icon icon = null; // putting null to simplify the example
    JButton btn = new JButton(new AbstractAction("Click to go!", icon) {
        public void actionPerformed(ActionEvent ev) {
        try {
            go();
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "Fail to execute 'go' task.";
            JOptionPane.showMessageDialog(null, msg, "Fail", JOptionPane.ERROR_MESSAGE);
        }
        }
    });
    }

    private void go() {
    // do task
    }

}

..I just write:

class MyClass {

    public MyClass() {
    String msg = "Fail to execute 'go' task.";
    Icon icon = null; // putting null to simplify the example
    TomAction act = new TomAction("go", this, "Click to go!", msg, icon);
    JButton btn = new JButton(act);
    }

    private void go() {
    // do task
    }

}

..and the program have the same behaviour.

The problem is that if I type a wrong method name as argument to TomAction, I will see it just in runtime. I would like to see it in compile time. Got it? :)

By the way, this class is working very fine, I just want to do this enhancement.

--update

studing the Annotations approach

like image 929
The Student Avatar asked Apr 09 '10 13:04

The Student


People also ask

How do you check if a method exists in a class Java?

Use the typeof operator to check if a method exists in a class, e.g. if (typeof myInstance. myMethod === 'function') {} . The typeof operator returns a string that indicates the type of the specific value and will return function if the method exists in the class.

How do you check if a method is called in Java?

While you can replace it with blank == true , which will work fine, it's unnecessary to use the == operator at all. Instead, use if (blank) to check if it is true, and if (! blank) to check if it is false.

What is compile-time type checking?

During compile time, the source code is translated to a byte code like from . java to . class. During compile time the compiler check for the syntax, semantic, and type of the code.

Does function exist in Java?

There are no functions per se in Java. All you've got is methods. To imitate functions, Java generally uses static methods (as in java. lang.


2 Answers

Sounds as if you'd like to set up a contract that a specific method must be implemented.

In Java you usually do this via an interface:

public interface Frobnicator {
  public void frobnicate();
}

Then in your other code, you simply expect to get an object of that type, this way the compiler will verify that the method exists:

public void frobnicateorize(Frobnicator forb) {
  frob.frobnicate();
}

This way you can even avoid using reflection to call the method.

Edit regarding the update: No, you can't have that kind of code be statically checked by the Java compiler. You might have to write your own tool to do that checking for you.

like image 107
Joachim Sauer Avatar answered Oct 01 '22 02:10

Joachim Sauer


No. There is no way to do that.

like image 37
Pablo Santa Cruz Avatar answered Oct 01 '22 01:10

Pablo Santa Cruz