Skip to content

Commit a198bfe

Browse files
slarsewoutersmeenk
authored andcommitted
feat: Add CtAbstractInvocation.addArgumentAt method (INRIA#3885)
1 parent d2ea85e commit a198bfe

File tree

5 files changed

+85
-0
lines changed

5 files changed

+85
-0
lines changed

src/main/java/spoon/reflect/code/CtAbstractInvocation.java

+10
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ public interface CtAbstractInvocation<T> extends CtElement {
3939
@PropertySetter(role = ARGUMENT)
4040
<C extends CtAbstractInvocation<T>> C addArgument(CtExpression<?> argument);
4141

42+
/**
43+
* Adds an argument expression to the invocation at the specified position.
44+
*
45+
* @param position position to add the argument at.
46+
* @param argument argument to add.
47+
* @return the receiver.
48+
*/
49+
@PropertySetter(role = ARGUMENT)
50+
<C extends CtAbstractInvocation<T>> C addArgumentAt(int position, CtExpression<?> argument);
51+
4252
/**
4353
* Removes an argument expression from the invocation.
4454
*/

src/main/java/spoon/support/reflect/code/CtConstructorCallImpl.java

+5
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ public <C extends CtAbstractInvocation<T>> C addArgument(CtExpression<?> argumen
123123
return addArgument(arguments.size(), argument);
124124
}
125125

126+
@Override
127+
public <C extends CtAbstractInvocation<T>> C addArgumentAt(int position, CtExpression<?> argument) {
128+
return addArgument(position, argument);
129+
}
130+
126131
@Override
127132
public void removeArgument(CtExpression<?> argument) {
128133
if (arguments == CtElementImpl.<CtExpression<?>>emptyList()) {

src/main/java/spoon/support/reflect/code/CtInvocationImpl.java

+5
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ public <C extends CtAbstractInvocation<T>> C addArgument(CtExpression<?> argumen
6969
return addArgument(arguments.size(), argument);
7070
}
7171

72+
@Override
73+
public <C extends CtAbstractInvocation<T>> C addArgumentAt(int position, CtExpression<?> argument) {
74+
return addArgument(position, argument);
75+
}
76+
7277
@Override
7378
public void removeArgument(CtExpression<?> argument) {
7479
if (arguments == CtElementImpl.<CtExpression<?>>emptyList()) {

src/test/java/spoon/test/constructorcallnewclass/ConstructorCallTest.java

+30
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
import java.util.TreeSet;
4040
import java.util.stream.Collectors;
4141

42+
import static org.hamcrest.CoreMatchers.equalTo;
43+
import static org.hamcrest.MatcherAssert.assertThat;
4244
import static org.junit.Assert.assertEquals;
4345
import static org.junit.Assert.assertSame;
4446
import static org.junit.Assert.assertTrue;
@@ -179,6 +181,34 @@ public void testParameterizedConstructorCallOmittedTypeArgsResolvedTypeNoClasspa
179181
assertTrue(executableType.isParameterized());
180182
}
181183

184+
@Test
185+
public void test_addArgumentAt_addsArgumentToSpecifiedPosition() {
186+
// contract: addArgumentAt should add arguments to the specified position.
187+
188+
// arrange
189+
Factory factory = new Launcher().getFactory();
190+
factory.getEnvironment().setAutoImports(true);
191+
CtConstructorCall<?> newLinkedHashMap = (CtConstructorCall<?>) factory
192+
// make it raw on purpose to simplify assertion
193+
.createCodeSnippetExpression("new java.util.LinkedHashMap()")
194+
.compile();
195+
196+
// act
197+
// LinkedHashMap has multiple constructors, we're going for:
198+
// LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder) by adding
199+
// arguments a bit haphazardly
200+
201+
// 10
202+
newLinkedHashMap.addArgumentAt(0, factory.createLiteral(10))
203+
// 10, true
204+
.addArgumentAt(1, factory.createLiteral(true))
205+
// 10, 1.4, true
206+
.addArgumentAt(1, factory.createLiteral(1.4));
207+
208+
// assert
209+
assertThat(newLinkedHashMap.toString(), equalTo("new LinkedHashMap(10, 1.4, true)"));
210+
}
211+
182212
private CtTypeReference<?> getConstructorCallTypeFrom(String simpleName, String sourceFile) {
183213
final Launcher launcher = new Launcher();
184214
launcher.getEnvironment().setNoClasspath(true);

src/test/java/spoon/test/invocations/InvocationTest.java

+35
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,20 @@
2525
import spoon.reflect.declaration.CtClass;
2626
import spoon.reflect.declaration.CtExecutable;
2727
import spoon.reflect.declaration.CtMethod;
28+
import spoon.reflect.declaration.CtType;
2829
import spoon.reflect.factory.Factory;
2930
import spoon.reflect.reference.CtExecutableReference;
3031
import spoon.reflect.visitor.filter.AbstractFilter;
3132
import spoon.reflect.visitor.filter.TypeFilter;
3233
import spoon.test.invocations.testclasses.Bar;
3334
import spoon.test.invocations.testclasses.Foo;
3435

36+
import java.util.Collections;
3537
import java.util.List;
3638
import java.util.stream.Collectors;
3739

40+
import static org.hamcrest.CoreMatchers.equalTo;
41+
import static org.hamcrest.MatcherAssert.assertThat;
3842
import static org.junit.Assert.assertEquals;
3943
import static org.junit.Assert.assertNull;
4044
import static org.junit.Assert.assertNotNull;
@@ -97,4 +101,35 @@ public void testIssue1753() {
97101
final CtExecutable exe = executables.get(0);
98102
assertNotNull(exe.getReference().getDeclaration());
99103
}
104+
105+
@Test
106+
public void test_addArgumentAt_addsArgumentAtSpecifiedPosition() {
107+
// contract: addArgumentAt should add arguments to the specified position.
108+
109+
// arrange
110+
Factory factory = new Launcher().getFactory();
111+
factory.getEnvironment().setAutoImports(true);;
112+
CtType<?> collections = factory.Type().get(Collections.class);
113+
CtInvocation<?> addAllInv = factory.createInvocation(
114+
factory.createTypeAccess(collections.getReference()),
115+
collections.getMethodsByName("addAll").get(0).getReference());
116+
117+
// act
118+
// want to add the arguments new ArrayList<Integer>, 4, 99, 7, -2 s.t. they end up in that
119+
// order, but we do it haphazardly with addArgumentAt.
120+
121+
// AL
122+
addAllInv.addArgumentAt(0, factory.createCodeSnippetExpression("new java.util.ArrayList<Integer>()").compile())
123+
// AL, 99
124+
.addArgumentAt(1, factory.createLiteral(99))
125+
// AL, 99, -2
126+
.addArgumentAt(2, factory.createLiteral(-2))
127+
// AL, 4, 99, -2
128+
.addArgumentAt(1, factory.createLiteral(4))
129+
// AL, 4, 99, 7, -2
130+
.addArgumentAt(3, factory.createLiteral(7));
131+
132+
// assert
133+
assertThat(addAllInv.toString(), equalTo("Collections.addAll(new ArrayList<Integer>(), 4, 99, 7, -2)"));
134+
}
100135
}

0 commit comments

Comments
 (0)