Skip to content
Merged
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 @@ -766,7 +766,7 @@ public UWhileLoop visitWhileLoop(WhileLoopTree tree, Void v) {
public UVariableDecl visitVariable(VariableTree tree, Void v) {
return UVariableDecl.create(
tree.getName(),
(tree.getType() == null) ? null : templateType(tree.getType()),
UVariableDecl.isVarType(tree) ? null : templateType(tree.getType()),
template(getType(tree)),
(tree.getInitializer() == null) ? null : template(tree.getInitializer()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.errorprone.util.ASTHelpers;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.ModifiersTree;
import com.sun.source.tree.Tree;
import com.sun.source.tree.TreeVisitor;
import com.sun.source.tree.VariableTree;
import com.sun.tools.javac.tree.JCTree.JCModifiers;
Expand Down Expand Up @@ -84,11 +85,16 @@ ULocalVarIdent.Key key() {
return new ULocalVarIdent.Key(getName());
}

static boolean isVarType(VariableTree tree) {
Tree type = tree.getType();
return type == null || type.getKind().name().equals("VAR_TYPE");
}

@Override
public Choice<Unifier> visitVariable(VariableTree decl, Unifier unifier) {
return Choice.condition(unifier.getBinding(key()) == null, unifier)
.flatMap(
(getType() != null && decl.getType() != null)
(getType() != null && !isVarType(decl))
? unifications(getType(), decl.getType())
: unifications(getVariableType(), ASTHelpers.getType(decl)))
.flatMap(unifications(getInitializer(), decl.getInitializer()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -847,4 +847,28 @@ public void anonymousClass() {
UClassType.create("java.lang.String"))),
ULocalVarIdent.create("b")))))))))));
}

@Test
public void localVariableWithVar() {
compile(
"""
class LocalVariableExample {
public void example(String str) {
var x = str;
}
}
""");
assertThat(UTemplater.createTemplate(context, getMethodDeclaration("example")))
.isEqualTo(
BlockTemplate.create(
ImmutableMap.of("str", UClassType.create("java.lang.String")),
UVariableDecl.create(
"x",
// https://bugs.openjdk.org/browse/JDK-8268850
Runtime.version().feature() >= 27
? null
: UClassIdent.create("java.lang.String"),
UClassType.create("java.lang.String"),
UFreeIdent.create("str"))));
}
}
Loading