Bug 480918 - [1.8] Generics code compiles in javac 1.8 but not in eclipse Luna 4.4.2
Summary: [1.8] Generics code compiles in javac 1.8 but not in eclipse Luna 4.4.2
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.4.2   Edit
Hardware: PC Windows 7
: P3 major (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords:
Depends on:
Blocks:
 
Reported: 2015-10-28 15:35 EDT by Prakash Varigonda CLA
Modified: 2024-01-23 02:25 EST (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Prakash Varigonda CLA 2015-10-28 15:35:24 EDT
Generics code snipped below compiles in javac 1.8 but not in eclipse Luna 4.4.2.

Error is: The method apply(Collection<capture#5-of ? super T>) in the type GenericsEclipseCompilerIssue.CollectionToString<capture#5-of ? super T> is not applicable for the arguments (Collection<T>)

Please paste below code to eclipse to reproduce the error. 


import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

public class GenericsEclipseCompilerIssue
{
	
	public static <T> Function<Optional<Collection<T>>, String> optionalCollectionToString(
		final Function<? super T, String> elementTransformer ) {
		return (collection) -> {
			return new CollectionToString<>( elementTransformer )
				.apply( collection.orElse( Collections.emptyList() ) );
		};
	}
	
	static class CollectionToString<E> implements Function<Collection<E>, String> {

		private final Function<? super E, String> elementTransformer;

		public CollectionToString( final Function<? super E, String> elementTransformer )
		{
			this.elementTransformer = elementTransformer;
		}

		@Override
		public String apply( final Collection<E> collection )
		{
			List<String> strings = collection.stream()
				.filter( Objects::nonNull )
				.map( elementTransformer::apply )
				.sorted( String.CASE_INSENSITIVE_ORDER )
				.collect( Collectors.toList() );

			return  strings.toString();
		}
	}
}
Comment 1 Stephan Herrmann CLA 2015-10-28 17:34:03 EDT
Just judging from the error message ecj reports, my first guess would be: the difference between ecj and javac in this case is caused by https://bugs.openjdk.java.net/browse/JDK-8016207

And here's a suggestion to improve the code to be compilable with both compilers:

//---
    public static <T> Function<Optional<Collection<T>>, String>
optionalCollectionToString(
        final Function<? super T, String> elementTransformer ) {
        return (collection) -> {
            return new CollectionToString<T>( elementTransformer )
                .apply( collection.orElse( Collections.emptyList() ) );
        };
    }
//---

I'm suggesting this, because inference of the diamond "new CollectionToString<>(..)" is of limited use in a position with no target type (aka as a "standalone expression").

See that the desired result would also be achieved by providing a target type to the diamond operator like this:

//---
    public static <T> Function<Optional<Collection<T>>, String>
optionalCollectionToString(
        final Function<? super T, String> elementTransformer ) {
        return (collection) -> {
            CollectionToString<T> collToString = new CollectionToString<>( elementTransformer );
            return collToString
                .apply( collection.orElse( Collections.emptyList() ) );
        };
    }
//---

OTOH, if diamond inference only has the single type hint from elementTransformer, then indeed a capture enters the picture and rejecting the program is probably the correct answer.

Does anybody have indications to the contrary?
Comment 2 Eclipse Genie CLA 2022-02-01 19:40:44 EST
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.
Comment 3 Eclipse Genie CLA 2024-01-23 02:25:36 EST
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.