에 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)
내가 위에서 게시 된 개념의 증거로 봉사 아니라 한 코드의 조각.
REAL [mcve] : A) 필요하지 않은 콘텐츠가 없습니다 (패턴 일치). B) 쉽게 컴파일하고 실행할 수있는 콘텐츠. – GhostCat
@GhostCat 제가 말할 수있는 한, * MCVE입니다. –
최소한의 것은 아닙니다. 다시 : 그 패턴과 일치하는 것은 무엇입니까? – GhostCat