-
Notifications
You must be signed in to change notification settings - Fork 330
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
Improvement to support multiple generics class parameters #992
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
45 changes: 45 additions & 0 deletions
45
vraptor-core/src/main/java/br/com/caelum/vraptor/util/TypeExtractor.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,45 @@ | ||
package br.com.caelum.vraptor.util; | ||
|
||
import java.lang.reflect.ParameterizedType; | ||
import java.lang.reflect.Type; | ||
import java.lang.reflect.TypeVariable; | ||
|
||
import javax.enterprise.inject.Vetoed; | ||
|
||
/** | ||
* Extract and resolve type of a generic parameter | ||
* | ||
* @author Fábio Franco | ||
*/ | ||
@Vetoed | ||
public class TypeExtractor { | ||
|
||
private Class<?> cls; | ||
|
||
public TypeExtractor(Class<?> cls) { | ||
this.cls = cls; | ||
} | ||
|
||
private Type resolveTypeVariable(TypeVariable<?> type) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you create a few more tests for this method? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. I do. |
||
ParameterizedType parameterizedType = (ParameterizedType) cls.getGenericSuperclass(); | ||
Class<?> rawType = (Class<?>) parameterizedType.getRawType(); | ||
TypeVariable<?>[] typeParameters = rawType.getTypeParameters(); | ||
if (typeParameters.length > 0) { | ||
for (int i = 0; i < typeParameters.length; i++) { | ||
TypeVariable<?> typeVariable = typeParameters[i]; | ||
if (typeVariable.getName().equals(type.getName())) { | ||
return (Class<?>) parameterizedType.getActualTypeArguments()[i]; | ||
} | ||
} | ||
} | ||
return type; | ||
} | ||
|
||
public Type extractType(Type type) { | ||
if (type instanceof TypeVariable) { | ||
return resolveTypeVariable((TypeVariable<?>) type); | ||
} | ||
return type; | ||
} | ||
|
||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about changing
TypeExtractor
to be a CDI managed bean?so it could be specialized -- extensible
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. Good idea.
What is your suggestion of the package for include this class?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is really needed? This method is used in one place only. So I'd keep
as private method in the same class, to using kiss principle.
On Jul 6, 2015 15:39, "Rodrigo Turini" [email protected] wrote:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the other hand, extracting to a class are giving us the single responsibility principle.
It's a small class, easily testable, with a well-defined purpose. That's why I'm ok with
this change. But sure, we can keep it as a private method. @francofabio it's your call.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I created the TypeExtractor class, my intention was to allow this feature to be reused if there was a need.
Thinking about the opinion of @garcia-jj, it makes sense because VRaptor exists a few years ago and yet there was no need to use this feature.
Maybe have less impact on the project if we keep this feature in their own class (private method).
@Turini, What do you think?
** Sorry for my terrible english. :-/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That sounds great to me, go ahead @francofabio