Maven compilation error using Java Generics

I'm using Java 8 and has 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 great when I run the web application in Eclipse and embedded Tomcat. There is also no problem in the Problems with Eclipse view.

But when im starting to build maven from my shell / bash im getting the following error:

[ERROR] COMPILATION ERROR: [INFORMATION] ----------------------------------------- ------ --------------

[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 anyone have an idea what I am doing wrong? The maven compiler plugin seems to be updated and my JAVA_HOME is pointing to Java 8 version.

Snippet from 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>

      

Java.version config - 1.8; Maven - 3.0.4.

+3


source to share


2 answers


I am getting the same error as you. But the following code fixes it. I made the code a method instead of a constructor, but I think you can ignore that, huh ??



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");
    }
}

      

+4


source


It works with objects instead of primitive types.



+1


source







All Articles