我正在
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]
我的方法
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);
}
其中PropertyModel
是org.apache.wicket.model.PropertyModel
,但实际上这并不重要(这个类只返回某个字段的值,顺便说一句:例如,在Spring中是否存在一些具有类似功能的类?)。
问题在于调用该方法:
return this.useComparator(o1, o2, sortProperty);
我可以通过
解决这个问题return this.<Comparable> useComparator(o1, o2, sortProperty);
但我想知道..在Eclipse和NetBeans中编译这样的代码时我没有问题。据我所知,我正在使用相同的java,但在Maven中,我从帖子的开头得到了错误。我错过了一些编译器标志吗?
不确定这是否有帮助,但我的Maven版本是:
> 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"
来自pom.xml
的Maven编译器插件设置:
<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>
编辑:已添加请求的代码
创建wicket项目(仅用于依赖项)。我使用Wicket Quickstart page来生成maven项目。我用过:
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 =假
将项目导入您喜欢的IDE。
使用此JUnit测试来模拟问题:
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);
}
}
}
在我的环境中,这在Eclipse中运行良好,但在尝试使用
编译时失败mvn clean install
当我尝试将useComparator
移出PropertyComparator
并使其静止无效时......