-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: support classes that extend or implement an Iterable (#2397)
* Fix extended iterables in the parser * Add test and reformat * Accept unknown return types * Fix formatting --------- Co-authored-by: Anton Platonov <platosha@gmail.com>
- Loading branch information
Showing
8 changed files
with
157 additions
and
3 deletions.
There are no files selected for viewing
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
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
43 changes: 43 additions & 0 deletions
43
packages/java/parser-jvm-utils/src/main/java/com/vaadin/hilla/parser/utils/Generics.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,43 @@ | ||
package com.vaadin.hilla.parser.utils; | ||
|
||
import com.googlecode.gentyref.GenericTypeReflector; | ||
|
||
import java.lang.reflect.AnnotatedElement; | ||
import java.lang.reflect.ParameterizedType; | ||
import java.lang.reflect.Type; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Utility class for working with generics. | ||
*/ | ||
public class Generics { | ||
/** | ||
* Returns the exact type of the elements in an iterable class. | ||
* | ||
* @param cls | ||
* the class to get the exact type of | ||
* @return the exact type of the elements in the iterable class | ||
*/ | ||
public static Optional<Type> getExactIterableType(Class<?> cls) { | ||
try { | ||
// We can find the exact type by looking at the iterator method | ||
// return type | ||
var method = cls.getMethod("iterator"); | ||
var exactReturnType = GenericTypeReflector | ||
.getExactReturnType(method, cls); | ||
|
||
// We know the format of the method, so we can safely cast the type | ||
if (exactReturnType instanceof ParameterizedType) { | ||
return Optional.of(((ParameterizedType) exactReturnType) | ||
.getActualTypeArguments()[0]); | ||
} | ||
|
||
return Optional.empty(); | ||
} catch (NoSuchMethodException e) { | ||
// This should really never happen if the passed class is an | ||
// Iterable | ||
throw new IllegalArgumentException( | ||
"Class " + cls.getName() + " is not an Iterable"); | ||
} | ||
} | ||
} |
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