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

Improvement to support multiple generics class parameters #992

Merged
merged 2 commits into from
Aug 12, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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 @@ -18,7 +18,6 @@
package br.com.caelum.vraptor.http.iogi;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.ArrayList;
Expand All @@ -39,6 +38,7 @@
import br.com.caelum.vraptor.http.Parameter;
import br.com.caelum.vraptor.http.ParameterNameProvider;
import br.com.caelum.vraptor.http.ParametersProvider;
import br.com.caelum.vraptor.util.TypeExtractor;
import br.com.caelum.vraptor.validator.Message;

@RequestScoped
Expand Down Expand Up @@ -90,23 +90,19 @@ private Object instantiateOrAddError(Parameters parameters, List<Message> errors
private List<Target<Object>> createTargets(ControllerMethod method) {
Method javaMethod = method.getMethod();
List<Target<Object>> targets = new ArrayList<>();
TypeExtractor typeExtractor = new TypeExtractor(method.getController().getType());
Copy link
Member

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

Copy link
Contributor Author

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?

Copy link
Member

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:

In
vraptor-core/src/main/java/br/com/caelum/vraptor/http/iogi/IogiParametersProvider.java
#992 (comment):

@@ -90,11 +90,12 @@ private Object instantiateOrAddError(Parameters parameters, List errors
private List<Target> createTargets(ControllerMethod method) {
Method javaMethod = method.getMethod();
List<Target> targets = new ArrayList<>();

  •   TypeExtractor typeExtractor = new TypeExtractor(method.getController().getType());
    

what about changing TypeExtractor to be a CDI managed bean?
so it could be specialized -- extensible


Reply to this email directly or view it on GitHub
https://github.com/caelum/vraptor4/pull/992/files#r33967540.

Copy link
Member

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.

Copy link
Contributor Author

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. :-/

Copy link
Member

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


for (Parameter p : nameProvider.parametersFor(javaMethod)) {
Type type = p.getParameterizedType();
if (type instanceof TypeVariable) {
type = extractType(method);
type = typeExtractor.extractType(type);
}

targets.add(new Target<>(type, p.getName()));
}

return targets;
}

private Type extractType(ControllerMethod method) {
ParameterizedType superclass = (ParameterizedType) method.getController().getType().getGenericSuperclass();
return superclass.getActualTypeArguments()[0];
}

private Parameters parseParameters(HttpServletRequest request) {
Map<String, String[]> parameters = request.getParameterMap();
Expand Down
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you create a few more tests for this method?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,21 @@ public void isCapableOfDealingWithGenerics() throws Exception {
assertThat(abc.x, is(123l));
}

@Test
public void isCapableOfDealingWithGenericsAndMultipleParameters() throws Exception {
requestParametersAre(ImmutableMap.of("key", new String[] { "age" }, "value", new String[] { "32" }));

ControllerMethod generic = method(SpecificKeyValueResource.class, GenericKeyValueResource.class, "put", Object.class, Object.class);
Object[] params = iogi.getParametersFor(generic, errors);
String key = (String) params[0];
Long value = (Long) params[1];

assertThat(key, is("age"));
assertThat(value, notNullValue());
assertThat(value, instanceOf(Long.class));
assertThat(value, is(32L));
}

@Test
public void isCapableOfDealingWithIndexedLists() throws Exception {
requestParameterIs("abc[2]", "1");
Expand Down Expand Up @@ -602,6 +617,14 @@ void generic(T t) { }

static class Specific extends Generic<ABC> {
}

static class GenericKeyValueResource<K, V> {
void put(K key, V value) { }
}

static class SpecificKeyValueResource extends GenericKeyValueResource<String, Long> {

}

public static class Cat {
private String id;
Expand Down