author | ohair |
Tue, 25 May 2010 15:58:33 -0700 | |
changeset 5506 | 202f599c92aa |
parent 1308 | db0929bb4cd4 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2006, 2008, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
5506 | 19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
2 | 22 |
*/ |
23 |
||
24 |
import java.beans.PropertyEditor; |
|
25 |
import java.beans.PropertyEditorManager; |
|
26 |
||
27 |
final class TestEditor { |
|
28 |
private final PropertyEditor editor; |
|
29 |
||
30 |
TestEditor(Class type) { |
|
31 |
System.out.println("Property class: " + type); |
|
32 |
||
33 |
this.editor = PropertyEditorManager.findEditor(type); |
|
34 |
if (this.editor == null) |
|
35 |
throw new Error("could not find editor for " + type); |
|
36 |
||
37 |
System.out.println("PropertyEditor class: " + this.editor.getClass()); |
|
38 |
validate(null, null); |
|
39 |
} |
|
40 |
||
41 |
void testJava(Object value) { |
|
42 |
this.editor.setValue(value); |
|
43 |
||
1308
db0929bb4cd4
6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
2
diff
changeset
|
44 |
Object object = execute("Executor", "execute", this.editor.getJavaInitializationString()); |
2 | 45 |
|
46 |
System.out.println("Property value before: " + value); |
|
47 |
System.out.println("Property value after: " + object); |
|
48 |
||
49 |
if (!areEqual(value, object)) |
|
50 |
throw new Error("values are not equal"); |
|
51 |
} |
|
52 |
||
53 |
void testValue(Object value, String text) { |
|
54 |
this.editor.setValue(value); |
|
55 |
validate(value, text); |
|
56 |
} |
|
57 |
||
58 |
void testText(String text, Object value) { |
|
59 |
this.editor.setAsText(text); |
|
60 |
validate(value, text); |
|
61 |
} |
|
62 |
||
63 |
private void validate(Object value, String text) { |
|
64 |
if (!areEqual(value, this.editor.getValue())) |
|
65 |
throw new Error("value should be " + value); |
|
66 |
||
67 |
if (!areEqual(text, this.editor.getAsText())) |
|
68 |
throw new Error("text should be " + text); |
|
69 |
} |
|
70 |
||
71 |
private static boolean areEqual(Object object1, Object object2) { |
|
72 |
return (object1 == null) |
|
73 |
? object2 == null |
|
74 |
: object1.equals(object2); |
|
75 |
} |
|
76 |
||
1308
db0929bb4cd4
6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
2
diff
changeset
|
77 |
private static Object execute(String classname, String methodname, String value) { |
db0929bb4cd4
6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
2
diff
changeset
|
78 |
String content |
db0929bb4cd4
6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
2
diff
changeset
|
79 |
= "public class " + classname + " {" |
db0929bb4cd4
6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
2
diff
changeset
|
80 |
+ " public static Object " + methodname + "() throws Exception {" |
db0929bb4cd4
6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
2
diff
changeset
|
81 |
+ " return " + value + ";" |
db0929bb4cd4
6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
2
diff
changeset
|
82 |
+ " }" |
db0929bb4cd4
6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
2
diff
changeset
|
83 |
+ "}"; |
2 | 84 |
|
1308
db0929bb4cd4
6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
2
diff
changeset
|
85 |
try { |
db0929bb4cd4
6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
2
diff
changeset
|
86 |
MemoryClassLoader loader = new MemoryClassLoader(); |
db0929bb4cd4
6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
2
diff
changeset
|
87 |
Class type = loader.compile(classname, content); |
db0929bb4cd4
6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
2
diff
changeset
|
88 |
return type.getMethod(methodname).invoke(null); |
2 | 89 |
} |
1308
db0929bb4cd4
6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
2
diff
changeset
|
90 |
catch (Exception exception) { |
db0929bb4cd4
6397609: DOC: De-register API required for PropertyEditorManager and/or doc change
malenkov
parents:
2
diff
changeset
|
91 |
throw new Error(exception); |
2 | 92 |
} |
93 |
} |
|
94 |
} |