为什么我的代码会导致编译器标记“未检查或不安全的操作”警告?

时间:2012-08-31 19:37:08

标签: java compiler-construction javac unsafe unchecked

我的java编译器抱怨我的代码使用“未经检查或不安全的操作”。知道哪些行在两个代码片段中引起了什么?

    @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用来抑制它,但我想知道原因。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

Class是一个参数化类。你应该使用Class<?>(或者在必要时使用更具体的东西)。当然,编译器错误可能会告诉您问题的确切位置。

此外,这一行是未经检查的演员,但你没有选择:

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