Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.reflect.ParameterizedType #146

Closed
gedhi opened this issue Sep 23, 2015 · 13 comments
Labels
Milestone

Comments

@gedhi
Copy link

gedhi commented Sep 23, 2015

It happens when you have an object-field that is specialized with generics.
For example:

@ApiObject(group="foo")
public class FooPojo {
@ApiObjectField
private K fooField;
...
}

Same problem with collections and maps.

ASAP will release the HotFix.

Regards

@smcgregor83
Copy link

This defect appears to have regressed as of version 1.2.8. This can be worked around by rolling back to version 1.2.7.

@fabiomaffioletti
Copy link
Owner

Thank you. Would you be able to provide the object that generates the
exception?
Il 02/ott/2015 22:55, "smcgregor83" notifications@github.com ha scritto:

This defect appears to have regressed as of version 1.2.8. This can be
worked around by rolling back to version 1.2.7.


Reply to this email directly or view it on GitHub
#146 (comment)
.

@smcgregor83
Copy link

Yes certainly. It is the org.springframework.hateoas.Resources class that is causing problems.

@fabiomaffioletti
Copy link
Owner

Hopefully with the fix I just released this will be solved... version 1.2.9 will be synched in a few hours on maven central.

@smcgregor83
Copy link

Yep that seems to have done the trick. Nice turnaround!

piccarsa pushed a commit to gedhi/jsondoc that referenced this issue Nov 3, 2015
@vk77
Copy link

vk77 commented Nov 4, 2015

I'm getting same exception on both 1.2.9 and 1.2.7 versions.
That's the controllers configuration:

@RestController
@RequestMapping("/some/path")
public class MyController
    extends AbstractMyController<MyType> {

}
public abstract class AbstractMyController<ET extends AbstractMyType> {
...
}

public class MyType extends AbstractMyType{
...
}

There is a stacktrace:

java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.reflect.ParameterizedType
    at org.jsondoc.springmvc.scanner.AbstractSpringJSONDocScanner.buildJSONDocObjectsCandidates(AbstractSpringJSONDocScanner.java:127)
    at org.jsondoc.springmvc.scanner.AbstractSpringJSONDocScanner.buildJSONDocObjectsCandidates(AbstractSpringJSONDocScanner.java:104)
    at org.jsondoc.springmvc.scanner.AbstractSpringJSONDocScanner.buildJSONDocObjectsCandidates(AbstractSpringJSONDocScanner.java:127)
    at org.jsondoc.springmvc.scanner.AbstractSpringJSONDocScanner.jsondocObjects(AbstractSpringJSONDocScanner.java:145)
    at org.jsondoc.core.scanner.AbstractJSONDocScanner.getJSONDoc(AbstractJSONDocScanner.java:96)
    at org.jsondoc.springmvc.controller.JSONDocController.getApi(JSONDocController.java:70)
    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:497)
...

Should the issue be re-opened?

Thanks.

@piccarsa
Copy link
Contributor

piccarsa commented Nov 4, 2015

This commit should fix the problem that happens when one of the "candidate" objects is an istance of TypeVariable.

#166

You face this problem also when the return object of a method annotated with @RequestMapping is an instance of java.lang.reflect.TypeVariable . I leave here an example:

@ApiMethod(path = { "/rest/foocontroller/foos" }, description = "Your REST foo service")
@RequestMapping(value = "/foos", method = RequestMethod.GET)
@ApiResponseObject
@ResponseBody
public MyResponseBodyWrapper<FooBean> getFoos(){
   // service code here
}

Conforming to the java.lang.reflect.TypeVariable api documentation:

TypeVariable is the common superinterface for type variables of kinds.

while for java.lang.reflect.WildcardType is written:

WildcardType represents a wildcard type expression, such as ?, ? extends Number, or ? super Integer.

@seredkin
Copy link

versions 12.7..9
java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.reflect.ParameterizedType
at org.jsondoc.springmvc.scanner.AbstractSpringJSONDocScanner.buildJSONDocObjectsCandidates(AbstractSpringJSONDocScanner.java:104)

The class signature is

public class RepairOrderPlanController extends GenericController< RepairOrderPlan, RepairOrderPlanRepository >

@dcerecedo
Copy link

Similar issue here with JSONDoc 1.2.9. I have also tried 1.2.7 with same result.

We use a hierarchy of abstract controllers to centralize all common controller stuff. For example, at the bottom of the Hierarchy we have an abstract controller with all the logic for creating resources through POST:

public abstract class AbstractController<T extends Updatable<ID>, R extends ResourceSupport, ID extends Serializable> extends ReadAndUpdateAbstractController<T, R, ID> {

@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<R> create(@Valid @RequestBody R inputResource) throws Exception {
    T entity= getAssembler().convert(inputResource);
    T updated= getService().create(entity);
    R outputResource= getAssembler().toResource(updated);
    return new ResponseEntity<>(outputResource, HttpStatus.CREATED);
}
}

When invoking the /jsondoc endpoint, JSON Doc tries to analyze this class and I get:

java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.reflect.ParameterizedType
at org.jsondoc.springmvc.scanner.AbstractSpringJSONDocScanner.buildJSONDocObjectsCandidates(AbstractSpringJSONDocScanner.java:127)
....

@maximelebastard
Copy link

I've got the same issue. My controllers are using this method: https://gist.github.com/wvuong/5673644

I just added this to my pom.xml (in 1.2.6, 1.2.7, 1.2.8 and 1.2.9)

       <!-- JSON Doc -->
        <dependency>
            <groupId>org.jsondoc</groupId>
            <artifactId>spring-boot-starter-jsondoc</artifactId>
            <version>1.2.8</version>
        </dependency>

And I annotated my app with @EnableJsonDoc.

I added this packages to scan in my application.properties:

# JSON Doc
jsondoc.version=1.0
jsondoc.basePath=http://localhost:8080
jsondoc.packages[0]=com.bin.webservice.controller
jsondoc.packages[1]=com.bin.webservice.entity
jsondoc.playgroundEnabled=true
jsondoc.displayMethodAs=URI

I did not annotated any Controller or Model yet. The applications starts correctly, but when I access to http://localhost:8080/jsondoc, I get this stack trace:

2015-11-22 22:46:33.502 ERROR 8199 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.reflect.ParameterizedType] with root cause

java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.reflect.ParameterizedType
    at org.jsondoc.springmvc.scanner.AbstractSpringJSONDocScanner.buildJSONDocObjectsCandidates(AbstractSpringJSONDocScanner.java:104)
    at org.jsondoc.springmvc.scanner.AbstractSpringJSONDocScanner.jsondocObjects(AbstractSpringJSONDocScanner.java:145)
    at org.jsondoc.core.scanner.AbstractJSONDocScanner.getJSONDoc(AbstractJSONDocScanner.java:96)
    at org.jsondoc.springmvc.controller.JSONDocController.getApi(JSONDocController.java:70)
    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:497)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:775)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:858)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:85)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

PS: I do not have the error in 1.1.16

fabiomaffioletti added a commit that referenced this issue Nov 29, 2015
fixed regression on issue #146
@fabiomaffioletti
Copy link
Owner

I just merged a PR from @gedhi that should fix the problem. Please let me know if you are still having issues and in that case provide me with details and if possible a failing unit test. Thank you everyone :)

@bpsilva
Copy link

bpsilva commented Apr 15, 2016

I'm having the same problem using version 1.2.13. My question is: shouldn't the problem have ended since I'm using @ApiObject(show=false)?

@mfnunes
Copy link

mfnunes commented Aug 23, 2016

I'm having the same problem too. Using version: 1.2.16.
My controller is:
public abstract class QuestionGenericCRUDController <T extends Question, T1 extends QuestionDto>

and the error when I try to get jsondoc is:
java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.reflect.ParameterizedType

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

10 participants