From 1a3c8de027302816e222aa1ca2a7a14a6efa69bc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20Vojt=C4=9Bchovsk=C3=BD?=
Date: Tue, 4 Dec 2018 19:03:29 +0100
Subject: [PATCH] refactor: Refactoring
---
.../java/spoon/refactoring/Refactoring.java | 45 ++++++++++++++++---
1 file changed, 38 insertions(+), 7 deletions(-)
diff --git a/src/main/java/spoon/refactoring/Refactoring.java b/src/main/java/spoon/refactoring/Refactoring.java
index e28e2dc7d7d..48faba810e4 100644
--- a/src/main/java/spoon/refactoring/Refactoring.java
+++ b/src/main/java/spoon/refactoring/Refactoring.java
@@ -123,6 +123,7 @@ public static CtType> copyType(final CtType> type) {
final String cloneTypeName = tentativeTypeName.toString();
clone.setSimpleName(cloneTypeName);
type.getPackage().addType(clone);
+ //fix cloned type name
new CtScanner() {
@Override
public void visitCtTypeReference(CtTypeReference reference) {
@@ -132,9 +133,6 @@ public void visitCtTypeReference(CtTypeReference reference) {
if (reference.getDeclaration() == type) {
reference.setSimpleName(cloneTypeName);
}
- if (reference.getDeclaration() != clone) {
- throw new SpoonException("post condition broken " + reference);
- }
super.visitCtTypeReference(reference);
}
@@ -147,9 +145,6 @@ public void visitCtExecutableReference(CtExecutableReference reference) {
if (declaration.hasParent(type)) {
reference.getDeclaringType().setSimpleName(cloneTypeName);
}
- if (!reference.getDeclaration().hasParent(clone)) {
- throw new SpoonException("post condition broken " + reference);
- }
super.visitCtExecutableReference(reference);
}
@@ -163,7 +158,43 @@ public void visitCtFieldReference(CtFieldReference reference) {
if (declaration.hasParent(type)) {
reference.getDeclaringType().setSimpleName(cloneTypeName);
}
- if (reference.getDeclaration() == null || !reference.getDeclaration().hasParent(clone)) {
+ super.visitCtFieldReference(reference);
+ }
+
+ }.scan(clone);
+ //check that everything is OK
+ new CtScanner() {
+ @Override
+ public void visitCtTypeReference(CtTypeReference reference) {
+ if (reference.getDeclaration() == null) {
+ return;
+ }
+ if (reference.getDeclaration().getTopLevelType() != clone) {
+ throw new SpoonException("post condition broken " + reference);
+ }
+ super.visitCtTypeReference(reference);
+ }
+
+ @Override
+ public void visitCtExecutableReference(CtExecutableReference reference) {
+ CtExecutable declaration = reference.getDeclaration();
+ if (declaration == null) {
+ return;
+ }
+ if (!declaration.hasParent(clone)) {
+ throw new SpoonException("post condition broken " + reference);
+ }
+ super.visitCtExecutableReference(reference);
+
+ }
+
+ @Override
+ public void visitCtFieldReference(CtFieldReference reference) {
+ CtField declaration = reference.getDeclaration();
+ if (declaration == null) {
+ return;
+ }
+ if (!declaration.hasParent(clone)) {
throw new SpoonException("post condition broken " + reference);
}
super.visitCtFieldReference(reference);