---
title: "OSG框架下使用JAXB出现ClassCastException"
author: "colorfire"
date: "2011-05-17T09:55:50Z"
tags: []
categories: ["编程语言"]
slug: "osg-jaxb-classcastexception"
draft: false
lastmod: "2011-05-17T09:55:50Z"
id: 1046643
url: "https://colorfire.iteye.com/blog/1046643"
---

以下是实现代码：

```java

JAXBContext jc = JAXBContext.newInstance("cn.capitek.stat.fuse.predeal.config.ftp");
	        Unmarshaller u = jc.createUnmarshaller();
	        InputStream in = CCommonFunc.getInputStream("META-INF/conf-test/ftp.xml");
	        JAXBElement<?> reportconfigElement = (JAXBElement<?>)u.unmarshal(in);
	        in.close();
	        
	        //测试对象类型
	        Object obj = reportconfigElement.getValue();
	        System.out.println(obj.getClass());
	        System.out.println(obj.equals(new FtpConfigType()));
	        
	        List<FtpType> ftpArray=((FtpConfigType)reportconfigElement.getValue()).getFtp();
	        System.out.println("============="+ftpArray.size());
```


使用spring osgi test进行单元测试


> 
> java.lang.ClassCastException: cn.capitek.stat.fuse.predeal.config.ftp.FtpConfigType
> 	at cn.capitek.stat.fuse.predeal.service.collector.impl.FtpFileCollectorsManager.getFtpList(FtpFileCollectorsManager.java:156)
> 	at cn.capitek.stat.fuse.predeal.service.collector.impl.FtpFileCollectorsManager.startCollectors(FtpFileCollectorsManager.java:95)
> 	at test.cn.capitek.stat.fuse.predeal.service.collector.impl.TestFtpFileCollectorsManager.testGetFile(TestFtpFileCollectorsManager.java:38)
> 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> 	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
> 	at java.lang.reflect.Method.invoke(Method.java:585)
> 	at junit.framework.TestCase.runTest(TestCase.java:164)
> 	at org.springframework.osgi.test.AbstractOsgiTests.osgiRunTest(AbstractOsgiTests.java:553)
> 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> 	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
> 	at java.lang.reflect.Method.invoke(Method.java:585)
> 	at org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:193)
> 	at org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:178)
> 	at org.springframework.osgi.test.internal.support.OsgiJUnitTestAdapter.osgiRunTest(OsgiJUnitTestAdapter.java:76)
> 	at org.springframework.osgi.test.internal.support.OsgiJUnitService$1.protect(OsgiJUnitService.java:101)
> 	at junit.framework.TestResult.runProtected(TestResult.java:124)
> 	at org.springframework.osgi.test.internal.support.OsgiJUnitService.runTest(OsgiJUnitService.java:98)
> 	at org.springframework.osgi.test.internal.support.OsgiJUnitService.executeTest(OsgiJUnitService.java:71)
> 	at org.springframework.osgi.test.internal.support.OsgiJUnitService.runTest(OsgiJUnitService.java:45)
> 	at org.springframework.osgi.test.JUnitTestActivator.executeTest(JUnitTestActivator.java:73)
> 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> 	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
> 	at java.lang.reflect.Method.invoke(Method.java:585)
> 	at org.springframework.osgi.test.AbstractOsgiTests.invokeOSGiTestExecution(AbstractOsgiTests.java:397)
> 	at org.springframework.osgi.test.AbstractOsgiTests.runBare(AbstractOsgiTests.java:206)
> 	at org.springframework.osgi.test.AbstractOsgiTests$1.protect(AbstractOsgiTests.java:184)
> 	at junit.framework.TestResult.runProtected(TestResult.java:124)
> 	at org.springframework.osgi.test.AbstractOsgiTests.run(AbstractOsgiTests.java:181)
> 	at junit.framework.TestSuite.runTest(TestSuite.java:230)
> 	at junit.framework.TestSuite.run(TestSuite.java:225)
> 	at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
> 	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
> 	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
> 	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
> 	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
> 	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
> 
> 




直接junit测试该段代码没有问题，个人猜测又是可恶的classloader导致的。