java - how to create instance from an inmutable objects? -
i trying create new instance inmutable object. in case, list, , getting error:
java.lang.instantiationexception: java.util.collections$unmodifiablerandomaccesslist @ java.lang.class.newinstance(class.java:364) @ com.example.movies.domain.transformer.domainentitytransformer.transformentityarray(domainentitytransformer.java:32) @ com.example.movies.domain.service.client.impl.clientserviceimpl.getclients(clientserviceimpl.java:58) @ com.example.movies.service.controller.clientcontroller.getclients(clientcontroller.java:50) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:606) @ org.springframework.web.method.support.invocablehandlermethod.doinvoke(invocablehandlermethod.java:221) @ org.springframework.web.method.support.invocablehandlermethod.invokeforrequest(invocablehandlermethod.java:137) @ org.springframework.web.servlet.mvc.method.annotation.servletinvocablehandlermethod.invokeandhandle(servletinvocablehandlermethod.java:110) @ org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter.invokehandlemethod(requestmappinghandleradapter.java:777) @ org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter.handleinternal(requestmappinghandleradapter.java:706) @ org.springframework.web.servlet.mvc.method.abstracthandlermethodadapter.handle(abstracthandlermethodadapter.java:85) @ org.springframework.web.servlet.dispatcherservlet.dodispatch(dispatcherservlet.java:943) @ org.springframework.web.servlet.dispatcherservlet.doservice(dispatcherservlet.java:877) @ org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet.java:966) @ org.springframework.web.servlet.frameworkservlet.doget(frameworkservlet.java:857) @ javax.servlet.http.httpservlet.service(httpservlet.java:618) @ org.springframework.web.servlet.frameworkservlet.service(frameworkservlet.java:842) @ javax.servlet.http.httpservlet.service(httpservlet.java:725) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:291) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:206) @ org.apache.tomcat.websocket.server.wsfilter.dofilter(wsfilter.java:52) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:239) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:206) @ org.springframework.web.filter.hiddenhttpmethodfilter.dofilterinternal(hiddenhttpmethodfilter.java:77) @ org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:239) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:206) @ org.springframework.web.filter.characterencodingfilter.dofilterinternal(characterencodingfilter.java:88) @ org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:239) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:206) @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:219) @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:106) @ org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:501) @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:142) @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:79) @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:88) @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:516) @ org.apache.coyote.http11.abstracthttp11processor.process(abstracthttp11processor.java:1086) @ org.apache.coyote.abstractprotocol$abstractconnectionhandler.process(abstractprotocol.java:659) @ org.apache.coyote.http11.http11nioprotocol$http11connectionhandler.process(http11nioprotocol.java:223) @ org.apache.tomcat.util.net.nioendpoint$socketprocessor.dorun(nioendpoint.java:1558) @ org.apache.tomcat.util.net.nioendpoint$socketprocessor.run(nioendpoint.java:1515) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1145) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:615) @ org.apache.tomcat.util.threads.taskthread$wrappingrunnable.run(taskthread.java:61) @ java.lang.thread.run(thread.java:745)
this transformer class exception has been thrown
public collection<dom> transformentityarray(collection<ent> iterable) { try { collection<dom> transformed = iterable.getclass().newinstance(); iterator<ent> iterator = iterable.iterator(); while (iterator.hasnext()) { ent element = iterator.next(); transformed.add(this.transformentity(element)); } return transformed; } catch (exception e) { e.printstacktrace(); return null; } }
this implementation
page<client> clientsentity = this.dao.findall(new pagerequest(offset, limit)); list<clientdomain> clientsdomain = (list<clientdomain>) this.transformer.transformentityarray(clientsentity .getcontent());
do know people how fix it? regards!
yes,
transformed = new arraylist<>();
and (even though makes no sense in case), can create inmutable objects calling appropriete constructor necessary parameters set state of object.
in case unmodifiablerandomaccesslist(list<? extends e> list)
, can access getclass().getconstructor...
(and still need temporary arraylist store results).
but cannot provide generic solution work input collection, need know contructor call == know java code of actual call parameters. renders using genereirc , interfaces pointless.
so create arbitrary chosen collection (arraylist) not affect further parts of code.
Comments
Post a Comment