我越来越
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"
Maven 编译器插件设置来自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>
编辑:添加了请求的代码
创建 wicket 项目(仅用于依赖项)。我使用Wicket Quickstart 页面生成 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=false
将项目导入您喜欢的 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
使其静止时没有帮助......