Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to print a functional interface?

Suppose I have an interface:

public interface Function {
    double function (double input);
}

Now, suppose I have created an instance of this interface somewhere in my main class,

Function f = (x) -> x;

How can I go about printing this function, in plain text? So, something a bit like this:

int f (double x) {return x}

Running a .toString on this Function prints something like Main$1@6d06d69c. How can I go about getting the java representation of this interface?

like image 456
MazeOfEncryption Avatar asked Apr 20 '18 18:04

MazeOfEncryption


People also ask

How do I identify a functional interface?

An Interface that contains exactly one abstract method is known as functional interface. It can have any number of default, static methods but can contain only one abstract method. It can also declare methods of object class. Functional Interface is also known as Single Abstract Method Interfaces or SAM Interfaces.

Can you create your own functional interface?

The functional interface is a simple interface with only one abstract method. A lambda expression can be used through a functional interface in Java 8. We can declare our own/custom functional interface by defining the Single Abstract Method (SAM) in an interface.

How do you annotate a functional interface in Java?

@FunctionalInterface annotation is used to ensure that the functional interface can't have more than one abstract method. In case more than one abstract methods are present, the compiler flags an 'Unexpected @FunctionalInterface annotation' message. However, it is not mandatory to use this annotation.

What is a functional interface?

Annotation Type FunctionalInterface Conceptually, a functional interface has exactly one abstract method. Since default methods have an implementation, they are not abstract. If an interface declares an abstract method overriding one of the public methods of java.

What is the purpose of a functional interface?

Functional interfaces provide target types for lambda expressions and method references. Each functional interface has a single abstract method, called the functional method for that functional interface, to which the lambda expression’s parameter and return types are matched or adapted. Hm.

How many methods can be declared in a functional interface?

It can have any number of default, static methods but can contain only one abstract method. It can also declare methods of object class. Functional Interface is also known as Single Abstract Method Interfaces or SAM Interfaces.

How to create our own/custom functional interface in Java?

How to create our own/custom functional interface in Java? The functional interface is a simple interface with only one abstract method. A lambda expression can be used through a functional interface in Java 8. We can declare our own/custom functional interface by defining the Single Abstract Method (SAM) in an interface.

What are some examples of functional interfaces in Java?

Runnable, ActionListener, Comparable are some of the examples of functional interfaces. Before Java 8, we had to create anonymous inner class objects or implement these interfaces.


2 Answers

Remember that the text of a function (otherwise known as "code") only exists when you write it. You compile this to bytecode which is then run on the Java Virtual Machine. At runtime, the original code which you wrote no longer exists and cannot be easily retrieved.

like image 168
Code-Apprentice Avatar answered Oct 19 '22 23:10

Code-Apprentice


Unfortunately, the answer (as of Java 9) is that there isn't a simple way to get the toString() method to give you a human-meaningful value for an arbitrary instance of a functional interface.

Here are a couple of alternatives that are applicable for some use-cases:

  • Instead of using a lambda, implement the interface using a class, and include an appropriate override for the toString() method.

  • Populate a Map<Function, String> with meaningful names for all of your Function instances.

It would be theoretically possible to build a library that can retrieve the ".class" file for (say) a lambda, analyse it, work out what the bytecodes do, and then produce an appropriate summary. But it would be difficult project.


It would be nice if there was a simple, clean solution to this. Maybe "someone" could suggest it as an RFE for a future version of Java.

like image 40
Stephen C Avatar answered Oct 19 '22 23:10

Stephen C