2011-11-17 3 views
0

Play Framework를 약 1 개월 사용하고 있는데 큰 일이지만 큰 문제가 하나 있습니다. . I`ve 보안 컨트롤러에서 다음 코드를 실행하려고 : 라인 소개처리되지 않은 예외 유형 NoSuchFieldException (자바 리플렉션)

MyModel myModel = MyModel.all().first(); 
Field idField = myModel.getClass().getField("id"); 

2 플레이는 말한다 : 컴파일 오류

The file /app/controllers/Security.java could not be compiled. Error 
raised is : Unhandled exception type NoSuchFieldException 

어쩌면 핵심 버그 그것은`? 감사합니다. .

+0

이 모델 코드를 보여를 – sdespolit

답변

4

getField (String fieldName)에서 처리 할 수있는 예외를 처리해야합니다. 이 경우에는 NoSuchFieldException이 발생합니다.

시도는로 쓰기 :

Field idField = null; 
try { 
    idField = myModel.getClass().getField("id"); 
} catch (NoSuchFieldException nsfe) { 
    throw new RuntimeException(nsfe); 
} 
+0

일! 감사. – user1051870

1

당신이 사용하는 경우 dp4j@TestPrivates 또는 @Reflect(catchExceptions =true) 당신은 catch 문 직접 작성할 필요가 없습니다 :

public class Security{ 

@Reflect(catchExceptions =true) //when false it will add the exceptions to the throws list. 
public void aMethod(){ 
    MyModel myModel = MyModel.all().first(); 
    Field idField = myModel.getClass().getField("id"); 
    //you might as well write: 
    // int id = myModel.id; 
} 
관련 문제