2016-06-22 19 views
3

我想使不可變的我的DTO對象。爲此,我想使用http://immutables.github.io/如何使用不可變的庫與接口<T>

我的下一個傳統的層級:

public interface Interface1 extends Serializable{ 
    public void method1(); 
} 

public interface Interface2<T> extends Interface1{ 
    public void method2(); 
} 

public class Implementation1<T> implements Interface2<T>{ 
    public void method1(){ } 

    public void method2(){ 
     return null; 
    } 
} 

爲了保持向後兼容性,我想imeplements一個新的接口和實現下面給出:

@Value.Immutable 
public Interface3<T> extends Interface2<T>{ 
    public void method3(); 
} 

這應該產生ImmutableInterface3類是能夠獲得建造者並能夠建立班級實施。

的問題是,我不能使用下面的語句:

Interface3<Object> immutableImplementationOfInterface3 = ImmutableInterface3<Object>.builder().build(); 

是否有任何問題與此解決方案?

+0

爲什麼你不能用這種說法?這個班不存在嗎?該方法不存在嗎? – ipsi

+0

我只是發佈解決方案。謝謝你的幫助。 –

回答

2

解決的辦法是:

Interface3<Object> immutableImplementationOfInterface3 = ImmutableInterface3.<Object> builder().build();