diff --git a/core/src/main/java/com/google/errorprone/refaster/UMethodInvocation.java b/core/src/main/java/com/google/errorprone/refaster/UMethodInvocation.java index 9bd11d17271..1e85cf7f9b3 100644 --- a/core/src/main/java/com/google/errorprone/refaster/UMethodInvocation.java +++ b/core/src/main/java/com/google/errorprone/refaster/UMethodInvocation.java @@ -34,14 +34,28 @@ */ @AutoValue public abstract class UMethodInvocation extends UExpression implements MethodInvocationTree { - public static UMethodInvocation create(UExpression methodSelect, List arguments) { - return new AutoValue_UMethodInvocation(methodSelect, ImmutableList.copyOf(arguments)); + public static UMethodInvocation create( + List typeArguments, + UExpression methodSelect, + List arguments) { + return new AutoValue_UMethodInvocation( + ImmutableList.copyOf(typeArguments), methodSelect, ImmutableList.copyOf(arguments)); + } + + public static UMethodInvocation create( + List typeArguments, + UExpression methodSelect, + UExpression... arguments) { + return create(typeArguments, methodSelect, ImmutableList.copyOf(arguments)); } public static UMethodInvocation create(UExpression methodSelect, UExpression... arguments) { - return create(methodSelect, ImmutableList.copyOf(arguments)); + return create(ImmutableList.of(), methodSelect, ImmutableList.copyOf(arguments)); } + @Override + public abstract ImmutableList getTypeArguments(); + @Override public abstract UExpression getMethodSelect(); @@ -69,17 +83,12 @@ public Kind getKind() { return Kind.METHOD_INVOCATION; } - @Override - public List> getTypeArguments() { - return ImmutableList.of(); - } - @Override public JCMethodInvocation inline(Inliner inliner) throws CouldNotResolveImportException { return inliner .maker() .Apply( - com.sun.tools.javac.util.List.nil(), + inliner.inlineList(getTypeArguments()), getMethodSelect().inline(inliner), inliner.inlineList(getArguments())); } diff --git a/core/src/main/java/com/google/errorprone/refaster/UTemplater.java b/core/src/main/java/com/google/errorprone/refaster/UTemplater.java index 205e2afa46b..c762497387c 100644 --- a/core/src/main/java/com/google/errorprone/refaster/UTemplater.java +++ b/core/src/main/java/com/google/errorprone/refaster/UTemplater.java @@ -466,7 +466,9 @@ public UExpression visitMethodInvocation(MethodInvocationTree tree, Void v) { templateExpressions(tree.getArguments())); } else { return UMethodInvocation.create( - template(tree.getMethodSelect()), templateExpressions(tree.getArguments())); + templateTypeExpressions(tree.getTypeArguments()), + template(tree.getMethodSelect()), + templateExpressions(tree.getArguments())); } } diff --git a/core/src/test/java/com/google/errorprone/refaster/TemplateIntegrationTest.java b/core/src/test/java/com/google/errorprone/refaster/TemplateIntegrationTest.java index c928231983e..e9000acc4be 100644 --- a/core/src/test/java/com/google/errorprone/refaster/TemplateIntegrationTest.java +++ b/core/src/test/java/com/google/errorprone/refaster/TemplateIntegrationTest.java @@ -369,4 +369,9 @@ public void staticImportClassToken() throws IOException { public void suppressWarnings() throws IOException { runTest("SuppressWarningsTemplate"); } + + @Test + public void typeArgumentsMethodInvocation() throws IOException { + runTest("TypeArgumentsMethodInvocationTemplate"); + } } diff --git a/core/src/test/java/com/google/errorprone/refaster/UMethodInvocationTest.java b/core/src/test/java/com/google/errorprone/refaster/UMethodInvocationTest.java index 32bf0adfdae..7a17cf5713c 100644 --- a/core/src/test/java/com/google/errorprone/refaster/UMethodInvocationTest.java +++ b/core/src/test/java/com/google/errorprone/refaster/UMethodInvocationTest.java @@ -41,7 +41,8 @@ public void match() { ULiteral oneLit = ULiteral.intLit(1); ULiteral barLit = ULiteral.stringLit("bar"); UMethodInvocation invocation = - UMethodInvocation.create(fooIdent, ImmutableList.of(oneLit, barLit)); + UMethodInvocation.create( + ImmutableList.of(), fooIdent, ImmutableList.of(oneLit, barLit)); assertUnifies("foo(1, \"bar\")", invocation); } diff --git a/core/src/test/java/com/google/errorprone/refaster/testdata/input/TypeArgumentsMethodInvocationTemplateExample.java b/core/src/test/java/com/google/errorprone/refaster/testdata/input/TypeArgumentsMethodInvocationTemplateExample.java new file mode 100644 index 00000000000..ff586986f52 --- /dev/null +++ b/core/src/test/java/com/google/errorprone/refaster/testdata/input/TypeArgumentsMethodInvocationTemplateExample.java @@ -0,0 +1,27 @@ +/* + * Copyright 2022 The Error Prone Authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package com.google.errorprone.refaster.testdata; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +/** Test data for {@code TypeArgumentsMethodInvocationTemplate}. */ +public class TypeArgumentsMethodInvocationTemplateExample { + public Future example() { + ExecutorService executorService = Executors.newSingleThreadExecutor(); + return executorService.submit(() -> new Object()); + } +} diff --git a/core/src/test/java/com/google/errorprone/refaster/testdata/output/TypeArgumentsMethodInvocationTemplateExample.java b/core/src/test/java/com/google/errorprone/refaster/testdata/output/TypeArgumentsMethodInvocationTemplateExample.java new file mode 100644 index 00000000000..c2b3d8746c5 --- /dev/null +++ b/core/src/test/java/com/google/errorprone/refaster/testdata/output/TypeArgumentsMethodInvocationTemplateExample.java @@ -0,0 +1,27 @@ +/* + * Copyright 2022 The Error Prone Authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package com.google.errorprone.refaster.testdata; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +/** Test data for {@code TypeArgumentsMethodInvocationTemplate}. */ +public class TypeArgumentsMethodInvocationTemplateExample { + public Future example() { + ExecutorService executorService = Executors.newSingleThreadExecutor(); + return executorService.submit(() -> new Object()); + } +} diff --git a/core/src/test/java/com/google/errorprone/refaster/testdata/template/TypeArgumentsMethodInvocationTemplate.java b/core/src/test/java/com/google/errorprone/refaster/testdata/template/TypeArgumentsMethodInvocationTemplate.java new file mode 100644 index 00000000000..7c9f6bbbe4b --- /dev/null +++ b/core/src/test/java/com/google/errorprone/refaster/testdata/template/TypeArgumentsMethodInvocationTemplate.java @@ -0,0 +1,36 @@ +/* + * Copyright 2022 The Error Prone Authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.errorprone.refaster.testdata.template; + +import com.google.errorprone.refaster.annotation.AfterTemplate; +import com.google.errorprone.refaster.annotation.BeforeTemplate; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Future; + +/** Sample template for introducing type arguments on a method invocation in an @AfterTemplate. */ +public class TypeArgumentsMethodInvocationTemplate { + @BeforeTemplate + Future before(ExecutorService executorService, Callable callable) { + return executorService.submit(callable); + } + + @AfterTemplate + Future after(ExecutorService executorService, Callable callable) { + return executorService.submit(callable); + } +}