Package org.helidb.doc

Source Code of org.helidb.doc.TestClassFileManager

/* HeliDB -- A simple database for Java, http://www.helidb.org
* Copyright (C) 2008, 2009 Karl Gustafsson
*
* This file is a part of HeliDB.
*
* HeliDB is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeliDB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package org.helidb.doc;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.locks.Lock;

import javax.tools.FileObject;
import javax.tools.ForwardingJavaFileManager;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
import javax.tools.StandardLocation;
import javax.tools.JavaFileObject.Kind;

import org.entityfs.Directory;
import org.entityfs.DirectoryView;
import org.entityfs.EFile;
import org.entityfs.EntityView;
import org.entityfs.el.AbsoluteLocation;
import org.entityfs.el.RelativeLocation;
import org.entityfs.util.Directories;
import org.entityfs.util.Entities;
import org.entityfs.util.cap.entity.ECFileResolvableUtil;
import org.entityfs.util.filter.entity.EFileNameExtensionFilter;
import org.entityfs.util.itr.FilteringIterator;

class TestClassFileManager<T extends JavaFileManager> extends ForwardingJavaFileManager<T>
{
  private final Directory m_outDir;
  private final ClassLoader m_classLoader;

  TestClassFileManager(T mgr, Directory outDir, ClassLoader cl)
  {
    super(mgr);
    m_outDir = outDir;
    m_classLoader = cl;
  }

  @Override
  public boolean hasLocation(JavaFileManager.Location location)
  {
    // I have the following locations
    return location == StandardLocation.SOURCE_PATH || location == StandardLocation.CLASS_PATH || location == StandardLocation.CLASS_OUTPUT;
  }

  @Override
  public FileObject getFileForInput(JavaFileManager.Location loc, String packageName, String relativeName) throws IOException
  {
    System.out.println(loc + " " + packageName + " " + relativeName);
    return super.getFileForInput(loc, packageName, relativeName);
  }

  @Override
  public JavaFileObject getJavaFileForInput(JavaFileManager.Location loc, String className, JavaFileObject.Kind kind) throws IOException
  {
    System.out.println(loc + " " + className + " " + kind);
    return super.getJavaFileForInput(loc, className, kind);
  }

  @Override
  public JavaFileObject getJavaFileForOutput(JavaFileManager.Location loc, String className, JavaFileObject.Kind kind, FileObject sibling)
  {
    Directory dir;
    int pos = className.lastIndexOf('.');
    if (pos >= 0)
    {
      dir = Directories.putIfAbsentDirectory(m_outDir, new RelativeLocation(className.substring(0, pos).replace('.', '/')));
      className = className.substring(pos + 1);
    }
    else
    {
      dir = m_outDir;
    }
    return new FooSimpleJavaFileObject(new File(ECFileResolvableUtil.getFileObject(dir), className + ".class").toURI(), kind);
  }

  @Override
  public ClassLoader getClassLoader(JavaFileManager.Location loc)
  {
    return m_classLoader;
  }

  @Override
  public String inferBinaryName(JavaFileManager.Location location, JavaFileObject file)
  {
    if (file instanceof EFileJavaFileObjectAdapter)
    {
      String res = ((EFileJavaFileObjectAdapter) file).getRelativeLocation().getLocation().replace('/', '.');
      return res.substring(0, res.lastIndexOf('.'));
    }
    else
    {
      return super.inferBinaryName(location, file);
    }
  }

  private Collection<JavaFileObject> getClassFilesForDirectory(DirectoryView baseDir, Directory d, boolean recurse)
  {
    AbsoluteLocation baseDirLoc = Entities.getAbsoluteLocation(baseDir);
    ArrayList<JavaFileObject> res = new ArrayList<JavaFileObject>();
    Lock rl = d.lockForReading();
    try
    {
      Iterator<EntityView> itr = new FilteringIterator<EntityView>((recurse ? Directories.getDepthLastIterator(d) : Directories.getIterator(d)), new EFileNameExtensionFilter("class"));
      while (itr.hasNext())
      {
        EFile f = (EFile) itr.next();
        res.add(new EFileJavaFileObjectAdapter(f, Kind.CLASS, baseDirLoc, Directories.getChildRelativeLocation(d, f)));
      }
    }
    finally
    {
      rl.unlock();
    }
    return res;
  }

  private Collection<JavaFileObject> listClasses(JavaFileManager.Location location, String packageName, boolean recurse)
  {
    if (location == StandardLocation.CLASS_PATH)
    {
      RelativeLocation packageLocation = new RelativeLocation(packageName.replace('.', '/'));
      Collection<JavaFileObject> res = new ArrayList<JavaFileObject>();
      // Search the target dir
      Directory classDir = (Directory) Directories.getEntityOrNull(m_outDir, packageLocation);
      if (classDir != null)
      {
        res.addAll(getClassFilesForDirectory(m_outDir, classDir, recurse));
      }
      return res;
    }
    else
    {
      throw new UnsupportedOperationException("The location " + location + " is not handled");
    }
  }

  private void addAllFromIterable(Collection<JavaFileObject> target, Iterable<JavaFileObject> source)
  {
    for (JavaFileObject jfo : source)
    {
      target.add(jfo);
    }
  }

  @Override
  public Iterable<JavaFileObject> list(JavaFileManager.Location location, String packageName, Set<JavaFileObject.Kind> kinds, boolean recurse) throws IOException
  {
    if (location == StandardLocation.PLATFORM_CLASS_PATH || location == StandardLocation.SOURCE_PATH)
    {
      return super.list(location, packageName, kinds, recurse);
    }
    else
    {
      // A location that we might handle
      List<JavaFileObject> res = new ArrayList<JavaFileObject>();
      for (JavaFileObject.Kind k : kinds)
      {
        switch (k)
        {
          case CLASS:
            res.addAll(listClasses(location, packageName, recurse));
            addAllFromIterable(res, super.list(location, packageName, kinds, recurse));
            break;
          default:
            Set<JavaFileObject.Kind> ks = EnumSet.of(k);
            for (JavaFileObject jfo : super.list(location, packageName, ks, recurse))
            {
              res.add(jfo);
            }
        }
      }
      return res;
    }
  }
}
TOP

Related Classes of org.helidb.doc.TestClassFileManager

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.