Advertisement
Guest User

Pascal Thivent

a guest
Apr 11th, 2010
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.78 KB | None | 0 0
  1. /*
  2.  * Copyright 2005-2006 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. package sun.tools.evalexpr;
  25.  
  26. import java.io.ByteArrayOutputStream;
  27. import java.io.FilterOutputStream;
  28. import java.io.IOException;
  29. import java.io.OutputStream;
  30. import java.net.URI;
  31. import java.net.URISyntaxException;
  32. import java.util.HashMap;
  33. import java.util.Map;
  34.  
  35. import javax.tools.FileObject;
  36. import javax.tools.ForwardingJavaFileManager;
  37. import javax.tools.JavaFileManager;
  38. import javax.tools.JavaFileObject;
  39. import javax.tools.SimpleJavaFileObject;
  40. import javax.tools.JavaFileObject.Kind;
  41.  
  42. /**
  43.  * A file manager for compiling strings to byte arrays. This file manager delegates to another file manager to lookup
  44.  * classes on boot class path.
  45.  * <p>
  46.  * <b>This is NOT part of any API supported by Sun Microsystems. If you write code that depends on this, you do so at
  47.  * your own risk. This code and its internal interfaces are subject to change or deletion without notice.</b>
  48.  * </p>
  49.  *
  50.  * @author Peter von der Ah&eacute;
  51.  */
  52. public final class MemoryFileManager extends ForwardingJavaFileManager {
  53.     /**
  54.      * Maps binary class names to class files stored as byte arrays.
  55.      */
  56.     private final Map<String, byte[]> classes;
  57.  
  58.     /**
  59.      * Creates a JavaFileObject representing the given compilation unit.
  60.      *
  61.      * @param name a name representing this source code, for example, the name of a class
  62.      * @param code a compilation unit (source code for a Java program)
  63.      * @return a JavaFileObject represtenting the given compilation unit
  64.      */
  65.     public static JavaFileObject makeSource(String name, String code) {
  66.         return new JavaSourceFromString(name, code);
  67.     }
  68.  
  69.     /**
  70.      * Construct a memory file manager which delegates to the specified file manager for unknown sources.
  71.      *
  72.      * @param fileManager a file manager used to look up class files on class path, etc.
  73.      */
  74.     public MemoryFileManager(JavaFileManager fileManager) {
  75.         super(fileManager);
  76.         classes = new HashMap<String, byte[]>();
  77.     }
  78.  
  79.     /**
  80.      * Get a class loader which first search the classes stored by this file mananger.
  81.      *
  82.      * @return a class loader for compiled files
  83.      */
  84.     @Override
  85.     public ClassLoader getClassLoader(Location location) {
  86.         return new ByteArrayClassLoader(classes);
  87.     }
  88.  
  89.     @Override
  90.     public JavaFileObject getJavaFileForOutput(Location location, String name, Kind kind, FileObject originatingSource)
  91.         throws UnsupportedOperationException {
  92.         if (originatingSource instanceof JavaSourceFromString) {
  93.             return new JavaClassInArray(name);
  94.         } else {
  95.             throw new UnsupportedOperationException();
  96.         }
  97.     }
  98.  
  99.     protected static URI uriFromString(String uri) {
  100.         try {
  101.             return new URI(uri);
  102.         } catch (URISyntaxException e) {
  103.             throw new IllegalArgumentException(e);
  104.         }
  105.     }
  106.  
  107.     /**
  108.      * A file object representing a Java class file stored in a byte array.
  109.      */
  110.     private class JavaClassInArray extends SimpleJavaFileObject {
  111.  
  112.         private final String name;
  113.  
  114.         /**
  115.          * Constructs a JavaClassInArray object.
  116.          *
  117.          * @param name binary name of the class to be stored in this file object
  118.          */
  119.         JavaClassInArray(String name) {
  120.             super(uriFromString("mfm:///" + name.replace('.', '/') + Kind.CLASS.extension), Kind.CLASS);
  121.             this.name = name;
  122.         }
  123.  
  124.         @Override
  125.         public OutputStream openOutputStream() {
  126.             return new FilterOutputStream(new ByteArrayOutputStream()) {
  127.                 @Override
  128.                 public void close() throws IOException {
  129.                     out.close();
  130.                     ByteArrayOutputStream bos = (ByteArrayOutputStream) out;
  131.                     classes.put(name, bos.toByteArray());
  132.                 }
  133.             };
  134.         }
  135.     }
  136.  
  137.     /**
  138.      * A file object used to represent source coming from a string.
  139.      */
  140.     private static class JavaSourceFromString extends SimpleJavaFileObject {
  141.         /**
  142.          * The source code of this "file".
  143.          */
  144.         final String code;
  145.  
  146.         /**
  147.          * Constructs a new JavaSourceFromString.
  148.          *
  149.          * @param name the name of the compilation unit represented by this file object
  150.          * @param code the source code for the compilation unit represented by this file object
  151.          */
  152.         JavaSourceFromString(String name, String code) {
  153.             super(uriFromString("mfm:///" + name.replace('.', '/') + Kind.SOURCE.extension), Kind.SOURCE);
  154.             this.code = code;
  155.         }
  156.  
  157.         @Override
  158.         public CharSequence getCharContent(boolean ignoreEncodingErrors) {
  159.             return code;
  160.         }
  161.     }
  162.  
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement