J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 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 | |
| 24 | import java.beans.PropertyEditor; |
| 25 | import java.beans.PropertyEditorManager; |
| 26 | import java.io.ByteArrayOutputStream; |
| 27 | import java.net.URI; |
| 28 | import java.net.URISyntaxException; |
| 29 | import java.util.Arrays; |
| 30 | import java.util.HashMap; |
| 31 | import java.util.Map; |
| 32 | import javax.tools.FileObject; |
| 33 | import javax.tools.ForwardingJavaFileManager; |
| 34 | import javax.tools.JavaCompiler; |
| 35 | import javax.tools.JavaFileObject.Kind; |
| 36 | import javax.tools.SimpleJavaFileObject; |
| 37 | import javax.tools.ToolProvider; |
| 38 | |
| 39 | final 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 | } |