diff --git a/src/main/java/spoon/generating/ReplacementVisitorGenerator.java b/src/main/java/spoon/generating/ReplacementVisitorGenerator.java new file mode 100644 index 00000000000..9550eb1e259 --- /dev/null +++ b/src/main/java/spoon/generating/ReplacementVisitorGenerator.java @@ -0,0 +1,66 @@ +/** + * Copyright (C) 2006-2015 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.generating; + +import spoon.generating.replace.ReplaceScanner; +import spoon.processing.AbstractProcessor; +import spoon.reflect.declaration.CtClass; +import spoon.reflect.declaration.CtPackage; +import spoon.reflect.declaration.CtType; +import spoon.reflect.declaration.ModifierKind; +import spoon.reflect.reference.CtTypeReference; +import spoon.reflect.visitor.CtScanner; +import spoon.reflect.visitor.ReferenceFilter; + +import java.util.List; + +import static spoon.generating.replace.ReplaceScanner.GENERATING_REPLACE_VISITOR; +import static spoon.generating.replace.ReplaceScanner.TARGET_REPLACE_PACKAGE; + +public class ReplacementVisitorGenerator extends AbstractProcessor> { + @Override + public boolean isToBeProcessed(CtType candidate) { + return CtScanner.class.getName().equals(candidate.getQualifiedName()) && super.isToBeProcessed(candidate); + } + + @Override + public void process(CtType element) { + new ReplaceScanner(createReplacementVisitor()).scan(getFactory().Class().get(CtScanner.class)); + } + + private CtClass createReplacementVisitor() { + final CtPackage aPackage = getFactory().Package().getOrCreate(TARGET_REPLACE_PACKAGE); + final CtClass target = getFactory().Class().get(GENERATING_REPLACE_VISITOR); + target.addModifier(ModifierKind.PUBLIC); + aPackage.addType(target); + final List references = target.getReferences(new ReferenceFilter() { + @Override + public boolean matches(CtTypeReference reference) { + return reference != null && GENERATING_REPLACE_VISITOR.equals(reference.getQualifiedName()); + } + + @Override + public Class getType() { + return CtTypeReference.class; + } + }); + for (CtTypeReference reference : references) { + reference.setPackage(aPackage.getReference()); + } + return target; + } +} diff --git a/src/main/java/spoon/generating/replace/CtListener.java b/src/main/java/spoon/generating/replace/CtListener.java new file mode 100644 index 00000000000..721bd4a15ad --- /dev/null +++ b/src/main/java/spoon/generating/replace/CtListener.java @@ -0,0 +1,31 @@ +/** + * Copyright (C) 2006-2015 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.generating.replace; + +import spoon.reflect.declaration.CtElement; + +class CtListener implements ReplaceListener { + private CtElement element; + + CtListener(CtElement element) { + this.element = element; + } + + @Override + public void set(CtElement replace) { + } +} diff --git a/src/main/java/spoon/generating/replace/ReplaceListListener.java b/src/main/java/spoon/generating/replace/ReplaceListListener.java new file mode 100644 index 00000000000..fe81aded2be --- /dev/null +++ b/src/main/java/spoon/generating/replace/ReplaceListListener.java @@ -0,0 +1,23 @@ +/** + * Copyright (C) 2006-2015 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.generating.replace; + +import java.util.List; + +public interface ReplaceListListener { + void set(T replace); +} diff --git a/src/main/java/spoon/generating/replace/ReplaceListener.java b/src/main/java/spoon/generating/replace/ReplaceListener.java new file mode 100644 index 00000000000..0e586db52d2 --- /dev/null +++ b/src/main/java/spoon/generating/replace/ReplaceListener.java @@ -0,0 +1,23 @@ +/** + * Copyright (C) 2006-2015 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.generating.replace; + +import spoon.reflect.declaration.CtElement; + +public interface ReplaceListener { + void set(T replace); +} diff --git a/src/main/java/spoon/generating/replace/ReplaceMapListener.java b/src/main/java/spoon/generating/replace/ReplaceMapListener.java new file mode 100644 index 00000000000..bdf5607f86c --- /dev/null +++ b/src/main/java/spoon/generating/replace/ReplaceMapListener.java @@ -0,0 +1,23 @@ +/** + * Copyright (C) 2006-2015 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.generating.replace; + +import java.util.Map; + +public interface ReplaceMapListener { + void set(T replace); +} diff --git a/src/main/java/spoon/generating/replace/ReplaceScanner.java b/src/main/java/spoon/generating/replace/ReplaceScanner.java new file mode 100644 index 00000000000..385ba9f93db --- /dev/null +++ b/src/main/java/spoon/generating/replace/ReplaceScanner.java @@ -0,0 +1,231 @@ +/** + * Copyright (C) 2006-2015 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.generating.replace; + +import spoon.SpoonException; +import spoon.reflect.code.CtBlock; +import spoon.reflect.code.CtConstructorCall; +import spoon.reflect.code.CtExpression; +import spoon.reflect.code.CtInvocation; +import spoon.reflect.declaration.CtClass; +import spoon.reflect.declaration.CtConstructor; +import spoon.reflect.declaration.CtField; +import spoon.reflect.declaration.CtInterface; +import spoon.reflect.declaration.CtMethod; +import spoon.reflect.declaration.CtParameter; +import spoon.reflect.declaration.CtType; +import spoon.reflect.factory.Factory; +import spoon.reflect.reference.CtExecutableReference; +import spoon.reflect.reference.CtTypeParameterReference; +import spoon.reflect.reference.CtTypeReference; +import spoon.reflect.visitor.CtScanner; +import spoon.reflect.visitor.ReferenceFilter; + +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +public class ReplaceScanner extends CtScanner { + public static final String TARGET_REPLACE_PACKAGE = "spoon.support.visitor.replace"; + public static final String GENERATING_REPLACE_PACKAGE = "spoon.generating.replace"; + public static final String GENERATING_REPLACE_VISITOR = GENERATING_REPLACE_PACKAGE + ".ReplacementVisitor"; + + private final List excludes = Collections.singletonList("spoon.reflect.code.CtLiteral#getValue()"); + private final Map listeners = new HashMap(); + private final CtClass target; + private final CtExecutableReference element; + private final CtExecutableReference list; + private final CtExecutableReference map; + private final CtExecutableReference set; + + public ReplaceScanner(CtClass target) { + this.target = target; + this.element = target.getMethodsByName("replaceElementIfExist").get(0).getReference(); + this.list = target.getMethodsByName("replaceInListIfExist").get(0).getReference(); + this.map = target.getMethodsByName("replaceInMapIfExist").get(0).getReference(); + this.set = target.getMethodsByName("replaceInSetIfExist").get(0).getReference(); + } + + @Override + public void visitCtMethod(CtMethod element) { + if (!element.getSimpleName().startsWith("visitCt")) { + return; + } + + Factory factory = element.getFactory(); + CtMethod clone = factory.Core().clone(element); + clone.getBody().getStatements().clear(); + for (int i = 1; i < element.getBody().getStatements().size() - 1; i++) { + CtInvocation inv = element.getBody().getStatement(i); + CtInvocation getter = (CtInvocation) inv.getArguments().get(0); + if (excludes.contains(getter.getExecutable().toString())) { + continue; + } + Class actualClass = getter.getType().getActualClass(); + CtInvocation invocation = createInvocation(factory, element, inv, getter, actualClass); + clone.getBody().addStatement(invocation); + } + target.addMethod(clone); + } + + private CtInvocation createInvocation(Factory factory, CtMethod candidate, CtInvocation current, CtInvocation getter, Class getterTypeClass) { + CtInvocation invocation; + Type type; + if (getterTypeClass.equals(Collection.class) || getterTypeClass.equals(List.class)) { + invocation = factory.Code().createInvocation(null, this.list, current.getArguments()); + type = Type.LIST; + } else if (getterTypeClass.equals(Map.class)) { + invocation = factory.Code().createInvocation(null, this.map, current.getArguments()); + type = Type.MAP; + } else if (getterTypeClass.equals(Set.class)) { + invocation = factory.Code().createInvocation(null, this.set, current.getArguments()); + type = Type.SET; + } else { + invocation = factory.Code().createInvocation(null, this.element, current.getArguments()); + type = Type.ELEMENT; + } + // Listener + final String name = getter.getExecutable().getSimpleName().substring(3); + final String listenerName = getter.getExecutable().getDeclaringType().getSimpleName() + name + "ReplaceListener"; + + CtClass listener; + if (listeners.containsKey(listenerName)) { + listener = listeners.get(listenerName); + } else { + final CtTypeReference getterType = getGetterType(factory, getter); + listener = createListenerClass(factory, listenerName, getterType, type); + final CtMethod setter = getSetter(name, getter.getTarget().getType().getDeclaration()); + final CtField field = updateField(listener, setter.getDeclaringType().getReference()); + updateConstructor(listener, setter.getDeclaringType().getReference()); + updateSetter(factory, (CtMethod) listener.getMethodsByName("set").get(0), getterType, field, setter); + listeners.put(listenerName, listener); + } + + invocation.addArgument(getConstructorCall(listener, factory.Code().createVariableRead(candidate.getParameters().get(0).getReference(), false))); + return invocation; + } + + private CtTypeReference getGetterType(Factory factory, CtInvocation getter) { + CtTypeReference getterType; + final CtTypeReference type = getter.getType(); + if (type instanceof CtTypeParameterReference) { + getterType = getTypeFromTypeParameterReference((CtTypeParameterReference) getter.getExecutable().getDeclaration().getType()); + } else { + getterType = factory.Core().clone(type); + } + getterType.getActualTypeArguments().clear(); + return getterType; + } + + private CtTypeReference getTypeFromTypeParameterReference(CtTypeParameterReference ctTypeParameterRef) { + final CtMethod parentMethod = ctTypeParameterRef.getParent(CtMethod.class); + for (CtTypeReference formal : parentMethod.getFormalTypeParameters()) { + if (formal.getSimpleName().equals(ctTypeParameterRef.getSimpleName())) { + return ((CtTypeParameterReference) formal).getBoundingType(); + } + } + final CtInterface parentInterface = ctTypeParameterRef.getParent(CtInterface.class); + for (CtTypeReference formal : parentInterface.getFormalTypeParameters()) { + if (formal.getSimpleName().equals(ctTypeParameterRef.getSimpleName())) { + return ((CtTypeParameterReference) formal).getBoundingType(); + } + } + throw new SpoonException("Can't get the type of the CtTypeParameterReference " + ctTypeParameterRef); + } + + private CtClass createListenerClass(Factory factory, String listenerName, CtTypeReference getterType, Type type) { + CtClass listener; + listener = factory.Core().clone(factory.Class().get(GENERATING_REPLACE_PACKAGE + ".CtListener")); + listener.setSimpleName(listenerName); + target.addNestedType(listener); + final List references = listener.getReferences(new ReferenceFilter() { + @Override + public boolean matches(CtTypeReference reference) { + return reference != null && (GENERATING_REPLACE_PACKAGE + ".CtListener").equals(reference.getQualifiedName()); + } + + @Override + public Class getType() { + return CtTypeReference.class; + } + }); + for (CtTypeReference reference : references) { + reference.setPackage(listener.getPackage().getReference()); + } + final CtTypeReference theInterface = factory.Class().createReference(GENERATING_REPLACE_PACKAGE + "." + type.name); + theInterface.addActualTypeArgument(getterType); + final Set> interfaces = new HashSet>(); + interfaces.add(theInterface); + listener.setSuperInterfaces(interfaces); + return listener; + } + + private CtParameter updateConstructor(CtClass listener, CtTypeReference type) { + final CtConstructor ctConstructor = (CtConstructor) listener.getConstructors().toArray(new CtConstructor[listener.getConstructors().size()])[0]; + final CtParameter aParameter = (CtParameter) ctConstructor.getParameters().get(0); + aParameter.setType(type); + return aParameter; + } + + private CtField updateField(CtClass listener, CtTypeReference type) { + final CtField field = listener.getField("element"); + field.setType(type); + return field; + } + + private void updateSetter(Factory factory, CtMethod setListener, CtTypeReference getterType, CtField field, CtMethod setter) { + setListener.getParameters().get(0).setType(getterType); + + CtInvocation ctInvocation = factory.Code().createInvocation(// + factory.Code().createVariableRead(field.getReference(), false), // + setter.getReference(), // + factory.Code().createVariableRead(setListener.getParameters().get(0).getReference(), false) // + ); + CtBlock ctBlock = factory.Code().createCtBlock(ctInvocation); + setListener.setBody(ctBlock); + } + + private CtMethod getSetter(String name, CtType declaration) { + Set allMethods = declaration.getAllMethods(); + CtMethod setter = null; + for (CtMethod aMethod : allMethods) { + if (("set" + name).equals(aMethod.getSimpleName())) { + setter = aMethod; + break; + } + } + return setter; + } + + private CtConstructorCall getConstructorCall(CtClass listener, CtExpression argument) { + return listener.getFactory().Code().createConstructorCall(listener.getReference(), argument); + } + + enum Type { + ELEMENT("ReplaceListener"), LIST("ReplaceListListener"), SET("ReplaceSetListener"), MAP("ReplaceMapListener"); + + String name; + + Type(String name) { + this.name = name; + } + } +} diff --git a/src/main/java/spoon/generating/replace/ReplaceSetListener.java b/src/main/java/spoon/generating/replace/ReplaceSetListener.java new file mode 100644 index 00000000000..4a97695b3ae --- /dev/null +++ b/src/main/java/spoon/generating/replace/ReplaceSetListener.java @@ -0,0 +1,23 @@ +/** + * Copyright (C) 2006-2015 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.generating.replace; + +import java.util.Set; + +public interface ReplaceSetListener { + void set(T replace); +} diff --git a/src/main/java/spoon/generating/replace/ReplacementVisitor.java b/src/main/java/spoon/generating/replace/ReplacementVisitor.java new file mode 100644 index 00000000000..38c55c87d31 --- /dev/null +++ b/src/main/java/spoon/generating/replace/ReplacementVisitor.java @@ -0,0 +1,120 @@ +/** + * Copyright (C) 2006-2015 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.generating.replace; + +import spoon.SpoonException; +import spoon.reflect.declaration.CtElement; +import spoon.reflect.visitor.CtScanner; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * Used to replace an element by another one. + * + * This class is generated automatically by the processor {@link spoon.generating.ReplacementVisitorGenerator}. + */ +class ReplacementVisitor extends CtScanner { + public static void replace(CtElement original, CtElement replace) { + try { + new ReplacementVisitor(original, replace).scan(original.getParent()); + } catch (SpoonException ignore) { + } + } + + private CtElement original; + private CtElement replace; + + private ReplacementVisitor(CtElement original, CtElement replace) { + this.original = original; + this.replace = replace; + } + + private void replaceInMapIfExist(Map mapProtected, ReplaceMapListener listener) { + Map map = new HashMap(mapProtected); + V shouldBeDeleted = null; + K key = null; + for (Map.Entry entry : map.entrySet()) { + if (entry.getValue() == original) { + shouldBeDeleted = entry.getValue(); + key = entry.getKey(); + break; + } + } + if (shouldBeDeleted != null) { + map.remove(key); + if (replace != null) { + map.put(key, (V) replace); + replace.setParent(shouldBeDeleted.getParent()); + } + listener.set(map); + } + } + + private void replaceInSetIfExist(Set setProtected, ReplaceSetListener listener) { + Set set = new HashSet(setProtected); + T shouldBeDeleted = null; + for (T element : set) { + if (element == original) { + shouldBeDeleted = element; + break; + } + } + if (shouldBeDeleted != null) { + set.remove(shouldBeDeleted); + if (replace != null) { + set.add((T) replace); + replace.setParent(shouldBeDeleted.getParent()); + } + listener.set(set); + } + } + + private void replaceInListIfExist(List listProtected, ReplaceListListener listener) { + List list = new ArrayList(listProtected); + T shouldBeDeleted = null; + int index = 0; + for (int i = 0; i < list.size(); i++) { + if (list.get(i) == original) { + index = i; + shouldBeDeleted = list.get(i); + break; + } + } + if (shouldBeDeleted != null) { + list.remove(index); + if (replace != null) { + list.add(index, (T) replace); + replace.setParent(shouldBeDeleted.getParent()); + } + listener.set(list); + } + } + + private void replaceElementIfExist(CtElement candidate, ReplaceListener listener) { + if (candidate == original) { + listener.set(replace); + if (replace != null) { + replace.setParent(candidate.getParent()); + } + } + } +} diff --git a/src/main/java/spoon/reflect/declaration/CtAnnotation.java b/src/main/java/spoon/reflect/declaration/CtAnnotation.java index 8e0961bf66f..23ddf181635 100644 --- a/src/main/java/spoon/reflect/declaration/CtAnnotation.java +++ b/src/main/java/spoon/reflect/declaration/CtAnnotation.java @@ -123,6 +123,13 @@ public interface CtAnnotation extends CtExpression, CtS */ > T setElementValues(Map values); + /** + * Set's this annotation's element names and their values. This is in the + * form of a map that associates element names with their corresponding + * values. + */ + > T setValues(Map values); + /** * Returns the element which is annotated by this annotation. * diff --git a/src/main/java/spoon/reflect/declaration/CtType.java b/src/main/java/spoon/reflect/declaration/CtType.java index 3e634403887..69190d3e7fb 100644 --- a/src/main/java/spoon/reflect/declaration/CtType.java +++ b/src/main/java/spoon/reflect/declaration/CtType.java @@ -128,6 +128,11 @@ public interface CtType extends CtNamedElement, CtTypeInformation, CtTypeMemb */ > C addField(int index, CtField field); + /** + * Sets all fields in the type. + */ + > C setFields(List> fields); + /** * remove a Field * @@ -152,6 +157,11 @@ public interface CtType extends CtNamedElement, CtTypeInformation, CtTypeMemb */ boolean removeNestedType(CtType nestedType); + /** + * Sets all nested types. + */ + > C setNestedTypes(Set> nestedTypes); + /** * Compiles and replace all the code snippets that are found in this type. * diff --git a/src/main/java/spoon/reflect/factory/CodeFactory.java b/src/main/java/spoon/reflect/factory/CodeFactory.java index 344f0c194c1..34411afb531 100644 --- a/src/main/java/spoon/reflect/factory/CodeFactory.java +++ b/src/main/java/spoon/reflect/factory/CodeFactory.java @@ -33,12 +33,14 @@ import spoon.reflect.code.CtLiteral; import spoon.reflect.code.CtLocalVariable; import spoon.reflect.code.CtNewArray; +import spoon.reflect.code.CtNewClass; import spoon.reflect.code.CtStatement; import spoon.reflect.code.CtStatementList; import spoon.reflect.code.CtThisAccess; import spoon.reflect.code.CtThrow; import spoon.reflect.code.CtTypeAccess; import spoon.reflect.code.CtVariableAccess; +import spoon.reflect.declaration.CtClass; import spoon.reflect.declaration.CtField; import spoon.reflect.declaration.CtNamedElement; import spoon.reflect.declaration.CtVariable; @@ -170,6 +172,25 @@ public CtConstructorCall createConstructorCall(CtTypeReference type, C return constructorCall; } + /** + * Creates a new class with an anonymous class. + * + * @param type the decelerating type of the constructor. + * @param anonymousClass Anonymous class in the new class. + * @param parameters the arguments of the constructor call. + * @param the actual type of the decelerating type of the constructor if available/ + * @return the new class. + */ + public CtNewClass createNewClass(CtTypeReference type, CtClass anonymousClass, CtExpression...parameters) { + CtNewClass ctNewClass = factory.Core().createNewClass(); + CtExecutableReference executableReference = factory.Constructor().createReference(type, parameters); + ctNewClass.setArguments(Arrays.asList(parameters)); + ctNewClass.setExecutable(executableReference); + ctNewClass.setAnonymousClass(anonymousClass); + anonymousClass.setSimpleName("0"); + return ctNewClass; + } + /** * Creates an invocation (can be a statement or an expression). * diff --git a/src/main/java/spoon/reflect/factory/ConstructorFactory.java b/src/main/java/spoon/reflect/factory/ConstructorFactory.java index 89ec920fb03..d3e9f0e57a4 100644 --- a/src/main/java/spoon/reflect/factory/ConstructorFactory.java +++ b/src/main/java/spoon/reflect/factory/ConstructorFactory.java @@ -16,12 +16,8 @@ */ package spoon.reflect.factory; -import java.lang.reflect.Constructor; -import java.util.Arrays; -import java.util.List; -import java.util.Set; - import spoon.reflect.code.CtBlock; +import spoon.reflect.code.CtExpression; import spoon.reflect.declaration.CtClass; import spoon.reflect.declaration.CtConstructor; import spoon.reflect.declaration.CtMethod; @@ -30,6 +26,12 @@ import spoon.reflect.reference.CtExecutableReference; import spoon.reflect.reference.CtTypeReference; +import java.lang.reflect.Constructor; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Set; + /** * The {@link CtConstructor} sub-factory. */ @@ -154,4 +156,24 @@ public CtExecutableReference createReference(Constructor constructor) factory.Type().createReferences(Arrays.asList(constructor.getParameterTypes()))); } + /** + * Creates a constructor reference. + * @param type Declaring type of the constructor. + * @param parameters Constructor parameters. + * @param Infered type of the constructor. + * @return CtExecutablereference if a constructor. + */ + public CtExecutableReference createReference(CtTypeReference type, CtExpression...parameters) { + final CtExecutableReference executableReference = factory.Core().createExecutableReference(); + executableReference.setType(type); + executableReference.setDeclaringType(type); + executableReference.setSimpleName(CtExecutableReference.CONSTRUCTOR_NAME); + List> typeReferences = new ArrayList>(); + for (CtExpression parameter : parameters) { + typeReferences.add(parameter.getType()); + } + executableReference.setParameters(typeReferences); + return executableReference; + } + } diff --git a/src/main/java/spoon/reflect/visitor/CtScanner.java b/src/main/java/spoon/reflect/visitor/CtScanner.java index d2e03f582e5..e19ecb302ea 100644 --- a/src/main/java/spoon/reflect/visitor/CtScanner.java +++ b/src/main/java/spoon/reflect/visitor/CtScanner.java @@ -95,6 +95,7 @@ import java.lang.annotation.Annotation; import java.util.Collection; +import java.util.Map; /** * This visitor implements a deep-search scan on the model. @@ -148,11 +149,11 @@ public void scan(CtElement element) { } public void visitCtAnnotation( - CtAnnotation annotation) { + final CtAnnotation annotation) { enter(annotation); scan(annotation.getAnnotationType()); scan(annotation.getAnnotations()); - scan(annotation.getElementValues().values()); + scan(annotation.getValues()); exit(annotation); } @@ -172,10 +173,15 @@ public void scan(Object o) { scan(obj); } } + if (o instanceof Map) { + for (Object obj : ((Map) o).values()) { + scan(obj); + } + } } public void visitCtAnnotationType( - CtAnnotationType annotationType) { + final CtAnnotationType annotationType) { enter(annotationType); scan(annotationType.getAnnotations()); scan(annotationType.getNestedTypes()); @@ -184,7 +190,7 @@ public void visitCtAnnotationType( exit(annotationType); } - public void visitCtAnonymousExecutable(CtAnonymousExecutable anonymousExec) { + public void visitCtAnonymousExecutable(final CtAnonymousExecutable anonymousExec) { enter(anonymousExec); scan(anonymousExec.getAnnotations()); scan(anonymousExec.getBody()); @@ -192,7 +198,7 @@ public void visitCtAnonymousExecutable(CtAnonymousExecutable anonymousExec) { exit(anonymousExec); } - public > void visitCtArrayAccess(CtArrayAccess arrayAccess) { + public > void visitCtArrayAccess(final CtArrayAccess arrayAccess) { enter(arrayAccess); scan(arrayAccess.getAnnotations()); scan(arrayAccess.getType()); @@ -204,7 +210,7 @@ public > void visitCtArrayAccess(CtArrayAccess void visitCtArrayRead(CtArrayRead arrayRead) { + public void visitCtArrayRead(final CtArrayRead arrayRead) { enter(arrayRead); scan(arrayRead.getAnnotations()); scan(arrayRead.getType()); @@ -216,7 +222,7 @@ public void visitCtArrayRead(CtArrayRead arrayRead) { } @Override - public void visitCtArrayWrite(CtArrayWrite arrayWrite) { + public void visitCtArrayWrite(final CtArrayWrite arrayWrite) { enter(arrayWrite); scan(arrayWrite.getAnnotations()); scan(arrayWrite.getType()); @@ -227,7 +233,7 @@ public void visitCtArrayWrite(CtArrayWrite arrayWrite) { exit(arrayWrite); } - public void visitCtArrayTypeReference(CtArrayTypeReference reference) { + public void visitCtArrayTypeReference(final CtArrayTypeReference reference) { enter(reference); scan(reference.getDeclaringType()); scan(reference.getPackage()); @@ -238,11 +244,11 @@ public void visitCtArrayTypeReference(CtArrayTypeReference reference) { } @Override - public void visitCtImplicitArrayTypeReference(CtImplicitArrayTypeReference reference) { + public void visitCtImplicitArrayTypeReference(final CtImplicitArrayTypeReference reference) { visitCtArrayTypeReference(reference); } - public void visitCtAssert(CtAssert asserted) { + public void visitCtAssert(final CtAssert asserted) { enter(asserted); scan(asserted.getAnnotations()); scan(asserted.getAssertExpression()); @@ -252,7 +258,7 @@ public void visitCtAssert(CtAssert asserted) { } public void visitCtAssignment( - CtAssignment assignement) { + final CtAssignment assignement) { enter(assignement); scan(assignement.getAnnotations()); scan(assignement.getType()); @@ -263,7 +269,7 @@ public void visitCtAssignment( exit(assignement); } - public void visitCtBinaryOperator(CtBinaryOperator operator) { + public void visitCtBinaryOperator(final CtBinaryOperator operator) { enter(operator); scan(operator.getAnnotations()); scan(operator.getType()); @@ -274,7 +280,7 @@ public void visitCtBinaryOperator(CtBinaryOperator operator) { exit(operator); } - public void visitCtBlock(CtBlock block) { + public void visitCtBlock(final CtBlock block) { enter(block); scan(block.getAnnotations()); scan(block.getStatements()); @@ -282,14 +288,14 @@ public void visitCtBlock(CtBlock block) { exit(block); } - public void visitCtBreak(CtBreak breakStatement) { + public void visitCtBreak(final CtBreak breakStatement) { enter(breakStatement); scan(breakStatement.getAnnotations()); scan(breakStatement.getComments()); exit(breakStatement); } - public void visitCtCase(CtCase caseStatement) { + public void visitCtCase(final CtCase caseStatement) { enter(caseStatement); scan(caseStatement.getAnnotations()); scan(caseStatement.getCaseExpression()); @@ -298,7 +304,7 @@ public void visitCtCase(CtCase caseStatement) { exit(caseStatement); } - public void visitCtCatch(CtCatch catchBlock) { + public void visitCtCatch(final CtCatch catchBlock) { enter(catchBlock); scan(catchBlock.getAnnotations()); scan(catchBlock.getParameter()); @@ -307,7 +313,7 @@ public void visitCtCatch(CtCatch catchBlock) { exit(catchBlock); } - public void visitCtClass(CtClass ctClass) { + public void visitCtClass(final CtClass ctClass) { enter(ctClass); scan(ctClass.getAnnotations()); scan(ctClass.getSuperclass()); @@ -322,7 +328,7 @@ public void visitCtClass(CtClass ctClass) { exit(ctClass); } - public void visitCtConditional(CtConditional conditional) { + public void visitCtConditional(final CtConditional conditional) { enter(conditional); scan(conditional.getAnnotations()); scan(conditional.getCondition()); @@ -332,7 +338,7 @@ public void visitCtConditional(CtConditional conditional) { exit(conditional); } - public void visitCtConstructor(CtConstructor c) { + public void visitCtConstructor(final CtConstructor c) { enter(c); scan(c.getAnnotations()); scan(c.getParameters()); @@ -343,7 +349,7 @@ public void visitCtConstructor(CtConstructor c) { exit(c); } - public void visitCtContinue(CtContinue continueStatement) { + public void visitCtContinue(final CtContinue continueStatement) { enter(continueStatement); scan(continueStatement.getAnnotations()); scan(continueStatement.getLabelledStatement()); @@ -351,7 +357,7 @@ public void visitCtContinue(CtContinue continueStatement) { exit(continueStatement); } - public void visitCtDo(CtDo doLoop) { + public void visitCtDo(final CtDo doLoop) { enter(doLoop); scan(doLoop.getAnnotations()); scan(doLoop.getLoopingExpression()); @@ -360,7 +366,7 @@ public void visitCtDo(CtDo doLoop) { exit(doLoop); } - public > void visitCtEnum(CtEnum ctEnum) { + public > void visitCtEnum(final CtEnum ctEnum) { enter(ctEnum); scan(ctEnum.getAnnotations()); scan(ctEnum.getSuperInterfaces()); @@ -373,7 +379,7 @@ public > void visitCtEnum(CtEnum ctEnum) { } public void visitCtExecutableReference( - CtExecutableReference reference) { + final CtExecutableReference reference) { enter(reference); scan(reference.getDeclaringType()); scan(reference.getType()); @@ -384,7 +390,7 @@ public void visitCtExecutableReference( exit(reference); } - public void visitCtField(CtField f) { + public void visitCtField(final CtField f) { enter(f); scan(f.getAnnotations()); scan(f.getType()); @@ -394,7 +400,7 @@ public void visitCtField(CtField f) { } @Override - public void visitCtEnumValue(CtEnumValue enumValue) { + public void visitCtEnumValue(final CtEnumValue enumValue) { enter(enumValue); scan(enumValue.getAnnotations()); scan(enumValue.getType()); @@ -404,7 +410,7 @@ public void visitCtEnumValue(CtEnumValue enumValue) { } @Override - public void visitCtThisAccess(CtThisAccess thisAccess) { + public void visitCtThisAccess(final CtThisAccess thisAccess) { enter(thisAccess); scan(thisAccess.getType()); scan(thisAccess.getTypeCasts()); @@ -414,7 +420,7 @@ public void visitCtThisAccess(CtThisAccess thisAccess) { } public void visitCtAnnotationFieldAccess( - CtAnnotationFieldAccess annotationFieldAccess) { + final CtAnnotationFieldAccess annotationFieldAccess) { enter(annotationFieldAccess); scan(annotationFieldAccess.getAnnotations()); scan(annotationFieldAccess.getType()); @@ -425,7 +431,7 @@ public void visitCtAnnotationFieldAccess( exit(annotationFieldAccess); } - public void visitCtFieldReference(CtFieldReference reference) { + public void visitCtFieldReference(final CtFieldReference reference) { enter(reference); scan(reference.getDeclaringType()); scan(reference.getType()); @@ -433,7 +439,7 @@ public void visitCtFieldReference(CtFieldReference reference) { exit(reference); } - public void visitCtFor(CtFor forLoop) { + public void visitCtFor(final CtFor forLoop) { enter(forLoop); scan(forLoop.getAnnotations()); scan(forLoop.getForInit()); @@ -444,7 +450,7 @@ public void visitCtFor(CtFor forLoop) { exit(forLoop); } - public void visitCtForEach(CtForEach foreach) { + public void visitCtForEach(final CtForEach foreach) { enter(foreach); scan(foreach.getAnnotations()); scan(foreach.getVariable()); @@ -454,7 +460,7 @@ public void visitCtForEach(CtForEach foreach) { exit(foreach); } - public void visitCtIf(CtIf ifElement) { + public void visitCtIf(final CtIf ifElement) { enter(ifElement); scan(ifElement.getAnnotations()); scan(ifElement.getCondition()); @@ -464,7 +470,7 @@ public void visitCtIf(CtIf ifElement) { exit(ifElement); } - public void visitCtInterface(CtInterface intrface) { + public void visitCtInterface(final CtInterface intrface) { enter(intrface); scan(intrface.getAnnotations()); scan(intrface.getSuperInterfaces()); @@ -476,7 +482,7 @@ public void visitCtInterface(CtInterface intrface) { exit(intrface); } - public void visitCtInvocation(CtInvocation invocation) { + public void visitCtInvocation(final CtInvocation invocation) { enter(invocation); scan(invocation.getAnnotations()); scan(invocation.getTypeCasts()); @@ -487,7 +493,7 @@ public void visitCtInvocation(CtInvocation invocation) { exit(invocation); } - public void visitCtLiteral(CtLiteral literal) { + public void visitCtLiteral(final CtLiteral literal) { enter(literal); scan(literal.getAnnotations()); scan(literal.getType()); @@ -497,7 +503,7 @@ public void visitCtLiteral(CtLiteral literal) { exit(literal); } - public void visitCtLocalVariable(CtLocalVariable localVariable) { + public void visitCtLocalVariable(final CtLocalVariable localVariable) { enter(localVariable); scan(localVariable.getAnnotations()); scan(localVariable.getType()); @@ -507,14 +513,14 @@ public void visitCtLocalVariable(CtLocalVariable localVariable) { } public void visitCtLocalVariableReference( - CtLocalVariableReference reference) { + final CtLocalVariableReference reference) { enter(reference); scan(reference.getType()); scan(reference.getAnnotations()); exit(reference); } - public void visitCtCatchVariable(CtCatchVariable catchVariable) { + public void visitCtCatchVariable(final CtCatchVariable catchVariable) { enter(catchVariable); scan(catchVariable.getAnnotations()); scan(catchVariable.getType()); @@ -522,14 +528,14 @@ public void visitCtCatchVariable(CtCatchVariable catchVariable) { exit(catchVariable); } - public void visitCtCatchVariableReference(CtCatchVariableReference reference) { + public void visitCtCatchVariableReference(final CtCatchVariableReference reference) { enter(reference); scan(reference.getType()); scan(reference.getAnnotations()); exit(reference); } - public void visitCtMethod(CtMethod m) { + public void visitCtMethod(final CtMethod m) { enter(m); scan(m.getAnnotations()); scan(m.getFormalTypeParameters()); @@ -541,7 +547,7 @@ public void visitCtMethod(CtMethod m) { exit(m); } - public void visitCtNewArray(CtNewArray newArray) { + public void visitCtNewArray(final CtNewArray newArray) { enter(newArray); scan(newArray.getAnnotations()); scan(newArray.getType()); @@ -553,7 +559,7 @@ public void visitCtNewArray(CtNewArray newArray) { } @Override - public void visitCtConstructorCall(CtConstructorCall ctConstructorCall) { + public void visitCtConstructorCall(final CtConstructorCall ctConstructorCall) { enter(ctConstructorCall); scan(ctConstructorCall.getAnnotations()); scan(ctConstructorCall.getTypeCasts()); @@ -564,7 +570,7 @@ public void visitCtConstructorCall(CtConstructorCall ctConstructorCall) { exit(ctConstructorCall); } - public void visitCtNewClass(CtNewClass newClass) { + public void visitCtNewClass(final CtNewClass newClass) { enter(newClass); scan(newClass.getAnnotations()); scan(newClass.getType()); @@ -578,7 +584,7 @@ public void visitCtNewClass(CtNewClass newClass) { } @Override - public void visitCtLambda(CtLambda lambda) { + public void visitCtLambda(final CtLambda lambda) { enter(lambda); scan(lambda.getAnnotations()); scan(lambda.getType()); @@ -592,7 +598,7 @@ public void visitCtLambda(CtLambda lambda) { @Override public > void visitCtExecutableReferenceExpression( - CtExecutableReferenceExpression expression) { + final CtExecutableReferenceExpression expression) { enter(expression); scan(expression.getType()); scan(expression.getTypeCasts()); @@ -602,7 +608,7 @@ public > void visitCtExecutableReferenceExpression( } public void visitCtOperatorAssignment( - CtOperatorAssignment assignment) { + final CtOperatorAssignment assignment) { enter(assignment); scan(assignment.getAnnotations()); scan(assignment.getType()); @@ -613,7 +619,7 @@ public void visitCtOperatorAssignment( exit(assignment); } - public void visitCtPackage(CtPackage ctPackage) { + public void visitCtPackage(final CtPackage ctPackage) { enter(ctPackage); scan(ctPackage.getAnnotations()); scan(ctPackage.getPackages()); @@ -622,12 +628,12 @@ public void visitCtPackage(CtPackage ctPackage) { exit(ctPackage); } - public void visitCtPackageReference(CtPackageReference reference) { + public void visitCtPackageReference(final CtPackageReference reference) { enter(reference); exit(reference); } - public void visitCtParameter(CtParameter parameter) { + public void visitCtParameter(final CtParameter parameter) { enter(parameter); scan(parameter.getAnnotations()); scan(parameter.getType()); @@ -635,14 +641,14 @@ public void visitCtParameter(CtParameter parameter) { exit(parameter); } - public void visitCtParameterReference(CtParameterReference reference) { + public void visitCtParameterReference(final CtParameterReference reference) { enter(reference); scan(reference.getType()); scan(reference.getAnnotations()); exit(reference); } - public void visitCtReturn(CtReturn returnStatement) { + public void visitCtReturn(final CtReturn returnStatement) { enter(returnStatement); scan(returnStatement.getAnnotations()); scan(returnStatement.getReturnedExpression()); @@ -650,7 +656,7 @@ public void visitCtReturn(CtReturn returnStatement) { exit(returnStatement); } - public void visitCtStatementList(CtStatementList statements) { + public void visitCtStatementList(final CtStatementList statements) { enter(statements); scan(statements.getAnnotations()); scan(statements.getStatements()); @@ -658,7 +664,7 @@ public void visitCtStatementList(CtStatementList statements) { exit(statements); } - public void visitCtSwitch(CtSwitch switchStatement) { + public void visitCtSwitch(final CtSwitch switchStatement) { enter(switchStatement); scan(switchStatement.getAnnotations()); scan(switchStatement.getSelector()); @@ -667,7 +673,7 @@ public void visitCtSwitch(CtSwitch switchStatement) { exit(switchStatement); } - public void visitCtSynchronized(CtSynchronized synchro) { + public void visitCtSynchronized(final CtSynchronized synchro) { enter(synchro); scan(synchro.getAnnotations()); scan(synchro.getExpression()); @@ -676,7 +682,7 @@ public void visitCtSynchronized(CtSynchronized synchro) { exit(synchro); } - public void visitCtThrow(CtThrow throwStatement) { + public void visitCtThrow(final CtThrow throwStatement) { enter(throwStatement); scan(throwStatement.getAnnotations()); scan(throwStatement.getThrownExpression()); @@ -684,7 +690,7 @@ public void visitCtThrow(CtThrow throwStatement) { exit(throwStatement); } - public void visitCtTry(CtTry tryBlock) { + public void visitCtTry(final CtTry tryBlock) { enter(tryBlock); scan(tryBlock.getAnnotations()); scan(tryBlock.getBody()); @@ -695,7 +701,7 @@ public void visitCtTry(CtTry tryBlock) { } @Override - public void visitCtTryWithResource(CtTryWithResource tryWithResource) { + public void visitCtTryWithResource(final CtTryWithResource tryWithResource) { enter(tryWithResource); scan(tryWithResource.getAnnotations()); scan(tryWithResource.getResources()); @@ -706,7 +712,7 @@ public void visitCtTryWithResource(CtTryWithResource tryWithResource) { exit(tryWithResource); } - public void visitCtTypeParameterReference(CtTypeParameterReference ref) { + public void visitCtTypeParameterReference(final CtTypeParameterReference ref) { enter(ref); scan(ref.getPackage()); scan(ref.getDeclaringType()); @@ -717,13 +723,13 @@ public void visitCtTypeParameterReference(CtTypeParameterReference ref) { } @Override - public void visitCtIntersectionTypeReference(CtIntersectionTypeReference reference) { + public void visitCtIntersectionTypeReference(final CtIntersectionTypeReference reference) { enter(reference); scan(reference.getBounds()); exit(reference); } - public void visitCtTypeReference(CtTypeReference reference) { + public void visitCtTypeReference(final CtTypeReference reference) { enter(reference); scan(reference.getPackage()); scan(reference.getDeclaringType()); @@ -734,18 +740,18 @@ public void visitCtTypeReference(CtTypeReference reference) { } @Override - public void visitCtCircularTypeReference(CtCircularTypeReference reference) { + public void visitCtCircularTypeReference(final CtCircularTypeReference reference) { enter(reference); exit(reference); } @Override - public void visitCtImplicitTypeReference(CtImplicitTypeReference reference) { + public void visitCtImplicitTypeReference(final CtImplicitTypeReference reference) { visitCtTypeReference(reference); } @Override - public void visitCtTypeAccess(CtTypeAccess typeAccess) { + public void visitCtTypeAccess(final CtTypeAccess typeAccess) { enter(typeAccess); scan(typeAccess.getAnnotations()); scan(typeAccess.getType()); @@ -755,7 +761,7 @@ public void visitCtTypeAccess(CtTypeAccess typeAccess) { exit(typeAccess); } - public void visitCtUnaryOperator(CtUnaryOperator operator) { + public void visitCtUnaryOperator(final CtUnaryOperator operator) { enter(operator); scan(operator.getAnnotations()); scan(operator.getType()); @@ -766,7 +772,7 @@ public void visitCtUnaryOperator(CtUnaryOperator operator) { } @Override - public void visitCtVariableRead(CtVariableRead variableRead) { + public void visitCtVariableRead(final CtVariableRead variableRead) { enter(variableRead); scan(variableRead.getAnnotations()); scan(variableRead.getType()); @@ -777,7 +783,7 @@ public void visitCtVariableRead(CtVariableRead variableRead) { } @Override - public void visitCtVariableWrite(CtVariableWrite variableWrite) { + public void visitCtVariableWrite(final CtVariableWrite variableWrite) { enter(variableWrite); scan(variableWrite.getAnnotations()); scan(variableWrite.getType()); @@ -787,7 +793,7 @@ public void visitCtVariableWrite(CtVariableWrite variableWrite) { exit(variableWrite); } - public void visitCtWhile(CtWhile whileLoop) { + public void visitCtWhile(final CtWhile whileLoop) { enter(whileLoop); scan(whileLoop.getAnnotations()); scan(whileLoop.getLoopingExpression()); @@ -796,23 +802,23 @@ public void visitCtWhile(CtWhile whileLoop) { exit(whileLoop); } - public void visitCtCodeSnippetExpression(CtCodeSnippetExpression expression) { + public void visitCtCodeSnippetExpression(final CtCodeSnippetExpression expression) { enter(expression); exit(expression); } - public void visitCtCodeSnippetStatement(CtCodeSnippetStatement statement) { + public void visitCtCodeSnippetStatement(final CtCodeSnippetStatement statement) { enter(statement); exit(statement); } - public void visitCtUnboundVariableReference(CtUnboundVariableReference reference) { + public void visitCtUnboundVariableReference(final CtUnboundVariableReference reference) { enter(reference); exit(reference); } @Override - public void visitCtFieldRead(CtFieldRead fieldRead) { + public void visitCtFieldRead(final CtFieldRead fieldRead) { enter(fieldRead); scan(fieldRead.getAnnotations()); scan(fieldRead.getTypeCasts()); @@ -823,7 +829,7 @@ public void visitCtFieldRead(CtFieldRead fieldRead) { } @Override - public void visitCtFieldWrite(CtFieldWrite fieldWrite) { + public void visitCtFieldWrite(final CtFieldWrite fieldWrite) { enter(fieldWrite); scan(fieldWrite.getAnnotations()); scan(fieldWrite.getTypeCasts()); @@ -834,7 +840,7 @@ public void visitCtFieldWrite(CtFieldWrite fieldWrite) { } @Override - public void visitCtSuperAccess(CtSuperAccess f) { + public void visitCtSuperAccess(final CtSuperAccess f) { enter(f); scan(f.getAnnotations()); scan(f.getType()); @@ -845,7 +851,7 @@ public void visitCtSuperAccess(CtSuperAccess f) { } @Override - public void visitCtComment(CtComment comment) { + public void visitCtComment(final CtComment comment) { enter(comment); exit(comment); } diff --git a/src/main/java/spoon/support/reflect/code/CtArrayAccessImpl.java b/src/main/java/spoon/support/reflect/code/CtArrayAccessImpl.java index ac6203b0351..ad519a1134a 100644 --- a/src/main/java/spoon/support/reflect/code/CtArrayAccessImpl.java +++ b/src/main/java/spoon/support/reflect/code/CtArrayAccessImpl.java @@ -33,7 +33,9 @@ public CtExpression getIndexExpression() { @Override public > C setIndexExpression(CtExpression expression) { - expression.setParent(this); + if (expression != null) { + expression.setParent(this); + } this.expression = expression; return (C) this; } diff --git a/src/main/java/spoon/support/reflect/code/CtAssertImpl.java b/src/main/java/spoon/support/reflect/code/CtAssertImpl.java index 2c105afb70b..a02345ce319 100644 --- a/src/main/java/spoon/support/reflect/code/CtAssertImpl.java +++ b/src/main/java/spoon/support/reflect/code/CtAssertImpl.java @@ -39,7 +39,9 @@ public CtExpression getAssertExpression() { @Override public > A setAssertExpression(CtExpression asserted) { - asserted.setParent(this); + if (asserted != null) { + asserted.setParent(this); + } this.asserted = asserted; return (A) this; } diff --git a/src/main/java/spoon/support/reflect/code/CtAssignmentImpl.java b/src/main/java/spoon/support/reflect/code/CtAssignmentImpl.java index 3ca1eceb2c1..b20ff9cb46b 100644 --- a/src/main/java/spoon/support/reflect/code/CtAssignmentImpl.java +++ b/src/main/java/spoon/support/reflect/code/CtAssignmentImpl.java @@ -70,14 +70,18 @@ public List> getTypeCasts() { @Override public > C setAssigned(CtExpression assigned) { - assigned.setParent(this); + if (assigned != null) { + assigned.setParent(this); + } this.assigned = assigned; return (C) this; } @Override public > C setAssignment(CtExpression assignment) { - assignment.setParent(this); + if (assignment != null) { + assignment.setParent(this); + } this.assignment = assignment; return (C) this; } diff --git a/src/main/java/spoon/support/reflect/code/CtBinaryOperatorImpl.java b/src/main/java/spoon/support/reflect/code/CtBinaryOperatorImpl.java index 8410e9117b1..e3892b87069 100644 --- a/src/main/java/spoon/support/reflect/code/CtBinaryOperatorImpl.java +++ b/src/main/java/spoon/support/reflect/code/CtBinaryOperatorImpl.java @@ -47,14 +47,18 @@ public CtExpression getRightHandOperand() { @Override public > C setLeftHandOperand(CtExpression expression) { - expression.setParent(this); + if (expression != null) { + expression.setParent(this); + } leftHandOperand = expression; return (C) this; } @Override public > C setRightHandOperand(CtExpression expression) { - expression.setParent(this); + if (expression != null) { + expression.setParent(this); + } rightHandOperand = expression; return (C) this; } diff --git a/src/main/java/spoon/support/reflect/code/CtIfImpl.java b/src/main/java/spoon/support/reflect/code/CtIfImpl.java index 70eee84fbce..231db2212d8 100644 --- a/src/main/java/spoon/support/reflect/code/CtIfImpl.java +++ b/src/main/java/spoon/support/reflect/code/CtIfImpl.java @@ -56,7 +56,9 @@ public S getThenStatement() { @Override public T setCondition(CtExpression condition) { - condition.setParent(this); + if (condition != null) { + condition.setParent(this); + } this.condition = condition; return (T) this; } diff --git a/src/main/java/spoon/support/reflect/declaration/CtAnnotationImpl.java b/src/main/java/spoon/support/reflect/declaration/CtAnnotationImpl.java index 5a8fd1486c7..0aa38889797 100644 --- a/src/main/java/spoon/support/reflect/declaration/CtAnnotationImpl.java +++ b/src/main/java/spoon/support/reflect/declaration/CtAnnotationImpl.java @@ -363,6 +363,15 @@ public > T setElementValues(Map values return (T) this; } + @Override + public > T setValues(Map values) { + this.elementValues.clear(); + for (Entry e : values.entrySet()) { + addValue(e.getKey(), e.getValue()); + } + return (T) this; + } + @Override public CtElement getAnnotatedElement() { return this.getParent(); diff --git a/src/main/java/spoon/support/reflect/declaration/CtElementImpl.java b/src/main/java/spoon/support/reflect/declaration/CtElementImpl.java index 7e5cb88ec80..f4539b34507 100644 --- a/src/main/java/spoon/support/reflect/declaration/CtElementImpl.java +++ b/src/main/java/spoon/support/reflect/declaration/CtElementImpl.java @@ -17,8 +17,6 @@ package spoon.support.reflect.declaration; import org.apache.log4j.Logger; -import spoon.Launcher; -import spoon.processing.FactoryAccessor; import spoon.reflect.code.CtComment; import spoon.reflect.cu.SourcePosition; import spoon.reflect.declaration.CtAnnotation; @@ -35,15 +33,14 @@ import spoon.reflect.visitor.Query; import spoon.reflect.visitor.ReferenceFilter; import spoon.reflect.visitor.filter.AnnotationFilter; -import spoon.support.util.RtHelper; import spoon.support.visitor.EqualVisitor; import spoon.support.visitor.HashcodeVisitor; import spoon.support.visitor.SignaturePrinter; import spoon.support.visitor.TypeReferenceScanner; +import spoon.support.visitor.replace.ReplacementVisitor; import java.io.Serializable; import java.lang.annotation.Annotation; -import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -436,58 +433,7 @@ public void setFactory(Factory factory) { @Override public void replace(CtElement element) { - try { - replaceIn(this, element, getParent()); - } catch (CtUncomparableException e1) { - // do nothing - } catch (Exception e1) { - Launcher.LOGGER.error(e1.getMessage(), e1); - } - } - - private void replaceIn(Object toReplace, T replacement, Object parent) throws IllegalArgumentException, IllegalAccessException { - - for (Field f : RtHelper.getAllFields(parent.getClass())) { - f.setAccessible(true); - Object tmp = f.get(parent); - - if (tmp != null) { - if (tmp instanceof List) { - @SuppressWarnings("unchecked") List lst = (List) tmp; - for (int i = 0; i < lst.size(); i++) { - if (lst.get(i) != null && compare(lst.get(i), toReplace)) { - lst.remove(i); - if (replacement != null) { - lst.add(i, getReplacement(replacement, parent)); - } - } - } - } else if (tmp instanceof Collection) { - @SuppressWarnings("unchecked") Collection collect = (Collection) tmp; - Object[] array = collect.toArray(); - for (Object obj : array) { - if (compare(obj, toReplace)) { - collect.remove(obj); - collect.add(getReplacement(replacement, parent)); - } - } - } else if (compare(tmp, toReplace)) { - f.set(parent, getReplacement(replacement, parent)); - } - } - } - } - - private T getReplacement(T replacement, Object parent) { - // T ret = replacement.getFactory().Core().clone(replacement); - if (replacement instanceof CtElement && parent instanceof CtElement) { - ((CtElement) replacement).setParent((CtElement) parent); - } - return replacement; - } - - private boolean compare(Object o1, Object o2) { - return o1 == o2; + ReplacementVisitor.replace(this, element); } @Override diff --git a/src/main/java/spoon/support/reflect/declaration/CtTypeImpl.java b/src/main/java/spoon/support/reflect/declaration/CtTypeImpl.java index abe4ab3af0d..d88768bfc78 100644 --- a/src/main/java/spoon/support/reflect/declaration/CtTypeImpl.java +++ b/src/main/java/spoon/support/reflect/declaration/CtTypeImpl.java @@ -116,6 +116,15 @@ public > C addField(int index, CtField field) { return (C) this; } + @Override + public > C setFields(List> fields) { + this.fields.clear(); + for (CtField field : fields) { + addField(field); + } + return (C) this; + } + @Override public boolean removeField(CtField field) { return this.fields.remove(field); @@ -162,6 +171,18 @@ public boolean removeNestedType(CtType nestedType) { } } + @Override + public > C setNestedTypes(Set> nestedTypes) { + if (this.nestedTypes == CtElementImpl.>emptySet()) { + this.nestedTypes = new TreeSet>(); + } + this.nestedTypes.clear(); + for (CtType nestedType : nestedTypes) { + addNestedType(nestedType); + } + return (C) this; + } + @Override public Set> getUsedTypes(boolean includeSamePackage) { Set> typeRefs = new HashSet>(); diff --git a/src/main/java/spoon/support/template/SubstitutionVisitor.java b/src/main/java/spoon/support/template/SubstitutionVisitor.java index a7f16f81f70..cf8768a3c1d 100644 --- a/src/main/java/spoon/support/template/SubstitutionVisitor.java +++ b/src/main/java/spoon/support/template/SubstitutionVisitor.java @@ -312,6 +312,9 @@ private void visitFieldAccess(CtFieldAccess fieldAccess) { @SuppressWarnings("unchecked") @Override public void visitCtInvocation(CtInvocation invocation) { + if (invocation.getExecutable().getDeclaringType() == null) { + System.err.println(invocation.getExecutable()); + } if (invocation.getExecutable().isOverriding(S)) { CtFieldAccess fa = null; if ((invocation.getTarget() instanceof CtFieldAccess)) { @@ -423,6 +426,11 @@ public void scanCtTypedElement(CtTypedElement e) { @Override public void visitCtExecutableReference(CtExecutableReference reference) { scanCtReference(reference); + if (reference.getDeclaringType() == null) { + System.err.println("Blabla"); + System.err.println(reference); + System.err.println(reference.getParent()); + } visitCtTypeReference(reference.getDeclaringType()); scanCtGenericElementReference(reference); } diff --git a/src/main/java/spoon/support/visitor/replace/ReplacementVisitor.java b/src/main/java/spoon/support/visitor/replace/ReplacementVisitor.java new file mode 100644 index 00000000000..97dd3559a1d --- /dev/null +++ b/src/main/java/spoon/support/visitor/replace/ReplacementVisitor.java @@ -0,0 +1,1714 @@ +/** + * Copyright (C) 2006-2015 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.support.visitor.replace; + + +/** + * Used to replace an element by another one. + * + * This class is generated automatically by the processor {@link spoon.generating.ReplacementVisitorGenerator}. + */ +public class ReplacementVisitor extends spoon.reflect.visitor.CtScanner { + class CtAbstractInvocationArgumentsReplaceListener implements spoon.generating.replace.ReplaceListListener { + private spoon.reflect.code.CtAbstractInvocation element; + + CtAbstractInvocationArgumentsReplaceListener(spoon.reflect.code.CtAbstractInvocation element) { + this.element = element; + } + + @java.lang.Override + public void set(java.util.List replace) { + this.element.setArguments(replace); + } + } + + class CtAbstractInvocationExecutableReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.code.CtAbstractInvocation element; + + CtAbstractInvocationExecutableReplaceListener(spoon.reflect.code.CtAbstractInvocation element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.reference.CtExecutableReference replace) { + this.element.setExecutable(replace); + } + } + + class CtAnnotationAnnotationTypeReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.declaration.CtAnnotation element; + + CtAnnotationAnnotationTypeReplaceListener(spoon.reflect.declaration.CtAnnotation element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.reference.CtTypeReference replace) { + this.element.setAnnotationType(replace); + } + } + + class CtAnnotationFieldAccessVariableReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.code.CtVariableAccess element; + + CtAnnotationFieldAccessVariableReplaceListener(spoon.reflect.code.CtVariableAccess element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.reference.CtFieldReference replace) { + this.element.setVariable(replace); + } + } + + class CtAnnotationValuesReplaceListener implements spoon.generating.replace.ReplaceMapListener { + private spoon.reflect.declaration.CtAnnotation element; + + CtAnnotationValuesReplaceListener(spoon.reflect.declaration.CtAnnotation element) { + this.element = element; + } + + @java.lang.Override + public void set(java.util.Map replace) { + this.element.setValues(replace); + } + } + + class CtArrayAccessIndexExpressionReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.code.CtArrayAccess element; + + CtArrayAccessIndexExpressionReplaceListener(spoon.reflect.code.CtArrayAccess element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.code.CtExpression replace) { + this.element.setIndexExpression(replace); + } + } + + class CtArrayTypeReferenceComponentTypeReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.reference.CtArrayTypeReference element; + + CtArrayTypeReferenceComponentTypeReplaceListener(spoon.reflect.reference.CtArrayTypeReference element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.reference.CtTypeReference replace) { + this.element.setComponentType(replace); + } + } + + class CtAssertAssertExpressionReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.code.CtAssert element; + + CtAssertAssertExpressionReplaceListener(spoon.reflect.code.CtAssert element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.code.CtExpression replace) { + this.element.setAssertExpression(replace); + } + } + + class CtAssertExpressionReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.code.CtAssert element; + + CtAssertExpressionReplaceListener(spoon.reflect.code.CtAssert element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.code.CtExpression replace) { + this.element.setExpression(replace); + } + } + + class CtAssignmentAssignedReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.code.CtAssignment element; + + CtAssignmentAssignedReplaceListener(spoon.reflect.code.CtAssignment element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.code.CtExpression replace) { + this.element.setAssigned(replace); + } + } + + class CtBinaryOperatorLeftHandOperandReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.code.CtBinaryOperator element; + + CtBinaryOperatorLeftHandOperandReplaceListener(spoon.reflect.code.CtBinaryOperator element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.code.CtExpression replace) { + this.element.setLeftHandOperand(replace); + } + } + + class CtBinaryOperatorRightHandOperandReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.code.CtBinaryOperator element; + + CtBinaryOperatorRightHandOperandReplaceListener(spoon.reflect.code.CtBinaryOperator element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.code.CtExpression replace) { + this.element.setRightHandOperand(replace); + } + } + + class CtCaseCaseExpressionReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.code.CtCase element; + + CtCaseCaseExpressionReplaceListener(spoon.reflect.code.CtCase element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.code.CtExpression replace) { + this.element.setCaseExpression(replace); + } + } + + class CtCatchBodyReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.code.CtCatch element; + + CtCatchBodyReplaceListener(spoon.reflect.code.CtCatch element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.code.CtBlock replace) { + this.element.setBody(replace); + } + } + + class CtCatchParameterReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.code.CtCatch element; + + CtCatchParameterReplaceListener(spoon.reflect.code.CtCatch element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.code.CtCatchVariable replace) { + this.element.setParameter(replace); + } + } + + class CtClassAnonymousExecutablesReplaceListener implements spoon.generating.replace.ReplaceListListener { + private spoon.reflect.declaration.CtClass element; + + CtClassAnonymousExecutablesReplaceListener(spoon.reflect.declaration.CtClass element) { + this.element = element; + } + + @java.lang.Override + public void set(java.util.List replace) { + this.element.setAnonymousExecutables(replace); + } + } + + class CtClassConstructorsReplaceListener implements spoon.generating.replace.ReplaceSetListener { + private spoon.reflect.declaration.CtClass element; + + CtClassConstructorsReplaceListener(spoon.reflect.declaration.CtClass element) { + this.element = element; + } + + @java.lang.Override + public void set(java.util.Set replace) { + this.element.setConstructors(replace); + } + } + + class CtClassFieldsReplaceListener implements spoon.generating.replace.ReplaceListListener { + private spoon.reflect.declaration.CtType element; + + CtClassFieldsReplaceListener(spoon.reflect.declaration.CtType element) { + this.element = element; + } + + @java.lang.Override + public void set(java.util.List replace) { + this.element.setFields(replace); + } + } + + class CtConditionalConditionReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.code.CtConditional element; + + CtConditionalConditionReplaceListener(spoon.reflect.code.CtConditional element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.code.CtExpression replace) { + this.element.setCondition(replace); + } + } + + class CtConditionalElseExpressionReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.code.CtConditional element; + + CtConditionalElseExpressionReplaceListener(spoon.reflect.code.CtConditional element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.code.CtExpression replace) { + this.element.setElseExpression(replace); + } + } + + class CtConditionalThenExpressionReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.code.CtConditional element; + + CtConditionalThenExpressionReplaceListener(spoon.reflect.code.CtConditional element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.code.CtExpression replace) { + this.element.setThenExpression(replace); + } + } + + class CtContinueLabelledStatementReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.code.CtContinue element; + + CtContinueLabelledStatementReplaceListener(spoon.reflect.code.CtContinue element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.code.CtStatement replace) { + this.element.setLabelledStatement(replace); + } + } + + class CtDoLoopingExpressionReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.code.CtDo element; + + CtDoLoopingExpressionReplaceListener(spoon.reflect.code.CtDo element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.code.CtExpression replace) { + this.element.setLoopingExpression(replace); + } + } + + class CtElementAnnotationsReplaceListener implements spoon.generating.replace.ReplaceListListener { + private spoon.reflect.declaration.CtElement element; + + CtElementAnnotationsReplaceListener(spoon.reflect.declaration.CtElement element) { + this.element = element; + } + + @java.lang.Override + public void set(java.util.List replace) { + this.element.setAnnotations(replace); + } + } + + class CtElementCommentsReplaceListener implements spoon.generating.replace.ReplaceListListener { + private spoon.reflect.declaration.CtElement element; + + CtElementCommentsReplaceListener(spoon.reflect.declaration.CtElement element) { + this.element = element; + } + + @java.lang.Override + public void set(java.util.List replace) { + this.element.setComments(replace); + } + } + + class CtExecutableBodyReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.declaration.CtExecutable element; + + CtExecutableBodyReplaceListener(spoon.reflect.declaration.CtExecutable element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.code.CtBlock replace) { + this.element.setBody(replace); + } + } + + class CtExecutableParametersReplaceListener implements spoon.generating.replace.ReplaceListListener { + private spoon.reflect.declaration.CtExecutable element; + + CtExecutableParametersReplaceListener(spoon.reflect.declaration.CtExecutable element) { + this.element = element; + } + + @java.lang.Override + public void set(java.util.List replace) { + this.element.setParameters(replace); + } + } + + class CtExecutableReferenceDeclaringTypeReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.reference.CtExecutableReference element; + + CtExecutableReferenceDeclaringTypeReplaceListener(spoon.reflect.reference.CtExecutableReference element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.reference.CtTypeReference replace) { + this.element.setDeclaringType(replace); + } + } + + class CtExecutableReferenceExpressionExecutableReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.code.CtExecutableReferenceExpression element; + + CtExecutableReferenceExpressionExecutableReplaceListener(spoon.reflect.code.CtExecutableReferenceExpression element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.reference.CtExecutableReference replace) { + this.element.setExecutable(replace); + } + } + + class CtExecutableReferenceParametersReplaceListener implements spoon.generating.replace.ReplaceListListener { + private spoon.reflect.reference.CtExecutableReference element; + + CtExecutableReferenceParametersReplaceListener(spoon.reflect.reference.CtExecutableReference element) { + this.element = element; + } + + @java.lang.Override + public void set(java.util.List replace) { + this.element.setParameters(replace); + } + } + + class CtExecutableReferenceTypeReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.reference.CtExecutableReference element; + + CtExecutableReferenceTypeReplaceListener(spoon.reflect.reference.CtExecutableReference element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.reference.CtTypeReference replace) { + this.element.setType(replace); + } + } + + class CtExecutableThrownTypesReplaceListener implements spoon.generating.replace.ReplaceSetListener { + private spoon.reflect.declaration.CtExecutable element; + + CtExecutableThrownTypesReplaceListener(spoon.reflect.declaration.CtExecutable element) { + this.element = element; + } + + @java.lang.Override + public void set(java.util.Set replace) { + this.element.setThrownTypes(replace); + } + } + + class CtExpressionTypeCastsReplaceListener implements spoon.generating.replace.ReplaceListListener { + private spoon.reflect.code.CtExpression element; + + CtExpressionTypeCastsReplaceListener(spoon.reflect.code.CtExpression element) { + this.element = element; + } + + @java.lang.Override + public void set(java.util.List replace) { + this.element.setTypeCasts(replace); + } + } + + class CtFieldAccessVariableReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.code.CtVariableAccess element; + + CtFieldAccessVariableReplaceListener(spoon.reflect.code.CtVariableAccess element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.reference.CtFieldReference replace) { + this.element.setVariable(replace); + } + } + + class CtFieldReferenceDeclaringTypeReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.reference.CtFieldReference element; + + CtFieldReferenceDeclaringTypeReplaceListener(spoon.reflect.reference.CtFieldReference element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.reference.CtTypeReference replace) { + this.element.setDeclaringType(replace); + } + } + + class CtForEachExpressionReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.code.CtForEach element; + + CtForEachExpressionReplaceListener(spoon.reflect.code.CtForEach element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.code.CtExpression replace) { + this.element.setExpression(replace); + } + } + + class CtForEachVariableReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.code.CtForEach element; + + CtForEachVariableReplaceListener(spoon.reflect.code.CtForEach element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.code.CtLocalVariable replace) { + this.element.setVariable(replace); + } + } + + class CtForExpressionReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.code.CtFor element; + + CtForExpressionReplaceListener(spoon.reflect.code.CtFor element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.code.CtExpression replace) { + this.element.setExpression(replace); + } + } + + class CtForForInitReplaceListener implements spoon.generating.replace.ReplaceListListener { + private spoon.reflect.code.CtFor element; + + CtForForInitReplaceListener(spoon.reflect.code.CtFor element) { + this.element = element; + } + + @java.lang.Override + public void set(java.util.List replace) { + this.element.setForInit(replace); + } + } + + class CtForForUpdateReplaceListener implements spoon.generating.replace.ReplaceListListener { + private spoon.reflect.code.CtFor element; + + CtForForUpdateReplaceListener(spoon.reflect.code.CtFor element) { + this.element = element; + } + + @java.lang.Override + public void set(java.util.List replace) { + this.element.setForUpdate(replace); + } + } + + class CtGenericElementFormalTypeParametersReplaceListener implements spoon.generating.replace.ReplaceListListener { + private spoon.reflect.declaration.CtGenericElement element; + + CtGenericElementFormalTypeParametersReplaceListener(spoon.reflect.declaration.CtGenericElement element) { + this.element = element; + } + + @java.lang.Override + public void set(java.util.List replace) { + this.element.setFormalTypeParameters(replace); + } + } + + class CtGenericElementReferenceActualTypeArgumentsReplaceListener implements spoon.generating.replace.ReplaceListListener { + private spoon.reflect.reference.CtGenericElementReference element; + + CtGenericElementReferenceActualTypeArgumentsReplaceListener(spoon.reflect.reference.CtGenericElementReference element) { + this.element = element; + } + + @java.lang.Override + public void set(java.util.List replace) { + this.element.setActualTypeArguments(replace); + } + } + + class CtIfConditionReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.code.CtIf element; + + CtIfConditionReplaceListener(spoon.reflect.code.CtIf element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.code.CtExpression replace) { + this.element.setCondition(replace); + } + } + + class CtIfElseStatementReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.code.CtIf element; + + CtIfElseStatementReplaceListener(spoon.reflect.code.CtIf element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.code.CtStatement replace) { + this.element.setElseStatement(replace); + } + } + + class CtIfThenStatementReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.code.CtIf element; + + CtIfThenStatementReplaceListener(spoon.reflect.code.CtIf element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.code.CtStatement replace) { + this.element.setThenStatement(replace); + } + } + + class CtIntersectionTypeReferenceBoundsReplaceListener implements spoon.generating.replace.ReplaceListListener { + private spoon.reflect.reference.CtIntersectionTypeReference element; + + CtIntersectionTypeReferenceBoundsReplaceListener(spoon.reflect.reference.CtIntersectionTypeReference element) { + this.element = element; + } + + @java.lang.Override + public void set(java.util.List replace) { + this.element.setBounds(replace); + } + } + + class CtLambdaExpressionReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.code.CtLambda element; + + CtLambdaExpressionReplaceListener(spoon.reflect.code.CtLambda element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.code.CtExpression replace) { + this.element.setExpression(replace); + } + } + + class CtLoopBodyReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.code.CtLoop element; + + CtLoopBodyReplaceListener(spoon.reflect.code.CtLoop element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.code.CtStatement replace) { + this.element.setBody(replace); + } + } + + class CtNewArrayDimensionExpressionsReplaceListener implements spoon.generating.replace.ReplaceListListener { + private spoon.reflect.code.CtNewArray element; + + CtNewArrayDimensionExpressionsReplaceListener(spoon.reflect.code.CtNewArray element) { + this.element = element; + } + + @java.lang.Override + public void set(java.util.List replace) { + this.element.setDimensionExpressions(replace); + } + } + + class CtNewArrayElementsReplaceListener implements spoon.generating.replace.ReplaceListListener { + private spoon.reflect.code.CtNewArray element; + + CtNewArrayElementsReplaceListener(spoon.reflect.code.CtNewArray element) { + this.element = element; + } + + @java.lang.Override + public void set(java.util.List replace) { + this.element.setElements(replace); + } + } + + class CtNewClassAnonymousClassReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.code.CtNewClass element; + + CtNewClassAnonymousClassReplaceListener(spoon.reflect.code.CtNewClass element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.declaration.CtClass replace) { + this.element.setAnonymousClass(replace); + } + } + + class CtPackagePackagesReplaceListener implements spoon.generating.replace.ReplaceSetListener { + private spoon.reflect.declaration.CtPackage element; + + CtPackagePackagesReplaceListener(spoon.reflect.declaration.CtPackage element) { + this.element = element; + } + + @java.lang.Override + public void set(java.util.Set replace) { + this.element.setPackages(replace); + } + } + + class CtPackageTypesReplaceListener implements spoon.generating.replace.ReplaceSetListener { + private spoon.reflect.declaration.CtPackage element; + + CtPackageTypesReplaceListener(spoon.reflect.declaration.CtPackage element) { + this.element = element; + } + + @java.lang.Override + public void set(java.util.Set replace) { + this.element.setTypes(replace); + } + } + + class CtRHSReceiverAssignmentReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.code.CtRHSReceiver element; + + CtRHSReceiverAssignmentReplaceListener(spoon.reflect.code.CtRHSReceiver element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.code.CtExpression replace) { + this.element.setAssignment(replace); + } + } + + class CtReturnReturnedExpressionReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.code.CtReturn element; + + CtReturnReturnedExpressionReplaceListener(spoon.reflect.code.CtReturn element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.code.CtExpression replace) { + this.element.setReturnedExpression(replace); + } + } + + class CtStatementListStatementsReplaceListener implements spoon.generating.replace.ReplaceListListener { + private spoon.reflect.code.CtStatementList element; + + CtStatementListStatementsReplaceListener(spoon.reflect.code.CtStatementList element) { + this.element = element; + } + + @java.lang.Override + public void set(java.util.List replace) { + this.element.setStatements(replace); + } + } + + class CtSwitchCasesReplaceListener implements spoon.generating.replace.ReplaceListListener { + private spoon.reflect.code.CtSwitch element; + + CtSwitchCasesReplaceListener(spoon.reflect.code.CtSwitch element) { + this.element = element; + } + + @java.lang.Override + public void set(java.util.List replace) { + this.element.setCases(replace); + } + } + + class CtSwitchSelectorReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.code.CtSwitch element; + + CtSwitchSelectorReplaceListener(spoon.reflect.code.CtSwitch element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.code.CtExpression replace) { + this.element.setSelector(replace); + } + } + + class CtSynchronizedBlockReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.code.CtSynchronized element; + + CtSynchronizedBlockReplaceListener(spoon.reflect.code.CtSynchronized element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.code.CtBlock replace) { + this.element.setBlock(replace); + } + } + + class CtSynchronizedExpressionReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.code.CtSynchronized element; + + CtSynchronizedExpressionReplaceListener(spoon.reflect.code.CtSynchronized element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.code.CtExpression replace) { + this.element.setExpression(replace); + } + } + + class CtTargetedExpressionTargetReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.code.CtTargetedExpression element; + + CtTargetedExpressionTargetReplaceListener(spoon.reflect.code.CtTargetedExpression element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.code.CtExpression replace) { + this.element.setTarget(replace); + } + } + + class CtThrowThrownExpressionReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.code.CtThrow element; + + CtThrowThrownExpressionReplaceListener(spoon.reflect.code.CtThrow element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.code.CtExpression replace) { + this.element.setThrownExpression(replace); + } + } + + class CtTryBodyReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.code.CtTry element; + + CtTryBodyReplaceListener(spoon.reflect.code.CtTry element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.code.CtBlock replace) { + this.element.setBody(replace); + } + } + + class CtTryCatchersReplaceListener implements spoon.generating.replace.ReplaceListListener { + private spoon.reflect.code.CtTry element; + + CtTryCatchersReplaceListener(spoon.reflect.code.CtTry element) { + this.element = element; + } + + @java.lang.Override + public void set(java.util.List replace) { + this.element.setCatchers(replace); + } + } + + class CtTryFinalizerReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.code.CtTry element; + + CtTryFinalizerReplaceListener(spoon.reflect.code.CtTry element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.code.CtBlock replace) { + this.element.setFinalizer(replace); + } + } + + class CtTryWithResourceResourcesReplaceListener implements spoon.generating.replace.ReplaceListListener { + private spoon.reflect.code.CtTryWithResource element; + + CtTryWithResourceResourcesReplaceListener(spoon.reflect.code.CtTryWithResource element) { + this.element = element; + } + + @java.lang.Override + public void set(java.util.List replace) { + this.element.setResources(replace); + } + } + + class CtTypeAccessAccessedTypeReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.code.CtTypeAccess element; + + CtTypeAccessAccessedTypeReplaceListener(spoon.reflect.code.CtTypeAccess element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.reference.CtTypeReference replace) { + this.element.setAccessedType(replace); + } + } + + class CtTypeAccessTypeReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.declaration.CtTypedElement element; + + CtTypeAccessTypeReplaceListener(spoon.reflect.declaration.CtTypedElement element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.reference.CtTypeReference replace) { + this.element.setType(replace); + } + } + + class CtTypeFieldsReplaceListener implements spoon.generating.replace.ReplaceListListener { + private spoon.reflect.declaration.CtType element; + + CtTypeFieldsReplaceListener(spoon.reflect.declaration.CtType element) { + this.element = element; + } + + @java.lang.Override + public void set(java.util.List replace) { + this.element.setFields(replace); + } + } + + class CtTypeInformationSuperInterfacesReplaceListener implements spoon.generating.replace.ReplaceSetListener { + private spoon.reflect.declaration.CtType element; + + CtTypeInformationSuperInterfacesReplaceListener(spoon.reflect.declaration.CtType element) { + this.element = element; + } + + @java.lang.Override + public void set(java.util.Set replace) { + this.element.setSuperInterfaces(replace); + } + } + + class CtTypeInformationSuperclassReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.declaration.CtClass element; + + CtTypeInformationSuperclassReplaceListener(spoon.reflect.declaration.CtClass element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.reference.CtTypeReference replace) { + this.element.setSuperclass(replace); + } + } + + class CtTypeMethodsReplaceListener implements spoon.generating.replace.ReplaceSetListener { + private spoon.reflect.declaration.CtType element; + + CtTypeMethodsReplaceListener(spoon.reflect.declaration.CtType element) { + this.element = element; + } + + @java.lang.Override + public void set(java.util.Set replace) { + this.element.setMethods(replace); + } + } + + class CtTypeNestedTypesReplaceListener implements spoon.generating.replace.ReplaceSetListener { + private spoon.reflect.declaration.CtType element; + + CtTypeNestedTypesReplaceListener(spoon.reflect.declaration.CtType element) { + this.element = element; + } + + @java.lang.Override + public void set(java.util.Set replace) { + this.element.setNestedTypes(replace); + } + } + + class CtTypeParameterReferenceBoundingTypeReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.reference.CtTypeParameterReference element; + + CtTypeParameterReferenceBoundingTypeReplaceListener(spoon.reflect.reference.CtTypeParameterReference element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.reference.CtTypeReference replace) { + this.element.setBoundingType(replace); + } + } + + class CtTypeReferenceDeclaringTypeReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.reference.CtTypeReference element; + + CtTypeReferenceDeclaringTypeReplaceListener(spoon.reflect.reference.CtTypeReference element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.reference.CtTypeReference replace) { + this.element.setDeclaringType(replace); + } + } + + class CtTypeReferencePackageReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.reference.CtTypeReference element; + + CtTypeReferencePackageReplaceListener(spoon.reflect.reference.CtTypeReference element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.reference.CtPackageReference replace) { + this.element.setPackage(replace); + } + } + + class CtTypedElementTypeReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.declaration.CtTypedElement element; + + CtTypedElementTypeReplaceListener(spoon.reflect.declaration.CtTypedElement element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.reference.CtTypeReference replace) { + this.element.setType(replace); + } + } + + class CtUnaryOperatorOperandReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.code.CtUnaryOperator element; + + CtUnaryOperatorOperandReplaceListener(spoon.reflect.code.CtUnaryOperator element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.code.CtExpression replace) { + this.element.setOperand(replace); + } + } + + class CtVariableAccessVariableReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.code.CtVariableAccess element; + + CtVariableAccessVariableReplaceListener(spoon.reflect.code.CtVariableAccess element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.reference.CtVariableReference replace) { + this.element.setVariable(replace); + } + } + + class CtVariableDefaultExpressionReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.declaration.CtVariable element; + + CtVariableDefaultExpressionReplaceListener(spoon.reflect.declaration.CtVariable element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.code.CtExpression replace) { + this.element.setDefaultExpression(replace); + } + } + + class CtVariableReferenceTypeReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.reference.CtVariableReference element; + + CtVariableReferenceTypeReplaceListener(spoon.reflect.reference.CtVariableReference element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.reference.CtTypeReference replace) { + this.element.setType(replace); + } + } + + class CtWhileLoopingExpressionReplaceListener implements spoon.generating.replace.ReplaceListener { + private spoon.reflect.code.CtWhile element; + + CtWhileLoopingExpressionReplaceListener(spoon.reflect.code.CtWhile element) { + this.element = element; + } + + @java.lang.Override + public void set(spoon.reflect.code.CtExpression replace) { + this.element.setLoopingExpression(replace); + } + } + + public static void replace(spoon.reflect.declaration.CtElement original, spoon.reflect.declaration.CtElement replace) { + try { + new spoon.support.visitor.replace.ReplacementVisitor(original, replace).scan(original.getParent()); + } catch (spoon.SpoonException ignore) { + } + } + + private spoon.reflect.declaration.CtElement original; + + private spoon.reflect.declaration.CtElement replace; + + private ReplacementVisitor(spoon.reflect.declaration.CtElement original, spoon.reflect.declaration.CtElement replace) { + spoon.support.visitor.replace.ReplacementVisitor.this.original = original; + spoon.support.visitor.replace.ReplacementVisitor.this.replace = replace; + } + + private void replaceInMapIfExist(java.util.Map mapProtected, spoon.generating.replace.ReplaceMapListener listener) { + java.util.Map map = new java.util.HashMap(mapProtected); + V shouldBeDeleted = null; + K key = null; + for (java.util.Map.Entry entry : map.entrySet()) { + if ((entry.getValue()) == (original)) { + shouldBeDeleted = entry.getValue(); + key = entry.getKey(); + break; + } + } + if (shouldBeDeleted != null) { + map.remove(key); + if ((replace) != null) { + map.put(key, ((V) (replace))); + replace.setParent(shouldBeDeleted.getParent()); + } + listener.set(map); + } + } + + private void replaceInSetIfExist(java.util.Set setProtected, spoon.generating.replace.ReplaceSetListener listener) { + java.util.Set set = new java.util.HashSet(setProtected); + T shouldBeDeleted = null; + for (T element : set) { + if (element == (original)) { + shouldBeDeleted = element; + break; + } + } + if (shouldBeDeleted != null) { + set.remove(shouldBeDeleted); + if ((replace) != null) { + set.add(((T) (replace))); + replace.setParent(shouldBeDeleted.getParent()); + } + listener.set(set); + } + } + + private void replaceInListIfExist(java.util.List listProtected, spoon.generating.replace.ReplaceListListener listener) { + java.util.List list = new java.util.ArrayList(listProtected); + T shouldBeDeleted = null; + int index = 0; + for (int i = 0; i < (list.size()); i++) { + if ((list.get(i)) == (original)) { + index = i; + shouldBeDeleted = list.get(i); + break; + } + } + if (shouldBeDeleted != null) { + list.remove(index); + if ((replace) != null) { + list.add(index, ((T) (replace))); + replace.setParent(shouldBeDeleted.getParent()); + } + listener.set(list); + } + } + + private void replaceElementIfExist(spoon.reflect.declaration.CtElement candidate, spoon.generating.replace.ReplaceListener listener) { + if (candidate == (original)) { + listener.set(replace); + if ((replace) != null) { + replace.setParent(candidate.getParent()); + } + } + } + + public void visitCtAnnotation(final spoon.reflect.declaration.CtAnnotation annotation) { + replaceElementIfExist(annotation.getAnnotationType(), new spoon.support.visitor.replace.ReplacementVisitor.CtAnnotationAnnotationTypeReplaceListener(annotation)); + replaceInListIfExist(annotation.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(annotation)); + replaceInMapIfExist(annotation.getValues(), new spoon.support.visitor.replace.ReplacementVisitor.CtAnnotationValuesReplaceListener(annotation)); + } + + public void visitCtAnnotationType(final spoon.reflect.declaration.CtAnnotationType annotationType) { + replaceInListIfExist(annotationType.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(annotationType)); + replaceInSetIfExist(annotationType.getNestedTypes(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypeNestedTypesReplaceListener(annotationType)); + replaceInListIfExist(annotationType.getFields(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypeFieldsReplaceListener(annotationType)); + replaceInListIfExist(annotationType.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(annotationType)); + } + + public void visitCtAnonymousExecutable(final spoon.reflect.declaration.CtAnonymousExecutable anonymousExec) { + replaceInListIfExist(anonymousExec.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(anonymousExec)); + replaceElementIfExist(anonymousExec.getBody(), new spoon.support.visitor.replace.ReplacementVisitor.CtExecutableBodyReplaceListener(anonymousExec)); + replaceInListIfExist(anonymousExec.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(anonymousExec)); + } + + public > void visitCtArrayAccess(final spoon.reflect.code.CtArrayAccess arrayAccess) { + replaceInListIfExist(arrayAccess.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(arrayAccess)); + replaceElementIfExist(arrayAccess.getType(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypedElementTypeReplaceListener(arrayAccess)); + replaceInListIfExist(arrayAccess.getTypeCasts(), new spoon.support.visitor.replace.ReplacementVisitor.CtExpressionTypeCastsReplaceListener(arrayAccess)); + replaceElementIfExist(arrayAccess.getTarget(), new spoon.support.visitor.replace.ReplacementVisitor.CtTargetedExpressionTargetReplaceListener(arrayAccess)); + replaceElementIfExist(arrayAccess.getIndexExpression(), new spoon.support.visitor.replace.ReplacementVisitor.CtArrayAccessIndexExpressionReplaceListener(arrayAccess)); + replaceInListIfExist(arrayAccess.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(arrayAccess)); + } + + @java.lang.Override + public void visitCtArrayRead(final spoon.reflect.code.CtArrayRead arrayRead) { + replaceInListIfExist(arrayRead.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(arrayRead)); + replaceElementIfExist(arrayRead.getType(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypedElementTypeReplaceListener(arrayRead)); + replaceInListIfExist(arrayRead.getTypeCasts(), new spoon.support.visitor.replace.ReplacementVisitor.CtExpressionTypeCastsReplaceListener(arrayRead)); + replaceElementIfExist(arrayRead.getTarget(), new spoon.support.visitor.replace.ReplacementVisitor.CtTargetedExpressionTargetReplaceListener(arrayRead)); + replaceElementIfExist(arrayRead.getIndexExpression(), new spoon.support.visitor.replace.ReplacementVisitor.CtArrayAccessIndexExpressionReplaceListener(arrayRead)); + replaceInListIfExist(arrayRead.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(arrayRead)); + } + + @java.lang.Override + public void visitCtArrayWrite(final spoon.reflect.code.CtArrayWrite arrayWrite) { + replaceInListIfExist(arrayWrite.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(arrayWrite)); + replaceElementIfExist(arrayWrite.getType(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypedElementTypeReplaceListener(arrayWrite)); + replaceInListIfExist(arrayWrite.getTypeCasts(), new spoon.support.visitor.replace.ReplacementVisitor.CtExpressionTypeCastsReplaceListener(arrayWrite)); + replaceElementIfExist(arrayWrite.getTarget(), new spoon.support.visitor.replace.ReplacementVisitor.CtTargetedExpressionTargetReplaceListener(arrayWrite)); + replaceElementIfExist(arrayWrite.getIndexExpression(), new spoon.support.visitor.replace.ReplacementVisitor.CtArrayAccessIndexExpressionReplaceListener(arrayWrite)); + replaceInListIfExist(arrayWrite.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(arrayWrite)); + } + + public void visitCtArrayTypeReference(final spoon.reflect.reference.CtArrayTypeReference reference) { + replaceElementIfExist(reference.getDeclaringType(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypeReferenceDeclaringTypeReplaceListener(reference)); + replaceElementIfExist(reference.getPackage(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypeReferencePackageReplaceListener(reference)); + replaceElementIfExist(reference.getComponentType(), new spoon.support.visitor.replace.ReplacementVisitor.CtArrayTypeReferenceComponentTypeReplaceListener(reference)); + replaceInListIfExist(reference.getActualTypeArguments(), new spoon.support.visitor.replace.ReplacementVisitor.CtGenericElementReferenceActualTypeArgumentsReplaceListener(reference)); + replaceInListIfExist(reference.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(reference)); + } + + @java.lang.Override + public void visitCtImplicitArrayTypeReference(final spoon.reflect.internal.CtImplicitArrayTypeReference reference) { + } + + public void visitCtAssert(final spoon.reflect.code.CtAssert asserted) { + replaceInListIfExist(asserted.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(asserted)); + replaceElementIfExist(asserted.getAssertExpression(), new spoon.support.visitor.replace.ReplacementVisitor.CtAssertAssertExpressionReplaceListener(asserted)); + replaceElementIfExist(asserted.getExpression(), new spoon.support.visitor.replace.ReplacementVisitor.CtAssertExpressionReplaceListener(asserted)); + replaceInListIfExist(asserted.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(asserted)); + } + + public void visitCtAssignment(final spoon.reflect.code.CtAssignment assignement) { + replaceInListIfExist(assignement.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(assignement)); + replaceElementIfExist(assignement.getType(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypedElementTypeReplaceListener(assignement)); + replaceInListIfExist(assignement.getTypeCasts(), new spoon.support.visitor.replace.ReplacementVisitor.CtExpressionTypeCastsReplaceListener(assignement)); + replaceElementIfExist(assignement.getAssigned(), new spoon.support.visitor.replace.ReplacementVisitor.CtAssignmentAssignedReplaceListener(assignement)); + replaceElementIfExist(assignement.getAssignment(), new spoon.support.visitor.replace.ReplacementVisitor.CtRHSReceiverAssignmentReplaceListener(assignement)); + replaceInListIfExist(assignement.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(assignement)); + } + + public void visitCtBinaryOperator(final spoon.reflect.code.CtBinaryOperator operator) { + replaceInListIfExist(operator.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(operator)); + replaceElementIfExist(operator.getType(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypedElementTypeReplaceListener(operator)); + replaceInListIfExist(operator.getTypeCasts(), new spoon.support.visitor.replace.ReplacementVisitor.CtExpressionTypeCastsReplaceListener(operator)); + replaceElementIfExist(operator.getLeftHandOperand(), new spoon.support.visitor.replace.ReplacementVisitor.CtBinaryOperatorLeftHandOperandReplaceListener(operator)); + replaceElementIfExist(operator.getRightHandOperand(), new spoon.support.visitor.replace.ReplacementVisitor.CtBinaryOperatorRightHandOperandReplaceListener(operator)); + replaceInListIfExist(operator.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(operator)); + } + + public void visitCtBlock(final spoon.reflect.code.CtBlock block) { + replaceInListIfExist(block.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(block)); + replaceInListIfExist(block.getStatements(), new spoon.support.visitor.replace.ReplacementVisitor.CtStatementListStatementsReplaceListener(block)); + replaceInListIfExist(block.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(block)); + } + + public void visitCtBreak(final spoon.reflect.code.CtBreak breakStatement) { + replaceInListIfExist(breakStatement.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(breakStatement)); + replaceInListIfExist(breakStatement.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(breakStatement)); + } + + public void visitCtCase(final spoon.reflect.code.CtCase caseStatement) { + replaceInListIfExist(caseStatement.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(caseStatement)); + replaceElementIfExist(caseStatement.getCaseExpression(), new spoon.support.visitor.replace.ReplacementVisitor.CtCaseCaseExpressionReplaceListener(caseStatement)); + replaceInListIfExist(caseStatement.getStatements(), new spoon.support.visitor.replace.ReplacementVisitor.CtStatementListStatementsReplaceListener(caseStatement)); + replaceInListIfExist(caseStatement.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(caseStatement)); + } + + public void visitCtCatch(final spoon.reflect.code.CtCatch catchBlock) { + replaceInListIfExist(catchBlock.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(catchBlock)); + replaceElementIfExist(catchBlock.getParameter(), new spoon.support.visitor.replace.ReplacementVisitor.CtCatchParameterReplaceListener(catchBlock)); + replaceElementIfExist(catchBlock.getBody(), new spoon.support.visitor.replace.ReplacementVisitor.CtCatchBodyReplaceListener(catchBlock)); + replaceInListIfExist(catchBlock.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(catchBlock)); + } + + public void visitCtClass(final spoon.reflect.declaration.CtClass ctClass) { + replaceInListIfExist(ctClass.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(ctClass)); + replaceElementIfExist(ctClass.getSuperclass(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypeInformationSuperclassReplaceListener(ctClass)); + replaceInSetIfExist(ctClass.getSuperInterfaces(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypeInformationSuperInterfacesReplaceListener(ctClass)); + replaceInListIfExist(ctClass.getFormalTypeParameters(), new spoon.support.visitor.replace.ReplacementVisitor.CtGenericElementFormalTypeParametersReplaceListener(ctClass)); + replaceInListIfExist(ctClass.getAnonymousExecutables(), new spoon.support.visitor.replace.ReplacementVisitor.CtClassAnonymousExecutablesReplaceListener(ctClass)); + replaceInSetIfExist(ctClass.getNestedTypes(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypeNestedTypesReplaceListener(ctClass)); + replaceInListIfExist(ctClass.getFields(), new spoon.support.visitor.replace.ReplacementVisitor.CtClassFieldsReplaceListener(ctClass)); + replaceInSetIfExist(ctClass.getConstructors(), new spoon.support.visitor.replace.ReplacementVisitor.CtClassConstructorsReplaceListener(ctClass)); + replaceInSetIfExist(ctClass.getMethods(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypeMethodsReplaceListener(ctClass)); + replaceInListIfExist(ctClass.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(ctClass)); + } + + public void visitCtConditional(final spoon.reflect.code.CtConditional conditional) { + replaceInListIfExist(conditional.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(conditional)); + replaceElementIfExist(conditional.getCondition(), new spoon.support.visitor.replace.ReplacementVisitor.CtConditionalConditionReplaceListener(conditional)); + replaceElementIfExist(conditional.getThenExpression(), new spoon.support.visitor.replace.ReplacementVisitor.CtConditionalThenExpressionReplaceListener(conditional)); + replaceElementIfExist(conditional.getElseExpression(), new spoon.support.visitor.replace.ReplacementVisitor.CtConditionalElseExpressionReplaceListener(conditional)); + replaceInListIfExist(conditional.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(conditional)); + } + + public void visitCtConstructor(final spoon.reflect.declaration.CtConstructor c) { + replaceInListIfExist(c.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(c)); + replaceInListIfExist(c.getParameters(), new spoon.support.visitor.replace.ReplacementVisitor.CtExecutableParametersReplaceListener(c)); + replaceInSetIfExist(c.getThrownTypes(), new spoon.support.visitor.replace.ReplacementVisitor.CtExecutableThrownTypesReplaceListener(c)); + replaceInListIfExist(c.getFormalTypeParameters(), new spoon.support.visitor.replace.ReplacementVisitor.CtGenericElementFormalTypeParametersReplaceListener(c)); + replaceElementIfExist(c.getBody(), new spoon.support.visitor.replace.ReplacementVisitor.CtExecutableBodyReplaceListener(c)); + replaceInListIfExist(c.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(c)); + } + + public void visitCtContinue(final spoon.reflect.code.CtContinue continueStatement) { + replaceInListIfExist(continueStatement.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(continueStatement)); + replaceElementIfExist(continueStatement.getLabelledStatement(), new spoon.support.visitor.replace.ReplacementVisitor.CtContinueLabelledStatementReplaceListener(continueStatement)); + replaceInListIfExist(continueStatement.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(continueStatement)); + } + + public void visitCtDo(final spoon.reflect.code.CtDo doLoop) { + replaceInListIfExist(doLoop.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(doLoop)); + replaceElementIfExist(doLoop.getLoopingExpression(), new spoon.support.visitor.replace.ReplacementVisitor.CtDoLoopingExpressionReplaceListener(doLoop)); + replaceElementIfExist(doLoop.getBody(), new spoon.support.visitor.replace.ReplacementVisitor.CtLoopBodyReplaceListener(doLoop)); + replaceInListIfExist(doLoop.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(doLoop)); + } + + public > void visitCtEnum(final spoon.reflect.declaration.CtEnum ctEnum) { + replaceInListIfExist(ctEnum.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(ctEnum)); + replaceInSetIfExist(ctEnum.getSuperInterfaces(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypeInformationSuperInterfacesReplaceListener(ctEnum)); + replaceInListIfExist(ctEnum.getFields(), new spoon.support.visitor.replace.ReplacementVisitor.CtClassFieldsReplaceListener(ctEnum)); + replaceInSetIfExist(ctEnum.getConstructors(), new spoon.support.visitor.replace.ReplacementVisitor.CtClassConstructorsReplaceListener(ctEnum)); + replaceInSetIfExist(ctEnum.getMethods(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypeMethodsReplaceListener(ctEnum)); + replaceInSetIfExist(ctEnum.getNestedTypes(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypeNestedTypesReplaceListener(ctEnum)); + replaceInListIfExist(ctEnum.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(ctEnum)); + } + + public void visitCtExecutableReference(final spoon.reflect.reference.CtExecutableReference reference) { + replaceElementIfExist(reference.getDeclaringType(), new spoon.support.visitor.replace.ReplacementVisitor.CtExecutableReferenceDeclaringTypeReplaceListener(reference)); + replaceElementIfExist(reference.getType(), new spoon.support.visitor.replace.ReplacementVisitor.CtExecutableReferenceTypeReplaceListener(reference)); + replaceInListIfExist(reference.getParameters(), new spoon.support.visitor.replace.ReplacementVisitor.CtExecutableReferenceParametersReplaceListener(reference)); + replaceInListIfExist(reference.getActualTypeArguments(), new spoon.support.visitor.replace.ReplacementVisitor.CtGenericElementReferenceActualTypeArgumentsReplaceListener(reference)); + replaceInListIfExist(reference.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(reference)); + replaceInListIfExist(reference.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(reference)); + } + + public void visitCtField(final spoon.reflect.declaration.CtField f) { + replaceInListIfExist(f.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(f)); + replaceElementIfExist(f.getType(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypedElementTypeReplaceListener(f)); + replaceElementIfExist(f.getDefaultExpression(), new spoon.support.visitor.replace.ReplacementVisitor.CtVariableDefaultExpressionReplaceListener(f)); + replaceInListIfExist(f.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(f)); + } + + @java.lang.Override + public void visitCtEnumValue(final spoon.reflect.declaration.CtEnumValue enumValue) { + replaceInListIfExist(enumValue.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(enumValue)); + replaceElementIfExist(enumValue.getType(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypedElementTypeReplaceListener(enumValue)); + replaceElementIfExist(enumValue.getDefaultExpression(), new spoon.support.visitor.replace.ReplacementVisitor.CtVariableDefaultExpressionReplaceListener(enumValue)); + replaceInListIfExist(enumValue.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(enumValue)); + } + + @java.lang.Override + public void visitCtThisAccess(final spoon.reflect.code.CtThisAccess thisAccess) { + replaceElementIfExist(thisAccess.getType(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypedElementTypeReplaceListener(thisAccess)); + replaceInListIfExist(thisAccess.getTypeCasts(), new spoon.support.visitor.replace.ReplacementVisitor.CtExpressionTypeCastsReplaceListener(thisAccess)); + replaceElementIfExist(thisAccess.getTarget(), new spoon.support.visitor.replace.ReplacementVisitor.CtTargetedExpressionTargetReplaceListener(thisAccess)); + replaceInListIfExist(thisAccess.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(thisAccess)); + } + + public void visitCtAnnotationFieldAccess(final spoon.reflect.code.CtAnnotationFieldAccess annotationFieldAccess) { + replaceInListIfExist(annotationFieldAccess.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(annotationFieldAccess)); + replaceElementIfExist(annotationFieldAccess.getType(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypedElementTypeReplaceListener(annotationFieldAccess)); + replaceInListIfExist(annotationFieldAccess.getTypeCasts(), new spoon.support.visitor.replace.ReplacementVisitor.CtExpressionTypeCastsReplaceListener(annotationFieldAccess)); + replaceElementIfExist(annotationFieldAccess.getTarget(), new spoon.support.visitor.replace.ReplacementVisitor.CtTargetedExpressionTargetReplaceListener(annotationFieldAccess)); + replaceElementIfExist(annotationFieldAccess.getVariable(), new spoon.support.visitor.replace.ReplacementVisitor.CtAnnotationFieldAccessVariableReplaceListener(annotationFieldAccess)); + replaceInListIfExist(annotationFieldAccess.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(annotationFieldAccess)); + } + + public void visitCtFieldReference(final spoon.reflect.reference.CtFieldReference reference) { + replaceElementIfExist(reference.getDeclaringType(), new spoon.support.visitor.replace.ReplacementVisitor.CtFieldReferenceDeclaringTypeReplaceListener(reference)); + replaceElementIfExist(reference.getType(), new spoon.support.visitor.replace.ReplacementVisitor.CtVariableReferenceTypeReplaceListener(reference)); + replaceInListIfExist(reference.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(reference)); + } + + public void visitCtFor(final spoon.reflect.code.CtFor forLoop) { + replaceInListIfExist(forLoop.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(forLoop)); + replaceInListIfExist(forLoop.getForInit(), new spoon.support.visitor.replace.ReplacementVisitor.CtForForInitReplaceListener(forLoop)); + replaceElementIfExist(forLoop.getExpression(), new spoon.support.visitor.replace.ReplacementVisitor.CtForExpressionReplaceListener(forLoop)); + replaceInListIfExist(forLoop.getForUpdate(), new spoon.support.visitor.replace.ReplacementVisitor.CtForForUpdateReplaceListener(forLoop)); + replaceElementIfExist(forLoop.getBody(), new spoon.support.visitor.replace.ReplacementVisitor.CtLoopBodyReplaceListener(forLoop)); + replaceInListIfExist(forLoop.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(forLoop)); + } + + public void visitCtForEach(final spoon.reflect.code.CtForEach foreach) { + replaceInListIfExist(foreach.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(foreach)); + replaceElementIfExist(foreach.getVariable(), new spoon.support.visitor.replace.ReplacementVisitor.CtForEachVariableReplaceListener(foreach)); + replaceElementIfExist(foreach.getExpression(), new spoon.support.visitor.replace.ReplacementVisitor.CtForEachExpressionReplaceListener(foreach)); + replaceElementIfExist(foreach.getBody(), new spoon.support.visitor.replace.ReplacementVisitor.CtLoopBodyReplaceListener(foreach)); + replaceInListIfExist(foreach.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(foreach)); + } + + public void visitCtIf(final spoon.reflect.code.CtIf ifElement) { + replaceInListIfExist(ifElement.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(ifElement)); + replaceElementIfExist(ifElement.getCondition(), new spoon.support.visitor.replace.ReplacementVisitor.CtIfConditionReplaceListener(ifElement)); + replaceElementIfExist(((spoon.reflect.code.CtStatement) (ifElement.getThenStatement())), new spoon.support.visitor.replace.ReplacementVisitor.CtIfThenStatementReplaceListener(ifElement)); + replaceElementIfExist(((spoon.reflect.code.CtStatement) (ifElement.getElseStatement())), new spoon.support.visitor.replace.ReplacementVisitor.CtIfElseStatementReplaceListener(ifElement)); + replaceInListIfExist(ifElement.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(ifElement)); + } + + public void visitCtInterface(final spoon.reflect.declaration.CtInterface intrface) { + replaceInListIfExist(intrface.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(intrface)); + replaceInSetIfExist(intrface.getSuperInterfaces(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypeInformationSuperInterfacesReplaceListener(intrface)); + replaceInListIfExist(intrface.getFormalTypeParameters(), new spoon.support.visitor.replace.ReplacementVisitor.CtGenericElementFormalTypeParametersReplaceListener(intrface)); + replaceInSetIfExist(intrface.getNestedTypes(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypeNestedTypesReplaceListener(intrface)); + replaceInListIfExist(intrface.getFields(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypeFieldsReplaceListener(intrface)); + replaceInSetIfExist(intrface.getMethods(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypeMethodsReplaceListener(intrface)); + replaceInListIfExist(intrface.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(intrface)); + } + + public void visitCtInvocation(final spoon.reflect.code.CtInvocation invocation) { + replaceInListIfExist(invocation.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(invocation)); + replaceInListIfExist(invocation.getTypeCasts(), new spoon.support.visitor.replace.ReplacementVisitor.CtExpressionTypeCastsReplaceListener(invocation)); + replaceElementIfExist(invocation.getTarget(), new spoon.support.visitor.replace.ReplacementVisitor.CtTargetedExpressionTargetReplaceListener(invocation)); + replaceElementIfExist(invocation.getExecutable(), new spoon.support.visitor.replace.ReplacementVisitor.CtAbstractInvocationExecutableReplaceListener(invocation)); + replaceInListIfExist(invocation.getArguments(), new spoon.support.visitor.replace.ReplacementVisitor.CtAbstractInvocationArgumentsReplaceListener(invocation)); + replaceInListIfExist(invocation.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(invocation)); + } + + public void visitCtLiteral(final spoon.reflect.code.CtLiteral literal) { + replaceInListIfExist(literal.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(literal)); + replaceElementIfExist(literal.getType(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypedElementTypeReplaceListener(literal)); + replaceInListIfExist(literal.getTypeCasts(), new spoon.support.visitor.replace.ReplacementVisitor.CtExpressionTypeCastsReplaceListener(literal)); + replaceInListIfExist(literal.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(literal)); + } + + public void visitCtLocalVariable(final spoon.reflect.code.CtLocalVariable localVariable) { + replaceInListIfExist(localVariable.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(localVariable)); + replaceElementIfExist(localVariable.getType(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypedElementTypeReplaceListener(localVariable)); + replaceElementIfExist(localVariable.getDefaultExpression(), new spoon.support.visitor.replace.ReplacementVisitor.CtVariableDefaultExpressionReplaceListener(localVariable)); + replaceInListIfExist(localVariable.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(localVariable)); + } + + public void visitCtLocalVariableReference(final spoon.reflect.reference.CtLocalVariableReference reference) { + replaceElementIfExist(reference.getType(), new spoon.support.visitor.replace.ReplacementVisitor.CtVariableReferenceTypeReplaceListener(reference)); + replaceInListIfExist(reference.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(reference)); + } + + public void visitCtCatchVariable(final spoon.reflect.code.CtCatchVariable catchVariable) { + replaceInListIfExist(catchVariable.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(catchVariable)); + replaceElementIfExist(catchVariable.getType(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypedElementTypeReplaceListener(catchVariable)); + replaceInListIfExist(catchVariable.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(catchVariable)); + } + + public void visitCtCatchVariableReference(final spoon.reflect.reference.CtCatchVariableReference reference) { + replaceElementIfExist(reference.getType(), new spoon.support.visitor.replace.ReplacementVisitor.CtVariableReferenceTypeReplaceListener(reference)); + replaceInListIfExist(reference.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(reference)); + } + + public void visitCtMethod(final spoon.reflect.declaration.CtMethod m) { + replaceInListIfExist(m.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(m)); + replaceInListIfExist(m.getFormalTypeParameters(), new spoon.support.visitor.replace.ReplacementVisitor.CtGenericElementFormalTypeParametersReplaceListener(m)); + replaceElementIfExist(m.getType(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypedElementTypeReplaceListener(m)); + replaceInListIfExist(m.getParameters(), new spoon.support.visitor.replace.ReplacementVisitor.CtExecutableParametersReplaceListener(m)); + replaceInSetIfExist(m.getThrownTypes(), new spoon.support.visitor.replace.ReplacementVisitor.CtExecutableThrownTypesReplaceListener(m)); + replaceElementIfExist(m.getBody(), new spoon.support.visitor.replace.ReplacementVisitor.CtExecutableBodyReplaceListener(m)); + replaceInListIfExist(m.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(m)); + } + + public void visitCtNewArray(final spoon.reflect.code.CtNewArray newArray) { + replaceInListIfExist(newArray.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(newArray)); + replaceElementIfExist(newArray.getType(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypedElementTypeReplaceListener(newArray)); + replaceInListIfExist(newArray.getTypeCasts(), new spoon.support.visitor.replace.ReplacementVisitor.CtExpressionTypeCastsReplaceListener(newArray)); + replaceInListIfExist(newArray.getElements(), new spoon.support.visitor.replace.ReplacementVisitor.CtNewArrayElementsReplaceListener(newArray)); + replaceInListIfExist(newArray.getDimensionExpressions(), new spoon.support.visitor.replace.ReplacementVisitor.CtNewArrayDimensionExpressionsReplaceListener(newArray)); + replaceInListIfExist(newArray.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(newArray)); + } + + @java.lang.Override + public void visitCtConstructorCall(final spoon.reflect.code.CtConstructorCall ctConstructorCall) { + replaceInListIfExist(ctConstructorCall.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(ctConstructorCall)); + replaceInListIfExist(ctConstructorCall.getTypeCasts(), new spoon.support.visitor.replace.ReplacementVisitor.CtExpressionTypeCastsReplaceListener(ctConstructorCall)); + replaceElementIfExist(ctConstructorCall.getExecutable(), new spoon.support.visitor.replace.ReplacementVisitor.CtAbstractInvocationExecutableReplaceListener(ctConstructorCall)); + replaceElementIfExist(ctConstructorCall.getTarget(), new spoon.support.visitor.replace.ReplacementVisitor.CtTargetedExpressionTargetReplaceListener(ctConstructorCall)); + replaceInListIfExist(ctConstructorCall.getArguments(), new spoon.support.visitor.replace.ReplacementVisitor.CtAbstractInvocationArgumentsReplaceListener(ctConstructorCall)); + replaceInListIfExist(ctConstructorCall.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(ctConstructorCall)); + } + + public void visitCtNewClass(final spoon.reflect.code.CtNewClass newClass) { + replaceInListIfExist(newClass.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(newClass)); + replaceElementIfExist(newClass.getType(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypedElementTypeReplaceListener(newClass)); + replaceInListIfExist(newClass.getTypeCasts(), new spoon.support.visitor.replace.ReplacementVisitor.CtExpressionTypeCastsReplaceListener(newClass)); + replaceElementIfExist(newClass.getExecutable(), new spoon.support.visitor.replace.ReplacementVisitor.CtAbstractInvocationExecutableReplaceListener(newClass)); + replaceElementIfExist(newClass.getTarget(), new spoon.support.visitor.replace.ReplacementVisitor.CtTargetedExpressionTargetReplaceListener(newClass)); + replaceInListIfExist(newClass.getArguments(), new spoon.support.visitor.replace.ReplacementVisitor.CtAbstractInvocationArgumentsReplaceListener(newClass)); + replaceElementIfExist(newClass.getAnonymousClass(), new spoon.support.visitor.replace.ReplacementVisitor.CtNewClassAnonymousClassReplaceListener(newClass)); + replaceInListIfExist(newClass.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(newClass)); + } + + @java.lang.Override + public void visitCtLambda(final spoon.reflect.code.CtLambda lambda) { + replaceInListIfExist(lambda.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(lambda)); + replaceElementIfExist(lambda.getType(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypedElementTypeReplaceListener(lambda)); + replaceInListIfExist(lambda.getTypeCasts(), new spoon.support.visitor.replace.ReplacementVisitor.CtExpressionTypeCastsReplaceListener(lambda)); + replaceInListIfExist(lambda.getParameters(), new spoon.support.visitor.replace.ReplacementVisitor.CtExecutableParametersReplaceListener(lambda)); + replaceElementIfExist(lambda.getBody(), new spoon.support.visitor.replace.ReplacementVisitor.CtExecutableBodyReplaceListener(lambda)); + replaceElementIfExist(lambda.getExpression(), new spoon.support.visitor.replace.ReplacementVisitor.CtLambdaExpressionReplaceListener(lambda)); + replaceInListIfExist(lambda.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(lambda)); + } + + @java.lang.Override + public > void visitCtExecutableReferenceExpression(final spoon.reflect.code.CtExecutableReferenceExpression expression) { + replaceElementIfExist(expression.getType(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypedElementTypeReplaceListener(expression)); + replaceInListIfExist(expression.getTypeCasts(), new spoon.support.visitor.replace.ReplacementVisitor.CtExpressionTypeCastsReplaceListener(expression)); + replaceElementIfExist(expression.getExecutable(), new spoon.support.visitor.replace.ReplacementVisitor.CtExecutableReferenceExpressionExecutableReplaceListener(expression)); + replaceElementIfExist(expression.getTarget(), new spoon.support.visitor.replace.ReplacementVisitor.CtTargetedExpressionTargetReplaceListener(expression)); + } + + public void visitCtOperatorAssignment(final spoon.reflect.code.CtOperatorAssignment assignment) { + replaceInListIfExist(assignment.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(assignment)); + replaceElementIfExist(assignment.getType(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypedElementTypeReplaceListener(assignment)); + replaceInListIfExist(assignment.getTypeCasts(), new spoon.support.visitor.replace.ReplacementVisitor.CtExpressionTypeCastsReplaceListener(assignment)); + replaceElementIfExist(assignment.getAssigned(), new spoon.support.visitor.replace.ReplacementVisitor.CtAssignmentAssignedReplaceListener(assignment)); + replaceElementIfExist(assignment.getAssignment(), new spoon.support.visitor.replace.ReplacementVisitor.CtRHSReceiverAssignmentReplaceListener(assignment)); + replaceInListIfExist(assignment.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(assignment)); + } + + public void visitCtPackage(final spoon.reflect.declaration.CtPackage ctPackage) { + replaceInListIfExist(ctPackage.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(ctPackage)); + replaceInSetIfExist(ctPackage.getPackages(), new spoon.support.visitor.replace.ReplacementVisitor.CtPackagePackagesReplaceListener(ctPackage)); + replaceInSetIfExist(ctPackage.getTypes(), new spoon.support.visitor.replace.ReplacementVisitor.CtPackageTypesReplaceListener(ctPackage)); + replaceInListIfExist(ctPackage.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(ctPackage)); + } + + public void visitCtPackageReference(final spoon.reflect.reference.CtPackageReference reference) { + } + + public void visitCtParameter(final spoon.reflect.declaration.CtParameter parameter) { + replaceInListIfExist(parameter.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(parameter)); + replaceElementIfExist(parameter.getType(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypedElementTypeReplaceListener(parameter)); + replaceInListIfExist(parameter.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(parameter)); + } + + public void visitCtParameterReference(final spoon.reflect.reference.CtParameterReference reference) { + replaceElementIfExist(reference.getType(), new spoon.support.visitor.replace.ReplacementVisitor.CtVariableReferenceTypeReplaceListener(reference)); + replaceInListIfExist(reference.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(reference)); + } + + public void visitCtReturn(final spoon.reflect.code.CtReturn returnStatement) { + replaceInListIfExist(returnStatement.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(returnStatement)); + replaceElementIfExist(returnStatement.getReturnedExpression(), new spoon.support.visitor.replace.ReplacementVisitor.CtReturnReturnedExpressionReplaceListener(returnStatement)); + replaceInListIfExist(returnStatement.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(returnStatement)); + } + + public void visitCtStatementList(final spoon.reflect.code.CtStatementList statements) { + replaceInListIfExist(statements.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(statements)); + replaceInListIfExist(statements.getStatements(), new spoon.support.visitor.replace.ReplacementVisitor.CtStatementListStatementsReplaceListener(statements)); + replaceInListIfExist(statements.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(statements)); + } + + public void visitCtSwitch(final spoon.reflect.code.CtSwitch switchStatement) { + replaceInListIfExist(switchStatement.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(switchStatement)); + replaceElementIfExist(switchStatement.getSelector(), new spoon.support.visitor.replace.ReplacementVisitor.CtSwitchSelectorReplaceListener(switchStatement)); + replaceInListIfExist(switchStatement.getCases(), new spoon.support.visitor.replace.ReplacementVisitor.CtSwitchCasesReplaceListener(switchStatement)); + replaceInListIfExist(switchStatement.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(switchStatement)); + } + + public void visitCtSynchronized(final spoon.reflect.code.CtSynchronized synchro) { + replaceInListIfExist(synchro.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(synchro)); + replaceElementIfExist(synchro.getExpression(), new spoon.support.visitor.replace.ReplacementVisitor.CtSynchronizedExpressionReplaceListener(synchro)); + replaceElementIfExist(synchro.getBlock(), new spoon.support.visitor.replace.ReplacementVisitor.CtSynchronizedBlockReplaceListener(synchro)); + replaceInListIfExist(synchro.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(synchro)); + } + + public void visitCtThrow(final spoon.reflect.code.CtThrow throwStatement) { + replaceInListIfExist(throwStatement.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(throwStatement)); + replaceElementIfExist(throwStatement.getThrownExpression(), new spoon.support.visitor.replace.ReplacementVisitor.CtThrowThrownExpressionReplaceListener(throwStatement)); + replaceInListIfExist(throwStatement.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(throwStatement)); + } + + public void visitCtTry(final spoon.reflect.code.CtTry tryBlock) { + replaceInListIfExist(tryBlock.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(tryBlock)); + replaceElementIfExist(tryBlock.getBody(), new spoon.support.visitor.replace.ReplacementVisitor.CtTryBodyReplaceListener(tryBlock)); + replaceInListIfExist(tryBlock.getCatchers(), new spoon.support.visitor.replace.ReplacementVisitor.CtTryCatchersReplaceListener(tryBlock)); + replaceElementIfExist(tryBlock.getFinalizer(), new spoon.support.visitor.replace.ReplacementVisitor.CtTryFinalizerReplaceListener(tryBlock)); + replaceInListIfExist(tryBlock.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(tryBlock)); + } + + @java.lang.Override + public void visitCtTryWithResource(final spoon.reflect.code.CtTryWithResource tryWithResource) { + replaceInListIfExist(tryWithResource.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(tryWithResource)); + replaceInListIfExist(tryWithResource.getResources(), new spoon.support.visitor.replace.ReplacementVisitor.CtTryWithResourceResourcesReplaceListener(tryWithResource)); + replaceElementIfExist(tryWithResource.getBody(), new spoon.support.visitor.replace.ReplacementVisitor.CtTryBodyReplaceListener(tryWithResource)); + replaceInListIfExist(tryWithResource.getCatchers(), new spoon.support.visitor.replace.ReplacementVisitor.CtTryCatchersReplaceListener(tryWithResource)); + replaceElementIfExist(tryWithResource.getFinalizer(), new spoon.support.visitor.replace.ReplacementVisitor.CtTryFinalizerReplaceListener(tryWithResource)); + replaceInListIfExist(tryWithResource.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(tryWithResource)); + } + + public void visitCtTypeParameterReference(final spoon.reflect.reference.CtTypeParameterReference ref) { + replaceElementIfExist(ref.getPackage(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypeReferencePackageReplaceListener(ref)); + replaceElementIfExist(ref.getDeclaringType(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypeReferenceDeclaringTypeReplaceListener(ref)); + replaceInListIfExist(ref.getActualTypeArguments(), new spoon.support.visitor.replace.ReplacementVisitor.CtGenericElementReferenceActualTypeArgumentsReplaceListener(ref)); + replaceInListIfExist(ref.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(ref)); + replaceElementIfExist(ref.getBoundingType(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypeParameterReferenceBoundingTypeReplaceListener(ref)); + } + + @java.lang.Override + public void visitCtIntersectionTypeReference(final spoon.reflect.reference.CtIntersectionTypeReference reference) { + replaceInListIfExist(reference.getBounds(), new spoon.support.visitor.replace.ReplacementVisitor.CtIntersectionTypeReferenceBoundsReplaceListener(reference)); + } + + public void visitCtTypeReference(final spoon.reflect.reference.CtTypeReference reference) { + replaceElementIfExist(reference.getPackage(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypeReferencePackageReplaceListener(reference)); + replaceElementIfExist(reference.getDeclaringType(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypeReferenceDeclaringTypeReplaceListener(reference)); + replaceInListIfExist(reference.getActualTypeArguments(), new spoon.support.visitor.replace.ReplacementVisitor.CtGenericElementReferenceActualTypeArgumentsReplaceListener(reference)); + replaceInListIfExist(reference.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(reference)); + replaceInListIfExist(reference.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(reference)); + } + + @java.lang.Override + public void visitCtCircularTypeReference(final spoon.reflect.internal.CtCircularTypeReference reference) { + } + + @java.lang.Override + public void visitCtImplicitTypeReference(final spoon.reflect.internal.CtImplicitTypeReference reference) { + } + + @java.lang.Override + public void visitCtTypeAccess(final spoon.reflect.code.CtTypeAccess typeAccess) { + replaceInListIfExist(typeAccess.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(typeAccess)); + replaceElementIfExist(typeAccess.getType(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypeAccessTypeReplaceListener(typeAccess)); + replaceInListIfExist(typeAccess.getTypeCasts(), new spoon.support.visitor.replace.ReplacementVisitor.CtExpressionTypeCastsReplaceListener(typeAccess)); + replaceElementIfExist(typeAccess.getAccessedType(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypeAccessAccessedTypeReplaceListener(typeAccess)); + replaceInListIfExist(typeAccess.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(typeAccess)); + } + + public void visitCtUnaryOperator(final spoon.reflect.code.CtUnaryOperator operator) { + replaceInListIfExist(operator.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(operator)); + replaceElementIfExist(operator.getType(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypedElementTypeReplaceListener(operator)); + replaceInListIfExist(operator.getTypeCasts(), new spoon.support.visitor.replace.ReplacementVisitor.CtExpressionTypeCastsReplaceListener(operator)); + replaceElementIfExist(operator.getOperand(), new spoon.support.visitor.replace.ReplacementVisitor.CtUnaryOperatorOperandReplaceListener(operator)); + replaceInListIfExist(operator.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(operator)); + } + + @java.lang.Override + public void visitCtVariableRead(final spoon.reflect.code.CtVariableRead variableRead) { + replaceInListIfExist(variableRead.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(variableRead)); + replaceElementIfExist(variableRead.getType(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypedElementTypeReplaceListener(variableRead)); + replaceInListIfExist(variableRead.getTypeCasts(), new spoon.support.visitor.replace.ReplacementVisitor.CtExpressionTypeCastsReplaceListener(variableRead)); + replaceElementIfExist(variableRead.getVariable(), new spoon.support.visitor.replace.ReplacementVisitor.CtVariableAccessVariableReplaceListener(variableRead)); + replaceInListIfExist(variableRead.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(variableRead)); + } + + @java.lang.Override + public void visitCtVariableWrite(final spoon.reflect.code.CtVariableWrite variableWrite) { + replaceInListIfExist(variableWrite.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(variableWrite)); + replaceElementIfExist(variableWrite.getType(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypedElementTypeReplaceListener(variableWrite)); + replaceInListIfExist(variableWrite.getTypeCasts(), new spoon.support.visitor.replace.ReplacementVisitor.CtExpressionTypeCastsReplaceListener(variableWrite)); + replaceElementIfExist(variableWrite.getVariable(), new spoon.support.visitor.replace.ReplacementVisitor.CtVariableAccessVariableReplaceListener(variableWrite)); + replaceInListIfExist(variableWrite.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(variableWrite)); + } + + public void visitCtWhile(final spoon.reflect.code.CtWhile whileLoop) { + replaceInListIfExist(whileLoop.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(whileLoop)); + replaceElementIfExist(whileLoop.getLoopingExpression(), new spoon.support.visitor.replace.ReplacementVisitor.CtWhileLoopingExpressionReplaceListener(whileLoop)); + replaceElementIfExist(whileLoop.getBody(), new spoon.support.visitor.replace.ReplacementVisitor.CtLoopBodyReplaceListener(whileLoop)); + replaceInListIfExist(whileLoop.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(whileLoop)); + } + + public void visitCtCodeSnippetExpression(final spoon.reflect.code.CtCodeSnippetExpression expression) { + } + + public void visitCtCodeSnippetStatement(final spoon.reflect.code.CtCodeSnippetStatement statement) { + } + + public void visitCtUnboundVariableReference(final spoon.reflect.reference.CtUnboundVariableReference reference) { + } + + @java.lang.Override + public void visitCtFieldRead(final spoon.reflect.code.CtFieldRead fieldRead) { + replaceInListIfExist(fieldRead.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(fieldRead)); + replaceInListIfExist(fieldRead.getTypeCasts(), new spoon.support.visitor.replace.ReplacementVisitor.CtExpressionTypeCastsReplaceListener(fieldRead)); + replaceElementIfExist(fieldRead.getTarget(), new spoon.support.visitor.replace.ReplacementVisitor.CtTargetedExpressionTargetReplaceListener(fieldRead)); + replaceElementIfExist(fieldRead.getVariable(), new spoon.support.visitor.replace.ReplacementVisitor.CtFieldAccessVariableReplaceListener(fieldRead)); + replaceInListIfExist(fieldRead.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(fieldRead)); + } + + @java.lang.Override + public void visitCtFieldWrite(final spoon.reflect.code.CtFieldWrite fieldWrite) { + replaceInListIfExist(fieldWrite.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(fieldWrite)); + replaceInListIfExist(fieldWrite.getTypeCasts(), new spoon.support.visitor.replace.ReplacementVisitor.CtExpressionTypeCastsReplaceListener(fieldWrite)); + replaceElementIfExist(fieldWrite.getTarget(), new spoon.support.visitor.replace.ReplacementVisitor.CtTargetedExpressionTargetReplaceListener(fieldWrite)); + replaceElementIfExist(fieldWrite.getVariable(), new spoon.support.visitor.replace.ReplacementVisitor.CtFieldAccessVariableReplaceListener(fieldWrite)); + replaceInListIfExist(fieldWrite.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(fieldWrite)); + } + + @java.lang.Override + public void visitCtSuperAccess(final spoon.reflect.code.CtSuperAccess f) { + replaceInListIfExist(f.getAnnotations(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementAnnotationsReplaceListener(f)); + replaceElementIfExist(f.getType(), new spoon.support.visitor.replace.ReplacementVisitor.CtTypedElementTypeReplaceListener(f)); + replaceInListIfExist(f.getTypeCasts(), new spoon.support.visitor.replace.ReplacementVisitor.CtExpressionTypeCastsReplaceListener(f)); + replaceElementIfExist(f.getTarget(), new spoon.support.visitor.replace.ReplacementVisitor.CtTargetedExpressionTargetReplaceListener(f)); + replaceInListIfExist(f.getComments(), new spoon.support.visitor.replace.ReplacementVisitor.CtElementCommentsReplaceListener(f)); + } + + @java.lang.Override + public void visitCtComment(final spoon.reflect.code.CtComment comment) { + } +} + diff --git a/src/main/java/spoon/testing/utils/ModelUtils.java b/src/main/java/spoon/testing/utils/ModelUtils.java index 0e7637e5917..6ddddccd4f0 100644 --- a/src/main/java/spoon/testing/utils/ModelUtils.java +++ b/src/main/java/spoon/testing/utils/ModelUtils.java @@ -78,7 +78,9 @@ public static Factory buildNoClasspath(Class... classesToBuild) throws Except } public static Factory build(File... filesToBuild) { - SpoonCompiler comp = new Launcher().createCompiler(); + final Launcher launcher = new Launcher(); + launcher.getEnvironment().setNoClasspath(true); + SpoonCompiler comp = launcher.createCompiler(); for (File fileToBuild : filesToBuild) { try { comp.addInputSource(SpoonResourceHelper.createResource(fileToBuild)); diff --git a/src/test/java/spoon/reflect/visitor/CtScannerTest.java b/src/test/java/spoon/reflect/visitor/CtScannerTest.java index 8ee2434c79a..f16cfe1a430 100644 --- a/src/test/java/spoon/reflect/visitor/CtScannerTest.java +++ b/src/test/java/spoon/reflect/visitor/CtScannerTest.java @@ -19,7 +19,18 @@ import org.junit.Test; import spoon.Launcher; +import spoon.generating.ReplacementVisitorGenerator; +import spoon.reflect.declaration.CtElement; +import spoon.reflect.declaration.CtType; import spoon.reflect.visitor.processors.CheckScannerProcessor; +import spoon.support.visitor.replace.ReplacementVisitor; + +import java.io.File; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import static spoon.testing.Assert.assertThat; +import static spoon.testing.utils.ModelUtils.build; public class CtScannerTest { @Test @@ -42,4 +53,47 @@ public void testScannerContract() throws Exception { // All assertions are in the processor. } + + @Test + public void testGenerateReplacementVisitor() throws Exception { + class RegexFilter implements Filter> { + private final Pattern regex; + + private RegexFilter(String regex) { + if (regex == null) { + throw new IllegalArgumentException(); + } + this.regex = Pattern.compile(regex); + } + + public boolean matches(CtType element) { + Matcher m = regex.matcher(element.getQualifiedName()); + return m.matches(); + } + + public Class getType() { + return CtElement.class; + } + } + + final Launcher launcher = new Launcher(); + launcher.getEnvironment().setNoClasspath(true); + launcher.getEnvironment().setGenerateJavadoc(true); + launcher.getEnvironment().useTabulations(true); + launcher.setSourceOutputDirectory("./target/generated/"); + // interfaces. + launcher.addInputResource("./src/main/java/spoon/reflect/code"); + launcher.addInputResource("./src/main/java/spoon/reflect/declaration"); + launcher.addInputResource("./src/main/java/spoon/reflect/reference"); + launcher.addInputResource("./src/main/java/spoon/reflect/internal"); + // Utils. + launcher.addInputResource("./src/main/java/spoon/reflect/visitor/CtScanner.java"); + launcher.addInputResource("./src/main/java/spoon/generating/replace/"); + launcher.addProcessor(new ReplacementVisitorGenerator()); + launcher.setOutputFilter(new RegexFilter("spoon.support.visitor.replace.*")); + launcher.run(); + + assertThat(build(new File("./src/main/java/spoon/support/visitor/replace/ReplacementVisitor.java")).Class().get(ReplacementVisitor.class)) + .isEqualTo(build(new File("./target/generated/spoon/support/visitor/replace/ReplacementVisitor.java")).Class().get(ReplacementVisitor.class)); + } } diff --git a/src/test/java/spoon/test/api/APITest.java b/src/test/java/spoon/test/api/APITest.java index 24d736fbf13..f409afc1a90 100644 --- a/src/test/java/spoon/test/api/APITest.java +++ b/src/test/java/spoon/test/api/APITest.java @@ -6,18 +6,36 @@ import spoon.Launcher; import spoon.SpoonAPI; import spoon.compiler.Environment; +import spoon.reflect.code.CtIf; +import spoon.reflect.code.CtStatement; +import spoon.reflect.code.CtVariableAccess; import spoon.reflect.declaration.CtClass; +import spoon.reflect.declaration.CtElement; import spoon.reflect.declaration.CtMethod; import spoon.reflect.declaration.CtPackage; +import spoon.reflect.declaration.CtParameter; import spoon.reflect.declaration.CtType; import spoon.reflect.factory.Factory; +import spoon.reflect.reference.CtTypeReference; +import spoon.reflect.visitor.CtVisitor; +import spoon.reflect.visitor.Query; import spoon.reflect.visitor.filter.AbstractFilter; +import spoon.reflect.visitor.filter.TypeFilter; import spoon.support.JavaOutputProcessor; +import spoon.support.reflect.declaration.CtElementImpl; +import spoon.template.Local; +import spoon.template.TemplateMatcher; +import spoon.template.TemplateParameter; import spoon.test.api.testclasses.Bar; import java.io.File; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.function.Function; import java.util.stream.Collectors; import static org.junit.Assert.assertEquals; @@ -260,4 +278,87 @@ public void testInvalidateCacheOfCompiler() throws Exception { assertTrue(spoon.getModelBuilder().compile()); } + + @Test + public void testSetterInNodes() throws Exception { + // contract: Check that all setters of an object have a condition to check + // that the new value is != null to avoid NPE when we set the parent. + class SetterMethodWithoutCollectionsFilter extends TypeFilter> { + private final List> collections = new ArrayList<>(4); + + public SetterMethodWithoutCollectionsFilter(Factory factory) { + super(CtMethod.class); + for (Class aCollectionClass : Arrays.asList(Collection.class, List.class, Map.class, Set.class)) { + collections.add(factory.Type().createReference(aCollectionClass)); + } + } + + @Override + public boolean matches(CtMethod element) { + return isSetterMethod(element) && !isSubTypeOfCollection(element) && super.matches(element); + } + + private boolean isSubTypeOfCollection(CtMethod element) { + final CtTypeReference type = element.getParameters().get(0).getType(); + for (CtTypeReference aCollectionRef : collections) { + if (type.isSubtypeOf(aCollectionRef) || type.equals(aCollectionRef)) { + return true; + } + } + return false; + } + + private boolean isSetterMethod(CtMethod element) { + final List> parameters = element.getParameters(); + if (parameters.size() != 1) { + return false; + } + final CtTypeReference typeParameter = parameters.get(0).getType(); + final CtTypeReference ctElementRef = element.getFactory().Type().createReference(CtElement.class); + if (!typeParameter.isSubtypeOf(ctElementRef) || !typeParameter.equals(ctElementRef)) { + return false; + } + return element.getSimpleName().startsWith("set") && element.getDeclaringType().getSimpleName().startsWith("Ct") && element.getBody() != null; + } + } + class CheckNotNullToSetParentMatcher extends CtElementImpl { + public TemplateParameter> _parameter_access_; + + public void matcher() { + if (_parameter_access_.S() != null) { + _parameter_access_.S().setParent(this); + } + } + + @Override + @Local + public void accept(CtVisitor visitor) { + } + } + + final Launcher launcher = new Launcher(); + launcher.getEnvironment().setNoClasspath(true); + launcher.setSourceOutputDirectory("./target/trash/"); + // Implementations + launcher.addInputResource("./src/main/java/spoon/support/reflect/code"); + launcher.addInputResource("./src/main/java/spoon/support/reflect/declaration"); + launcher.addInputResource("./src/main/java/spoon/support/reflect/reference"); + launcher.addInputResource("./src/test/java/" + this.getClass().getCanonicalName().replace(".", "/") + ".java"); + // Needed for #isSubTypeOf method. + launcher.addInputResource("./src/main/java/spoon/reflect/"); + launcher.buildModel(); + + // Template matcher. + CtClass matcherCtClass = launcher.getFactory().Class().get(CheckNotNullToSetParentMatcher.class); + CtIf templateRoot = matcherCtClass.getMethod("matcher").getBody().getStatement(0); + + final List> setters = Query.getElements(launcher.getFactory(), new SetterMethodWithoutCollectionsFilter(launcher.getFactory())); + for (CtStatement statement : setters.stream().map((Function, CtStatement>) ctMethod -> ctMethod.getBody().getStatement(0)).collect(Collectors.toList())) { + // First statement should be a condition to protect the setter of the parent. + assertTrue("Check the method " + statement.getParent(CtMethod.class).getSignature() + " in the declaring class " + statement.getParent(CtType.class).getQualifiedName(), statement instanceof CtIf); + CtIf ifCondition = (CtIf) statement; + TemplateMatcher matcher = new TemplateMatcher(templateRoot); + assertEquals(1, matcher.find(ifCondition).size()); + } + } }