Skip to content
Advertisement

ClassCastException being encountered even with correctly imported or assigned classes

I am receiving a ClassCastException randomly/intermittently. I already tried clearing and invalidating my local caches, doing an mvn clean install and depedency reimports. Still the issue persist. It also exist on the deployment of my microservice to aws. My directory structure is as follows. enter image description here

This BopAccountType (see screenshot) classes are the same with casa directory in terms of structure of folders but definitely in a different package as seen in this project structure. I also tried moving either of them to a new folder/package, still it doesnt stop refering a different class that I dont need it to. See error below.

enter image description here

Error text:

{
    "ErrorCode": "M.02.000",
    "Message": "class ph.com.bdo.api.casa.model.response.BopAccountTypes.BopAccountTypesCasa cannot be cast to class ph.com.bdo.api.creditcard.model.BopAccountTypesCreditCard (ph.com.bdo.api.casa.model.response.BopAccountTypes.BopAccountTypesCasa and ph.com.bdo.api.creditcard.model.BopAccountTypesCreditCard are in unnamed module of loader 'app')"
}

To debug the issue, I already deleted the entire package each of casa and credit card directories. Both of this two would function as designed and would not encounter this error if only either of them exist. But have this two directories together, and they would both intermittently work and not work. What I mean by this is the casa would work, and the credit card api would not work, or casa would not work but then credit card would then now work. ODDDD!!! And its the reason too that Its hard to debug the issue on my side and also the reason why I think the logical implementation of the code should at least be right as it works as designed, albeit intermittently. I already checked that the soutbound APIs are working fine either.

Is there a Spring boot configuration that has to be done for it to not confuse what classes to use or strictly use classes for the casa/credit card packages only and not refer to packages outside the directory?

I havent included a code full snippet as this doesnt seem to be a logical issue on either of the two package as they work fine independently if only one of them exist. Otherwise it may exist somewhere I dont know yet, like a pom file maybe?, can anyone help?. Thanks.

Below is a short snippet of my pom file.

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>11</java.version>
        <maven-checkstyle-plugin.version>3.1.1</maven-checkstyle-plugin.version>
        <checkstyle.version>8.36</checkstyle.version>
        <spotbugs-maven-plugin.version>4.0.4</spotbugs-maven-plugin.version>
        <maven-pmd-plugin.version>3.13.0</maven-pmd-plugin.version>
        <sonar-maven-plugin.version>3.7.0.1746</sonar-maven-plugin.version>
        <jacoco-maven-plugin.version>0.8.5</jacoco-maven-plugin.version>

        <!-- Base directory and package name for generated JOOQ code -->
        <jooq.generated.target.directory>src/main/java</jooq.generated.target.directory>
        <jooq.generated.target.package>jooq</jooq.generated.target.package>

        <!-- SonarQube Properties -->
        <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
        <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
        <sonar.jacoco.reportPaths>${basedir}/target/jacoco.exec</sonar.jacoco.reportPaths>
        <sonar.language>java</sonar.language>
        <sonar.java.sources>src/main/java</sonar.java.sources>
        <sonar.java.binaries>target/classes</sonar.java.binaries>
    </properties>

I read somewhere here that this could be caused by an issue in java version being used, hence I checked and its java 11 and also my project structure settings is also in Java 11 now too so its not there too.

Advertisement

Answer

For some reason, I was able to make my APIs work by removing the other two casa classes and just made casa refer to the creditcard classes since they both have the same exact implementation. Although this would make the casa package dependent on the creditcard package.

Basically this is what I did. Instead of importing this for the casa package

import ph.com.bdo.api.casa.model.BopAccountType;

I just had this class from a different package

import ph.com.bdo.api.creditcard.model.BopAccountType;

To make casa and creditcard apis work consistently without it having issues with regards to what clases to use that has the same implementation and function names except class names.

Any other inputs on how to resolve this other than the mentioned solution would be great.

ATM we have packages that should basically work independently of other packages. The reason why I have casa and creditcard packages.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement