From f4bc725d550f2008dddccd63705423ff78f78801 Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Tue, 23 Nov 2021 20:47:28 +0100 Subject: [PATCH] Refaster: support method invocation type argument inlining See google/error-prone#2706. --- .../refaster/UMethodInvocation.java | 27 +++++++++----- .../errorprone/refaster/UTemplater.java | 4 ++- .../refaster/TemplateIntegrationTest.java | 5 +++ .../refaster/UMethodInvocationTest.java | 3 +- ...umentsMethodInvocationTemplateExample.java | 27 ++++++++++++++ ...umentsMethodInvocationTemplateExample.java | 27 ++++++++++++++ ...TypeArgumentsMethodInvocationTemplate.java | 36 +++++++++++++++++++ 7 files changed, 118 insertions(+), 11 deletions(-) create mode 100644 core/src/test/java/com/google/errorprone/refaster/testdata/input/TypeArgumentsMethodInvocationTemplateExample.java create mode 100644 core/src/test/java/com/google/errorprone/refaster/testdata/output/TypeArgumentsMethodInvocationTemplateExample.java create mode 100644 core/src/test/java/com/google/errorprone/refaster/testdata/template/TypeArgumentsMethodInvocationTemplate.java 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 073d748cefd..7de87d51011 100644 --- a/core/src/main/java/com/google/errorprone/refaster/UTemplater.java +++ b/core/src/main/java/com/google/errorprone/refaster/UTemplater.java @@ -465,7 +465,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 09539d10c70..7236fe9d24a 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 suppressWarnings() throws IOException { public void memberSelectAndMethodParameterDisambiguation() throws IOException { runTest("MemberSelectAndMethodParameterDisambiguationTemplate"); } + + @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..4a2c119556f --- /dev/null +++ b/core/src/test/java/com/google/errorprone/refaster/testdata/input/TypeArgumentsMethodInvocationTemplateExample.java @@ -0,0 +1,27 @@ +/* + * Copyright 2021 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..629f40086fd --- /dev/null +++ b/core/src/test/java/com/google/errorprone/refaster/testdata/output/TypeArgumentsMethodInvocationTemplateExample.java @@ -0,0 +1,27 @@ +/* + * Copyright 2021 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..4243f7d8d94 --- /dev/null +++ b/core/src/test/java/com/google/errorprone/refaster/testdata/template/TypeArgumentsMethodInvocationTemplate.java @@ -0,0 +1,36 @@ +/* + * Copyright 2021 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); + } +}