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

Improve preconditions check #120

Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
a28be27
Starting point: PreConditionsVerifier
jevanlingen Dec 11, 2024
495a43e
Starting point: PreConditionsVerifier
jevanlingen Dec 12, 2024
481e084
Starting point: PreConditionsVerifier
jevanlingen Dec 12, 2024
c3ceb41
Starting point: PreConditionsVerifier (failing tests as intended)
jevanlingen Dec 12, 2024
db6081f
Starting point: PreConditionsVerifier (failing tests as intended)
jevanlingen Dec 12, 2024
d5559a9
Fix unit test
jevanlingen Dec 12, 2024
51c5146
Implementation
jevanlingen Dec 12, 2024
3e4bfeb
Implementation
jevanlingen Dec 12, 2024
c4528df
improvement
jevanlingen Dec 12, 2024
0202ed3
improvement
jevanlingen Dec 12, 2024
6714f48
Fix test
jevanlingen Dec 12, 2024
f8dd36d
Add extra info about pruning
jevanlingen Dec 12, 2024
1c9d98e
Start new implementation, introduce PreCondition objects
jevanlingen Dec 13, 2024
3d9f1a7
Start with PreConditionTest
jevanlingen Dec 13, 2024
8eefef3
Start with PreConditionTest
jevanlingen Dec 13, 2024
9266f9e
improvement
jevanlingen Dec 13, 2024
6eccf0c
toString improvement
jevanlingen Dec 16, 2024
b0a33a1
toString improvement
jevanlingen Dec 16, 2024
356e03d
Rename `PreCondition` to `Precondition` + add copyright header
jevanlingen Dec 16, 2024
16223ab
Merge branch 'main' into 119-when-using-multiple-before-templates-do-…
timtebeek Dec 16, 2024
8a392fb
Improve `fitsInto`
jevanlingen Dec 16, 2024
680c7e4
Apply suggestions from code review
jevanlingen Dec 16, 2024
1e53f66
Merge branch 'main' into 119-when-using-multiple-before-templates-do-…
timtebeek Dec 16, 2024
87bd944
Revert formatting
jevanlingen Dec 16, 2024
d7094c6
Revert formatting
jevanlingen Dec 16, 2024
b65f7c0
Revert formatting
jevanlingen Dec 16, 2024
afe33c0
Revert formatting
jevanlingen Dec 16, 2024
b00bb52
Merge branch 'main' into 119-when-using-multiple-before-templates-do-…
timtebeek Dec 16, 2024
b512443
Reorder imports in PreConditionsVerifier.java
timtebeek Dec 16, 2024
0257c95
Change name `PreConditionsVerifier` => `PreconditionsVerifier`
jevanlingen Dec 16, 2024
23f9475
Change name `PreConditionsVerifier` => `PreconditionsVerifier`
jevanlingen Dec 16, 2024
d458031
Improvement: extractCommonElements
jevanlingen Dec 16, 2024
4404440
Update src/main/java/org/openrewrite/java/template/processor/Refaster…
jevanlingen Dec 16, 2024
52d6dbb
Change test accordingly
jevanlingen Dec 16, 2024
605b1d7
Revert test
jevanlingen Dec 16, 2024
3856a7c
Remove unneeded `RequiredArgsConstructor`
jevanlingen Dec 17, 2024
7822f2e
Apply suggestions from code review
jevanlingen Dec 17, 2024
ef1ed2d
Apply suggestions from code review
jevanlingen Dec 17, 2024
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 @@ -688,7 +688,13 @@ public void scan(JCTree jcTree) {
jcIdent.sym.owner instanceof Symbol.MethodSymbol &&
((Symbol.MethodSymbol) jcIdent.sym.owner).params.contains(jcIdent.sym) &&
seenParams.add(jcIdent.sym)) {
afterParams.add(beforeParamOrder.get(((Symbol.MethodSymbol) jcIdent.sym.owner).params.indexOf(jcIdent.sym)));
Integer idx = beforeParamOrder.get(((Symbol.MethodSymbol) jcIdent.sym.owner).params.indexOf(jcIdent.sym));
// TODO fix ugly hack
if (idx == null && beforeParamOrder.size() == 1) {
afterParams.add(0);
} else {
afterParams.add(idx);
}
jevanlingen marked this conversation as resolved.
Show resolved Hide resolved
}
}
super.scan(jcTree);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ void skipRecipeGeneration(String recipeName) {
"SimplifyTernary",
"RefasterAnyOf",
"Parameters",
"PreConditionsVerifier",
})
void nestedRecipes(String recipeName) {
Compilation compilation = compileResource("refaster/" + recipeName + ".java");
Expand Down
148 changes: 148 additions & 0 deletions src/test/resources/refaster/PreConditionsVerifier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
* Copyright 2023 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 foo;

import java.util.List;
import java.util.Map;

timtebeek marked this conversation as resolved.
Show resolved Hide resolved
import com.google.errorprone.refaster.annotation.AfterTemplate;
jevanlingen marked this conversation as resolved.
Show resolved Hide resolved
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import com.sun.tools.javac.util.Constants;
import com.sun.tools.javac.util.Convert;

/**
* A refaster template to test when a `UsesType`and Preconditions.or should or should not be applied to the Preconditions check.
*/
public class PreConditionsVerifier {
public static class NoUsesTypeWhenBeforeTemplateContainsPrimitiveOrString {
jevanlingen marked this conversation as resolved.
Show resolved Hide resolved
@BeforeTemplate
void doubleAndInt(double actual, int ignore) {
System.out.println(actual);
jevanlingen marked this conversation as resolved.
Show resolved Hide resolved
}

@BeforeTemplate
void stringAndString(String actual, String ignore) {
System.out.println(actual);
}

@AfterTemplate
void after(Object actual) {
System.out.println("Changed: " + actual);
}
}

public static class NoUsesTypeWhenBeforeTemplateContainsPrimitiveOrStringAndTypeInSomeBeforeBody {
@BeforeTemplate
String string(String value) {
return Convert.quote(value);
}

@BeforeTemplate
String _int(int value) {
return String.valueOf(value);
}

@AfterTemplate
Object after(Object actual) {
return Convert.quote(String.valueOf(actual));
}
}

public static class UsesTypeWhenBeforeTemplateContainsPrimitiveOrStringAndTypeInAllBeforeBody {
@BeforeTemplate
String string(String value) {
return Convert.quote(value);
}

@BeforeTemplate
String _int(int value) {
return Convert.quote(String.valueOf(value));
}

@AfterTemplate
Object after(Object actual) {
return Convert.quote(String.valueOf(actual));
}
}

public static class NoUsesTypeWhenBeforeTemplateContainsPrimitiveAndAnotherType {
@BeforeTemplate
void _int(int actual) {
System.out.println(actual);
}

@BeforeTemplate
void map(Map<?, ?> actual) {
System.out.println(actual);
}

@AfterTemplate
void after(Object actual) {
System.out.println("Changed: " + actual);
}
}

public static class NoUsesTypeWhenBeforeTemplateContainsStringAndAnotherType {
@BeforeTemplate
void string(String actual) {
System.out.println(actual);
}

@BeforeTemplate
void map(Map<?, ?> actual) {
System.out.println(actual);
}

@AfterTemplate
void after(Object actual) {
System.out.println("Changed: " + actual);
}
}

public static class UsesTypeMapWhenAllBeforeTemplatesContainsMap {
@BeforeTemplate
void mapWithGeneric(Map<?, ?> actual) {
System.out.println(actual);
}

@BeforeTemplate
void mapWithGenericTwo(Map<?, ?> actual) {
System.out.println(actual);
}

@AfterTemplate
void mapWithoutGeneric(Map actual) {
System.out.println("Changed: " + actual);
}
}

public static class UsesTypeMapOrListWhenBeforeTemplateContainsMapAndList {
@BeforeTemplate
void list(List<?> actual) {
System.out.println(actual);
}

@BeforeTemplate
void map(Map<?, ?> actual) {
System.out.println(actual);
}

@AfterTemplate
void after(Object actual) {
System.out.println("Changed: " + actual);
}
}
}
Loading
Loading