Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java 10+ Local-Variable Type Inference #218

Merged
merged 24 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
0bcc1cb
Add test and dummy recipe as baseline for https://github.com/openrewr…
MBoegers May 3, 2023
3c41bca
Add tests for primitives and var keywords
MBoegers May 3, 2023
862f92e
Refine and group tests as nested tests
MBoegers May 9, 2023
937ba31
Merge branch 'openrewrite:main' into 217-usage_of_var
MBoegers May 10, 2023
4bd4a60
Refine and group tests as nested tests
MBoegers May 10, 2023
8984bb5
Implement UseVarKeyword reciepe except for generics
MBoegers May 10, 2023
0180123
Add Todo to implement generics and explicitly skip them for the moment
MBoegers May 10, 2023
8926e0c
UseVarKeyword: rename variables and improve null-checking in type che…
MBoegers May 12, 2023
208fc71
Add handling of static and instance initializer blocks
MBoegers May 24, 2023
f1b73f8
add skipping of generics types
MBoegers May 24, 2023
55a3d4b
replace
MBoegers May 24, 2023
78876d8
Merge branch 'openrewrite:main' into 217-usage_of_var
MBoegers Jun 14, 2023
57ce08d
Extract handling of primitive variable definition for local variable …
MBoegers Jun 14, 2023
856dbd9
Extract handling of Object variable definition for local variable typ…
MBoegers Jun 14, 2023
0c92217
Add Recipe that combines Object and Primitive handling for var. Refac…
MBoegers Jun 14, 2023
019a7a0
add licences to source code
MBoegers Jun 14, 2023
a8d4c4f
Add DocumentedExample to tests and remove Examples from Recipe
MBoegers Jun 16, 2023
5b8f0da
simplify null handling and reorder hot paths
MBoegers Jun 16, 2023
38bb55c
Apply suggestions from code review
MBoegers Jun 16, 2023
d96566b
remove NotNull Annotaions
MBoegers Jun 16, 2023
44ee158
rework determination if inside method and add test for edgecase
MBoegers Jun 16, 2023
6c8ff8b
use configuration UseJavaVersion
MBoegers Jun 16, 2023
0a47c80
Update license header
MBoegers Jun 16, 2023
4c485cb
Merge branch 'openrewrite:main' into 217-usage_of_var
MBoegers Jun 16, 2023
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
56 changes: 56 additions & 0 deletions src/main/java/org/openrewrite/java/migrate/lang/VarKeyword.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2021 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 org.openrewrite.java.migrate.lang;

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.search.HasJavaVersion;

import java.time.Duration;

@Value
@EqualsAndHashCode(callSuper = false)
public class VarKeyword extends Recipe {
public String getDisplayName() {
return "Use local variable type-inference (var) where possible";
}

@Override
public String getDescription() {
return "Local variable type-inference reduce the noise produces by repeating the type definitions in Java 10 or higher.";
}

@Override
public @Nullable Duration getEstimatedEffortPerOccurrence() {
return Duration.ofMinutes(1);
}

@Override
protected TreeVisitor<?, ExecutionContext> getSingleSourceApplicableTest() {
return new HasJavaVersion("11", true).getVisitor();
}

@Override
protected TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaVisitor<ExecutionContext>() {
};
}
}
225 changes: 225 additions & 0 deletions src/test/java/org/openrewrite/java/migrate/lang/VarUsageTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
/*
* Copyright 2021 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 org.openrewrite.java.migrate.lang;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
import org.openrewrite.test.TypeValidation;

import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.java.Assertions.version;

class VarUsageTest implements RewriteTest {
public void defaults(RecipeSpec spec) {
spec.recipe(new VarKeyword())
.typeValidationOptions(new TypeValidation().methodInvocations(false));
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
void donotTransformNull() {
//language=java
rewriteRun(
version(
java("""
package com.example.app;

class A {
void m() {
String str = null;
}
}
"""),
10
)
);
}

@Nested
class TransformObject {
@Test
void transformObjectInMethodBody() {
//language=java
rewriteRun(
version(
java("""
package com.example.app;

class A {
void m() {
Object o = new Object();
}
}
""", """
package com.example.app;

class A {
void m() {
var o = new Object();
}
}
"""),
10
)
);
}

@Test
void donotTransformObjectInParameter() {
//language=java
rewriteRun(
version(
java("""
package com.example.app;

class A {
Object m(Object o) {
return o;
}
}
"""),
10
)
);
}

@Test
void donotTransformObjectInClass() {
//language=java
rewriteRun(
version(
java("""
package com.example.app;

class A {
Object o;
Object m() {
return o;
}
}
"""),
10
)
);
}
}

@Nested
class Generics {
@Test
void transformFullGenerics() {
//language=java
rewriteRun(
version(
java("""
package com.example.app;

class A {
void m() {
List<String> strs = new ArrayList<String>();
}
}
""", """
package com.example.app;

class A {
void m() {
var str = new ArrayList<String>();
}
}
"""),
10
)
);
}

@Test
void transformGenericsWithDiamond() {
//language=java
rewriteRun(
version(
java("""
package com.example.app;

class A {
void m() {
List<String> strs = new ArrayList<>();
}
}
""", """
package com.example.app;

class A {
void m() {
var str = new ArrayList<String>();
}
}
"""),
10
)
);
}

@Test
void transformGenericsOnFactory() {
//language=java
rewriteRun(
version(
java(
"""
package com.example.app;

import java.util.ArrayList;class A {
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
void m() {
List<String> strs = List.of("one","two");
}
}
""", """
package com.example.app;

class A {
void m() {
var str = List.of("one","two");
}
}
"""),
10
)
);
}

@Test
void donotTransformGenericsOnEmptyFactory() {
//language=java
rewriteRun(
version(
java(
"""
package com.example.app;

class A {
void m() {
List<String> strs = List.of();
}
}
"""),
10
)
);
}
}
}