Google Guice 4.1.0 - Specified cast is not valid
Brought to you by:
jfrijters
Using ikvm 8.1.5717.0 from Debian repository.
We encounter a problem with Guice 4.1.0. Read at [2] that guice should most likely work.
INFORMATION: An exception was caught and reported. Message: java.lang.ClassCastException: Specified cast is not valid. java.lang.ClassCastException: Specified cast is not valid. at com.google.inject.internal.MoreTypes.canonicalizeForKey(MoreTypes.java:104) at com.google.inject.Key.<init>(Key.java:137) at com.google.inject.Key.get(Key.java:277) at com.google.inject.internal.Annotations.getKey(Annotations.java:283) at com.google.inject.assistedinject.FactoryProvider2.constructorHasMatchingParams(FactoryProvider2.java:552) at com.google.inject.assistedinject.FactoryProvider2.findMatchingConstructorInjectionPoint(FactoryProvider2.java:496) at com.google.inject.assistedinject.FactoryProvider2.<init>(FactoryProvider2.java:292) at com.google.inject.assistedinject.FactoryModuleBuilder$1.configure(FactoryModuleBuilder.java:334) at com.google.inject.AbstractModule.configure(AbstractModule.java:62) at com.google.inject.spi.Elements$RecordingBinder.install(Elements.java:340) at com.google.inject.AbstractModule.install(AbstractModule.java:122)
This method is the cause of all evil. I wasn't able to create a minimal failure case though.
public static <T> TypeLiteral<T> canonicalizeForKey(TypeLiteral<T> typeLiteral) { Type type = typeLiteral.getType(); if (!isFullySpecified(type)) { Errors errors = new Errors().keyNotFullySpecified(typeLiteral); throw new ConfigurationException(errors.getMessages()); } if (typeLiteral.getRawType() == javax.inject.Provider.class) { ParameterizedType parameterizedType = (ParameterizedType) type; // the following casts are generally unsafe, but com.google.inject.Provider extends // javax.inject.Provider and is covariant @SuppressWarnings("unchecked") TypeLiteral<T> guiceProviderType = (TypeLiteral<T>) TypeLiteral.get( Types.providerOf(parameterizedType.getActualTypeArguments()[0])); return guiceProviderType; }
We are trying to connect a converted jar from C# code. See some code snippets in the mailing list archive [1] linked below.
[1] https://sourceforge.net/p/ikvm/mailman/message/35654368/
[2] http://weblog.ikvm.net/CommentView.aspx?guid=5924f108-3375-4fe7-9809-2939ab1ba85f#commentstart
Unfortunately I don't have a lot of time to work on IKVM, so without a minimal repro this is unlikely to get investigated/fixed.
Tested with guice 3.0 - same error - just a different line of code. Will try to assemble a repro - but not that easy...
Found this on the mailinglist - looks similar: https://sourceforge.net/p/ikvm/mailman/message/26675740/
Ok, the problem is related to Providers. As ikvm removes the generic information from converted java classes, you can't inject Providers (or any other class with generics) - we worked around that problem by doing a factory that we can inject and not specifying the generic type at all (or better, hide this information in the factory).
As providers don't have and generics anymore in the ikvm world, the guice cast to ParameterizedType can't work (at least that is my guess)