Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.errorprone.refaster;

import static com.google.errorprone.util.ASTHelpers.stringContainsComments;
import static java.util.logging.Level.FINE;

import com.google.auto.value.AutoValue;
import com.google.errorprone.BugPattern.SeverityLevel;
Expand All @@ -37,12 +38,16 @@
import com.sun.source.util.SimpleTreeVisitor;
import com.sun.source.util.TreeScanner;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.code.Types;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
import com.sun.tools.javac.tree.JCTree.JCExpression;
import com.sun.tools.javac.tree.JCTree.JCStatement;
import com.sun.tools.javac.tree.TreeMaker;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.ListBuffer;
import java.util.logging.Logger;

/**
* Scanner that outputs suggested fixes generated by a {@code RefasterMatcher}.
Expand All @@ -52,6 +57,7 @@
@AutoValue
abstract class RefasterScanner<M extends TemplateMatch, T extends Template<M>>
extends TreeScanner<Void, Context> {
private static final Logger logger = Logger.getLogger(RefasterScanner.class.toString());
static <M extends TemplateMatch, T extends Template<M>> RefasterScanner<M, T> create(
RefasterRule<M, T> rule, DescriptionListener listener) {
return new AutoValue_RefasterScanner<>(rule, listener);
Expand Down Expand Up @@ -124,6 +130,9 @@ public Void scan(Tree tree, Context context) {
builder.addFix(SuggestedFix.prefixWith(match.getLocation(), "/* match found */ "));
} else {
for (T afterTemplate : rule().afterTemplates()) {
if (!isAfterTypeCompatible(beforeTemplate, afterTemplate, match, context)) {
continue;
}
builder.addFix(afterTemplate.replace(match));
}
}
Expand Down Expand Up @@ -180,6 +189,38 @@ public Void visitIf(IfTree node, Context context) {
return null;
}

/**
* Checks that the after template's return type is compatible with the before template's return
* type. This prevents applying replacements where the after template returns a wider type than
* the before template, which could break compilation at the match site.
*/
private static <M extends TemplateMatch, T extends Template<M>> boolean isAfterTypeCompatible(
T beforeTemplate, T afterTemplate, M match, Context context) {
if (!(beforeTemplate instanceof ExpressionTemplate beforeExprTemplate)
|| !(afterTemplate instanceof ExpressionTemplate afterExprTemplate)) {
return true;
}
try {
Inliner inliner = match.createInliner();
Type beforeReturnType = beforeExprTemplate.returnType().inline(inliner);
Type afterReturnType = afterExprTemplate.returnType().inline(inliner);
Types types = Types.instance(context);
if (!types.isSubtype(types.erasure(afterReturnType), types.erasure(beforeReturnType))) {
logger.log(
FINE,
String.format(
"Skipping match because after template return type %s is not a subtype of before"
+ " template return type %s",
afterReturnType, beforeReturnType));
return false;
}
} catch (CouldNotResolveImportException e) {
logger.log(FINE, "Failure to resolve import in template return type", e);
return false;
}
return true;
}

private boolean isSuppressed(Tree node, Context context) {
return RefasterSuppressionHelper.suppressed(rule(), node, context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,4 +382,9 @@ public void memberSelectAndMethodParameterDisambiguation() throws IOException {
public void unqualifiedMethod() throws IOException {
runTest("UnqualifiedMethodTemplate");
}

@Test
public void widerType() throws IOException {
runTest("WiderTypeTemplate");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2025 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;

/**
* Example input for {@code WiderTypeTemplate}.
*/
public class WiderTypeTemplateExample {
public void example(Object obj) {
// positive example: result is assigned to a String variable
String str = String.valueOf(obj);
// positive example: String method is called on the result
int len = String.valueOf(obj).length();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2025 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;

/**
* Example input for {@code WiderTypeTemplate}.
*/
public class WiderTypeTemplateExample {
public void example(Object obj) {
// positive example: result is assigned to a String variable
String str = String.valueOf(obj);
// positive example: String method is called on the result
int len = String.valueOf(obj).length();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2025 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;

/**
* Sample Refaster template where the {@code @AfterTemplate} returns a wider type than the {@code
* @BeforeTemplate}.
*/
public class WiderTypeTemplate {
@BeforeTemplate
String before(Object obj) {
return String.valueOf(obj);
}

@AfterTemplate
Object after(Object obj) {
return obj;
}
}