I'm getting
incompatible types; inferred type argument(s) java.lang.Object do not conform to bounds of type variable(s) T
[ERROR] found : <T>int
[ERROR] required: int
[ERROR] -> [Help 1]
for my method
private <T extends Comparable<T>> int useComparator(final Object o1, final Object o2, final String property) {
final T s1 = new PropertyModel<T>(o1, property).getObject();
final T s2 = new PropertyModel<T>(o2, property).getObject();
return s1.compareTo(s2);
}
where PropertyModel
is org.apache.wicket.model.PropertyModel
, but in fact this is not important much (this class simply returns value of some field, btw: is there some class with similar function in Spring for example?).
Problem is at line calling that method:
return this.useComparator(o1, o2, sortProperty);
I can fix this by
return this.<Comparable> useComparator(o1, o2, sortProperty);
but I'm wondering..I have no problem when compiling such code in Eclipse and NetBeans. As far as I can see I'm using same java, but in Maven I'm getting the error from the beggining of the post. Is there some compiler flag that I missed?
Not sure if that helps, but my Maven version is:
> mvn -version
Apache Maven 3.0.4 (r1232337; 2012-01-17 09:44:56+0100)
Maven home: c:\Programs\apache-maven-3.0.4
Java version: 1.6.0_37, vendor: Sun Microsystems Inc.
Java home: c:\Java\jdk1.6.0_37\jre
Default locale: sk_SK, platform encoding: Cp1250
OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"
Maven compiler plugin setting from pom.xml
:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<version>2.5.1</version>
<inherited>true</inherited>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
edit: requested code added
Create the wicket project (just for dependencies). I used Wicket Quickstart page to generate maven project. I used:
mvn archetype:generate -DarchetypeGroupId=org.apache.wicket -DarchetypeArtifactId=wicket-archetype-quickstart -DarchetypeVersion=6.6.0 -DgroupId=net.betlista -DartifactId=stackoverflow -DarchetypeRepository=https://repository.apache.org/ -DinteractiveMode=false
Import the project to your favorite IDE.
Use this JUnit test to simulate the problem:
package net.betlista;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import junit.framework.Assert;
import org.apache.wicket.extensions.markup.html.repeater.util.SortParam;
import org.apache.wicket.model.PropertyModel;
import org.junit.Test;
public class ComparatorTest {
@Test
public void test() {
final ArrayList<Person> persons = new ArrayList<Person>();
final Person alice = new Person("Alice", 30);
persons.add(alice);
final Person bob = new Person("Bob", 20);
persons.add(bob);
Collections.sort(persons, new PropertyComparator(new SortParam<String>("name", true)));
Assert.assertEquals(alice, persons.get(0));
Assert.assertEquals(bob, persons.get(1));
Collections.sort(persons, new PropertyComparator(new SortParam<String>("name", false)));
Assert.assertEquals(alice, persons.get(1));
Assert.assertEquals(bob, persons.get(0));
Collections.sort(persons, new PropertyComparator(new SortParam<String>("age", true)));
Assert.assertEquals(alice, persons.get(1));
Assert.assertEquals(bob, persons.get(0));
Collections.sort(persons, new PropertyComparator(new SortParam<String>("age", false)));
Assert.assertEquals(alice, persons.get(0));
Assert.assertEquals(bob, persons.get(1));
}
static class Person {
String name;
Integer age;
public Person(final String name, final Integer age) {
this.name = name;
this.age = age;
}
}
private static class PropertyComparator implements Comparator<Object> {
private SortParam<String> sort;
public PropertyComparator(final SortParam<String> sort) {
this.sort = sort;
}
@Override
public int compare(final Object o1, final Object o2) {
final int dir = sort.isAscending() ? 1 : -1;
final String sortProperty = sort.getProperty();
return dir * this.useComparator(o1, o2, sortProperty);
}
private <T extends Comparable<T>> int useComparator(final Object o1, final Object o2, final String property) {
final T p1 = new PropertyModel<T>(o1, property).getObject();
final T p2 = new PropertyModel<T>(o2, property).getObject();
return p1.compareTo(p2);
}
}
}
In my environment this works fine in Eclipse, but fails when tried to compile using
mvn clean install
When I tried to move useComparator
out of PropertyComparator
and made it static didn't help...