Skip to content

Commit

Permalink
feat: one could add a type member that already exists (equals but not…
Browse files Browse the repository at this point in the history
… same) and modify it afterwards (#1864)
  • Loading branch information
monperrus authored and pvojtechovsky committed Feb 18, 2018
1 parent 402129c commit fc1c421
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public <C extends CtType<T>> C addTypeMemberAt(int position, CtTypeMember member
if (this.typeMembers == CtElementImpl.<CtTypeMember>emptyList()) {
this.typeMembers = new SortedList<>(new CtLineElementComparator());
}
if (!this.typeMembers.contains(member)) {
if (!this.typeMembers.stream().anyMatch(m -> m == member)) {
member.setParent(this);
CtRole role;
if (member instanceof CtMethod) {
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/spoon/test/ctClass/CtClassTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static spoon.testing.utils.ModelUtils.build;
import static spoon.testing.utils.ModelUtils.buildClass;
import static spoon.testing.utils.ModelUtils.canBeBuilt;
Expand All @@ -20,10 +22,12 @@
import org.junit.Test;

import spoon.Launcher;
import spoon.SpoonException;
import spoon.reflect.CtModel;
import spoon.reflect.code.CtBlock;
import spoon.reflect.code.CtConstructorCall;
import spoon.reflect.code.CtNewClass;
import spoon.reflect.code.CtStatement;
import spoon.reflect.declaration.CtAnonymousExecutable;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtConstructor;
Expand Down Expand Up @@ -60,6 +64,21 @@ public void getConstructor() throws Exception {
typeStringArrayArray.setComponentType(typeStringArray);
constructor = foo.getConstructor(typeStringArrayArray);
assertEquals(typeStringArrayArray, constructor.getParameters().get(0).getType());

// contract: one could add a type member that already exists (equals but not same) and modify it afterwards
// this adds some flexibility for client code
// see https://github.com/INRIA/spoon/issues/1862
CtConstructor cons = foo.getConstructors().toArray(new CtConstructor[0])[0].clone();
foo.addConstructor(cons);
// as long as we have not changed the signature, getConstructors, which is based on signatures,
// thinks there is one single constructor (and that's OK)
assertEquals(3, foo.getConstructors().size());
cons.addParameter(cons.getFactory().createParameter().setType(cons.getFactory().Type().OBJECT));
// now that we have changed the signature we can call getConstructors safely
assertEquals(4, foo.getConstructors().size());
assertSame(cons, foo.getTypeMembers().get(3));
// the parent is set (the core problem described in the issue has been fixed)
assertSame(foo, cons.getParent());
}

@Test
Expand Down

0 comments on commit fc1c421

Please sign in to comment.