How to use Genson provider as default in Jersey client? - java

I have simple client based on Jersey framework.
I want to use Genson as default JSON provider.
Firstly I created some class which implemented ContextResolver:
#Provider
public class GensonCustomResolver implements ContextResolver<Genson> {
private final Genson genson = new Genson.Builder().setSkipNull(true).setDateFormat(new UtcDateFormat()).create();
#Override
public Genson getContext(Class<?> type) {
return genson;
}
}
And UtcDateFormat class:
public class UtcDateFormat extends DateFormat {
#Override
public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
return new StringBuffer("" + date.getTime());
}
#Override
public Date parse(String source, ParsePosition pos) {
return new Date(Long.parseLong(source));
}
}
Then I used this:
ClientConfig cfg = new DefaultClientConfig(GensonCustomResolver.class);
Client client = Client.create(cfg);
Integer meetingId = 1;
Meeting result = client.resource("http://mywebservise.com/").path("meeting")
.path(meetingId.toString()).get(ClientResponse.class).getEntity(Meeting.class);
But, it failed. Here are log:
javax.ws.rs.WebApplicationException: com.owlike.genson.TransformationException: Could not deserialize to property 'created' of class class com.mypack.model.Meeting
at com.owlike.genson.ext.jaxrs.GensonJsonConverter.readFrom(GensonJsonConverter.java:127)
at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:565)
at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:517)
at com.mypack.ws.MeetingsTest.getItem(MeetingsTest.java:159)
at com.mypack.ws.MeetingsTest.getItem(MeetingsTest.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:659)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:845)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1153)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:771)
at org.testng.TestRunner.run(TestRunner.java:621)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:357)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:352)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:310)
at org.testng.SuiteRunner.run(SuiteRunner.java:259)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1199)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1124)
at org.testng.TestNG.run(TestNG.java:1032)
at org.gradle.api.internal.tasks.testing.testng.TestNGTestClassProcessor.stop(TestNGTestClassProcessor.java:110)
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.stop(SuiteTestClassProcessor.java:58)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
at com.sun.proxy.$Proxy2.stop(Unknown Source)
at org.gradle.api.internal.tasks.testing.worker.TestWorker.stop(TestWorker.java:113)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:355)
at org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:66)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
Caused by: com.owlike.genson.TransformationException: Could not deserialize to property 'created' of class class com.mypack.model.Meeting
at com.owlike.genson.reflect.PropertyMutator.couldNotDeserialize(PropertyMutator.java:56)
at com.owlike.genson.reflect.PropertyMutator.deserialize(PropertyMutator.java:39)
at com.owlike.genson.reflect.BeanDescriptor.deserialize(BeanDescriptor.java:116)
at com.owlike.genson.reflect.BeanDescriptor.deserialize(BeanDescriptor.java:98)
at com.owlike.genson.convert.NullConverter$NullConverterWrapper.deserialize(NullConverter.java:61)
at com.owlike.genson.Genson.deserialize(Genson.java:452)
at com.owlike.genson.ext.jaxrs.GensonJsonConverter.readFrom(GensonJsonConverter.java:125)
... 48 more
Caused by: com.owlike.genson.TransformationException: Could not parse date 1432800716000
at com.owlike.genson.convert.DefaultConverters$DateConverter.deserialize(DefaultConverters.java:824)
at com.owlike.genson.convert.DefaultConverters$DateConverter.deserialize(DefaultConverters.java:788)
at com.owlike.genson.convert.NullConverter$NullConverterWrapper.deserialize(NullConverter.java:61)
at com.owlike.genson.reflect.PropertyMutator.deserialize(PropertyMutator.java:37)
... 53 more
Caused by: java.text.ParseException: Unparseable date: "1432800716000"
at java.text.DateFormat.parse(DateFormat.java:357)
at com.owlike.genson.convert.DefaultConverters$DateConverter.read(DefaultConverters.java:830)
at com.owlike.genson.convert.DefaultConverters$DateConverter.deserialize(DefaultConverters.java:822)
... 56 more
What's wrong?
P.S. Jersey client 1.8
Genson 0.98

You should upgrade to the latest Genson version, there have been some improvements and providing a custom instance to jersey is easier.
In the newer versions, Genson will automatically detect if the input is the time in millis or a date string and deser it accordingly.
The dateformat registered with useDateFormat(...) will be used during deserialization, but this will also serialize the dates as strings. If you want to serialize them as a long (which is the default in the newever versions), you have define it with useDateAsTimestamp(true).
I hope this makes things a bit more clear. You can have a look at the config options for more details.

Related

Issue deserializing checkpoint info restarting JSR352 Job - ClassNotFoundException: [B

I have a partitioned job that reads from and writes to a DB. I have a chunking size of 10. If a partition fails and i want the restart to happen from the last commit, I have overwritten the checkpointinfo method and wrote my logic according to my application requirement. whenever i try to restart the failed job, i am getting an exception that there is failure in Read-Process-Write Loop
Here is the exception logs:
[3/1/18 19:18:09:004 IST] [process partition0] com.ibm.ws.batch.JobLogger CWWKY0030I: An exception occurred while running the step process.
com.ibm.jbatch.container.exception.BatchContainerRuntimeException: Failure in Read-Process-Write Loop
at com.ibm.jbatch.container.controller.impl.ChunkStepControllerImpl.invokeChunk(ChunkStepControllerImpl.java:704)
at com.ibm.jbatch.container.controller.impl.ChunkStepControllerImpl.invokeCoreStep(ChunkStepControllerImpl.java:795)
at com.ibm.jbatch.container.controller.impl.BaseStepControllerImpl.execute(BaseStepControllerImpl.java:293)
at com.ibm.jbatch.container.controller.impl.ExecutionTransitioner.doExecutionLoop(ExecutionTransitioner.java:118)
at com.ibm.jbatch.container.controller.impl.WorkUnitThreadControllerImpl.executeCoreTransitionLoop(WorkUnitThreadControllerImpl.java:93)
at com.ibm.jbatch.container.controller.impl.WorkUnitThreadControllerImpl.executeWorkUnit(WorkUnitThreadControllerImpl.java:155)
at com.ibm.jbatch.container.controller.impl.WorkUnitThreadControllerImpl$AbstractControllerHelper.runExecutionOnThread(WorkUnitThreadControllerImpl.java:480)
at com.ibm.jbatch.container.controller.impl.WorkUnitThreadControllerImpl.runExecutionOnThread(WorkUnitThreadControllerImpl.java:89)
at com.ibm.jbatch.container.util.BatchWorkUnit.run(BatchWorkUnit.java:117)
at com.ibm.ws.context.service.serializable.ContextualRunnable.run(ContextualRunnable.java:79)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: com.ibm.jbatch.container.exception.BatchContainerRuntimeException: java.lang.ClassNotFoundException: [B
at com.ibm.jbatch.container.util.TCCLObjectInputStream.resolveClass(TCCLObjectInputStream.java:40)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1620)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1521)
at java.io.ObjectInputStream.readArray(ObjectInputStream.java:1671)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
at java.io.ObjectInputStream.access$300(ObjectInputStream.java:208)
at java.io.ObjectInputStream$GetFieldImpl.readFields(ObjectInputStream.java:2182)
at java.io.ObjectInputStream.readFields(ObjectInputStream.java:543)
at java.math.BigInteger.readObject(BigInteger.java:4406)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:1058)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1909)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1808)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1353)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:373)
at java.util.HashMap.readObject(HashMap.java:1404)
at sun.reflect.GeneratedMethodAccessor57.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:1058)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1909)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1808)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1353)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:373)
at java.util.HashMap.readObject(HashMap.java:1404)
at sun.reflect.GeneratedMethodAccessor57.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:1058)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1909)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1808)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1353)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2018)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1942)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1808)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1353)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:373)
at com.ibm.jbatch.container.persistence.CheckpointData.<init>(CheckpointData.java:47)
at com.ibm.jbatch.container.persistence.jpa.StepThreadInstanceEntity.getCheckpointData(StepThreadInstanceEntity.java:131)
at com.ibm.jbatch.container.controller.impl.ChunkStepControllerImpl.openReaderAndWriter(ChunkStepControllerImpl.java:935)
at com.ibm.jbatch.container.controller.impl.ChunkStepControllerImpl.invokeChunk(ChunkStepControllerImpl.java:599)
... 14 more
Caused by: java.lang.ClassNotFoundException: [B
at com.ibm.ws.classloading.internal.UnifiedClassLoader.findClass(UnifiedClassLoader.java:119)
at com.ibm.ws.classloading.internal.ThreadContextClassLoader.findClass(ThreadContextClassLoader.java:120)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at com.ibm.ws.classloading.internal.UnifiedClassLoader.loadClass0(UnifiedClassLoader.java:107)
at com.ibm.ws.classloading.internal.UnifiedClassLoader$Delegation.loadClass(UnifiedClassLoader.java:78)
at com.ibm.ws.classloading.internal.UnifiedClassLoader.loadClass(UnifiedClassLoader.java:102)
at com.ibm.ws.classloading.internal.ThreadContextClassLoader.loadClass(ThreadContextClassLoader.java:136)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at com.ibm.jbatch.container.util.TCCLObjectInputStream.resolveClass(TCCLObjectInputStream.java:38)
... 57 more
[3/1/18 19:18:09:005 IST] [process partition0] com.ibm.ws.batch.JobLogger CWWKY0014W: Partition 0 failed with batch status FAILED and exit status null for step process for job instance 105,001 and job execution 105,008.
[3/1/18 19:18:15:588 IST] [process partition0] com.ibm.ws.batch.JobLogger
Here are my code snippet
Item Reader
#Override
public Serializable checkpointInfo() throws Exception {
Checkpoint checkpoint=new Checkpoint();
for (CheckpointProperty prop:dbOp.getCheckpointInfo().getProperty()) {
final Object result = ExprUtils.evaluate(newProps,prop.getCheckpointValue().toString());
checkpoint.addCheckPoint(prop.getName(), result); } return checkpoint;
}
Item writer
#Override
public Serializable checkpointInfo() throws Exception {
final Properties checkpointInfo = new Properties();
for (String key : checkpointProps.stringPropertyNames()) {
final String expr = checkpointProps.getProperty(key);
final Object result = ExprUtils.evaluate(dataMap, expr);
checkpointInfo.setProperty(key, result.toString());
}
return checkpointInfo;
}
Checkpoint class
----------------------
public class Checkpoint implements Serializable {
private HashMap<String, Object> checkpoints = new HashMap();
public void reset() {
this.checkpoints.clear();
}
public void addCheckPoint(String stream, Object position) {
this.checkpoints.put(stream, position);
}
public Object get(String stream) {
return this.checkpoints.get(stream);
}
public HashMap<String, Object> getCheckpoints() {
return this.checkpoints;
}
}
This is because of the BigInteger Datatype used in oracle.
This appears to be a bug in Liberty. Opened issue #2792.
I think it will happen with any array type, and that you could workaround by using an ArrayList in place of an array.
UPDATE: A fix for this issue was delivered in 18.0.0.2.

IllegalArgumentException in jmockit unit test

I'm getting an IllegalArgumentException in my #BeforeMethod method. I'm new to using jmockit so I'm unsure of the annotations. I've tried switching around the annotations but nothing seems to get rid of the error.
Test class:
public class CSVResultHandlerTest {
CSVResultHandler csvResultHandler;//Not using #Tested since it's not an empty constructor
#Injectable
CSVConfig csvConfig;
private final static Logger logger = LoggerFactory.getLogger(CSVResultHandler.class);
FileDetails fileDetails;
CSVConfig.BulkDataFileConfig bulkDataFileConfig;
#BeforeMethod
public void init() throws IOException, SecurityVaultException {
bulkDataFileConfig = CSVConfig.BulkDataFileConfig.CZ_DEF;
fileDetails = new FileDetails();
fileDetails.setIntervalBeginTimeStamp(Instant.parse("2017-05-06T13:13:38.611Z"));
fileDetails.setIntervalEndTimeStamp(Instant.parse("2017-05-06T13:13:38.611Z"));
fileDetails.setPublicationNumber(12345);
fileDetails.setVersionNumber(1);
csvConfig = new CSVConfig(bulkDataFileConfig, fileDetails);
csvResultHandler = new CSVResultHandler(csvConfig);//this is the line getting the error
}
#Test
public void IOExceptionHandled() throws IOException {
IOException e = new IOException();
new Expectations() {{
csvConfig.getFileUrl(); result = new IOException("Mock Exception");
}};
csvResultHandler.open();
new Verifications() {{
logger.error("Exception thrown while putting file {}",csvConfig.getFileUrl(), e);
}};
}
}
CSVResultHandler has a constructor that takes a CSVConfig object as the only parameter. I know I'm missing something stupid/simple, just not sure what it is.
Stacktrace:
java.lang.IllegalArgumentException: PBOX00009: Invalid vaultString format: null
at org.jboss.security.vault.SecurityVaultUtil.getValue(SecurityVaultUtil.java:89)
at org.jboss.security.vault.SecurityVaultUtil.getValueAsString(SecurityVaultUtil.java:107)
at com.org.m2m.bulk.data.security.VaultUtil.getWebDavPwd(VaultUtil.java:13)
at com.org.m2m.bulk.data.fs.BulkDataFileSystemFactory.newWebDavFileSystem(BulkDataFileSystemFactory.java:25)
at com.org.m2m.bulk.interval.data.csv.CSVResultHandler.<init>(CSVResultHandler.java:38)
at com.org.m2m.bulk.interval.data.csv.CSVResultHandlerTest.init(CSVResultHandlerTest.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:514)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:215)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:589)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:782)
at org.testng.TestRunner.run(TestRunner.java:632)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
at org.testng.TestNG.run(TestNG.java:1064)
at org.gradle.api.internal.tasks.testing.testng.TestNGTestClassProcessor.runTests(TestNGTestClassProcessor.java:129)
at org.gradle.api.internal.tasks.testing.testng.TestNGTestClassProcessor.stop(TestNGTestClassProcessor.java:88)
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.stop(SuiteTestClassProcessor.java:61)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
at com.sun.proxy.$Proxy3.stop(Unknown Source)
at org.gradle.api.internal.tasks.testing.worker.TestWorker.stop(TestWorker.java:120)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:147)
at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:129)
at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:404)
at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
at org.gradle.internal.concurrent.StoppableExecutorImpl$1.run(StoppableExecutorImpl.java:46)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

JMockit throws "No class with name java_lang_System$currentTimeMillis" when used from Groovy

A test that works in a .java file throws an exception when put into a .groovy file. Why is that?
Groovy: 2.3.9
JMockit: 1.20
Java: 1.8.0_60
The code:
#Test
public void testSystemCurrentTimeMillis(#Mocked final System unused) {
new NonStrictExpectations() {{
System.currentTimeMillis(); result = 1438357206679L;
}};
long currentTime = System.currentTimeMillis();
Assert.assertEquals(1438357206679L, currentTime);
}
The exception:
java.lang.IllegalArgumentException: No class with name "java_lang_System$currentTimeMillis" found
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112)
at x.GroovyTest$1.<init>(GroovyTest.groovy:12)
at x.GroovyTest.testSystemCurrentTimeMillis(GroovyTest.groovy:11)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

"duplicate class definition bug occured" using jaxb in jboss

I'm using jaxb inside a jboss 5.0.1GA container, and seeing a strange exception at startup. It appears once only.
From the stacktrace you can see that it is during the class initialisation of FrameworkUtil class.
FrameworkUtil line 119 looks like this:
JAXBContext j = JAXBContext.newInstance(Validate.class, Response.class ....<more classes>);
Validate.java has this in it:
#XmlRootElement(name="validate")
public class Validate {
List<String> userGroups;
#XmlElementWrapper(name="userGroups")
#XmlElement(name="item")
public String[] getUserGroups() {
if (userGroups != null) {
return userGroups.toArray(new String[userGroups.size()]);
}
else return null;
}
public void setUserGroups(String[] userGrps) {
userGroups = new ArrayList<String>();
if (userGrps != null) {
for (String userGrp : userGrps) {
userGroups.add(new String(userGrp));
}
}
}
}
In the stack trace is a reference to Validate. But sometimes the stacktrace is different, and refers to the same field (userGroups) in Response.class instead.
Here's the stack trace:
Mar 19, 2015 11:20:50 AM com.sun.xml.bind.v2.runtime.reflect.opt.Injector inject
WARNING: duplicate class definition bug occured? Please report this : com/mycompany/ecommerce/message/beans/Validate$JaxbAccessorM_getUserGroups_setUserGroups_[Ljava_lang_String;
java.lang.ClassFormatError: Illegal class name "com/mycompany/ecommerce/message/beans/Validate$JaxbAccessorM_getUserGroups_setUserGroups_[Ljava_lang_String;" in class file com/mycompany/ecommerce/messa
ge/beans/Validate$JaxbAccessorM_getUserGroups_setUserGroups_[Ljava_lang_String;
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.lang.ClassLoader.defineClass(ClassLoader.java:643)
at sun.reflect.GeneratedMethodAccessor209.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.sun.xml.bind.v2.runtime.reflect.opt.Injector.inject(Injector.java:205)
at com.sun.xml.bind.v2.runtime.reflect.opt.Injector.inject(Injector.java:85)
at com.sun.xml.bind.v2.runtime.reflect.opt.AccessorInjector.prepare(AccessorInjector.java:89)
at com.sun.xml.bind.v2.runtime.reflect.opt.OptimizedAccessorFactory.get(OptimizedAccessorFactory.java:114)
at com.sun.xml.bind.v2.runtime.reflect.Accessor$GetterSetterReflection.optimize(Accessor.java:369)
at com.sun.xml.bind.v2.runtime.property.ArrayProperty.<init>(ArrayProperty.java:65)
at com.sun.xml.bind.v2.runtime.property.ArrayERProperty.<init>(ArrayERProperty.java:84)
at com.sun.xml.bind.v2.runtime.property.ArrayElementProperty.<init>(ArrayElementProperty.java:96)
at com.sun.xml.bind.v2.runtime.property.ArrayElementLeafProperty.<init>(ArrayElementLeafProperty.java:66)
at sun.reflect.GeneratedConstructorAccessor172.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at com.sun.xml.bind.v2.runtime.property.PropertyFactory.create(PropertyFactory.java:124)
at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.<init>(ClassBeanInfoImpl.java:179)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:515)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:330)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1140)
at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:154)
at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:121)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:248)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:235)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:432)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:637)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:584)
at com.mycompany.global.er.util.FrameworkUtil.<clinit>(FrameworkUtil.java:119)
Did you check this bug out ?
Seems to be fixed in version 2.2.11 of jaxb implementation.
Maybe you could try and replace jaxb JAR files, they're probably in you server's lib, unless your webapp overwrites them in it's own lib folder.
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.11</version>
</dependency>

Correct Way to Reference a Serialized Class in Scala

I am trying to reference another class that has been serialized in Scala and I am new to Java in general. Here is the code sample I am having problems with
import scala.io.Source
import java.io._
class testcl extends Serializable {
val teststr = "serializestring"
}
class revivetest {
val file = "file.serial"
val fis = new FileInputStream(file)
val ois = new ObjectInputStream(fis)
val revived = ois.readObject.asInstanceOf[testcl]
}
Here is the behavior when I load the class through the REPL:
scala> val test = new revivetest
java.lang.ClassNotFoundException: testcl
at scala.tools.nsc.interpreter.AbstractFileClassLoader.findClass(AbstractFileClassLoader.scala:83)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:270)
at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:625)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1612)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1517)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1350)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
at revivetest.(:26)
at .(:20)
at .()
at .(:7)
at .()
at $print()
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:734)
at scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:983)
at scala.tools.nsc.interpreter.IMain.loadAndRunReq$1(IMain.scala:573)
at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:604)
at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:568)
at scala.tools.nsc.interpreter.ILoop.reallyInterpret$1(ILoop.scala:760)
at scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:805)
at scala.tools.nsc.interpreter.ILoop.command(ILoop.scala:717)
at scala.tools.nsc.interpreter.ILoop.processLine$1(ILoop.scala:581)
at scala.tools.nsc.interpreter.ILoop.innerLoop$1(ILoop.scala:588)
at scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:591)
at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply$mcZ$sp(ILoop.scala:882)
at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:837)
at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:837)
at scala.tools.nsc.util.ScalaClassLoader$.savingContextLoader(ScalaClassLoader.scala:135)
at scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:837)
at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:83)
at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:96)
at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:105)
at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
Question: What is the correct way to reference another class that has been serialized in order to avoid the java.lang.ClassNotFoundException?

Categories