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