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

Fix Get Adapters in Proxy Class #995

Merged
merged 3 commits into from
Jul 23, 2015
Merged
Show file tree
Hide file tree
Changes from 2 commits
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 @@ -15,6 +15,7 @@
*/
package br.com.caelum.vraptor.serialization.gson;

import static br.com.caelum.vraptor.proxy.CDIProxies.isCDIProxy;
import static java.util.Collections.singletonList;

import java.lang.reflect.ParameterizedType;
Expand All @@ -26,15 +27,15 @@
import javax.enterprise.inject.Instance;
import javax.inject.Inject;

import br.com.caelum.vraptor.core.ReflectionProvider;
import br.com.caelum.vraptor.serialization.Serializee;

import com.google.gson.ExclusionStrategy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonSerializer;

import br.com.caelum.vraptor.core.ReflectionProvider;
import br.com.caelum.vraptor.serialization.Serializee;

/**
* Builder Wrapper for JSON using GSON.
*
Expand Down Expand Up @@ -90,9 +91,20 @@ private void registerAdapter(Class<?> adapterType, Object adapter) {
}

private Class<?> getAdapterType(Object adapter) {
Type[] genericInterfaces = adapter.getClass().getGenericInterfaces();
ParameterizedType type = (ParameterizedType) genericInterfaces[0];
Type actualType = type.getActualTypeArguments()[0];
final Class<?> klazz;
Copy link
Member

Choose a reason for hiding this comment

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

Instead of using String comparison you should use CDIProxies.isCDIProxy method to check if the class is a proxy.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I didn't know CDIProxies.isCDIProxy is there.
I write a better test.

if(isCDIProxy(adapter.getClass())){
final String[] split = adapter.getClass().getName().split("\\$Proxy\\$");
Copy link
Member

Choose a reason for hiding this comment

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

please, use CDIProxies.extractRawTypeIfPossible instead.
As I can see, it could fix the problem without the if and reflection you've used

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Awesome, it works!!

try {
klazz = Class.forName(split[0]);
} catch (final ClassNotFoundException e) {
throw new RuntimeException(e);
}
}else{
klazz = adapter.getClass();
}
final Type[] genericInterfaces = klazz.getGenericInterfaces();
final ParameterizedType type = (ParameterizedType) genericInterfaces[0];
final Type actualType = type.getActualTypeArguments()[0];
Copy link
Member

Choose a reason for hiding this comment

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

yeah, much better! ;)
for curiosity, is this an eclipse save action adding final, or a personal habit?
no need for changing this, I'm ok with and without that

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is a personal habit.
I think it better to understand the code. But I can take off if necessary.
Thank's.

Copy link
Member

Choose a reason for hiding this comment

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

no worries, you can leave it as it is :)


if (actualType instanceof ParameterizedType) {
return (Class<?>) ((ParameterizedType) actualType).getRawType();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package br.com.caelum.vraptor.serialization.gson;

import static org.junit.Assert.assertEquals;

import java.lang.reflect.Type;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

import br.com.caelum.vraptor.WeldJunitRunner;

@RunWith(WeldJunitRunner.class)
public class GsonBuilderWrapperTest {
private @Inject GsonBuilderWrapper builder;
private Gson gson;

@Before
public void init(){
gson = builder.create();
}

@Test
public void test() {
String json = gson.toJson(new Bean());
assertEquals("{\"test123\":{}}", json);
}
}

class Bean{

}


@RegisterStrategy(RegisterType.SINGLE)
@RequestScoped
class BeanSerializer implements JsonSerializer<Bean> {
private static final JsonObject element = new JsonObject();
static{
element.add("test123", new JsonObject());
}

@Override
public JsonElement serialize(Bean src, Type typeOfSrc, JsonSerializationContext context) {
return element;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

@clairton this test fails without your changes?

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 @Turini.

Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,19 @@
import javax.enterprise.inject.Instance;
import javax.servlet.http.HttpServletRequest;

import net.vidageek.mirror.dsl.Mirror;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializer;

import br.com.caelum.vraptor.Consumes;
import br.com.caelum.vraptor.controller.BeanClass;
import br.com.caelum.vraptor.controller.ControllerMethod;
Expand All @@ -62,12 +66,7 @@
import br.com.caelum.vraptor.serialization.Serializee;
import br.com.caelum.vraptor.util.test.MockInstanceImpl;
import br.com.caelum.vraptor.view.GenericController;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializer;
import net.vidageek.mirror.dsl.Mirror;

public class GsonDeserializerTest {

Expand Down