📄 modelpropertydescriptor.java
字号:
/*******************************************************************************
* Copyright (c) 2004 Stefan Zeiger and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.novocode.com/legal/epl-v10.html
*
* Contributors:
* Stefan Zeiger (szeiger@novocode.com) - initial API and implementation
*******************************************************************************/
package com.novocode.naf.data;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import com.novocode.naf.app.NAFException;
/**
* A descriptor for a ModelProperty.
*
* @author Stefan Zeiger (szeiger@novocode.com)
* @since Dec 18, 2004
* @version $Id: ModelPropertyDescriptor.java,v 1.2 2005/01/14 22:10:25 szeiger Exp $
*/
public final class ModelPropertyDescriptor
{
private final Method getter, setter;
private final String xmlPropertyName;
public ModelPropertyDescriptor(PropertyDescriptor pd, ModelProperty p) throws NAFException
{
String xname = p.value();
if(xname == null || xname.length() == 0)
{
xname = pd.getName();
if(xname.endsWith("Model") && xname.length() > 5)
xname = xname.substring(0, xname.length()-5);
}
xmlPropertyName = xname;
setter = pd.getWriteMethod();
getter = pd.getReadMethod();
if(pd.getPropertyType() != ModelBinding.class) throw new NAFException("Model properties must be of type ModelBinding");
}
public Method getReadMethod() { return getter; }
public Method getWriteMethod() { return setter; }
public String getXMLPropertyName() { return xmlPropertyName; }
public void set(Object target, ModelBinding value) throws NAFException
{
if(setter == null) throw new NAFException("Model property "+xmlPropertyName+" is read-only");
try
{
setter.invoke(target, new Object[] { value });
}
catch(Exception ex)
{
Throwable cause = ex.getCause();
if(cause instanceof RuntimeException) throw (RuntimeException)cause;
else throw new NAFException("Error invoking setter method for model property "+target.getClass().getName()+":"+xmlPropertyName+": "+ex, ex);
}
}
public ModelBinding get(Object target) throws NAFException
{
if(getter == null) throw new NAFException("Model property "+xmlPropertyName+" is write-only");
try
{
return (ModelBinding)getter.invoke(target, new Object[0]);
}
catch(Exception ex)
{
Throwable cause = ex.getCause();
if(cause instanceof RuntimeException) throw (RuntimeException)cause;
else throw new NAFException("Error invoking getter method for model property "+target.getClass().getName()+":"+xmlPropertyName+": "+ex, ex);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -