0
我正在使用Java AST(JDT),並且必須將org.eclipse.jdt.core.dom.Type
實例添加到原始列表(Java 5之前的代碼),該列表僅包含元素類型Type
(API保證它)。我使用TypeDeclaration.superInterfaceTypes()
訪問列表由於類TypeDeclaration
是如何編寫的(沒有超類接口的setter),我必須直接添加元素,並且不能簡單地創建一個List<Type>
實例,複製元素,添加我的新元素和然後設置新的列表。因此,我最終得到一個編譯器警告。將對象安全地添加到無法設置的原始列表中(Java AST)
這是我當前的代碼:
@SuppressWarnings("unchecked")
private void changeSuperInterface(TypeDeclaration declaration) {
// some code...
Type newInterface = ast.newSimpleType(ast.newName(name));
declaration.superInterfaceTypes().remove(oldInterface);
declaration.superInterfaceTypes().add(newInterface); // Type safety warning here.
}
我能解決這個問題有一個解決方案,更優雅不僅僅是抑制警告?
我希望通過程序化的方式來解決這個問題,但似乎這是最好的解決方案。謝謝您的幫助! – ConveniencePatterns