Skip to content

Commit

Permalink
Refaster: support method invocation type argument inlining
Browse files Browse the repository at this point in the history
Fixes #2706

COPYBARA_INTEGRATE_REVIEW=#2706 from PicnicSupermarket:rossendrijver/type_arguments_umethodinvocation f44c1e6
PiperOrigin-RevId: 500302980
  • Loading branch information
rickie authored and Error Prone Team committed Jan 7, 2023
1 parent a57309b commit 7f459e1
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,28 @@
*/
@AutoValue
public abstract class UMethodInvocation extends UExpression implements MethodInvocationTree {
public static UMethodInvocation create(UExpression methodSelect, List<UExpression> arguments) {
return new AutoValue_UMethodInvocation(methodSelect, ImmutableList.copyOf(arguments));
public static UMethodInvocation create(
List<? extends UExpression> typeArguments,
UExpression methodSelect,
List<UExpression> arguments) {
return new AutoValue_UMethodInvocation(
ImmutableList.copyOf(typeArguments), methodSelect, ImmutableList.copyOf(arguments));
}

public static UMethodInvocation create(
List<? extends UExpression> 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<UExpression> getTypeArguments();

@Override
public abstract UExpression getMethodSelect();

Expand Down Expand Up @@ -69,17 +83,12 @@ public Kind getKind() {
return Kind.METHOD_INVOCATION;
}

@Override
public List<UTree<?>> getTypeArguments() {
return ImmutableList.of();
}

@Override
public JCMethodInvocation inline(Inliner inliner) throws CouldNotResolveImportException {
return inliner
.maker()
.Apply(
com.sun.tools.javac.util.List.<JCExpression>nil(),
inliner.<JCExpression>inlineList(getTypeArguments()),
getMethodSelect().inline(inliner),
inliner.<JCExpression>inlineList(getArguments()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,4 +369,9 @@ public void staticImportClassToken() throws IOException {
public void suppressWarnings() throws IOException {
runTest("SuppressWarningsTemplate");
}

@Test
public void typeArgumentsMethodInvocation() throws IOException {
runTest("TypeArgumentsMethodInvocationTemplate");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public void match() {
ULiteral oneLit = ULiteral.intLit(1);
ULiteral barLit = ULiteral.stringLit("bar");
UMethodInvocation invocation =
UMethodInvocation.create(fooIdent, ImmutableList.<UExpression>of(oneLit, barLit));
UMethodInvocation.create(
ImmutableList.of(), fooIdent, ImmutableList.<UExpression>of(oneLit, barLit));
assertUnifies("foo(1, \"bar\")", invocation);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<Object> example() {
ExecutorService executorService = Executors.newSingleThreadExecutor();
return executorService.submit(() -> new Object());
}
}
Original file line number Diff line number Diff line change
@@ -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<Object> example() {
ExecutorService executorService = Executors.newSingleThreadExecutor();
return executorService.<Object>submit(() -> new Object());
}
}
Original file line number Diff line number Diff line change
@@ -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<T> {
@BeforeTemplate
Future<T> before(ExecutorService executorService, Callable<T> callable) {
return executorService.submit(callable);
}

@AfterTemplate
Future<T> after(ExecutorService executorService, Callable<T> callable) {
return executorService.<T>submit(callable);
}
}

0 comments on commit 7f459e1

Please sign in to comment.