Discussion:
[Ikvm-developers] Google Guice problem - Specified cast is not valid. - Type <=> Class conversion problem?
Martin Gerhardy
2017-02-03 14:12:09 UTC
Permalink
Hi,

still fighting with guice - now whenever I configure a factory builder like this.

-----------------------------
java
------------------------------

public interface UserFactory {
User create(@Assisted UserId id);
}

public interface User {
UserId getId();
}


-----------------------------
csharp code using ikvm converted java classes from above given code
------------------------------

public class BasicUser : User {
}

public class MyModule : AbstractModule {
protected void configure() {
install(new FactoryModuleBuilder().implement(typeof(User), typeof(BasicUser)).build(typeof(UserFactory)));
}
}

-------------------------------

I get


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)

Is that just a feature that doesn't work? Looks like all the other binds are working properly for now - just not the factory builder.

Regards
Martin
Martin Gerhardy
2017-02-08 11:07:48 UTC
Permalink
Hi

This method is the cause of all evil. I wasn't able to create a minimal failure case.

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

Line 104 is this cast:

ParameterizedType parameterizedType = (ParameterizedType) type;

I've read in the changelog that there were particular guice test cases executed and some bugs were fixed in a previous release of ikvm. Can someone please tell me how to run the tests locally on my own? I would like to validate whether a newer guice version broke something else or whether the test cases are just not complete.

Thanks a lot
Regards
Martin

________________________________________
Von: Martin Gerhardy
Gesendet: Freitag, 3. Februar 2017 15:12
An: ikvm-***@lists.sourceforge.net
Betreff: Google Guice problem - Specified cast is not valid. - Type <=> Class conversion problem?

Hi,

still fighting with guice - now whenever I configure a factory builder like this.

-----------------------------
java
------------------------------

public interface UserFactory {
User create(@Assisted UserId id);
}

public interface User {
UserId getId();
}


-----------------------------
csharp code using ikvm converted java classes from above given code
------------------------------

public class BasicUser : User {
}

public class MyModule : AbstractModule {
protected void configure() {
install(new FactoryModuleBuilder().implement(typeof(User), typeof(BasicUser)).build(typeof(UserFactory)));
}
}

-------------------------------

I get


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)

Is that just a feature that doesn't work? Looks like all the other binds are working properly for now - just not the factory builder.

Regards
Martin

Loading...