blob: 85a9ec338a580489fd83bb7eb57180f7173cc323 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2006-2007 Sun Microsystems, Inc. All Rights Reserved.
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 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24import java.beans.PropertyEditor;
25import java.beans.PropertyEditorManager;
26import java.io.ByteArrayOutputStream;
27import java.net.URI;
28import java.net.URISyntaxException;
29import java.util.Arrays;
30import java.util.HashMap;
31import java.util.Map;
32import javax.tools.FileObject;
33import javax.tools.ForwardingJavaFileManager;
34import javax.tools.JavaCompiler;
35import javax.tools.JavaFileObject.Kind;
36import javax.tools.SimpleJavaFileObject;
37import javax.tools.ToolProvider;
38
39final class TestEditor {
40 private final PropertyEditor editor;
41
42 TestEditor(Class type) {
43 System.out.println("Property class: " + type);
44
45 this.editor = PropertyEditorManager.findEditor(type);
46 if (this.editor == null)
47 throw new Error("could not find editor for " + type);
48
49 System.out.println("PropertyEditor class: " + this.editor.getClass());
50 validate(null, null);
51 }
52
53 void testJava(Object value) {
54 this.editor.setValue(value);
55
56 MemoryFileManager manager = new MemoryFileManager();
57 Object object = manager.invoke(this.editor.getJavaInitializationString());
58
59 System.out.println("Property value before: " + value);
60 System.out.println("Property value after: " + object);
61
62 if (!areEqual(value, object))
63 throw new Error("values are not equal");
64 }
65
66 void testValue(Object value, String text) {
67 this.editor.setValue(value);
68 validate(value, text);
69 }
70
71 void testText(String text, Object value) {
72 this.editor.setAsText(text);
73 validate(value, text);
74 }
75
76 private void validate(Object value, String text) {
77 if (!areEqual(value, this.editor.getValue()))
78 throw new Error("value should be " + value);
79
80 if (!areEqual(text, this.editor.getAsText()))
81 throw new Error("text should be " + text);
82 }
83
84 private static boolean areEqual(Object object1, Object object2) {
85 return (object1 == null)
86 ? object2 == null
87 : object1.equals(object2);
88 }
89
90 private static final class MemoryFileManager extends ForwardingJavaFileManager {
91 private static final String CLASS = "Executor";
92 private static final String METHOD = "execute";
93 private static final JavaCompiler COMPILER = ToolProvider.getSystemJavaCompiler();
94 private final Map<String, MemoryClass> map = new HashMap<String, MemoryClass>();
95 private final MemoryClassLoader loader = new MemoryClassLoader();
96
97 MemoryFileManager() {
98 super(COMPILER.getStandardFileManager(null, null, null));
99 }
100
101 public Object invoke(String expression) {
102 MemorySource file = new MemorySource(CLASS, METHOD, expression);
103 if (!COMPILER.getTask(null, this, null, null, null, Arrays.asList(file)).call())
104 throw new Error("compilation failed");
105
106 MemoryClass mc = this.map.get(CLASS);
107 if (mc == null)
108 throw new Error("class not found: " + CLASS);
109
110 Class c = this.loader.loadClass(CLASS, mc.toByteArray());
111 try {
112 return c.getMethod(METHOD).invoke(null);
113 }
114 catch (Exception exception) {
115 throw new Error(exception);
116 }
117 }
118
119 public MemoryClass getJavaFileForOutput(Location location, String name, Kind kind, FileObject source) {
120 MemoryClass type = this.map.get(name);
121 if (type == null) {
122 type = new MemoryClass(name);
123 this.map.put(name, type);
124 }
125 return type;
126 }
127 }
128
129 private static final class MemoryClassLoader extends ClassLoader {
130 public Class<?> loadClass(String name, byte[] array) {
131 return defineClass(name, array, 0, array.length);
132 }
133 }
134
135 private static class MemoryObject extends SimpleJavaFileObject {
136 protected MemoryObject(String name, Kind kind) {
137 super(toURI(name, kind.extension), kind);
138 }
139
140 private static URI toURI(String name, String extension) {
141 try {
142 return new URI("mfm:///" + name.replace('.', '/') + extension);
143 }
144 catch (URISyntaxException exception) {
145 throw new Error(exception);
146 }
147 }
148 }
149
150 private static final class MemoryClass extends MemoryObject {
151 private final ByteArrayOutputStream baos = new ByteArrayOutputStream();
152
153 MemoryClass(String className) {
154 super(className, Kind.CLASS);
155 }
156
157 public ByteArrayOutputStream openOutputStream() {
158 this.baos.reset();
159 return this.baos;
160 }
161
162 public byte[] toByteArray() {
163 return this.baos.toByteArray();
164 }
165 }
166
167 private static final class MemorySource extends MemoryObject {
168 private final String value;
169
170 MemorySource(String className, String methodName, String expression) {
171 super(className, Kind.SOURCE);
172 this.value
173 = "public class " + className + " {\n"
174 + " public static Object " + methodName + "() throws Exception {\n"
175 + " return " + expression + ";\n"
176 + " }\n"
177 + "}\n";
178 }
179
180 public CharSequence getCharContent(boolean ignore) {
181 return this.value;
182 }
183 }
184}