-3

I have this Java method which his used to compare data:

org.apache.commons.lang3.builder.Diff;

public void addChangedPositions(DiffrentResult diffrentResult , List<UpdatedPositionsData> updatedPositionsData) {
    for (Diff<?> diff : diffResult.getDiffs()) {
      UpdatedPositionsData updatedData = new UpdatedPositionsData();
      updatedData.setName(diff.getFieldName() == null ? null : diff.getFieldName());
      updatedData.setOldValue(diff.getLeft() == null ? null : diff.getLeft().toString());
      updatedData.setNewValue(diff.getRight() == null ? null : diff.getRight().toString());
      updatedPositionsData.add(updatedField);
    }    
  }
........

@Getter
@Setter
public class UpdatedPositionsData {

  private String name;
  private String oldValue;
  private String newValue;

}

But for this line for (Diff<?> diff : diffResult.getDiffs()) {

I get error:

incompatible types: Object cannot be converted to Diff<?>

So I have:

Required type:
Object

Provided:
Diff
<?>

Do you know how I can fix this issue?

P.S unfortunately the problem is not solved.

I created this small example code:

https://github.com/rcbandit111/hateos_poc/blob/main/src/main/java/com/hateos/test/assembler/CodeLogAssembler.java#L14

Can you advise how can be fixed, please?

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808
  • 1
    What does getDiffs() return? – Thorbjørn Ravn Andersen Feb 17 '22 at 14:15
  • are you sure that the parameter DiffrentResult diffrentResult is correct? – dcolazin Feb 17 '22 at 14:16
  • @ThorbjørnRavnAndersen it's returning `public List> getDiffs() {` – Peter Penzov Feb 17 '22 at 14:23
  • 7
    Please provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). The code you have shown us is incomplete, and markspace's attempt to reconstruct a complete example that reproduces your problem has a failed. We can't advise a solution if we don't have sufficient information to understand what the problem is. – Stephen C Feb 19 '22 at 23:56
  • the issue it probably with the declaration of `DiffrentResult.getDiffs()` you should declare it as: `public List> getDiffs()` (replace `SomeType` with whatever you are returning) – ezer Feb 21 '22 at 08:53
  • Ummm.... I see `void addChangedPositions(DiffrentResult diffrentResult, ...` but `for (Diff> diff : diffResult.getDiffs())` - `diffrentResult` vs `diffResult`... Is that a typo or is `diffResult` declared in an enclosing context? This is why you MUST reduce the issue to a [mcve] (or ensure that what you post reflects the actual code). – Jim Garrison Feb 22 '22 at 01:26

4 Answers4

1

So this works for me when I try it. Check you have the right library imported, do a clean and re-build of your project, etc. Something funny going on here.

public class ListReturnType<X> {
   
   public static void main( String[] args ) {
      for( ListReturnType<?> x : getList() ) {
         
      }
   }
   
   public static List<ListReturnType<?>> getList() {
      return null;
   }
}

Check the docs, though they seem to match.

https://commons.apache.org/proper/commons-lang/apidocs/index.html?org/apache/commons/lang3/builder/DiffResult.html

markspace
  • 10,621
  • 3
  • 25
  • 39
1
import lombok.*;
import org.apache.commons.lang3.builder.Diff;

import java.util.List;

@Getter
@Setter
public class UpdatedPositionsData {

    private String name;
    private String oldValue;
    private String newValue;

    public void addChangedPositions(DiffrentResult diffResult, List<UpdatedPositionsData> updatedPositionsData) {
        for (Diff<?> diff : diffResult.getDiffs()) {
            UpdatedPositionsData updatedData = new UpdatedPositionsData();
            updatedData.setName(diff.getFieldName() == null ? null : diff.getFieldName());
            updatedData.setOldValue(diff.getLeft() == null ? null : diff.getLeft().toString());
            updatedData.setNewValue(diff.getRight() == null ? null : diff.getRight().toString());
            updatedPositionsData.add(updatedData);
        }
    }

    public interface DiffrentResult {
        List<Diff<?>> getDiffs();
    }
}

The code compiles without any problem, so I do not think diffResult.getDiffs returns a list of Diff<?>> in your code.

aristotll
  • 8,694
  • 6
  • 33
  • 53
1

If you have a generic type, like DiffResult, but you omit its generic type in the declaration, then all methods lose their generic information. In other words, if you declare the field, variable or parameter as DiffResult instead of DiffResult<WhatEverType>, then its getDiffs() method doesn't return List<Diff<?>> but instead List - without any generic information.

This can be shown with a very easy example:

class MyClass<T> {
    List<String> strings() {
        return List.of("a", "b", "c");
    }
}
MyClass m = new MyClass(); // raw type warning ignored
List<String> strings = m.strings(); // compiler warning
for (String s : m.strings()) { // compiler error, like the one you're getting
}

So my guess is, you've omitted the generic type in DiffResult. If so, the fix is simple: add a generic type, or if you don't know or care, use the wildcard: DiffResult<?>.

Rob Spoor
  • 6,186
  • 1
  • 19
  • 20
1

More specified input params (DiffResult<?> diffResult). I tested without complie error.

import java.util.List;

import org.apache.commons.lang3.builder.Diff;
import org.apache.commons.lang3.builder.DiffResult;

public class CodeLogAssembler {

  private void addNewUpdatedFields(DiffResult<?> diffResult, List<UpdatedFieldsResource> updatedFields) {
    for (Diff<?> diff : diffResult.getDiffs()) {
      UpdatedFieldsResource updatedField = new UpdatedFieldsResource();
      updatedField.setName(diff.getFieldName() == null ? null : diff.getFieldName());
      updatedField.setOldValue(diff.getLeft() == null ? null : diff.getLeft().toString());
      updatedField.setNewValue(diff.getRight() == null ? null : diff.getRight().toString());
      updatedFields.add(updatedField);
    }
  }
}