001/*
002 * Copyright (C) 2012 eXo Platform SAS.
003 *
004 * This is free software; you can redistribute it and/or modify it
005 * under the terms of the GNU Lesser General Public License as
006 * published by the Free Software Foundation; either version 2.1 of
007 * the License, or (at your option) any later version.
008 *
009 * This software is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public
015 * License along with this software; if not, write to the Free
016 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018 */
019package org.crsh.lang.java;
020
021import javax.tools.FileObject;
022import javax.tools.ForwardingJavaFileManager;
023import javax.tools.JavaFileObject;
024import javax.tools.StandardJavaFileManager;
025import javax.tools.StandardLocation;
026import java.io.IOException;
027import java.net.URISyntaxException;
028import java.util.Collection;
029import java.util.Collections;
030import java.util.LinkedHashMap;
031import java.util.Set;
032
033/** @author Julien Viet */
034class JavaFileManagerImpl extends ForwardingJavaFileManager<StandardJavaFileManager> {
035
036  /** . */
037  private final LinkedHashMap<String, JavaClassFileObject> classes = new LinkedHashMap<String, JavaClassFileObject>();
038
039  /** . */
040  private final ClasspathResolver finder;
041
042  JavaFileManagerImpl(StandardJavaFileManager fileManager, ClasspathResolver finder) {
043    super(fileManager);
044
045    //
046    this.finder = finder;
047  }
048
049  Collection<JavaClassFileObject> getClasses() {
050    return classes.values();
051  }
052
053  @Override
054  public boolean hasLocation(Location location) {
055    return location == StandardLocation.CLASS_PATH || location == StandardLocation.PLATFORM_CLASS_PATH;
056  }
057
058  @Override
059  public String inferBinaryName(Location location, JavaFileObject file) {
060    if (file instanceof URIJavaFileObject) {
061      return ((URIJavaFileObject)file).binaryName;
062    }
063    else {
064      return fileManager.inferBinaryName(location, file);
065    }
066  }
067
068  @Override
069  public Iterable<JavaFileObject> list(Location location, String packageName, Set<JavaFileObject.Kind> kinds, boolean recurse) throws IOException {
070    if (location == StandardLocation.PLATFORM_CLASS_PATH) {
071      return fileManager.list(location, packageName, kinds, recurse);
072    }
073    else if (location == StandardLocation.CLASS_PATH && kinds.contains(JavaFileObject.Kind.CLASS)) {
074      if (packageName.startsWith("java")) {
075        return fileManager.list(location, packageName, kinds, recurse);
076      }
077      else {
078        try {
079          return finder.resolve(packageName, recurse);
080        }
081        catch (URISyntaxException e) {
082          throw new IOException(e);
083        }
084      }
085    } else {
086      return Collections.emptyList();
087    }
088  }
089
090  @Override
091  public JavaFileObject getJavaFileForOutput(Location location, String className, JavaFileObject.Kind kind, FileObject sibling) throws IOException {
092
093    if (location != StandardLocation.CLASS_OUTPUT) {
094      throw new IOException("Location " + location + " not supported");
095    }
096    if (kind != JavaFileObject.Kind.CLASS) {
097      throw new IOException("Kind " + kind + " not supported");
098    }
099
100    //
101    JavaClassFileObject clazz = classes.get(className);
102    if (clazz == null) {
103      try {
104        classes.put(className, clazz = new JavaClassFileObject(className));
105      }
106      catch (URISyntaxException e) {
107        throw new IOException(e);
108      }
109    }
110    return clazz;
111  }
112}