From ef20e879fefc664a7c7f571254d4c03e484af385 Mon Sep 17 00:00:00 2001
From: Pavel Vojtechovsky
Date: Mon, 7 Nov 2016 23:33:00 +0100
Subject: [PATCH] added interface CtBodyHolder
---
.../java/spoon/reflect/code/CtBodyHolder.java | 30 +++++++++++++++++++
src/main/java/spoon/reflect/code/CtCatch.java | 2 +-
src/main/java/spoon/reflect/code/CtLoop.java | 2 +-
src/main/java/spoon/reflect/code/CtTry.java | 2 +-
.../reflect/declaration/CtExecutable.java | 5 ++--
.../compiler/jdt/JDTCommentBuilder.java | 15 ++--------
.../support/reflect/code/CtLambdaImpl.java | 4 +--
.../reflect/declaration/CtExecutableImpl.java | 4 +--
8 files changed, 43 insertions(+), 21 deletions(-)
create mode 100644 src/main/java/spoon/reflect/code/CtBodyHolder.java
diff --git a/src/main/java/spoon/reflect/code/CtBodyHolder.java b/src/main/java/spoon/reflect/code/CtBodyHolder.java
new file mode 100644
index 00000000000..5e3e9ab8c9d
--- /dev/null
+++ b/src/main/java/spoon/reflect/code/CtBodyHolder.java
@@ -0,0 +1,30 @@
+/**
+ * Copyright (C) 2006-2016 INRIA and contributors
+ * Spoon - http://spoon.gforge.inria.fr/
+ *
+ * This software is governed by the CeCILL-C License under French law and
+ * abiding by the rules of distribution of free software. You can use, modify
+ * and/or redistribute the software under the terms of the CeCILL-C license as
+ * circulated by CEA, CNRS and INRIA at http://www.cecill.info.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
+ *
+ * The fact that you are presently reading this means that you have had
+ * knowledge of the CeCILL-C license and that you accept its terms.
+ */
+package spoon.reflect.code;
+
+import spoon.reflect.declaration.CtElement;
+
+/**
+ * This abstract code element defines an element, which contains a body
+ */
+public interface CtBodyHolder extends CtElement {
+
+ /**
+ * Gets the body of this element
+ */
+ CtStatement getBody();
+}
diff --git a/src/main/java/spoon/reflect/code/CtCatch.java b/src/main/java/spoon/reflect/code/CtCatch.java
index 37d03d7edde..392f36abe4b 100644
--- a/src/main/java/spoon/reflect/code/CtCatch.java
+++ b/src/main/java/spoon/reflect/code/CtCatch.java
@@ -21,7 +21,7 @@
*
* @see spoon.reflect.code.CtTry
*/
-public interface CtCatch extends CtCodeElement {
+public interface CtCatch extends CtCodeElement, CtBodyHolder {
/**
* Gets the catch's parameter (a throwable).
diff --git a/src/main/java/spoon/reflect/code/CtLoop.java b/src/main/java/spoon/reflect/code/CtLoop.java
index ac675fe0993..d9ecb873b25 100644
--- a/src/main/java/spoon/reflect/code/CtLoop.java
+++ b/src/main/java/spoon/reflect/code/CtLoop.java
@@ -21,7 +21,7 @@
/**
* This abstract code element defines a loop.
*/
-public interface CtLoop extends CtStatement, TemplateParameter {
+public interface CtLoop extends CtStatement, TemplateParameter, CtBodyHolder {
/**
* Gets the body of this loop.
diff --git a/src/main/java/spoon/reflect/code/CtTry.java b/src/main/java/spoon/reflect/code/CtTry.java
index b1c7b3f73b1..81030580b4b 100644
--- a/src/main/java/spoon/reflect/code/CtTry.java
+++ b/src/main/java/spoon/reflect/code/CtTry.java
@@ -30,7 +30,7 @@
* } catch (Exception ignore) {}
*
*/
-public interface CtTry extends CtStatement, TemplateParameter {
+public interface CtTry extends CtStatement, TemplateParameter, CtBodyHolder {
/**
* Gets the catchers of this try
.
diff --git a/src/main/java/spoon/reflect/declaration/CtExecutable.java b/src/main/java/spoon/reflect/declaration/CtExecutable.java
index 5d8d6fafb8c..fa9500b60b6 100644
--- a/src/main/java/spoon/reflect/declaration/CtExecutable.java
+++ b/src/main/java/spoon/reflect/declaration/CtExecutable.java
@@ -17,6 +17,7 @@
package spoon.reflect.declaration;
import spoon.reflect.code.CtBlock;
+import spoon.reflect.code.CtBodyHolder;
import spoon.reflect.reference.CtExecutableReference;
import spoon.reflect.reference.CtTypeReference;
@@ -27,7 +28,7 @@
* This element represents an executable element such as a method, a
* constructor, or an anonymous block.
*/
-public interface CtExecutable extends CtNamedElement, CtTypedElement {
+public interface CtExecutable extends CtNamedElement, CtTypedElement, CtBodyHolder {
/**
* The separator for a string representation of an executable.
@@ -44,7 +45,7 @@ public interface CtExecutable extends CtNamedElement, CtTypedElement {
/**
* Gets the body expression.
*/
- CtBlock getBody();
+ CtBlock getBody();
/**
* Sets the body expression.
diff --git a/src/main/java/spoon/support/compiler/jdt/JDTCommentBuilder.java b/src/main/java/spoon/support/compiler/jdt/JDTCommentBuilder.java
index 87db69a9ce3..3e34aee37b8 100644
--- a/src/main/java/spoon/support/compiler/jdt/JDTCommentBuilder.java
+++ b/src/main/java/spoon/support/compiler/jdt/JDTCommentBuilder.java
@@ -22,24 +22,21 @@
import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
import spoon.reflect.code.CtBinaryOperator;
import spoon.reflect.code.CtBlock;
+import spoon.reflect.code.CtBodyHolder;
import spoon.reflect.code.CtCase;
-import spoon.reflect.code.CtCatch;
import spoon.reflect.code.CtComment;
import spoon.reflect.code.CtConditional;
import spoon.reflect.code.CtIf;
-import spoon.reflect.code.CtLoop;
import spoon.reflect.code.CtNewArray;
import spoon.reflect.code.CtStatement;
import spoon.reflect.code.CtStatementList;
import spoon.reflect.code.CtSwitch;
-import spoon.reflect.code.CtTry;
import spoon.reflect.cu.CompilationUnit;
import spoon.reflect.cu.SourcePosition;
import spoon.reflect.declaration.CtAnonymousExecutable;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtConstructor;
import spoon.reflect.declaration.CtElement;
-import spoon.reflect.declaration.CtExecutable;
import spoon.reflect.declaration.CtField;
import spoon.reflect.declaration.CtInterface;
import spoon.reflect.declaration.CtMethod;
@@ -436,14 +433,8 @@ public void scan(CtElement element) {
* @return body of element or null if this element has no body
*/
static CtElement getBody(CtElement e) {
- if (e instanceof CtLoop) {
- return ((CtLoop) e).getBody();
- } else if (e instanceof CtExecutable) {
- return ((CtExecutable>) e).getBody();
- } else if (e instanceof CtTry) {
- return ((CtTry) e).getBody();
- } else if (e instanceof CtCatch) {
- return ((CtCatch) e).getBody();
+ if (e instanceof CtBodyHolder) {
+ return ((CtBodyHolder) e).getBody();
}
return null;
}
diff --git a/src/main/java/spoon/support/reflect/code/CtLambdaImpl.java b/src/main/java/spoon/support/reflect/code/CtLambdaImpl.java
index 25975855a30..814e56a4a15 100644
--- a/src/main/java/spoon/support/reflect/code/CtLambdaImpl.java
+++ b/src/main/java/spoon/support/reflect/code/CtLambdaImpl.java
@@ -60,8 +60,8 @@ public C setSimpleName(String simpleName) {
@Override
@SuppressWarnings("unchecked")
- public CtBlock getBody() {
- return (CtBlock) body;
+ public CtBlock getBody() {
+ return (CtBlock) body;
}
@Override
diff --git a/src/main/java/spoon/support/reflect/declaration/CtExecutableImpl.java b/src/main/java/spoon/support/reflect/declaration/CtExecutableImpl.java
index 3600023948f..c1d08d29ba8 100644
--- a/src/main/java/spoon/support/reflect/declaration/CtExecutableImpl.java
+++ b/src/main/java/spoon/support/reflect/declaration/CtExecutableImpl.java
@@ -50,8 +50,8 @@ public CtExecutableImpl() {
@Override
@SuppressWarnings("unchecked")
- public CtBlock getBody() {
- return (CtBlock) body;
+ public CtBlock getBody() {
+ return (CtBlock) body;
}
@Override