Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven compile error with using Java Generics

Im using Java 8 and have the following code:

public WeatherDTO(Map<?,?> mappedJsonData) {
    if (mappedJsonData == null || mappedJsonData.isEmpty()) {
        return;
    }

    List<?> weather = (List<?>)mappedJsonData.get("weather");
    if (weather != null && !weather.isEmpty()) {
        this.weather = (String) ((Map<?,?>)weather.get(0)).get("description");
    }

    Map<?,?> jsonMain = (Map<?,?>)mappedJsonData.get("main");
    if (jsonMain != null && !jsonMain.isEmpty()) {
        this.temperature = (double)jsonMain.get("temp") - 273.0;
        this.humidity = (int)jsonMain.get("humidity");
    }
}

This code works fine when im running the web app within Eclipse and embedded Tomcat. There is also no Problem shown in Eclipse Problems view.

But when im starting a maven compile from my shell/bash im getting the following error:

[ERROR] COMPILATION ERROR : [INFO] -------------------------------------------------------------

[ERROR] /home/xxx/xxx/xxx/src/main/java/xxx/xxx/xxx/weather/WeatherDTO.java:[52,64] incompatible types: capture#1 of ? cannot be converted to double

[ERROR] /home/xxx/xxx/xxx/src/main/java/xxx/xxx/xxx/weather/WeatherDTO.java:[53,58] incompatible types: capture#2 of ? cannot be converted to int

Does anybody have a idea what im doing wrong? The maven compiler plugin seems to be up to date and my JAVA_HOME points to a Java 8 version.

Snippet from the pom.xml

        <plugin>
            <inherited>true</inherited>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
                <encoding>${project.build.sourceEncoding}</encoding>
                <showWarnings>false</showWarnings>
                <showDeprecation>false</showDeprecation>
            </configuration>
        </plugin>

Configuration of java.version is 1.8; Maven is 3.0.4.

like image 579
StephanM Avatar asked Mar 16 '23 04:03

StephanM


1 Answers

I get the same error as you. But the following code fixes it. I've made the code a method instead of a constructor, but I think you can ignore that, eh?

private String weather;
private double temperature;
private int humidity;
public void WeatherDTO(Map<?, ?> mappedJsonData) {
    if (mappedJsonData == null || mappedJsonData.isEmpty()) {
        return;
    }

    List<?> weather = (List<?>) mappedJsonData.get("weather");
    if (weather != null && !weather.isEmpty()) {
        this.weather = (String) ((Map<?, ?>) weather.get(0)).get("description");
    }

    Map<?, ?> jsonMain = (Map<?, ?>) mappedJsonData.get("main");
    if (jsonMain != null && !jsonMain.isEmpty()) {
        this.temperature = (Double) jsonMain.get("temp") - 273.0;
        this.humidity = (Integer) jsonMain.get("humidity");
    }
}
like image 159
johnstosh Avatar answered Mar 24 '23 07:03

johnstosh