好吧,NoSuchMethodException 通常是不言自明的。不幸的是,就我而言,我什至无法猜测为什么会出现这样的错误。
我正在处理其他开发人员的代码,我必须维护它。它的设计考虑了规范模式。为了使搜索引擎非常通用,基本上,从客户端传递到休息服务的任何字符串都会被拆分,以构建搜索条件。
执行“clazzSpec.getDeclaredConstructor(SearchCriteria.class, String[].class).newInstance(param);”时 我明白了
java.lang.NoSuchMethodException: br.com.mycompany.specification.SomethingSpecification.<init>(br.com.mycompany.specification.SearchCriteria, [Ljava.lang.String;)
看下面的图片,我看不到错过了什么
搜索条件:
public class SearchCriteria {
private String key;
private String operation;
private Object value;
public SearchCriteria(String key, String operation, Object value) {
this.key = key;
this.operation = operation;
this.value = value;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getOperation() {
return operation;
}
public void setOperation(String operation) {
this.operation = operation;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
}
东西规范
public class SomethingSpecification extends Specification<Something> {
public SomethingSpecification(SearchCriteria criteria) {
super(criteria);
}
@Override
public Predicate toPredicate(Root<Something> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
switch (criteria.getOperation()) {
case "=":
...
}
return super.toPredicate(root, query, builder);
}
}