1 package spoon.support.reflect.code; 2 3 import java.util.ArrayList ; 4 import java.util.List ; 5 6 import spoon.reflect.code.CtBlock; 7 import spoon.reflect.code.CtCodeElement; 8 import spoon.reflect.code.CtInvocation; 9 import spoon.reflect.code.CtStatement; 10 import spoon.reflect.code.CtStatementList; 11 import spoon.reflect.declaration.CtSimpleType; 12 import spoon.reflect.visitor.CtVisitor; 13 import spoon.reflect.visitor.Filter; 14 import spoon.reflect.visitor.Query; 15 import spoon.support.query.TypeFilter; 16 17 public class CtBlockImpl<R> extends CtStatementImpl implements CtBlock<R> { 18 private static final long serialVersionUID = 1L; 19 List <CtStatement> statements = new ArrayList <CtStatement>(); 20 21 public void accept(CtVisitor visitor) { 22 visitor.visitCtBlock(this); 23 } 24 25 public List <CtStatement> getStatements() { 26 return statements; 27 } 28 29 public void insertBegin(CtStatementList<?> statements) { 30 List <CtInvocation> invocations = Query.getElements(this, 31 new TypeFilter<CtInvocation>(CtInvocation.class)); 32 if (invocations.size() > 0) { 33 CtInvocation<?> invoc = invocations.get(0); 34 if (invoc.getExecutable().getSimpleName().startsWith("<init>")) { 35 invoc.insertAfter(statements); 36 return; 37 } 38 } 39 for(CtStatement s:statements.getStatements()) { 40 s.setParent(this); 41 } 42 getStatements().addAll(0, statements.getStatements()); 43 } 44 45 public void insertBegin(CtStatement statement) { 46 List <CtInvocation> invocations = Query.getElements(this, 47 new TypeFilter<CtInvocation>(CtInvocation.class)); 48 if (invocations.size() > 0) { 49 CtInvocation<?> invoc = invocations.get(0); 50 if (invoc.getExecutable().getSimpleName().startsWith("<init>")) { 51 invoc.insertAfter(statement); 52 return; 53 } 54 } 55 statement.setParent(this); 56 getStatements().add(0, statement); 57 } 58 59 public void insertEnd(CtStatement statement) { 60 getStatements().add(statement); 61 statement.setParent(this); 62 } 63 64 public void insertEnd(CtStatementList<?> statements) { 65 for (CtStatement s : statements.getStatements()) { 66 insertEnd(s); 67 } 68 } 69 70 public void insertAfter(Filter<? extends CtStatement> insertionPoints, 71 CtStatement statement) { 72 for (CtStatement e : Query.getElements(this, insertionPoints)) { 73 e.insertAfter(statement); 74 } 75 } 76 77 public void insertAfter(Filter<? extends CtStatement> insertionPoints, 78 CtStatementList<?> statements) { 79 for (CtStatement e : Query.getElements(this, insertionPoints)) { 80 e.insertAfter(statements); 81 } 82 } 83 84 public void insertBefore(Filter<? extends CtStatement> insertionPoints, 85 CtStatement statement) { 86 for (CtStatement e : Query.getElements(this, insertionPoints)) { 87 e.insertBefore(statement); 88 } 89 } 90 91 public void insertBefore(Filter<? extends CtStatement> insertionPoints, 92 CtStatementList<?> statements) { 93 for (CtStatement e : Query.getElements(this, insertionPoints)) { 94 e.insertBefore(statements); 95 } 96 } 97 98 public void setStatements(List <CtStatement> statements) { 99 this.statements = statements; 100 } 101 102 public R S() { 103 return null; 104 } 105 106 public void R(R value) { 107 108 } 109 110 public CtCodeElement getSubstitution(CtSimpleType targetType) { 111 return getFactory().Core().clone(this); 112 } 113 114 } 115 | Popular Tags |