追踪奇怪的类强制转换异常

时间:2014-10-12 16:25:41

标签: java classloader maven-plugin guice

我目前正致力于在Maven 3.1及更高版本中更新maven插件。看起来插件工作得很好,但是我很难将测试套件更新到最新的更改。在与一个艰难的lib版本战斗之后,所有看起来都没问题,但我在我的测试套件中遇到以下异常:

java.lang.ClassCastException: org.codehaus.plexus.DefaultPlexusContainer$LoggerManagerProvider cannot be cast to javax.inject.Provider
at com.google.inject.internal.InternalFactoryToInitializableAdapter.get(InternalFactoryToInitializableAdapter.java:46)
at com.google.inject.internal.SingleParameterInjector.inject(SingleParameterInjector.java:38)
at com.google.inject.internal.SingleParameterInjector.getAll(SingleParameterInjector.java:62)
at com.google.inject.internal.SingleMethodInjector.inject(SingleMethodInjector.java:84)
at com.google.inject.internal.MembersInjectorImpl.injectMembers(MembersInjectorImpl.java:132)
at com.google.inject.internal.MembersInjectorImpl$1.call(MembersInjectorImpl.java:93)
at com.google.inject.internal.MembersInjectorImpl$1.call(MembersInjectorImpl.java:80)
at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1059)
at com.google.inject.internal.MembersInjectorImpl.injectAndNotify(MembersInjectorImpl.java:80)
at com.google.inject.internal.Initializer$InjectableReference.get(Initializer.java:174)
at com.google.inject.internal.Initializer.injectAll(Initializer.java:108)
at com.google.inject.internal.InternalInjectorCreator.injectDynamically(InternalInjectorCreator.java:174)
at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:110)
at com.google.inject.Guice.createInjector(Guice.java:96)
at com.google.inject.Guice.createInjector(Guice.java:73)
at com.google.inject.Guice.createInjector(Guice.java:62)
at org.codehaus.plexus.DefaultPlexusContainer.addPlexusInjector(DefaultPlexusContainer.java:481)
at org.codehaus.plexus.DefaultPlexusContainer.<init>(DefaultPlexusContainer.java:206)
at org.codehaus.plexus.DefaultPlexusContainer.<init>(DefaultPlexusContainer.java:168)
at net.flexmojos.oss.plugin.compiler.DependencyFilteringTest.initPlexus(DependencyFilteringTest.java:66)

奇怪的是org.codehaus.plexus.DefaultPlexusContainer是Maven的一部分:org.eclipse.sisu:org.eclipse.sisu.plexus:0.3.0.M1 library

final class org.codehaus.plexus.DefaultPlexusContainer.LoggerManagerProvider
    implements org.eclipse.sisu.inject.DeferredProvider<LoggerManager>

位于org.eclipse.sisu:org.eclipse.sisu.inject:0.3.0.M1 with

public interface org.eclipse.sisu.inject.DeferredProvider<T>
    extends com.google.inject.Provider<T>

位于org.sonatype.sisu:sisu-guice:3.2.4 with

public interface com.google.inject.Provider<T> extends javax.inject.Provider<T> {

所以最后一个LoggerManagerProvider也必须是一个com.google.inject.Provider以及一个javax.inject.Provider ...如果我在调试器中停下来并检查:

tst instanceof com.google.inject.Provider --> True
tst instanceof javax.inject.Provider --> False 

这怎么可能?我想我的VM中有一些糟糕的类版本垃圾,但我不清楚如何跟踪它。

克里斯

1 个答案:

答案 0 :(得分:1)

好的...在我的问题中发布的类层次结构是:

org.codehaus.plexus.DefaultPlexusContainer.LoggerManagerProvider - &gt; org.eclipse.sisu.inject.DeferredProvider - &gt; com.google.inject.Provider - &gt; javax.inject.Provider

我检查过我的类路径只包含一个org.eclipse.sisu.inject.DeferredProvider和一个javax.inject.Provider ...但是今天我发现一个奇怪的包提供了一个不同的com.google.inject实现。提供商......在从构建中排除这一点之后,一切都很好了: - )

认为我必须习惯使用像Tattletale(http://tattletale.jboss.org/)之类的插件,并且类似于检测类似的东西。