下面列出了java.beans.PropertyEditor#getValue ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static Object getValueFromBeanInfoPropertyEditor(
Class<?> attrClass, String attrName, String attrValue,
Class<?> propertyEditorClass)
throws JasperException
{
try {
PropertyEditor pe = (PropertyEditor)propertyEditorClass.getConstructor().newInstance();
pe.setAsText(attrValue);
return pe.getValue();
} catch (Exception ex) {
throw new JasperException(
Localizer.getMessage("jsp.error.beans.property.conversion",
attrValue, attrClass.getName(), attrName,
ex.getMessage()));
}
}
public boolean isValueModified() {
PropertyEditor editor = getPropertyEditor();
boolean result = editor.getValue() != originalValue;
if (!result && editor instanceof EnhancedCustomPropertyEditor) {
Object entered = ((EnhancedCustomPropertyEditor) editor).getPropertyValue();
if (entered != null) {
result = entered.equals(originalValue);
} else {
result = originalValue == null;
}
}
return result;
}
public static Object getValueFromBeanInfoPropertyEditor(
Class<?> attrClass, String attrName, String attrValue,
Class<?> propertyEditorClass)
throws JasperException
{
try {
PropertyEditor pe =
(PropertyEditor)propertyEditorClass.newInstance();
pe.setAsText(attrValue);
return pe.getValue();
} catch (Exception ex) {
throw new JasperException(
Localizer.getMessage("jsp.error.beans.property.conversion",
attrValue, attrClass.getName(), attrName,
ex.getMessage()));
}
}
@Test
public void testUnqualifiedPathNameNotFound() throws Exception {
PropertyEditor pathEditor = new PathEditor();
String fileName = ClassUtils.classPackageAsResourcePath(getClass()) + "/" +
ClassUtils.getShortName(getClass()) + ".clazz";
pathEditor.setAsText(fileName);
Object value = pathEditor.getValue();
assertTrue(value instanceof Path);
Path path = (Path) value;
File file = path.toFile();
assertFalse(file.exists());
String absolutePath = file.getAbsolutePath();
if (File.separatorChar == '\\') {
absolutePath = absolutePath.replace('\\', '/');
}
assertTrue(absolutePath.endsWith(fileName));
}
@Test
public void testUnqualifiedFileNameFound() throws Exception {
PropertyEditor fileEditor = new FileEditor();
String fileName = ClassUtils.classPackageAsResourcePath(getClass()) + "/" + ClassUtils.getShortName(getClass())
+ ".class";
fileEditor.setAsText(fileName);
Object value = fileEditor.getValue();
assertTrue(value instanceof File);
File file = (File) value;
assertTrue(file.exists());
String absolutePath = file.getAbsolutePath();
if (File.separatorChar == '\\') {
absolutePath = absolutePath.replace('\\', '/');
}
assertTrue(absolutePath.endsWith(fileName));
}
private Object getValueFromString(Class<?> targetType, String text) {
final PropertyEditor editor = PropertyEditorManager.findEditor(targetType);
if (editor == null) {
throw new IllegalArgumentException("Cannot set properties of type " + targetType.getName());
}
editor.setAsText(text);
return editor.getValue();
}
@Test
public void standardURLWithWhitespace() throws Exception {
PropertyEditor uriEditor = new URIEditor();
uriEditor.setAsText(" http://www.springframework.org ");
Object value = uriEditor.getValue();
assertTrue(value instanceof URI);
URI uri = (URI) value;
assertEquals("http://www.springframework.org", uri.toString());
}
@Test
public void testClasspathURL() throws Exception {
PropertyEditor urlEditor = new URLEditor();
urlEditor.setAsText("classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) +
"/" + ClassUtils.getShortName(getClass()) + ".class");
Object value = urlEditor.getValue();
assertTrue(value instanceof URL);
URL url = (URL) value;
assertEquals(url.toExternalForm(), urlEditor.getAsText());
assertTrue(!url.getProtocol().startsWith("classpath"));
}
@Test
public void testClasspathURL() throws Exception {
PropertyEditor urlEditor = new URLEditor();
urlEditor.setAsText("classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) +
"/" + ClassUtils.getShortName(getClass()) + ".class");
Object value = urlEditor.getValue();
assertTrue(value instanceof URL);
URL url = (URL) value;
assertEquals(url.toExternalForm(), urlEditor.getAsText());
assertTrue(!url.getProtocol().startsWith("classpath"));
}
@Test
public void testAbsoluteFileName() throws Exception {
PropertyEditor fileEditor = new FileEditor();
fileEditor.setAsText("/no_way_this_file_is_found.doc");
Object value = fileEditor.getValue();
assertTrue(value instanceof File);
File file = (File) value;
assertTrue(!file.exists());
}
@Test
public void sunnyDay() throws Exception {
PropertyEditor editor = new ResourceEditor();
editor.setAsText("classpath:org/springframework/core/io/ResourceEditorTests.class");
Resource resource = (Resource) editor.getValue();
assertNotNull(resource);
assertTrue(resource.exists());
}
public Object setAsText(String str) {
PropertyEditor editor = fetchFromPool();
try {
editor.setAsText(str);
return editor.getValue();
} finally {
pool.putInPool(editor);
}
}
@Test
public void testStandardURL() throws Exception {
PropertyEditor urlEditor = new URLEditor();
urlEditor.setAsText("http://www.springframework.org");
Object value = urlEditor.getValue();
assertTrue(value instanceof URL);
URL url = (URL) value;
assertEquals(url.toExternalForm(), urlEditor.getAsText());
}
private void doTestURI(String uriSpec) {
PropertyEditor uriEditor = new URIEditor();
uriEditor.setAsText(uriSpec);
Object value = uriEditor.getValue();
assertTrue(value instanceof URI);
URI uri = (URI) value;
assertEquals(uriSpec, uri.toString());
}
@Test
public void testVanillaResource() throws Exception {
PropertyEditor editor = new ResourceArrayPropertyEditor();
editor.setAsText("classpath:org/springframework/core/io/support/ResourceArrayPropertyEditor.class");
Resource[] resources = (Resource[]) editor.getValue();
assertNotNull(resources);
assertTrue(resources[0].exists());
}
@Test
public void testWithNonExistentFile() throws Exception {
PropertyEditor fileEditor = new FileEditor();
fileEditor.setAsText("file:no_way_this_file_is_found.doc");
Object value = fileEditor.getValue();
assertTrue(value instanceof File);
File file = (File) value;
assertTrue(!file.exists());
}
private static Object convert(final Object value, final Class type)
throws URISyntaxException {
final PropertyEditor editor = PropertyEditorManager.findEditor(type);
if (editor != null) {
editor.setAsText(value.toString());
return editor.getValue();
}
if (type == URI.class) {
return URLs.uri(value.toString());
}
return null;
}
/**
* Convert the given text value using the given property editor.
* @param oldValue the previous value, if available (may be {@code null})
* @param newTextValue the proposed text value
* @param editor the PropertyEditor to use
* @return the converted value
*/
private Object doConvertTextValue(Object oldValue, String newTextValue, PropertyEditor editor) {
try {
editor.setValue(oldValue);
}
catch (Exception ex) {
if (logger.isDebugEnabled()) {
logger.debug("PropertyEditor [" + editor.getClass().getName() + "] does not support setValue call", ex);
}
// Swallow and proceed.
}
editor.setAsText(newTextValue);
return editor.getValue();
}
@Test
public void standardURLWithWhitespace() throws Exception {
PropertyEditor uriEditor = new URIEditor();
uriEditor.setAsText(" https://www.springframework.org ");
Object value = uriEditor.getValue();
assertTrue(value instanceof URI);
URI uri = (URI) value;
assertEquals("https://www.springframework.org", uri.toString());
}
@Test(expected=IllegalArgumentException.class)
public void testStrictSystemPropertyReplacement() {
PropertyEditor editor = new ResourceArrayPropertyEditor(
new PathMatchingResourcePatternResolver(), new StandardEnvironment(),
false);
System.setProperty("test.prop", "foo");
try {
editor.setAsText("${test.prop}-${bar}");
Resource[] resources = (Resource[]) editor.getValue();
assertEquals("foo-${bar}", resources[0].getFilename());
}
finally {
System.getProperties().remove("test.prop");
}
}