Play-Framework: Causing a 'IllegalArgumentException occured - java

The error I get is 'IllegalArgumentException occured' Can not set java.util.ArrayList field mi.types.ListOfObjects.objects to java.util.LinkedList
I'm doing the following...
ListOfObjects objects = li.getUsersObjects();
which works perfectly fine normally, however when I do the exact same call with the exact same code inside of Play it doesn't. I'm calling it in my Security Controller inside the authenticate() function. There's a case where the users objects are gotten from an external server. That's when this call is made. It works fine without Play and I'm pretty sure worked before I put it in the Security Controller. Why would putting it here cause such a problem?
EDIT
ListOfObjects is a custom object which contains some values.. It's located inside li which is a library I'm importing. It looks something like this...
public class ListOfObjects {
private ArrayList<Object> objects;
public ArrayList<Object> getObjects(){
return objects;
}
}
getUserObjects simply returns the objects for that user. It grabs them from a server and then uses gson to parse them. However I'm getting the above error when I attempt to.
EDIT2
No matter what I do my type for getUserObjects seems to always be returning as a LinkedList even though it's an ArrayList object. I've tried calling getObjects() directly and there's no change.
EDIT 3
getUserObjects is defined as...
Gson gson = new Gson();
ListOfObjects objects = gson.fromJson(r.getBody(), ListOfObjects.class);
StackTrace...
where r.getBody() is the JSON response from the server
Execution exception (In /app/controllers/Security.java around line 57)
IllegalArgumentException occured : Can not set java.util.ArrayList field lm.types.ListOfObjects.objects to java.util.LinkedList
play.exceptions.JavaExecutionException: Can not set java.util.ArrayList field lm.types.ListOfObjects.objects to java.util.LinkedList
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:227)
at Invocation.HTTP Request(Play!)
Caused by: java.lang.IllegalArgumentException: Can not set java.util.ArrayList field lm.types.ListOfObjects.objects to java.util.LinkedList
at com.google.gson.FieldAttributes.set(FieldAttributes.java:188)
at com.google.gson.JsonObjectDeserializationVisitor.visitFieldUsingCustomHandler(JsonObjectDeserializationVisitor.java:118)
at com.google.gson.ObjectNavigator.navigateClassFields(ObjectNavigator.java:158)
at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:131)
at com.google.gson.JsonDeserializationContextDefault.fromJsonObject(JsonDeserializationContextDefault.java:73)
at com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDeserializationContextDefault.java:51)
at com.google.gson.Gson.fromJson(Gson.java:568)
at com.google.gson.Gson.fromJson(Gson.java:515)
at com.google.gson.Gson.fromJson(Gson.java:484)
at com.google.gson.Gson.fromJson(Gson.java:434)
at com.google.gson.Gson.fromJson(Gson.java:406)
at lm.lib.LMethods.getUsersObjects(LMethods.java:165)
at controllers.Security.createNewUser(Security.java:57)
at controllers.Security.authenticate(Security.java:36)
at play.utils.Java.invokeStaticOrParent(Java.java:159)
at controllers.Secure$Security.invoke(Secure.java:193)
at controllers.Secure$Security.access$0(Secure.java:184)
at controllers.Secure.authenticate(Secure.java:64)
at play.mvc.ActionInvoker.invokeWithContinuation(ActionInvoker.java:540)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:498)
at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:474)
at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:469)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:157)
... 1 more
Debugger failed to attach: recv failed during handshake: Connection reset by peer

Exception message clearly says that Gson deserializes list as LinkedList, whereas field of your ListOfObjects is declared as ArrayList.
The general rule to avoid this kind of problems is not to use implementation classes in field declarations. Use List instead:
public class ListOfObjects {
private List<Object> objects;
...
}

Related

How to map cache to a list of DTO object?

I have this cache that is basically a list of a DTO:
ClassA:
#Cacheable("MyList")
public List<MyObjectDTO> setCachedList(){
return api.getList(); //call to another api to fetch that list
}
ClassB:
public List<MyObjectDTO> getCachedList(){
if(!CacheManager.getCacheNames().contains("MyList"){
ClassB.setCachedList();
}
Cache cache = CacheManager.getCache("MyList");
Type listType = new TypeToken<List<MyObjectDTO>>(){}.getType(); //error occurs here
List<MyObjectDTO> returnList = modelMapper.map(cache, listType);
return returnList;
I get the following error in above code:
Failed to instantiate instance of destination java.util.List. Ensure that java.util.List has a non-private no-argument constructor.
I saw a similar question but since I am using cache of a list, I cannot extend it to concrete class.
I don't think the error is in the line you mention but in the one right after. Type returns List, but List is an interface and therefore impossible to instantiate, your mapper must be catching this exception and throwing the error.
You should try another way for getting the cache type, maybe cache.getClass() or something of the sort will do the trick.
I was able to resolve the issue by passing cache.get(SimpleKey.EMPTY).get() in the modelMapper instead of cache.

Getting "declares multiple JSON fields named" error when serializing with GSON

Type type = new TypeToken<HashMap<String , MovieDb>>() {}.getType();
gson.fromJson(json, type); //ERROR HERE !!!
When I convert from Json to the hashmap object it works fine without minifying enabled. But when minify is enabled it gives me the following error at this line:
java.lang.IllegalArgumentException: class a.a.a.a.b declares multiple JSON fields named a
at com.google.b.b.a.i.a(ReflectiveTypeAdapterFactory.java:172)
at com.google.b.b.a.i.a(ReflectiveTypeAdapterFactory.java:102)
at com.google.b.e.a(Gson.java:458)
at com.google.b.b.a.b.a(CollectionTypeAdapterFactory.java:53)
at com.google.b.e.a(Gson.java:458)
at com.google.b.b.a.i.a(ReflectiveTypeAdapterFactory.java:117)
at com.google.b.b.a.i.a(ReflectiveTypeAdapterFactory.java:166)
at com.google.b.b.a.i.a(ReflectiveTypeAdapterFactory.java:102)
at com.google.b.e.a(Gson.java:458)
at com.google.b.b.a.g.a(MapTypeAdapterFactory.java:126)
at com.google.b.e.a(Gson.java:458)
at com.google.b.e.a(Gson.java:926)
at com.google.b.e.a(Gson.java:892)
at com.google.b.e.a(Gson.java:841)
at com.techy.nateshmbhat.moviego.i.onPreExecute(MovieInterface.java:180)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:648)
at android.os.AsyncTask.execute(AsyncTask.java:595)
at com.techy.nateshmbhat.moviego.i.b(MovieInterface.java:101)
at com.techy.nateshmbhat.moviego.a.a(Activity_InTheaterMovies.java:55)
Your error is most propably because you minify two fields to a same name a. Something like:
#SerializedName("a")
Long veryLongFieldName;
#SerializedName("a")
Long anotherVeryLongFieldName;
Note that these can be either in the same class or if using inheritance it is enough that they are in the same inheritance tree. Gson can not assign two values to one minimized field name a.
It could of course also be that you have minimized some field to a name that is already reserved by some other unminimized field.
I've faced this issue when I had context property/variable in the model class

Getting following error while inserting data in Ignite cache?

This error is occurred while using JsonObject(GSON Library) in cache value if I used String instead of JsonObject then operation perform successfully.
Error which I have get while inserting data in cache:
javax.cache.CacheException: class org.apache.ignite.transactions.TransactionRollbackException: Transaction has been rolled back: 9557fcfb061-00000000-078a-a446-0000-000000000001
at org.apache.ignite.internal.processors.cache.GridCacheUtils.convertToCacheException(GridCacheUtils.java:1287)
at org.apache.ignite.internal.processors.cache.IgniteCacheProxyImpl.cacheException(IgniteCacheProxyImpl.java:1648)
at org.apache.ignite.internal.processors.cache.IgniteCacheProxyImpl.put(IgniteCacheProxyImpl.java:1008)
at org.apache.ignite.internal.processors.cache.GatewayProtectedCacheProxy.put(GatewayProtectedCacheProxy.java:872)
at com.clouzer.databaseoperation.DataBaseOperation.getAllUSerGson(DataBaseOperation.java:527)
at com.mstorm.ignition.gson.LoginCacheOperation.createLoginCache(LoginCacheOperation.java:38)
at com.mstorm.ignition.gson.IgniteCacheCreator.createCaches(IgniteCacheCreator.java:30)
at com.mstorm.ignition.gson.IgniteServer.main(IgniteServer.java:102)
Caused by: class org.apache.ignite.transactions.TransactionRollbackException: Transaction has been rolled back: 9557fcfb061-00000000-078a-a446-0000-000000000001
at org.apache.ignite.internal.util.IgniteUtils$11.apply(IgniteUtils.java:860)
at org.apache.ignite.internal.util.IgniteUtils$11.apply(IgniteUtils.java:858)
... 8 more
Caused by: class org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException: Transaction has been rolled back: 9557fcfb061-00000000-078a-a446-0000-000000000001
at org.apache.ignite.internal.processors.cache.GridCacheAdapter.syncOp(GridCacheAdapter.java:4054)
at org.apache.ignite.internal.processors.cache.GridCacheAdapter.put0(GridCacheAdapter.java:2340)
at org.apache.ignite.internal.processors.cache.GridCacheAdapter.put(GridCacheAdapter.java:2321)
at org.apache.ignite.internal.processors.cache.GridCacheAdapter.put(GridCacheAdapter.java:2298)
at org.apache.ignite.internal.processors.cache.IgniteCacheProxyImpl.put(IgniteCacheProxyImpl.java:1005)
... 5 more
Caused by: class org.apache.ignite.IgniteCheckedException: Can not set final com.google.gson.internal.LinkedTreeMap field com.google.gson.JsonObject.members to java.util.LinkedHashMap
at org.apache.ignite.internal.util.IgniteUtils.cast(IgniteUtils.java:7252)
at org.apache.ignite.internal.util.future.GridFutureAdapter.resolve(GridFutureAdapter.java:259)
at org.apache.ignite.internal.util.future.GridFutureAdapter.get0(GridFutureAdapter.java:171)
at org.apache.ignite.internal.util.future.GridFutureAdapter.get(GridFutureAdapter.java:140)
at org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal$10.applyx(GridNearTxLocal.java:2396)
at org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal$10.applyx(GridNearTxLocal.java:2392)
at org.apache.ignite.internal.util.lang.IgniteClosureX.apply(IgniteClosureX.java:38)
at org.apache.ignite.internal.util.future.GridFutureChainListener.applyCallback(GridFutureChainListener.java:78)
at org.apache.ignite.internal.util.future.GridFutureChainListener.apply(GridFutureChainListener.java:70)
at org.apache.ignite.internal.util.future.GridFutureChainListener.apply(GridFutureChainListener.java:30)
at org.apache.ignite.internal.util.future.GridFutureAdapter.notifyListener(GridFutureAdapter.java:383)
at org.apache.ignite.internal.util.future.GridFutureAdapter.listen(GridFutureAdapter.java:353)
at org.apache.ignite.internal.util.future.GridFutureAdapter$ChainFuture.<init>(GridFutureAdapter.java:572)
at org.apache.ignite.internal.util.future.GridFutureAdapter.chain(GridFutureAdapter.java:358)
at org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal.optimisticPutFuture(GridNearTxLocal.java:2391)
at org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal.putAsync0(GridNearTxLocal.java:622)
at org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal.putAsync(GridNearTxLocal.java:385)
at org.apache.ignite.internal.processors.cache.GridCacheAdapter$22.op(GridCacheAdapter.java:2342)
at org.apache.ignite.internal.processors.cache.GridCacheAdapter$22.op(GridCacheAdapter.java:2340)
at org.apache.ignite.internal.processors.cache.GridCacheAdapter.syncOp(GridCacheAdapter.java:4040)
... 9 more
Cache Configuration:
CacheConfiguration<String, JsonObject> cacheConfig = new CacheConfiguration<>();
cacheConfig.setName("CACHE_NAME");
cacheConfig.setReadThrough(true);
cacheConfig.setBackups(2);
cacheConfig.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
cacheConfig.setWriteThrough(true);
cacheConfig.setCacheStoreFactory(FactoryBuilder.factoryOf(ObjectLoader.class));
IgniteCache<String, JsonObject> cache = ignite.getOrCreateCache(cacheConfig);
Operations:
1.create JsonObject (use GSON library)
2.set cache loader to the given cache which used for persistent store
3.insert data into the cache from ignite client-side
4.observe the issue
The problem is that JsonObject has members field of type LinkedTreeMap, and Ignite BinaryMarshaller, which is default one, deserializes it into a LinkedHashMap for some reason.
To avoid it you can use a different marshaller, for example, OptimizedMarshaller.
UPD:
Turns out, if you configure OptimizedMarshaller, then only puts and gets from cache will work fine. But if you try to get(...) some field of the deserialized representation of JsonObject, for example, then JVM will crash.
The reason is that LinkedTreeMap has the following writeReplace() method:
private Object writeReplace() throws ObjectStreamException {
return new LinkedHashMap(this);
}
So, if you serialize it, and after that deserialize, then you will get an instance of LinkedHashMap instead of LinkedTreeMap. So, BinaryMarshaller is doing nothing wrong.
Summary:
Looks like you shouldn't put JsonObjects into cache. It doesn't implement Serializable, so the authors didn't think about serializing it. I think, you will have to replace JsonObject with JSON strings.

ClassCastException after applying PathProperties

I'm trying to apply some PathProperties to my Finders but I keep getting this error :
[ClassCastException: java.util.ArrayList cannot be cast to com.avaje.ebean.bean.BeanCollection]
It only happens when I have a List<...> called in my PathProperties like so :
PathProperties pathProperties = PathProperties.parse("(*,historique(*))");
List<Devi> test = Devi.find.apply(pathProperties).findList();
Where my Finder is defined like this :
public static Finder<String,Devi> find = new Finder<String,Devi>(Devi.class);
Here, the object Devi is full of public variables, that I am able to call without any issue (the PathProperties "(*)" works), but when I try to access a List of objects inside this object (here, public List<Histo> historique), it won't work. I tried, and I'm also able to access an object within the object, as long as it's not a List.
I'm kinda lost here, I don't know what I did wrong.
According to https://github.com/ebean-orm/ebean/issues/591 , this is a bug triggered by initializing the ArrayList. Without the initialization it will apparently work.

java.lang.ClassCastException while trying to print container id in jade

I have a code for finding the list of containers from the ams in jade using queryPlatformAction method. I am getting a problem for typecasting the container id while putting the sop statement at the end..
Result result = (Result) content;
List listOfPlatforms = (List) result.getValue();
Iterator iter = listOfPlatforms.iterator();
while (iter.hasNext())
{
ContainerID next = (ContainerID) iter.next();
System.out.println(next.getID());
}
It is throwing an exception to me.
The exception is: java.lang.ClassCastException: jade.util.leap.ArrayList cannot
be cast to java.util.List
please help.
The two types of List are not in the same class hierarchy and therefore cannot be cast between each other, hence a ClassCastException.
The jade.util.leap.List interface is used within JADE to provide a List collection that looks the same between the different back-ends of JADE, but which is actually implemented differently. From the Javadocs, ArrayList only extends Object.
To fix this, declare listOfPlatforms to be of type jade.util.leap.List.

Categories