2012-08-31 25 views
0

我的java編譯器抱怨說我的代碼使用「未檢查或不安全的操作」。任何想法哪些行是在兩個代碼片段中造成的?爲什麼我的代碼會導致編譯器標記「unchecked或unsafe operations」警告?

@SuppressWarnings("unchecked") 
private final void prepareChannelAccountStringsParserImplementedClassConstructor() { 
    try { 
     String channelAccountStringsParserImplementedClassName = channelIdentifier + "AccountStringsParser"; 
     Class channelAccountStringsParserImplementedClass = Class.forName(channelAccountStringsParserImplementedClassName); 
     Class[] channelAccountStringsParserImplementedClassArguments = new Class[1]; 
     channelAccountStringsParserImplementedClassArguments[0] = String.class; 
     channelAccountStringsParserImplementedClassConstructor = channelAccountStringsParserImplementedClass.getConstructor(channelAccountStringsParserImplementedClassArguments); 
     channelAccountStringsParserImplementedParseMethod = channelAccountStringsParserImplementedClass.getMethod("parse", String.class); 
     channelAccountStringsParserGetRequestIDMethod = channelAccountStringsParserImplementedClass.getMethod("getRequestID"); 
    } catch (ClassNotFoundException classNotFoundException) { 
     NotifyMeListenersLogsDB notifyMeListenersLogsDB = DatabaseController.getInstance().getNotifyMeListenersLogsDB(); 
     notifyMeListenersLogsDB.writeDeveloperException(classNotFoundException); 
    } catch (NoSuchMethodException noSuchMethodException) { 
     NotifyMeListenersLogsDB notifyMeListenersLogsDB = DatabaseController.getInstance().getNotifyMeListenersLogsDB(); 
     notifyMeListenersLogsDB.writeDeveloperException(noSuchMethodException); 
    } 
} 

    @SuppressWarnings("unchecked") 
public synchronized final void requestChannelAccountsCompletionSuccess(String responseContent) { 
    Object channelAccountStringsParserImplementedInstance; 
    ArrayList<ChannelAccount> channelAccounts = null; 
    String requestID = null; 
    try { 
     channelAccountStringsParserImplementedInstance = channelAccountStringsParserImplementedClassConstructor.newInstance(responseContent); 
     channelAccounts = (ArrayList<ChannelAccount>) channelAccountStringsParserImplementedParseMethod.invoke(channelAccountStringsParserImplementedInstance, responseContent); 
     requestID = (String) channelAccountStringsParserGetRequestIDMethod.invoke(channelAccountStringsParserImplementedInstance); 
    } catch (InstantiationException instantiationException) { 
     NotifyMeListenersLogsDB notifyMeListenersLogsDB = DatabaseController.getInstance().getNotifyMeListenersLogsDB(); 
     notifyMeListenersLogsDB.writeDeveloperException(instantiationException); 
    } catch (IllegalAccessException illegalAccessException) { 
     NotifyMeListenersLogsDB notifyMeListenersLogsDB = DatabaseController.getInstance().getNotifyMeListenersLogsDB(); 
     notifyMeListenersLogsDB.writeDeveloperException(illegalAccessException); 
    } catch (InvocationTargetException invocationTargetException) { 
     NotifyMeListenersLogsDB notifyMeListenersLogsDB = DatabaseController.getInstance().getNotifyMeListenersLogsDB(); 
     notifyMeListenersLogsDB.writeDeveloperException(invocationTargetException); 
    } 
    channelAccountStringsParserImplementedInstance = null; 
    try { 
     startChannelConnections(channelAccounts); 
     isStarted = true; 
     LoadBalancer.getInstance().sendSuccessAcknowledgement(requestID); 
    } catch (NotifyMeListenersApplicationInitializationException notifyMeListenersApplicationInitializationException) { 
     NotifyMeListenersLogsDB notifyMeListenersLogsDB = DatabaseController.getInstance().getNotifyMeListenersLogsDB(); 
     notifyMeListenersLogsDB.writeNotifyMeListenersException(notifyMeListenersApplicationInitializationException); 
     System.exit(1); 
    } 
} 

當然,我把@SuppressWarnings壓制它,但我想知道原因。任何人都可以幫忙

+0

重新編譯'-Xlint:unchecked'瞭解更多詳情。 – oldrinb

+1

刪除@SuppressWarnings(「unchecked」),告訴你哪些行導致問題。 – kosa

+3

哇......這些都是很長的名字! :) – Jonatan

回答

1

類是一個參數化類。你應該使用Class<?>(或者如果需要的話可以更具體一些)。當然,編譯器錯誤可能會告訴你確切的問題在哪裏。

而且,這條線是一個未經檢查的演員,但你必須在一個沒有選擇:

channelAccounts = (ArrayList<ChannelAccount>)channelAccountStringsParserImplementedParseMethod.invoke(channelAccountStringsParserImplementedInstance, responseContent); 
+0

你的意思是沒有辦法解決這個這個,我有與@SuppressWarning一起生活? – ikevin8me

+1

@ikevinjp - 對於invoke()調用的結果,是的,如果沒有未經檢查的強制轉換,你不能解決這個問題(反射與泛型沒有很好的兼容)。 – jtahlborn

相關問題