-
Notifications
You must be signed in to change notification settings - Fork 332
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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. | ||
* | ||
|
@@ -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; | ||
if(isCDIProxy(adapter.getClass())){ | ||
final String[] split = adapter.getClass().getName().split("\\$Proxy\\$"); | ||
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. please, use 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. 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]; | ||
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. yeah, much better! ;) 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. It is a personal habit. 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. no worries, you can leave it as it is :) |
||
|
||
if (actualType instanceof ParameterizedType) { | ||
return (Class<?>) ((ParameterizedType) actualType).getRawType(); | ||
|
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; | ||
} | ||
} | ||
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. @clairton this test fails without your changes? 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 @Turini. |
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.
Instead of using String comparison you should use
CDIProxies.isCDIProxy
method to check if the class is a proxy.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.
Thanks, I didn't know CDIProxies.isCDIProxy is there.
I write a better test.