0

I've got an Object, that is an exact replica of the custom object I'm trying to cast it to. I'm grabbing the Object from a RedisTemplate (As seen here Java redis repository not working as expected)

I've debugged it to the point where my hashoperation is getting the value from redis...It returns the JwtToken class which I inputted in the first place. Here is a screenshot of the breakpoint, you can see that the hashoperation has found the JwtToken instance.

Screenshot of breakpoint

I'm testing to see if it's an instanceof JwtToken, which returns false, I then try run the exact same hashop.get method and cast it straight to the JwtToken class. It throws the following error:

java.lang.ClassCastException: com.userapi.spring.model.JwtToken cannot be cast to com.userapi.spring.model.JwtToken
    at com.userapi.spring.service.JwtTokenServiceImpl.findByUser(JwtTokenServiceImpl.java:48) ~[classes/:na]
    at com.userapi.spring.security.JwtFilter.doFilter(JwtFilter.java:62) ~[classes/:na]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) ~[tomcat-embed-core-8.0.33.jar:8.0.33]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) ~[tomcat-embed-core-8.0.33.jar:8.0.33]
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) ~[tomcat-embed-core-8.0.33.jar:8.0.33]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) ~[tomcat-embed-core-8.0.33.jar:8.0.33]
    at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:87) ~[spring-web-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) ~[tomcat-embed-core-8.0.33.jar:8.0.33]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) ~[tomcat-embed-core-8.0.33.jar:8.0.33]
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77) ~[spring-web-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) ~[tomcat-embed-core-8.0.33.jar:8.0.33]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) ~[tomcat-embed-core-8.0.33.jar:8.0.33]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121) ~[spring-web-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) ~[tomcat-embed-core-8.0.33.jar:8.0.33]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) ~[tomcat-embed-core-8.0.33.jar:8.0.33]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212) ~[tomcat-embed-core-8.0.33.jar:8.0.33]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) [tomcat-embed-core-8.0.33.jar:8.0.33]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) [tomcat-embed-core-8.0.33.jar:8.0.33]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141) [tomcat-embed-core-8.0.33.jar:8.0.33]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) [tomcat-embed-core-8.0.33.jar:8.0.33]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) [tomcat-embed-core-8.0.33.jar:8.0.33]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522) [tomcat-embed-core-8.0.33.jar:8.0.33]
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095) [tomcat-embed-core-8.0.33.jar:8.0.33]
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672) [tomcat-embed-core-8.0.33.jar:8.0.33]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1502) [tomcat-embed-core-8.0.33.jar:8.0.33]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1458) [tomcat-embed-core-8.0.33.jar:8.0.33]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_05]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_05]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.0.33.jar:8.0.33]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_05]

Here is what my JwtToken class looks like:

import java.io.Serializable;

/**
 * Created by james on 29/06/2016.
 */
public class JwtToken implements Serializable {
    private String token;
    private int userId;
    // #TODO add in more fields...


    public JwtToken(String token, int userId) {
        this.token = token;
        this.userId = userId;
    }

    public JwtToken() {

    }

    public String getToken() {
        return token;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public void setToken(String token) {

        this.token = token;
    }

    @Override
    public String toString() {
        return "JwtToken{" +
                "token='" + token + '\'' +
                ", userId=" + userId +
                '}';
    }
}
Community
  • 1
  • 1
James111
  • 15,378
  • 15
  • 78
  • 121
  • If it's serializable, the serial ids might not match, so it believes they're incompatible. – Carcigenicate Jun 30 '16 at 00:35
  • see if this helps ya: http://stackoverflow.com/questions/2371967/java-getting-class-cast-exception-where-both-classes-are-exactly-the-same – Joel Min Jun 30 '16 at 00:37
  • @Carcigenicate If it was a `serialVersionUID` problem it would throw `InvalidClassException`. – user207421 Jun 30 '16 at 00:44
  • It looks like the classes have been loaded with different class loaders. You could check this by printing or inspecting `oo.getClass().getClassLoader()` and `JwtToken.class.getClassLoader()`. – clstrfsck Jun 30 '16 at 00:52
  • @msandiford - `JwtToken` is loading `RestartClassLoader` while `oo` is using `Launcher$AppClassLoader`. Would I just do `Thread.currentThread().setContextClassLoader(RestartClassLoader);`? This just throws a compilation error. – James111 Jun 30 '16 at 00:59
  • I don't really know enough about the system setup to know how to get the right classloader used. I'm assuming `RestartLoader` is something that tomcat uses. `Launcher$AppClassLoader` is likely the default JVM class loader. Somehow you need to convince the internals of `hashOps` to load classes using the `RestartLoader` from tomcat. Another option would be to create a new object using serialization. – clstrfsck Jun 30 '16 at 01:24

0 Answers0