2017-01-25 11 views
1

에 getter 메소드를 throw하지 않습니다. 알다시피, 나는 TemplateVars 클래스에 'name'속성에 대한 getter를 가지고 있습니다. 비슷한 코드 조각이 시스템의 다른 곳에서도 작동합니다. 그럼 왜 다음 코드가 다음과 같은 예외를 던집니까?Java PropertyUtils.getProperty는 클래스

코드

public class Main { 

    public static void main(String[] args) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException { 
     String template = "Hello. My name is %%name%% %%surname%% %%contact.email%%"; 
     Pattern pattern = Pattern.compile("%%(.*?)%%"); 
     Matcher matcher = pattern.matcher(template); 

     TemplateVars item = new TemplateVars(); 

     while (matcher.find()) { 
      String placeHolder = matcher.group(1); 
      String value; 

      if(placeHolder.contains(".")){ 
       value = PropertyUtils.getNestedProperty(item, placeHolder).toString(); 
      }else{ 
       value = PropertyUtils.getProperty(item,placeHolder).toString(); 
      } 
      template = template.replace("%%" + placeHolder + "%%", value); 
     } 

     System.out.println(template); 
    } 

} 

class TemplateVars { 
    private String name = "Boo"; 
    private String surname = "Foo"; 
    private Contact contact; 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getSurname() { 
     return surname; 
    } 

    public void setSurname(String surname) { 
     this.surname = surname; 
    } 

    public Contact getContact() { 
     return contact; 
    } 

    public void setContact(Contact contact) { 
     this.contact = contact; 
    } 
} 

class Contact { 
    private String email = "[email protected]"; 

    public String getEmail() { 
     return email; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 
} 

예외

Exception in thread "main" java.lang.NoSuchMethodException: Property 'name' has no getter method in class 'class TemplateVars' 
    at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1274) 
    at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:808) 
    at org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:884) 
    at org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:464) 
    at Main.main(Main.java:24) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:497) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) 

내가 위에서 게시 된 개념의 증거로 봉사 아니라 한 코드의 조각.

+0

REAL [mcve] : A) 필요하지 않은 콘텐츠가 없습니다 (패턴 일치). B) 쉽게 컴파일하고 실행할 수있는 콘텐츠. – GhostCat

+0

@GhostCat 제가 말할 수있는 한, * MCVE입니다. –

+1

최소한의 것은 아닙니다. 다시 : 그 패턴과 일치하는 것은 무엇입니까? – GhostCat

답변

7

이것은 단지 클래스 TemplateVarsContact에 패키지 로컬 액세스 수정자가 있고 PropertyUtils이 액세스 할 수 없기 때문입니다.

해결하려면 최상위 클래스 (즉 public) 또는 public static inner 클래스를 Main 클래스로 만듭니다.

+0

정적 인 내부 클래스조차 공개되어야합니다. 이 동작에 대한 설명서를 찾을 수 없습니다. –

+0

@defaultlocale 확실하지만 의미는 있지만 언급하는 것을 잊었습니다. 감사합니다. 업데이트했습니다. – Andremoniy