-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: 🚑 [BUG] Generic type with concrete type parameters generate non-…
…compiling code (#139) * Create draft PR for #138 [skip ci] * fix: Generates records with complicated Generics structure --------- Co-authored-by: pawellabaj <pawellabaj@users.noreply.github.com> Co-authored-by: Pawel_Labaj <Pawel_Labaj@epam.com>
- Loading branch information
1 parent
92850f8
commit 708cf28
Showing
17 changed files
with
454 additions
and
125 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
...uto-record-tests/src/test/java/pl/com/labaj/autorecord/generic/GenericGenerationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package pl.com.labaj.autorecord.generic; | ||
|
||
/*- | ||
* Copyright © 2023 Auto Record | ||
* | ||
* 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. | ||
*/ | ||
|
||
import com.google.testing.compile.Compiler; | ||
import com.google.testing.compile.JavaFileObjects; | ||
import org.junit.jupiter.api.Test; | ||
import pl.com.labaj.autorecord.processor.AutoRecordProcessor; | ||
|
||
import java.util.List; | ||
|
||
import static com.google.testing.compile.Compiler.javac; | ||
import static org.junit.jupiter.api.Assertions.assertAll; | ||
import static pl.com.labaj.autorecord.test.TestUtils.assertThat; | ||
import static pl.com.labaj.autorecord.test.TestUtils.expectedGenearatedRecordName; | ||
import static pl.com.labaj.autorecord.test.TestUtils.expectedResourceName; | ||
import static pl.com.labaj.autorecord.test.TestUtils.inputResourceName; | ||
|
||
class GenericGenerationTest { | ||
|
||
private final Compiler compiler = javac().withProcessors(new AutoRecordProcessor()); | ||
|
||
@Test | ||
void shouldGenerateRecordWithProperGenerics() { | ||
//given | ||
var inputInterfaces = List.of( | ||
JavaFileObjects.forResource(inputResourceName("BaseType")), | ||
JavaFileObjects.forResource(inputResourceName("ConcreteType")), | ||
JavaFileObjects.forResource(inputResourceName("WithGeneric")), | ||
JavaFileObjects.forResource(inputResourceName("WithSubGeneric"))); | ||
var expectedRecord = JavaFileObjects.forResource(expectedResourceName("WithSubGeneric")); | ||
|
||
//when | ||
var compilation = compiler.compile(inputInterfaces); | ||
|
||
//then | ||
assertAll( | ||
() -> assertThat(compilation).succeeded(), | ||
() -> assertThat(compilation).generatedSourceFile(expectedGenearatedRecordName("WithSubGeneric")).hasSourceEquivalentTo(expectedRecord) | ||
); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
modules/auto-record-tests/src/test/resources/in/BaseType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package pl.com.labaj.autorecord.testcase; | ||
|
||
/*- | ||
* Copyright © 2023 Auto Record | ||
* | ||
* 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. | ||
*/ | ||
|
||
interface BaseType { | ||
} |
20 changes: 20 additions & 0 deletions
20
modules/auto-record-tests/src/test/resources/in/ConcreteType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package pl.com.labaj.autorecord.testcase; | ||
|
||
/*- | ||
* Copyright © 2023 Auto Record | ||
* | ||
* 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. | ||
*/ | ||
|
||
interface ConcreteType extends BaseType { | ||
} |
37 changes: 37 additions & 0 deletions
37
modules/auto-record-tests/src/test/resources/in/WithGeneric.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package pl.com.labaj.autorecord.testcase; | ||
|
||
/*- | ||
* Copyright © 2023 Auto Record | ||
* | ||
* 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. | ||
*/ | ||
|
||
import pl.com.labaj.autorecord.Memoized; | ||
|
||
import java.util.List; | ||
|
||
interface WithGeneric<A extends BaseType, B> { | ||
A a(); | ||
List<A> listOfA(); | ||
B b(); | ||
|
||
@Memoized | ||
default A memA() { | ||
return a(); | ||
} | ||
|
||
@Memoized | ||
default String fromAB(A anA, B anB) { | ||
return String.valueOf(anA) + String.valueOf(anB); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
modules/auto-record-tests/src/test/resources/in/WithSubGeneric.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package pl.com.labaj.autorecord.testcase; | ||
|
||
/*- | ||
* Copyright © 2023 Auto Record | ||
* | ||
* 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. | ||
*/ | ||
|
||
import pl.com.labaj.autorecord.AutoRecord; | ||
import pl.com.labaj.autorecord.Memoized; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
@AutoRecord | ||
interface WithSubGeneric<X, Y extends Collection<X>> extends WithGeneric<ConcreteType, X> { | ||
Y y(); | ||
|
||
@Memoized | ||
default Y memY() { | ||
return y(); | ||
} | ||
|
||
@Memoized | ||
String toString(); | ||
} |
91 changes: 91 additions & 0 deletions
91
modules/auto-record-tests/src/test/resources/out/WithSubGenericRecord.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package pl.com.labaj.autorecord.testcase; | ||
|
||
/*- | ||
* Copyright © 2023 Auto Record | ||
* | ||
* 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. | ||
*/ | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static java.util.Objects.requireNonNullElseGet; | ||
|
||
import java.lang.Override; | ||
import java.lang.String; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import javax.annotation.Nullable; | ||
import javax.annotation.processing.Generated; | ||
|
||
import pl.com.labaj.autorecord.GeneratedWithAutoRecord; | ||
import pl.com.labaj.autorecord.Memoized; | ||
import pl.com.labaj.autorecord.memoizer.Memoizer; | ||
|
||
@Generated("pl.com.labaj.autorecord.AutoRecord") | ||
@GeneratedWithAutoRecord | ||
record WithSubGenericRecord<X, Y extends Collection<X>>(ConcreteType a, | ||
List<ConcreteType> listOfA, | ||
X b, | ||
Y y, | ||
@Nullable Memoizer<ConcreteType> memAMemoizer, | ||
@Nullable Memoizer<String> fromABMemoizer, | ||
@Nullable Memoizer<Y> memYMemoizer, | ||
@Nullable Memoizer<String> toStringMemoizer) implements WithSubGeneric<X, Y> { | ||
WithSubGenericRecord { | ||
requireNonNull(a, "a must not be null"); | ||
requireNonNull(listOfA, "listOfA must not be null"); | ||
requireNonNull(b, "b must not be null"); | ||
requireNonNull(y, "y must not be null"); | ||
|
||
memAMemoizer = requireNonNullElseGet(memAMemoizer, Memoizer::new); | ||
fromABMemoizer = requireNonNullElseGet(fromABMemoizer, Memoizer::new); | ||
memYMemoizer = requireNonNullElseGet(memYMemoizer, Memoizer::new); | ||
toStringMemoizer = requireNonNullElseGet(toStringMemoizer, Memoizer::new); | ||
} | ||
|
||
WithSubGenericRecord(ConcreteType a, List<ConcreteType> listOfA, X b, Y y) { | ||
this(a, listOfA, b, y, new Memoizer<>(), new Memoizer<>(), new Memoizer<>(), new Memoizer<>()); | ||
} | ||
|
||
@Memoized | ||
@Override | ||
public ConcreteType memA() { | ||
return memAMemoizer.computeIfAbsent(WithSubGeneric.super::memA); | ||
} | ||
|
||
@Memoized | ||
@Override | ||
public String fromAB(ConcreteType anA, X anB) { | ||
return fromABMemoizer.computeIfAbsent(() -> WithSubGeneric.super.fromAB(anA, anB)); | ||
} | ||
|
||
@Memoized | ||
@Override | ||
public Y memY() { | ||
return memYMemoizer.computeIfAbsent(WithSubGeneric.super::memY); | ||
} | ||
|
||
@Memoized | ||
@Override | ||
public String toString() { | ||
return toStringMemoizer.computeIfAbsent(this::_toString); | ||
} | ||
|
||
private String _toString() { | ||
return "WithSubGenericRecord[" + | ||
"a = " + a + ", " + | ||
"listOfA = " + listOfA + ", " + | ||
"b = " + b + ", " + | ||
"y = " + y + | ||
"]"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.