简体   繁体   中英

Get Intellij Idea to highlight incompatible compiler code?

So we ran into an issue where javac can compile something but eclipse can't . This may be a javac bug, but I suspect that neither side is going to fix the problem considering how long the initial jdk bug report has been there.

Error is: The method apply(Collection) in the type GenericsEclipseCompilerIssue.CollectionToString is not applicable for the arguments (Collection)

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();
        }
    }
}

Is there any way to get Intellij Idea to highlight this so that I don't accidentally introduce this issue?

In this scenario, it'd make more sense to do as the Romans do. Since everyone is using the Eclipse compiler, you should be too (at least until that policy decides to change).

To do this, under Settings > Build, Execution and Deployment > Compiler > Java Compiler, switch the setting from Javac to Eclipse. Now, IntelliJ will make use of the Eclipse compiler to build your code.

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