Skip to content

Commit

Permalink
Bugfix: Preconditions of overloaded @BeforeTemplate methods are filte…
Browse files Browse the repository at this point in the history
…red away (#123)
  • Loading branch information
jevanlingen authored Dec 20, 2024
1 parent 98c4659 commit e2336a8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ private Set<String> getImportsAsStrings(Map<TemplateDescriptor, Set<String>> imp

/* Generate the minimal precondition that would allow to match each before template individually. */
private @Nullable Precondition generatePreconditions(List<TemplateDescriptor> beforeTemplates, int indent) {
Map<String, Set<Precondition>> preconditions = new LinkedHashMap<>();
Set<Set<Precondition>> preconditions = new HashSet<>();
for (TemplateDescriptor beforeTemplate : beforeTemplates) {
int arity = beforeTemplate.getArity();
for (int i = 0; i < arity; i++) {
Expand All @@ -612,7 +612,7 @@ private Set<String> getImportsAsStrings(Map<TemplateDescriptor, Set<String>> imp
}

if (!usesVisitors.isEmpty()) {
preconditions.put(beforeTemplate.method.name.toString() + (arity == 1 ? "" : "$" + i), usesVisitors);
preconditions.add(usesVisitors);
} else {
return null; // At least one of the before templates has no preconditions, so we can not use any preconditions
}
Expand All @@ -624,7 +624,7 @@ private Set<String> getImportsAsStrings(Map<TemplateDescriptor, Set<String>> imp
}

return new Precondition.Or(
preconditions.values().stream()
preconditions.stream()
.map(Precondition.And::new)
.collect(toSet())
).prune();
Expand Down
24 changes: 12 additions & 12 deletions src/test/resources/refaster/PreconditionsVerifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
public class PreconditionsVerifier {
public static class NoUsesTypeWhenBeforeTemplateContainsPrimitiveOrString {
@BeforeTemplate
void doubleAndInt(double actual, int ignore) {
void before(double actual, int ignore) {
System.out.println(actual);
}

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

Expand All @@ -45,12 +45,12 @@ void after(Object actual) {

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

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

Expand All @@ -62,12 +62,12 @@ Object after(Object actual) {

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

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

Expand All @@ -79,12 +79,12 @@ Object after(Object actual) {

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

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

Expand All @@ -96,12 +96,12 @@ void after(Object actual) {

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

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

Expand Down Expand Up @@ -130,12 +130,12 @@ void mapWithoutGeneric(Map actual) {

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

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

Expand Down
Loading

0 comments on commit e2336a8

Please sign in to comment.