private void replace(CtElement e, CtElement op) {
   if (e instanceof CtStatement && op instanceof CtStatement) {
     ((CtStatement) e).replace((CtStatement) op);
     return;
   }
   if (e instanceof CtExpression && op instanceof CtExpression) {
     ((CtExpression) e).replace((CtExpression) op);
     return;
   }
   throw new IllegalArgumentException(e.getClass() + " " + op.getClass());
 }
Exemple #2
0
 @Override
 public void scan(final CtElement element) {
   if (element != null && clazz.isAssignableFrom(element.getClass())) {
     final T potentialVariable = (T) element;
     if (name.equals(potentialVariable.getSimpleName())) {
       // Since the AST is not completely available yet, we can not check if element's
       // parent (ep) contains the innermost element of `stack` (ie). Therefore, we
       // have to check if one of the following condition holds:
       //
       //    1) Does `stack` contain `ep`?
       //    2) Is `ep` the body of one of `stack`'s CtExecutable elements?
       //
       // The first condition is easy to see. If `stack` contains `ep` then `ep` and
       // all it's declared variables are in scope of `ie`. Unfortunately, there is a
       // special case in which a variable (a CtLocalVariable) has been declared in a
       // block (CtBlock) of, for instance, a method. Such a block is not contained in
       // `stack`. This peculiarity calls for the second condition.
       final CtElement parentOfPotentialVariable = potentialVariable.getParent();
       for (final ASTPair astPair : stack) {
         if (astPair.element == parentOfPotentialVariable) {
           finish(potentialVariable);
           return;
         } else if (astPair.element instanceof CtExecutable) {
           final CtExecutable executable = (CtExecutable) astPair.element;
           if (executable.getBody() == parentOfPotentialVariable) {
             finish(potentialVariable);
             return;
           }
         }
       }
     }
   }
   super.scan(element);
 }