Peter Penzov
Peter Penzov

Reputation: 1740

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

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?

Upvotes: -3

Views: 1160

Answers (4)

Hung Nguyen The
Hung Nguyen The

Reputation: 91

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

Upvotes: 1

Rob Spoor
Rob Spoor

Reputation: 9175

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<?>.

Upvotes: 1

aristotll
aristotll

Reputation: 9175

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.

Upvotes: 1

markspace
markspace

Reputation: 11030

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

Upvotes: 1

Related Questions