📄 xmlpropertydescriptor.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.InvocationTargetException;
import java.lang.reflect.Method;
import com.novocode.naf.app.NAFException;
/**
* A descriptor for an XMLProperty.
*
* @author Stefan Zeiger (szeiger@novocode.com)
* @since Dec 17, 2004
* @version $Id: XMLPropertyDescriptor.java,v 1.1 2004/12/19 00:11:20 szeiger Exp $
*/
public final class XMLPropertyDescriptor
{
private final Method getter, setter;
private final IDataConverter conv;
private final Class propertyType;
private final String xmlPropertyName;
public XMLPropertyDescriptor(PropertyDescriptor pd, XMLProperty p)
{
String xname = p.value();
if(xname == null || xname.length() == 0) xname = pd.getName();
xmlPropertyName = xname;
setter = pd.getWriteMethod();
getter = pd.getReadMethod();
propertyType = pd.getPropertyType();
if(propertyType == String[].class)
conv = StringArrayDataConverter.getDefaultInstance();
else
conv = DefaultDataConverter.getInstance();
}
public Method getReadMethod() { return getter; }
public Method getWriteMethod() { return setter; }
public String getXMLPropertyName() { return xmlPropertyName; }
public void set(Object target, String value) throws NAFException
{
if(setter == null) throw new NAFException("Property "+xmlPropertyName+" is read-only");
try
{
setter.invoke(target, new Object[] { conv.toInternal(propertyType, value) });
}
catch(InvocationTargetException ex)
{
Throwable cause = ex.getCause();
if(cause instanceof RuntimeException) throw (RuntimeException)cause;
else throw new NAFException("Error invoking setter method for property "+target.getClass().getName()+":"+xmlPropertyName+": "+ex, ex);
}
catch(Exception ex)
{
throw new NAFException("Error converting data for property "+target.getClass().getName()+":"+xmlPropertyName+": "+ex, ex);
}
}
public String get(Object target) throws NAFException
{
if(getter == null) throw new NAFException("Property "+xmlPropertyName+" is write-only");
try
{
return conv.toExternal(propertyType, getter.invoke(target, new Object[0]));
}
catch(InvocationTargetException ex)
{
Throwable cause = ex.getCause();
if(cause instanceof RuntimeException) throw (RuntimeException)cause;
else throw new NAFException("Error invoking getter method for property "+target.getClass().getName()+":"+xmlPropertyName+": "+ex, ex);
}
catch(Exception ex)
{
throw new NAFException("Error converting data for property "+target.getClass().getName()+":"+xmlPropertyName+": "+ex, ex);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -