简体   繁体   中英

How to solve this Type Erasure?

I'm wondering how can I solve this Compilation Failure I got in JDK 1.7, the code works perfectly with JDK 1.6.

Error I got: MatchStrings.java:[71,36] error: name clash: with(ElementMatcher) in MatchStrings and with(ElementMatcher) in MatchElements have the same erasure, yet neither hides the other

My classes in questions:

public class MatchStrings extends MatchElements
{


public static class StringMatcherImpl extends ElementMatcherImpl<String> 
{
    public StringMatcherImpl(ElementMatcher<String> matcher)
    {
        super(matcher);
    }
}

public static class PrefixMatcher implements ElementMatcher<String>
{

    private final String _prefix;

    public PrefixMatcher(String prefix)
    {
        _prefix = prefix;
    }

    @Override
    public boolean matches(String str)
    {
        return str.startsWith(_prefix);
    }
}

public static class SuffixMatcher implements ElementMatcher<String>
{

    private final String _suffix;

    public SuffixMatcher(String suffix)
    {
        _suffix = suffix;
    }

    @Override
    public boolean matches(String str)
    {
        return str.endsWith(_suffix);
    }
}

public static StringMatcherImpl with(ElementMatcher<String> matcher) 
{
    return new StringMatcherImpl(matcher);
}

public static StringMatcherImpl startingWith(String prefix) 
{
    return with(new PrefixMatcher(prefix));
}

public static StringMatcherImpl endingWith(String prefix) 
{
    return with(new SuffixMatcher(prefix));
}

}

public class MatchElements
{

public static class ElementMatcherImpl<E> implements ElementMatcher<E> 
{
    private ElementMatcher<E> _matcher;

    ElementMatcherImpl(ElementMatcher<E> matcher) 
    {
        _matcher = matcher;
    }

    @Override
    public boolean matches(E e)
    {
        return _matcher.matches(e);
    }

    public ElementMatcherImpl<E> and(ElementMatcher<E> m)
    {
        _matcher = new ElementMatcherAndChain<E>(_matcher, m);
        return this;
    }

    public ElementMatcherImpl<E> or(ElementMatcher<E> m)
    {
        _matcher = new ElementMatcherOrChain<E>(_matcher, m);
        return this;
    }

    @Override
    public String toString()
    {
        return _matcher.toString();
    }
}

public static <E> ElementMatcherImpl<E> with(ElementMatcher<E> matcher) 
{
    return new ElementMatcherImpl<E>(matcher);
}

public static <E> ElementMatcherImpl<E> not(ElementMatcher<E> matcher)
{
    return with(new ElementMatcherNegator<E>(matcher));
}

}
public static StringMatcherImpl with(ElementMatcher<String> matcher)

in MatchStrings and

public static <E> ElementMatcherImpl<E> with(ElementMatcher<E> matcher)

in MatchElements type-erase to

with(ElementMatcher matcher)

which is no longer valid, starting with Java 7, as @Tom said.

If possible, rename with(ElementMatcher<String> matcher) to something more specific, like withStringMatcher(ElementMatcher<String> matcher)

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