001: /*
002: * Spoon - http://spoon.gforge.inria.fr/
003: * Copyright (C) 2006 INRIA Futurs <renaud.pawlak@inria.fr>
004: *
005: * This software is governed by the CeCILL-C License under French law and
006: * abiding by the rules of distribution of free software. You can use, modify
007: * and/or redistribute the software under the terms of the CeCILL-C license as
008: * circulated by CEA, CNRS and INRIA at http://www.cecill.info.
009: *
010: * This program is distributed in the hope that it will be useful, but WITHOUT
011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: * FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
013: *
014: * The fact that you are presently reading this means that you have had
015: * knowledge of the CeCILL-C license and that you accept its terms.
016: */
017:
018: package spoon.support.reflect.code;
019:
020: import java.util.ArrayList;
021: import java.util.List;
022:
023: import spoon.reflect.code.CtBlock;
024: import spoon.reflect.code.CtCodeElement;
025: import spoon.reflect.code.CtInvocation;
026: import spoon.reflect.code.CtStatement;
027: import spoon.reflect.code.CtStatementList;
028: import spoon.reflect.declaration.CtSimpleType;
029: import spoon.reflect.visitor.CtVisitor;
030: import spoon.reflect.visitor.Filter;
031: import spoon.reflect.visitor.Query;
032: import spoon.reflect.visitor.filter.TypeFilter;
033:
034: public class CtBlockImpl<R> extends CtStatementImpl implements
035: CtBlock<R> {
036: private static final long serialVersionUID = 1L;
037:
038: List<CtStatement> statements = new ArrayList<CtStatement>();
039:
040: public void accept(CtVisitor visitor) {
041: visitor.visitCtBlock(this );
042: }
043:
044: public List<CtStatement> getStatements() {
045: return statements;
046: }
047:
048: public void insertBegin(CtStatementList<?> statements) {
049: List<CtInvocation<?>> invocations = Query.getElements(this ,
050: new TypeFilter<CtInvocation<?>>(CtInvocation.class));
051: if (invocations.size() > 0) {
052: CtInvocation<?> invoc = invocations.get(0);
053: if (invoc.getExecutable().getSimpleName().startsWith(
054: "<init>")) {
055: invoc.insertAfter(statements);
056: return;
057: }
058: }
059: for (CtStatement s : statements.getStatements()) {
060: s.setParent(this );
061: }
062: getStatements().addAll(0, statements.getStatements());
063: }
064:
065: public void insertBegin(CtStatement statement) {
066: List<CtInvocation<?>> invocations = Query.getElements(this ,
067: new TypeFilter<CtInvocation<?>>(CtInvocation.class));
068: if (invocations.size() > 0) {
069: CtInvocation<?> invoc = invocations.get(0);
070: if (invoc.getExecutable().getSimpleName().startsWith(
071: "<init>")) {
072: invoc.insertAfter(statement);
073: return;
074: }
075: }
076: statement.setParent(this );
077: getStatements().add(0, statement);
078: }
079:
080: public void insertEnd(CtStatement statement) {
081: getStatements().add(statement);
082: statement.setParent(this );
083: }
084:
085: public void insertEnd(CtStatementList<?> statements) {
086: for (CtStatement s : statements.getStatements()) {
087: insertEnd(s);
088: }
089: }
090:
091: public void insertAfter(
092: Filter<? extends CtStatement> insertionPoints,
093: CtStatement statement) {
094: for (CtStatement e : Query.getElements(this , insertionPoints)) {
095: e.insertAfter(statement);
096: }
097: }
098:
099: public void insertAfter(
100: Filter<? extends CtStatement> insertionPoints,
101: CtStatementList<?> statements) {
102: for (CtStatement e : Query.getElements(this , insertionPoints)) {
103: e.insertAfter(statements);
104: }
105: }
106:
107: public void insertBefore(
108: Filter<? extends CtStatement> insertionPoints,
109: CtStatement statement) {
110: for (CtStatement e : Query.getElements(this , insertionPoints)) {
111: e.insertBefore(statement);
112: }
113: }
114:
115: public void insertBefore(
116: Filter<? extends CtStatement> insertionPoints,
117: CtStatementList<?> statements) {
118: for (CtStatement e : Query.getElements(this , insertionPoints)) {
119: e.insertBefore(statements);
120: }
121: }
122:
123: public void setStatements(List<CtStatement> statements) {
124: this .statements = statements;
125: }
126:
127: public R S() {
128: return null;
129: }
130:
131: public void R(R value) {
132:
133: }
134:
135: public CtCodeElement getSubstitution(CtSimpleType<?> targetType) {
136: return getFactory().Core().clone(this);
137: }
138:
139: }
|