【问题标题】:Why is my code causing "unchecked or unsafe operations" warning being flagged by compiler?为什么我的代码会导致编译器标记“未经检查或不安全的操作”警告?
【发布时间】:2012-08-31 19:37:07
【问题描述】:

我的 java 编译器抱怨我的代码使用了“未经检查或不安全的操作”。知道在两个代码 sn-ps 中哪些行导致了这种情况吗?

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

【问题讨论】:

  • -Xlint:unchecked重新编译以获得更多细节。
  • 删除 @SuppressWarnings("unchecked") 告诉您哪些行导致问题。
  • 哇..这些名字很长! :)
  • Class&lt;?&gt;替换Class有帮助吗?
  • 不要介意未经检查或不安全的操作,您需要担心的是冗长和重复的操作......

标签: java compiler-construction javac unsafe unchecked


【解决方案1】:

Class 是一个参数化的类。你应该使用Class&lt;?&gt;(或者如果需要更具体的东西)。当然,编译器错误可能会告诉你问题出在哪里。

此外,这一行是未经检查的演员表,但你别无选择:

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

【讨论】:

  • 你的意思是这个没有解决方案,我不得不忍受@SuppressWarning?
  • @ikevinjp - 对于invoke() 调用的结果,是的,如果没有未经检查的演员表,你无法在java中解决这个问题(反射不能很好地与泛型配合使用)。
猜你喜欢
  • 1970-01-01
  • 2019-02-09
  • 2010-09-16
  • 2013-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多