Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Print Predicate or Consumer type with Java proposal or Template
Print Predicate or Consumer type with Java proposal or Template [message #1858732] Tue, 18 April 2023 15:30 Go to next message
Florent Jitiaux is currently offline Florent JitiauxFriend
Messages: 1
Registered: April 2023
Junior Member
Hi guys,

I would like to know if it is possible to print the type of a Predicate or a Consumer with a Java Proposal or a Template.
For example when I stream a String List, with methods like #forEach by using the content assist there is a first Java Proposal "forEach(Consumer<? super String> action : void - Stream" to print ".forEach(null)" with null selected. By invoking a second time the content assist, there is a Java Proposal "(String t) -> : void - Consumer" which prints "t -> ".

I would like to also print the type : (String t) ->
Then it is possible to do it with a Java Proposal ? (I didn't find where the Java Proposals are defined)
If not, is it possible to do it with a template ?

Thanks.
Florent
Re: Print Predicate or Consumer type with Java proposal or Template [message #1858739 is a reply to message #1858732] Wed, 19 April 2023 05:33 Go to previous message
Alan Scott is currently offline Alan ScottFriend
Messages: 3
Registered: April 2023
Junior Member
It is possible to print the type of a Predicate or a Consumer using a template in Java. You can define a template for a functional interface that takes a specific type of parameter, such as a String for a Consumer. Here's an example template for a Consumer that takes a String parameter and prints its type:

Consumer<String> ${name} = (${param}) -> {
    System.out.println("Type of ${param}: " + ${param}.getClass().getSimpleName());
};


When you use this template and provide a name for the Consumer and a name for the parameter, it will generate code that creates a Consumer that prints the type of the parameter. For example, if you use the template with the name "printType" and the parameter name "str", it will generate the following code: Heart Touching Birthday Wishes for Special Person

Consumer<String> printType = (str) -> {
    System.out.println("Type of str: " + str.getClass().getSimpleName());
};

You can then use this Consumer in your code to print the type of String objects:

List<String> strings = Arrays.asList("foo", "bar", "baz");
strings.forEach(printType);

This will print the following output:

Type of foo: String
Type of bar: String
Type of baz: String

Regarding Java proposals, they are provided by the IDE you are using, such as Eclipse or IntelliJ IDEA. You can configure the proposals and add your own templates in the settings of your IDE.
Previous Topic:Issue with ecj compiler and Java 11
Next Topic:Formatting source code JDT
Goto Forum:
  


Current Time: Fri Apr 26 13:42:04 GMT 2024

Powered by FUDForum. Page generated in 0.02954 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top