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 #7

Open
ghost opened this issue Dec 12, 2017 · 7 comments
Open

ClassCastException #7

ghost opened this issue Dec 12, 2017 · 7 comments

Comments

@ghost
Copy link

ghost commented Dec 12, 2017

Hello again,

I am having a different issue now.

Same bean

@Data
@Document(collection = "beans", schemaVersion = "1.0")
public class Bean {
  @Id
  private String id;
  
  private List<String> stuff;

  public Bean() {
    this.id = UUID.randomUUID().toString();
  }
}

and database class

public class Database {
 private final JsonDBTemplate jsonDBTemplate = new JsonDBTemplate...

  public void a() {
   List<Bean> all = jsonBDTemplate.findAll(Bean.class);
   for (Bean bean : all) {
     System.out.println(bean);
   }
 }
}

I also have a Spring Controller

@RestController
@RequestMapping("/test")
public class TestController {
  @Autowired
  private Database database;

  @RequestMapping("/a")
  public void invokeA() {
    database.a();
  }
}

So when I call http://localhost/test/a I get

java.lang.ClassCastException: Bean cannot be cast to Bean
  at Database.a() ~[]
  at TestController.invokeA() ~[]
  at sun.reflect .....

where the at Database.a() points to the line for (Bean bean : all) .

I was looking online and people suggest that somehow I may have several class loaders and somehow I may be casting classes from different class loaders. Have you ever had this, and how do I obtain more information for debug?

Spring boot starter parent: 1.5.8.RELEASE
Version: 1.0.64

@ghost
Copy link
Author

ghost commented Dec 12, 2017

Ok, I have found out the root cause of the issue. It happened because I used spring-boot-devtools which would restart the application every time you save a file. So the devtools artifact has its own class loader that pretty much makes all the objects during the development, instead of sun.misc.Launcher$AppClassLoader.

Would be great to know if there is a way to make it work with the devtools. Not a priority though.

@FarooqKhan
Copy link
Collaborator

Hi,

Sorry could not get to studying the problem, been busy with work. Just wanted to mention As far as I remember have not used any custom classloader in JsonDB.

I do know that there is a user who has used JsonDB with a lot of custom classloader code and it works fine.

@FarooqKhan
Copy link
Collaborator

I have not used spring-boot-devtools myself, but i will see if i can try it out over the weekend.

@FarooqKhan
Copy link
Collaborator

If you suspect classloaders are causing this, then what if you saved the classloader used at time of instantiation of JsonDBTemplate
final ClassLoader cached = Thread.currentThread().getContextClassLoader();
And then before you run your for each loop set the cached loader
Thread.currentThread().setContextClassLoader(cached)

Would this maybe solve your problem

@ghost
Copy link
Author

ghost commented Dec 17, 2017

I think that fiddling with class loaders just for the development environment is not worth it. I have disabled boot-dev, and I think I am fine with that.
Is there a way to integrate with that framework though?

@vince-bickers
Copy link

@oiale @FarooqKhan @SJKA Please check: https://github.com/vince-bickers/spring-data-jsondb. It's still WIP, but basic Repository and REST integration is working.

@FarooqKhan
Copy link
Collaborator

@vince-bickers for creating this let me know when it is complete I can add this detail to the webpage and provide links to your repository. Optionally if you are willing we can move this under the jsondb organization on github you can still own it.

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

No branches or pull requests

2 participants