1 package spoon.support.reflect.code; 2 3 import spoon.reflect.code.CtBlock; 4 import spoon.reflect.code.CtStatement; 5 import spoon.reflect.code.CtStatementList; 6 import spoon.reflect.declaration.CtElement; 7 import spoon.reflect.declaration.CtExecutable; 8 9 public abstract class CtStatementImpl extends CtCodeElementImpl implements 10 CtStatement { 11 12 public static void insertAfter(CtStatement target, CtStatement statement) { 13 CtStatementList<?> sts=target.getFactory().Core().createStatementList(); 14 sts.getStatements().add(statement); 15 insertAfter(target,sts); 16 } 17 18 public static void replace(CtStatement target, CtStatementList statements) { 19 insertAfter(target,statements); 20 CtBlock<?> parentBlock = (CtBlock) target.getParent(); 21 parentBlock.getStatements().remove(target); 22 } 23 24 public static void insertAfter(CtStatement target, CtStatementList<?> statements) { 25 CtElement e = target.getParent(); 26 if (e instanceof CtExecutable) { 27 throw new RuntimeException ( 28 "cannot insert in this context (use insertEnd?)"); 29 } 30 CtBlock<?> parentBlock = (CtBlock) e; 31 int i = 0; 32 for (CtStatement s : parentBlock.getStatements()) { 33 i++; 34 if (s == target) { 35 break; 36 } 37 } 38 for (int j = statements.getStatements().size() - 1; j >= 0; j--) { 39 CtStatement s = statements.getStatements().get(j); 40 parentBlock.getStatements().add(i, s); 41 s.setParent(parentBlock); 42 } 43 } 44 45 public static void insertBefore(CtStatement target, CtStatement statement) { 46 CtStatementList<?> sts=target.getFactory().Core().createStatementList(); 47 sts.getStatements().add(statement); 48 insertBefore(target,sts); 49 } 50 51 public static void insertBefore(CtStatement target, CtStatementList<?> statements) { 52 CtElement e = target.getParent(); 53 if ((e instanceof CtExecutable)) { 54 throw new RuntimeException ( 55 "cannot insert in this context (use insertEnd?)"); 56 } 57 CtBlock<?> parentBlock = (CtBlock) e; 58 int i = 0; 59 for (CtStatement s : parentBlock.getStatements()) { 60 if (s == target) { 61 break; 62 } 63 i++; 64 } 65 for (int j = statements.getStatements().size() - 1; j >= 0; j--) { 66 CtStatement s = statements.getStatements().get(j); 67 parentBlock.getStatements().add(i, s); 68 s.setParent(parentBlock); 69 } 70 } 71 72 public void insertBefore(CtStatement statement) { 73 insertBefore(this, statement); 74 } 75 76 public void insertBefore(CtStatementList statements) { 77 insertBefore(this, statements); 78 } 79 80 public void insertAfter(CtStatement statement) { 81 insertAfter(this, statement); 82 } 83 84 public void insertAfter(CtStatementList statements) { 85 insertAfter(this, statements); 86 } 87 88 public void replace(CtElement element) { 89 if(element instanceof CtStatementList) { 90 CtStatementImpl.replace(this,(CtStatementList)element); 91 } else { 92 super.replace(element); 93 } 94 } 95 96 String label; 97 98 public String getLabel() { 99 return label; 100 } 101 102 public void setLabel(String label) { 103 this.label = label; 104 } 105 106 } 107 | Popular Tags |