Skip to content

Commit 7f459e1

Browse files
rickieError Prone Team
authored and
Error Prone Team
committed
Refaster: support method invocation type argument inlining
Fixes #2706 COPYBARA_INTEGRATE_REVIEW=#2706 from PicnicSupermarket:rossendrijver/type_arguments_umethodinvocation f44c1e6 PiperOrigin-RevId: 500302980
1 parent a57309b commit 7f459e1

File tree

7 files changed

+118
-11
lines changed

7 files changed

+118
-11
lines changed

core/src/main/java/com/google/errorprone/refaster/UMethodInvocation.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,28 @@
3434
*/
3535
@AutoValue
3636
public abstract class UMethodInvocation extends UExpression implements MethodInvocationTree {
37-
public static UMethodInvocation create(UExpression methodSelect, List<UExpression> arguments) {
38-
return new AutoValue_UMethodInvocation(methodSelect, ImmutableList.copyOf(arguments));
37+
public static UMethodInvocation create(
38+
List<? extends UExpression> typeArguments,
39+
UExpression methodSelect,
40+
List<UExpression> arguments) {
41+
return new AutoValue_UMethodInvocation(
42+
ImmutableList.copyOf(typeArguments), methodSelect, ImmutableList.copyOf(arguments));
43+
}
44+
45+
public static UMethodInvocation create(
46+
List<? extends UExpression> typeArguments,
47+
UExpression methodSelect,
48+
UExpression... arguments) {
49+
return create(typeArguments, methodSelect, ImmutableList.copyOf(arguments));
3950
}
4051

4152
public static UMethodInvocation create(UExpression methodSelect, UExpression... arguments) {
42-
return create(methodSelect, ImmutableList.copyOf(arguments));
53+
return create(ImmutableList.of(), methodSelect, ImmutableList.copyOf(arguments));
4354
}
4455

56+
@Override
57+
public abstract ImmutableList<UExpression> getTypeArguments();
58+
4559
@Override
4660
public abstract UExpression getMethodSelect();
4761

@@ -69,17 +83,12 @@ public Kind getKind() {
6983
return Kind.METHOD_INVOCATION;
7084
}
7185

72-
@Override
73-
public List<UTree<?>> getTypeArguments() {
74-
return ImmutableList.of();
75-
}
76-
7786
@Override
7887
public JCMethodInvocation inline(Inliner inliner) throws CouldNotResolveImportException {
7988
return inliner
8089
.maker()
8190
.Apply(
82-
com.sun.tools.javac.util.List.<JCExpression>nil(),
91+
inliner.<JCExpression>inlineList(getTypeArguments()),
8392
getMethodSelect().inline(inliner),
8493
inliner.<JCExpression>inlineList(getArguments()));
8594
}

core/src/main/java/com/google/errorprone/refaster/UTemplater.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,9 @@ public UExpression visitMethodInvocation(MethodInvocationTree tree, Void v) {
466466
templateExpressions(tree.getArguments()));
467467
} else {
468468
return UMethodInvocation.create(
469-
template(tree.getMethodSelect()), templateExpressions(tree.getArguments()));
469+
templateTypeExpressions(tree.getTypeArguments()),
470+
template(tree.getMethodSelect()),
471+
templateExpressions(tree.getArguments()));
470472
}
471473
}
472474

core/src/test/java/com/google/errorprone/refaster/TemplateIntegrationTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,4 +369,9 @@ public void staticImportClassToken() throws IOException {
369369
public void suppressWarnings() throws IOException {
370370
runTest("SuppressWarningsTemplate");
371371
}
372+
373+
@Test
374+
public void typeArgumentsMethodInvocation() throws IOException {
375+
runTest("TypeArgumentsMethodInvocationTemplate");
376+
}
372377
}

core/src/test/java/com/google/errorprone/refaster/UMethodInvocationTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ public void match() {
4141
ULiteral oneLit = ULiteral.intLit(1);
4242
ULiteral barLit = ULiteral.stringLit("bar");
4343
UMethodInvocation invocation =
44-
UMethodInvocation.create(fooIdent, ImmutableList.<UExpression>of(oneLit, barLit));
44+
UMethodInvocation.create(
45+
ImmutableList.of(), fooIdent, ImmutableList.<UExpression>of(oneLit, barLit));
4546
assertUnifies("foo(1, \"bar\")", invocation);
4647
}
4748

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2022 The Error Prone Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.errorprone.refaster.testdata;
16+
17+
import java.util.concurrent.ExecutorService;
18+
import java.util.concurrent.Executors;
19+
import java.util.concurrent.Future;
20+
21+
/** Test data for {@code TypeArgumentsMethodInvocationTemplate}. */
22+
public class TypeArgumentsMethodInvocationTemplateExample {
23+
public Future<Object> example() {
24+
ExecutorService executorService = Executors.newSingleThreadExecutor();
25+
return executorService.submit(() -> new Object());
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2022 The Error Prone Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.errorprone.refaster.testdata;
16+
17+
import java.util.concurrent.ExecutorService;
18+
import java.util.concurrent.Executors;
19+
import java.util.concurrent.Future;
20+
21+
/** Test data for {@code TypeArgumentsMethodInvocationTemplate}. */
22+
public class TypeArgumentsMethodInvocationTemplateExample {
23+
public Future<Object> example() {
24+
ExecutorService executorService = Executors.newSingleThreadExecutor();
25+
return executorService.<Object>submit(() -> new Object());
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2022 The Error Prone Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.errorprone.refaster.testdata.template;
18+
19+
import com.google.errorprone.refaster.annotation.AfterTemplate;
20+
import com.google.errorprone.refaster.annotation.BeforeTemplate;
21+
import java.util.concurrent.Callable;
22+
import java.util.concurrent.ExecutorService;
23+
import java.util.concurrent.Future;
24+
25+
/** Sample template for introducing type arguments on a method invocation in an @AfterTemplate. */
26+
public class TypeArgumentsMethodInvocationTemplate<T> {
27+
@BeforeTemplate
28+
Future<T> before(ExecutorService executorService, Callable<T> callable) {
29+
return executorService.submit(callable);
30+
}
31+
32+
@AfterTemplate
33+
Future<T> after(ExecutorService executorService, Callable<T> callable) {
34+
return executorService.<T>submit(callable);
35+
}
36+
}

0 commit comments

Comments
 (0)