java - 为什么我的代码会导致编译器标记 "unchecked or unsafe operations"警告?

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

最佳答案

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

此外,这一行是未经检查的转换,但您对此别无选择:

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

关于java - 为什么我的代码会导致编译器标记 "unchecked or unsafe operations"警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12221516/

相关文章:

java - 使用 JDBC Statement 或PreparedStatement 执行一组查询

java - 有没有一种直接的方法来关闭 java 中的整数位?

compiler-construction - 如果clang是编译器的前端,那么为什么它可以生成可执行文件?

ios - 如何抑制 Xcode 中的警告?

java - Spring Cloud Stream 中的 Autowiring 问题

java - 将背景图像添加到重复的 JPanel

c++ - 与/bigobj 链接时间过长

java - 为什么Java编译器不下载导入的包?

java - 以编程方式编译.java文件,错误

java - 设置类路径。无法识别 javac