Java compile, the reason for prompting Deadcode

Source: Internet
Author: User

At the time of Project compilation, the compiler discovers that some of the code is useless code, you will be prompted: A line of code is deadcode. Today, compile project found that a certain cycle of this problem, as follows:

     Public voidMouseOver (Finalstring[] xpatharray) {        Final intBrowserType =Globalsettings.browsercoretype; //Selenium doesn ' t support the Safari browser        if(BrowserType = = 4) {Assert.fail ("Mouseover isn't supported for Safari now"); Assert.fail ("Incorrect browser type"); } pause (pause);Robot RB =NULL; Try{RB=NewRobot (); } Catch(awtexception e) {e.printstacktrace (); } rb.mousemove (0, 0); Try{webdriverwait.until (NewExpectedcondition<boolean>() {@Override PublicBoolean apply (Webdriver driver) {Boolean flag=false;  for(String xpath:xpatharray) {webelement we=findelement (XPath);                         if(BrowserType = = 2 | | browsertype = = 5 | | browsertype = = 6) {                            Try{Actions Builder=NewActions (Browsercore);                                Builder.movetoelement (We). Build (). Perform (); Flag=true;  Break; } Catch(Exception e) {logger.error ("Failed to MouseOver" +XPath, E); Flag=false;                            Handlefailure (E.getmessage ()); } logger.info ("Mouseover" +XPath); } Else if(BrowserType = = 1 | | browsertype = = 3) {                            Try {                                 for(inti = 0; I < 5; i++) {//DeadcodeActions Builder =NewActions (Browsercore);                                    Builder.movetoelement (We). Build (). Perform (); Logger.info ("Mouseover" +XPath); Flag=true;  Break; }                            }Catch(Exception e) {logger.error ("Failed to MouseOver" +XPath, E); Flag=false;                            Handlefailure (E.getmessage ()); }                        }                    }                    returnFlag;        }            }); }Catch(Exception e) {handlefailure (E.getmessage ()); }

Analysis of the next, in fact, the second part of the loop will not be executed, because the compiler has checked the status of the flag after execution is true, so gave a deadcode warning. And the code does not actually run the second time during the execution.

As an example:

if (truetrue) {    System.out.println ("will always be executed here");} Else {    System.out.println ("Never be executed here");   }

In the above example, because compiler has determined that the if result will only be true, the statement in else is never executed, so the else is deadcode.

Statements that cannot be pre-judged

Truetrue; if (A && b) {   System.out.println ("will be executed here");} Else {   System.out.println ("Never be executed here");}

In the above example, since A and B are 2 variables, the compiler cannot pre-convict if the result is true, so it does not warn else that the content is deadcode.

Java compile, the reason for prompting Deadcode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.