Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy collections in CloneBuilder. #727

Merged
merged 3 commits into from
Jun 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/main/java/spoon/generating/CloneVisitorGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public class CloneVisitorGenerator extends AbstractManualProcessor {
private static final String TARGET_CLONE_PACKAGE = "spoon.support.visitor.clone";
Expand Down Expand Up @@ -376,14 +377,24 @@ private <T> CtInvocation<T> createSuperInvocation(CtMethod<T> element) {
}

/**
* Creates <code>((CtElement) other).setX(element.getX())</code>.
* Creates <code>((CtElement) other).setX(element.getX())</code>
* or <code>((CtElement) other).setX(new Collection(element.getX()))</code>
* if the field is a collection.
*
* @param type <code>CtElement</code>
* @param setter <code>setX</code>.
* @param getter <code>getX</code>.
*/
private CtInvocation<?> createSetterInvocation(CtTypeReference<?> type, CtMethod<?> setter, CtInvocation<?> getter) {
return factory.Code().createInvocation(otherRead.clone().addTypeCast(type), setter.getReference(), getter);
CtExpression getterArgument;
if (getter.getType().equals(SET_REFERENCE)) {
getterArgument = factory.Code().createConstructorCall(factory.Type().createReference(TreeSet.class), getter);
} else if (getter.getType().equals(COLLECTION_REFERENCE) || getter.getType().equals(LIST_REFERENCE)) {
getterArgument = factory.Code().createConstructorCall(factory.Type().createReference(ArrayList.class), getter);
} else {
getterArgument = getter;
}
return factory.Code().createInvocation(otherRead.clone().addTypeCast(type), setter.getReference(), getterArgument);
}

/**
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/spoon/support/visitor/clone/CloneBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void scanCtStatement(spoon.reflect.code.CtStatement s) {
*/
// auto-generated, see spoon.generating.CloneVisitorGenerator
public <T> void scanCtType(spoon.reflect.declaration.CtType<T> type) {
((spoon.reflect.declaration.CtType<T>) (this.other)).setModifiers(type.getModifiers());
((spoon.reflect.declaration.CtType<T>) (this.other)).setModifiers(new java.util.TreeSet(type.getModifiers()));
((spoon.reflect.declaration.CtType<T>) (this.other)).setShadow(type.isShadow());
super.scanCtType(type);
}
Expand All @@ -118,7 +118,7 @@ public <A extends java.lang.annotation.Annotation> void visitCtAnnotation(spoon.

// auto-generated, see spoon.generating.CloneVisitorGenerator
public void visitCtAnonymousExecutable(spoon.reflect.declaration.CtAnonymousExecutable e) {
((spoon.reflect.declaration.CtAnonymousExecutable) (this.other)).setModifiers(e.getModifiers());
((spoon.reflect.declaration.CtAnonymousExecutable) (this.other)).setModifiers(new java.util.TreeSet(e.getModifiers()));
super.visitCtAnonymousExecutable(e);
}

Expand All @@ -136,7 +136,7 @@ public void visitCtBreak(spoon.reflect.code.CtBreak e) {

// auto-generated, see spoon.generating.CloneVisitorGenerator
public <T> void visitCtConstructor(spoon.reflect.declaration.CtConstructor<T> e) {
((spoon.reflect.declaration.CtConstructor<T>) (this.other)).setModifiers(e.getModifiers());
((spoon.reflect.declaration.CtConstructor<T>) (this.other)).setModifiers(new java.util.TreeSet(e.getModifiers()));
((spoon.reflect.declaration.CtConstructor<T>) (this.other)).setShadow(e.isShadow());
super.visitCtConstructor(e);
}
Expand All @@ -155,7 +155,7 @@ public <T> void visitCtExecutableReference(spoon.reflect.reference.CtExecutableR

// auto-generated, see spoon.generating.CloneVisitorGenerator
public <T> void visitCtField(spoon.reflect.declaration.CtField<T> e) {
((spoon.reflect.declaration.CtField<T>) (this.other)).setModifiers(e.getModifiers());
((spoon.reflect.declaration.CtField<T>) (this.other)).setModifiers(new java.util.TreeSet(e.getModifiers()));
((spoon.reflect.declaration.CtField<T>) (this.other)).setShadow(e.isShadow());
super.visitCtField(e);
}
Expand Down Expand Up @@ -186,7 +186,7 @@ public <T> void visitCtLocalVariable(spoon.reflect.code.CtLocalVariable<T> e) {
if (!(((this.other) instanceof spoon.reflect.declaration.CtAnonymousExecutable) || ((this.other) instanceof spoon.reflect.declaration.CtConstructor))) {
((spoon.reflect.code.CtLocalVariable<T>) (this.other)).setSimpleName(e.getSimpleName());
}
((spoon.reflect.code.CtLocalVariable<T>) (this.other)).setModifiers(e.getModifiers());
((spoon.reflect.code.CtLocalVariable<T>) (this.other)).setModifiers(new java.util.TreeSet(e.getModifiers()));
super.visitCtLocalVariable(e);
}

Expand All @@ -195,14 +195,14 @@ public <T> void visitCtCatchVariable(spoon.reflect.code.CtCatchVariable<T> e) {
if (!(((this.other) instanceof spoon.reflect.declaration.CtAnonymousExecutable) || ((this.other) instanceof spoon.reflect.declaration.CtConstructor))) {
((spoon.reflect.code.CtCatchVariable<T>) (this.other)).setSimpleName(e.getSimpleName());
}
((spoon.reflect.code.CtCatchVariable<T>) (this.other)).setModifiers(e.getModifiers());
((spoon.reflect.code.CtCatchVariable<T>) (this.other)).setModifiers(new java.util.TreeSet(e.getModifiers()));
super.visitCtCatchVariable(e);
}

// auto-generated, see spoon.generating.CloneVisitorGenerator
public <T> void visitCtMethod(spoon.reflect.declaration.CtMethod<T> e) {
((spoon.reflect.declaration.CtMethod<T>) (this.other)).setDefaultMethod(e.isDefaultMethod());
((spoon.reflect.declaration.CtMethod<T>) (this.other)).setModifiers(e.getModifiers());
((spoon.reflect.declaration.CtMethod<T>) (this.other)).setModifiers(new java.util.TreeSet(e.getModifiers()));
((spoon.reflect.declaration.CtMethod<T>) (this.other)).setShadow(e.isShadow());
super.visitCtMethod(e);
}
Expand Down Expand Up @@ -240,7 +240,7 @@ public void visitCtPackage(spoon.reflect.declaration.CtPackage e) {
// auto-generated, see spoon.generating.CloneVisitorGenerator
public <T> void visitCtParameter(spoon.reflect.declaration.CtParameter<T> e) {
((spoon.reflect.declaration.CtParameter<T>) (this.other)).setVarArgs(e.isVarArgs());
((spoon.reflect.declaration.CtParameter<T>) (this.other)).setModifiers(e.getModifiers());
((spoon.reflect.declaration.CtParameter<T>) (this.other)).setModifiers(new java.util.TreeSet(e.getModifiers()));
((spoon.reflect.declaration.CtParameter<T>) (this.other)).setShadow(e.isShadow());
super.visitCtParameter(e);
}
Expand Down
45 changes: 45 additions & 0 deletions src/test/java/spoon/test/method/MethodTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.test.method;

import static org.junit.Assert.assertEquals;
import static spoon.testing.utils.ModelUtils.build;

import org.junit.Test;

import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.ModifierKind;
import spoon.reflect.factory.Factory;
import spoon.test.delete.testclasses.Adobada;

public class MethodTest {

@Test
public void testClone() throws Exception {

final Factory factory = build(Adobada.class);
final CtClass<Adobada> adobada = factory.Class().get(Adobada.class);
final CtMethod<?> m2 = adobada.getMethod("m2");

CtMethod<?> clone = m2.clone();
clone.setVisibility(ModifierKind.PRIVATE);

assertEquals(ModifierKind.PUBLIC, m2.getModifiers().iterator().next());
}
}