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 spoon.reflect.code.CtBlock;
021: import spoon.reflect.code.CtStatement;
022: import spoon.reflect.code.CtStatementList;
023: import spoon.reflect.declaration.CtElement;
024: import spoon.reflect.declaration.CtExecutable;
025:
026: public abstract class CtStatementImpl extends CtCodeElementImpl
027: implements CtStatement {
028:
029: public static void insertAfter(CtStatement target,
030: CtStatement statement) {
031: CtStatementList<?> sts = target.getFactory().Core()
032: .createStatementList();
033: sts.getStatements().add(statement);
034: insertAfter(target, sts);
035: }
036:
037: public static void replace(CtStatement target,
038: CtStatementList<?> statements) {
039: insertAfter(target, statements);
040: CtBlock<?> parentBlock = (CtBlock<?>) target.getParent();
041: parentBlock.getStatements().remove(target);
042: }
043:
044: public static void insertAfter(CtStatement target,
045: CtStatementList<?> statements) {
046: CtElement e = target.getParent();
047: if (e instanceof CtExecutable) {
048: throw new RuntimeException(
049: "cannot insert in this context (use insertEnd?)");
050: }
051: CtBlock<?> parentBlock = (CtBlock<?>) e;
052: int i = 0;
053: for (CtStatement s : parentBlock.getStatements()) {
054: i++;
055: if (s == target) {
056: break;
057: }
058: }
059: for (int j = statements.getStatements().size() - 1; j >= 0; j--) {
060: CtStatement s = statements.getStatements().get(j);
061: parentBlock.getStatements().add(i, s);
062: s.setParent(parentBlock);
063: }
064: }
065:
066: public static void insertBefore(CtStatement target,
067: CtStatement statement) {
068: CtStatementList<?> sts = target.getFactory().Core()
069: .createStatementList();
070: sts.getStatements().add(statement);
071: insertBefore(target, sts);
072: }
073:
074: public static void insertBefore(CtStatement target,
075: CtStatementList<?> statements) {
076: CtElement e = target.getParent();
077: if ((e instanceof CtExecutable)) {
078: throw new RuntimeException(
079: "cannot insert in this context (use insertEnd?)");
080: }
081: CtBlock<?> parentBlock = (CtBlock<?>) e;
082: int i = 0;
083: for (CtStatement s : parentBlock.getStatements()) {
084: if (s == target) {
085: break;
086: }
087: i++;
088: }
089: for (int j = statements.getStatements().size() - 1; j >= 0; j--) {
090: CtStatement s = statements.getStatements().get(j);
091: parentBlock.getStatements().add(i, s);
092: s.setParent(parentBlock);
093: }
094: }
095:
096: public void insertBefore(CtStatement statement) {
097: insertBefore(this , statement);
098: }
099:
100: public void insertBefore(CtStatementList<?> statements) {
101: insertBefore(this , statements);
102: }
103:
104: public void insertAfter(CtStatement statement) {
105: insertAfter(this , statement);
106: }
107:
108: public void insertAfter(CtStatementList<?> statements) {
109: insertAfter(this , statements);
110: }
111:
112: public void replace(CtElement element) {
113: if (element instanceof CtStatementList) {
114: CtStatementImpl.replace(this , (CtStatementList<?>) element);
115: } else {
116: super .replace(element);
117: }
118: }
119:
120: String label;
121:
122: public String getLabel() {
123: return label;
124: }
125:
126: public void setLabel(String label) {
127: this.label = label;
128: }
129:
130: }
|