TestEditor.java revision 0
0N/A/*
0N/A * Copyright 2006-2007 Sun Microsystems, Inc. All Rights Reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0N/A * CA 95054 USA or visit www.sun.com if you need additional information or
0N/A * have any questions.
0N/A */
0N/A
0N/Aimport java.beans.PropertyEditor;
0N/Aimport java.beans.PropertyEditorManager;
0N/Aimport java.io.ByteArrayOutputStream;
0N/Aimport java.net.URI;
0N/Aimport java.net.URISyntaxException;
0N/Aimport java.util.Arrays;
0N/Aimport java.util.HashMap;
0N/Aimport java.util.Map;
0N/Aimport javax.tools.FileObject;
0N/Aimport javax.tools.ForwardingJavaFileManager;
0N/Aimport javax.tools.JavaCompiler;
0N/Aimport javax.tools.JavaFileObject.Kind;
0N/Aimport javax.tools.SimpleJavaFileObject;
0N/Aimport javax.tools.ToolProvider;
0N/A
0N/Afinal class TestEditor {
0N/A private final PropertyEditor editor;
0N/A
0N/A TestEditor(Class type) {
0N/A System.out.println("Property class: " + type);
0N/A
0N/A this.editor = PropertyEditorManager.findEditor(type);
0N/A if (this.editor == null)
0N/A throw new Error("could not find editor for " + type);
0N/A
0N/A System.out.println("PropertyEditor class: " + this.editor.getClass());
0N/A validate(null, null);
0N/A }
0N/A
0N/A void testJava(Object value) {
0N/A this.editor.setValue(value);
0N/A
0N/A MemoryFileManager manager = new MemoryFileManager();
0N/A Object object = manager.invoke(this.editor.getJavaInitializationString());
0N/A
0N/A System.out.println("Property value before: " + value);
0N/A System.out.println("Property value after: " + object);
0N/A
0N/A if (!areEqual(value, object))
0N/A throw new Error("values are not equal");
0N/A }
0N/A
0N/A void testValue(Object value, String text) {
0N/A this.editor.setValue(value);
0N/A validate(value, text);
0N/A }
0N/A
0N/A void testText(String text, Object value) {
0N/A this.editor.setAsText(text);
0N/A validate(value, text);
0N/A }
0N/A
0N/A private void validate(Object value, String text) {
0N/A if (!areEqual(value, this.editor.getValue()))
0N/A throw new Error("value should be " + value);
0N/A
0N/A if (!areEqual(text, this.editor.getAsText()))
0N/A throw new Error("text should be " + text);
0N/A }
0N/A
0N/A private static boolean areEqual(Object object1, Object object2) {
0N/A return (object1 == null)
0N/A ? object2 == null
0N/A : object1.equals(object2);
0N/A }
0N/A
0N/A private static final class MemoryFileManager extends ForwardingJavaFileManager {
0N/A private static final String CLASS = "Executor";
0N/A private static final String METHOD = "execute";
0N/A private static final JavaCompiler COMPILER = ToolProvider.getSystemJavaCompiler();
0N/A private final Map<String, MemoryClass> map = new HashMap<String, MemoryClass>();
0N/A private final MemoryClassLoader loader = new MemoryClassLoader();
0N/A
0N/A MemoryFileManager() {
0N/A super(COMPILER.getStandardFileManager(null, null, null));
0N/A }
0N/A
0N/A public Object invoke(String expression) {
0N/A MemorySource file = new MemorySource(CLASS, METHOD, expression);
0N/A if (!COMPILER.getTask(null, this, null, null, null, Arrays.asList(file)).call())
0N/A throw new Error("compilation failed");
0N/A
0N/A MemoryClass mc = this.map.get(CLASS);
0N/A if (mc == null)
0N/A throw new Error("class not found: " + CLASS);
0N/A
0N/A Class c = this.loader.loadClass(CLASS, mc.toByteArray());
0N/A try {
0N/A return c.getMethod(METHOD).invoke(null);
0N/A }
0N/A catch (Exception exception) {
0N/A throw new Error(exception);
0N/A }
0N/A }
0N/A
0N/A public MemoryClass getJavaFileForOutput(Location location, String name, Kind kind, FileObject source) {
0N/A MemoryClass type = this.map.get(name);
0N/A if (type == null) {
0N/A type = new MemoryClass(name);
0N/A this.map.put(name, type);
0N/A }
0N/A return type;
0N/A }
0N/A }
0N/A
0N/A private static final class MemoryClassLoader extends ClassLoader {
0N/A public Class<?> loadClass(String name, byte[] array) {
0N/A return defineClass(name, array, 0, array.length);
0N/A }
0N/A }
0N/A
0N/A private static class MemoryObject extends SimpleJavaFileObject {
0N/A protected MemoryObject(String name, Kind kind) {
0N/A super(toURI(name, kind.extension), kind);
0N/A }
0N/A
0N/A private static URI toURI(String name, String extension) {
0N/A try {
0N/A return new URI("mfm:///" + name.replace('.', '/') + extension);
0N/A }
0N/A catch (URISyntaxException exception) {
0N/A throw new Error(exception);
0N/A }
0N/A }
0N/A }
0N/A
0N/A private static final class MemoryClass extends MemoryObject {
0N/A private final ByteArrayOutputStream baos = new ByteArrayOutputStream();
0N/A
0N/A MemoryClass(String className) {
0N/A super(className, Kind.CLASS);
0N/A }
0N/A
0N/A public ByteArrayOutputStream openOutputStream() {
0N/A this.baos.reset();
0N/A return this.baos;
0N/A }
0N/A
0N/A public byte[] toByteArray() {
0N/A return this.baos.toByteArray();
0N/A }
0N/A }
0N/A
0N/A private static final class MemorySource extends MemoryObject {
0N/A private final String value;
0N/A
0N/A MemorySource(String className, String methodName, String expression) {
0N/A super(className, Kind.SOURCE);
0N/A this.value
0N/A = "public class " + className + " {\n"
0N/A + " public static Object " + methodName + "() throws Exception {\n"
0N/A + " return " + expression + ";\n"
0N/A + " }\n"
0N/A + "}\n";
0N/A }
0N/A
0N/A public CharSequence getCharContent(boolean ignore) {
0N/A return this.value;
0N/A }
0N/A }
0N/A}