简体   繁体   English

警告:使用-Xlint:unchecked pops编译此代码!

[英]Warning: compile with -Xlint:unchecked pops on this code!

import java.lang.reflect.Method;

public class ClassLoadingTester
{
    public static void main(String a[]){
        try{
            ClassLoader loader=new CustomClassLoader();
            Class c=loader.loadClass("AddClass");
            loader=null;
            Object o=c.newInstance();
            Method m=c.getMethod("getTwoNumbers",new Class[]{String.class,String.class});
            m.invoke(o,new Object[]{"2","3"});

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

This code snippet generates a warning when i compile in java. 当我在Java中编译时,此代码段生成警告。 I think it pertains to this line, 我认为这与这条线有关

Method m=c.getMethod("getTwoNumbers",new Class[]{String.class,String.class});

When I attempt to put <String> like this new Class<String>[]{String.class,String.class} 当我尝试将<String>放置为new Class<String>[]{String.class,String.class}

It generates an error saying generic array type I'm doing my code in BlueJ. 它生成一个错误,指出我正在BlueJ中执行代码的generic array type

But when I leave the code as is it generates that irritating warning ^^. 但是当我按原样保留代码时,它会产生令人讨厌的警告^^。

I just want to get rid of that warning what should I do with that line of code? 我只是想摆脱该警告,该行代码应该怎么做?

You can't create arrays of generic types like Class<String> , hence the error. 您不能创建泛型类型的数组,例如Class<String> ,因此会出错。 So you don't have much choice other than adding a @SuppressWarnings("unchecked") annotation before the statement to silence the warning. 因此,除了在语句之前添加@SuppressWarnings("unchecked")注释以使警告静音之外,您别无选择。

Note that these warnings almost always have a reason, and silencing them this way regularly may easily result in runtime class cast exceptions! 请注意,这些警告几乎总是有原因的,以这种方式使它们静默可能会轻易导致运行时类强制转换异常! So always think three times before using @SuppressWarnings("unchecked") - use it only when you are absolutely sure the cast is safe and there can be no negative consequences. 因此,在使用@SuppressWarnings("unchecked")之前,请务必三思@SuppressWarnings("unchecked") -仅在绝对确定@SuppressWarnings("unchecked")是安全的并且不会有负面影响的情况下使用它。 In this case it looks safe to me to use it. 在这种情况下,使用它对我来说看起来很安全。

int t=10, a=7, b=52, d=97;

if((t + d) / 2 > b && (d - t) % 2 != 0)
   if(a%2==0 || b%2==0)
      System.out.println(--t + a);
   else
      System.out.println(--t - a);
else
   System.out.println(t + ++a);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何设置IntelliJ IDEA以使用-Xlint:unchecked参数编译代码? - How to setup IntelliJ IDEA to compile code with a parameter -Xlint:unchecked? 重新编译 -Xlint:unchecked for details 警告? - Recompile with -Xlint:unchecked for details Warning? 如何使用 -Xlint:unchecked 进行编译? - How do I compile with -Xlint:unchecked? 如何在 Maven 项目中使用 -Xlint:unchecked 进行编译? - How to compile using -Xlint:unchecked in a Maven project? Play框架:如何使用“ -Xlint:unchecked”选项编译应用程序? - Play Framework: How to compile an application with the option “-Xlint:unchecked”? 警告:[unchecked]转换期间未选中的转换 - warning: [unchecked] unchecked conversion duaring compile 注意:Anpr.java 使用未经检查或不安全的操作。 注意:使用 -Xlint 重新编译:未选中 JComboBox 中的详细警告 - Note: Anpr.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details warning in JComboBox 使用 -Xlint 重新编译:详细信息未选中 - Recompile with -Xlint : unchecked for details 代码中未经检查的强制转换警告 - Unchecked cast warning in code 使用-Xlint重新编译:取消选中以获取详细信息 - Recompile with -Xlint:unchecked for details
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM