2015-02-11 43 views
1

我想知道如何解決我在JDK 1.7中遇到的編譯失敗問題,該代碼與JDK 1.6完美協作。如何解決這種類型的擦除?

錯誤我: MatchStrings.java:[71,36]錯誤:名稱衝突:以(ElementMatcher)在MatchStrings與(ElementMatcher)在MatchElements具有相同的擦除,但既不隱藏其他

我的問題類:

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

} 
+0

製作方法非靜態,用物化仿製藥或重構不靠這個整個事情。問題是 - 你最初是如何得到這個代碼的? – Ordous 2015-02-11 14:53:54

+2

關於「在Java 6下編譯但不是Java 7」:http://stackoverflow.com/questions/28442177/why-didnt-java-type-erasure-prevents-this-code-from-compiling/28442599#28442599。 – Tom 2015-02-11 14:59:37

+0

@Ordous有些代碼在現實生活中會一直延續下去,如果我想到在我的生活中所有的項目,只有很小的一部分是關於全新的開發,最大的部分是關於維護和開發新功能而客戶往往沒有時間或金錢來重構某些愚蠢的東西。 – napulitano 2015-02-11 15:12:49

回答

0
public static StringMatcherImpl with(ElementMatcher<String> matcher) 
MatchStrings

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

MatchElements型擦除到

with(ElementMatcher matcher) 

後者不再有效,開始與Java 7,如@湯姆說。

如果可能的話,重命名with(ElementMatcher<String> matcher)更具體的東西,像withStringMatcher(ElementMatcher<String> matcher)

+0

你好,是的,這是一個選項,但你的建議我會打破界面。我想知道如果我能在不破壞接口的情況下解決這個問題,否則我會這樣做。感謝您的回答! – napulitano 2015-02-11 15:24:25

+0

我有一個解決方案,以解決這個類型擦除我已經創建了一個名爲StringMatcher的空接口,擴展ElementMatcher ,所以MatchStrings.with()已更改爲; MatchStrings.with(StringMatcher)和類似魔法的類型擦除消失了。但是我想知道上面的原始MatchStrings.with()是否已經是MatchElements.with()的子簽名,那麼這可能是Java 7中的一個新bug嗎? – napulitano 2015-02-11 20:33:10

相關問題