我試圖將此ControllerAnnotationHelper轉換爲服務,並且出現了奇怪的問題。無法調用需要java.lang.Class參數的方法?
No signature of method AnnotationScannerService.findAnnotatedClosures() is applicable for argument types:
(java.lang.Class, java.lang.Class) values: [class MyController, interface MyAnnotationRequired]
這裏的原始方法:
private static Map<String, List<Class>> findAnnotatedClosures(
Class clazz, Class... annotationClasses) {
def map = [:]
for (field in clazz.declaredFields) {
def fieldAnnotations = []
for (annotationClass in annotationClasses) {
if (field.isAnnotationPresent(annotationClass)) {
fieldAnnotations << annotationClass
}
}
if (fieldAnnotations) {
map[field.name] = fieldAnnotations
}
}
return map
}
和礦山:
protected Map<String, List<Class>> findAnnotatedClosures(Class clazz, Class... annotationClasses) {
def map = [:]
for (field in clazz.declaredFields) {
def fieldAnnotations = []
for (annotationClass in annotationClasses) {
if (field.isAnnotationPresent(annotationClass)) {
fieldAnnotations << annotationClass
}
}
if (fieldAnnotations) {
map[field.name] = fieldAnnotations
}
}
return map
}
隨着調用:
public void test_findAnnotatedClosures() {
Map<String, List<Class>> annotatedClosures =
annotationScannerService.findAnnotatedClosures(MyController, MyRequiredAnnotation)
}
我怎樣才能宣佈這個方法,這樣我可以打電話它與一個控制器類和cl各種註釋界面的屁股?
這適用於我。有一件很奇怪的事情是,您保留了受保護的訪問權限,但您將其稱爲公共方法。它有助於刪除「受保護的」(因爲默認範圍是公開的)還是明確公開?另外 - 嘗試運行'grails clean'強制重新編譯。 –
我沒有嘗試'grails clean',但它被'保護',所以我可以從集成/單元測試中打它。我會嘗試明天去除靜態修飾符/清理/公共訪問。值得注意的是我正在運行1.3.7,所以這可能是1.7.x版本的問題。 –
@Burt Beckwith:很奇怪。它看起來像(在運行之間有一個乾淨的),只有當這個方法是公開的時候,這纔有效。如果標記爲protected,那麼init方法和測試都不能調用findAnnotatedClosures方法。我不明白這一點。 –