如何防止java程序中的rawtype警告?

时间:2014-11-26 11:58:44

标签: java

我在我的program.i中使用了原始类型警告 @SuppressWarnings(" rawtypes"),但是如果有其他方法来解决这个问题会更方便。任何人都可以帮助我 我在下面的NamingEnumeration部分代码中有这个警告:

try {

        String[] attrIDs = { Constants.UNIQUEMEMBER };
        Attributes answer = iniDirContext.getAttributes("cn=" + groupName
                + "," + Constants.PROJECT_DN, attrIDs);
        NamingEnumeration ae = answer.getAll();
        Attribute attr = (Attribute) ae.next();
        for (NamingEnumeration e = attr.getAll(); e.hasMore();) {
            String str = e.next().toString();
            if (getAppUserRole(str))
                count = count + 1;

        }
    } catch (Exception e1) {
        e1.printStackTrace();
    }

2 个答案:

答案 0 :(得分:3)

使用list1 NamingEnumeration<NameClassPair>代替原始类型。同样适用于results1 - 使用NamingEnumeration<SearchResult>。这样,你甚至不必演员。

将您的代码与此进行比较:

ArrayList list = new ArrayList(); //raw typed
list.add("entry"); //add a string
String s = (String)list.get(0); //but return type is Object, because list in raw typed

ArrayList<String> list2 = new ArrayList<String>(); //strong typed
list2.add("entry"); //add a string
String s2 = list2.get(0); //return type is String, because list2 is strong typed

整个代码变为:

SearchControls controls = new SearchControls();
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration<NameClassPair> list1 = iniDirContext.list(Constants.ORG_SEARCH_BASE);
while (list1.hasMore()) {
    String base = list1.next().getName() + "," + Constants.ORG_SEARCH_BASE; //no cast needed, list1 is strong typed
    SearchControls searchControls = new SearchControls();
    searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    String filter = "(&(objectclass=groupOfUniqueNames))";
    NamingEnumeration<SearchResult> results1 = iniDirContext.search(base, filter, searchControls);
    while (results1.hasMore()) {
        SearchResult searchResult = results1.next();
        Attributes attributes = searchResult.getAttributes();
        Attribute cn = attributes.get(Constants.CN);
        Attribute address = attributes.get(Constants.POSTAL_ADDRESS);
        if (cn.get().equals(oranizationData.getOrgName()) && address.equals(oranizationData.getOrgAddress())) {
          return false;
        }
    }
}

答案 1 :(得分:0)

使用<Class_Name>,很可能是NameClassPair。类似于NameClassPair<String, String>