Interface CtIf

All Superinterfaces:
Cloneable, CtCodeElement, CtElement, CtQueryable, CtStatement, CtVisitable, FactoryAccessor, Serializable, SourcePositionHolder, TemplateParameter<Void>
All Known Implementing Classes:
CtIfImpl

public interface CtIf extends CtStatement, TemplateParameter<Void>
This code element represents an if statement. Example:
     if (1==0) {
        System.out.println("foo");
     } else {
        System.out.println("bar");
     }
 
  • Method Details

    • getCondition

      CtExpression<Boolean> getCondition()
      Gets the boolean expression that represents the if's condition.
    • getElseStatement

      <S extends CtStatement> S getElseStatement()
      Gets the statement executed when the condition is false.

      An else if like

           if (a) {
               doA();
           } else if (b) {
               doB();
           } else {
               doC();
           }
       
      will be represented as
           if (a) {
               doA();
           } else {
               if (b) {
                   doB();
               } else {
                   doC();
               }
           }
       
      To differentiate between an else Block with an if and an else if, CtElement.isImplicit() is set to true.
      Returns:
      the statement of the else or null if no else is specified.
    • getThenStatement

      <S extends CtStatement> S getThenStatement()
      Gets the statement executed when the condition is true.

      This method will return null for if (condition);.

      Returns:
      the statement of the if, in most cases this is a CtBlock.
    • setCondition

      <T extends CtIf> T setCondition(CtExpression<Boolean> expression)
      Sets the boolean expression that represents the if's condition.
    • setElseStatement

      <T extends CtIf> T setElseStatement(CtStatement elseStatement)
      Sets the statement executed when the condition is false.
    • setThenStatement

      <T extends CtIf> T setThenStatement(CtStatement thenStatement)
      Sets the statement executed when the condition is true.
    • clone

      CtIf clone()
      Description copied from interface: CtElement
      Clone the element which calls this method in a new object. Note that that references are kept as is, and thus, so if you clone whole classes or methods, some parts of the cloned element (eg executable references) may still point to the initial element. In this case, consider using methods Refactoring.copyType(CtType) and Refactoring.copyMethod(CtMethod) instead which does additional work beyond cloning.
      Specified by:
      clone in interface CtCodeElement
      Specified by:
      clone in interface CtElement
      Specified by:
      clone in interface CtStatement