All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jppf.utils.compilation.InMemoryFileManager Maven / Gradle / Ivy

There is a newer version: 6.3-alpha
Show newest version
/*
 * JPPF.
 * Copyright (C) 2005-2015 JPPF Team.
 * http://www.jppf.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jppf.utils.compilation;

import java.io.IOException;
import java.util.*;

import javax.tools.*;

/**
 * A {@link JavaFileManager} which outputs the classes byte code to memory.
 * @param  the type of file manager to delegate to.
 * @author Laurent Cohen
 */
class InMemoryFileManager extends ForwardingJavaFileManager
{
  /**
   * Map of class names to JavaFileObject instances for the bytecode.
   */
  private Map classMap = new HashMap<>();

  /**
   * Construct this file manager.
   * @param fileManager the file manager to delegate to.
   */
  public InMemoryFileManager(final M fileManager)
  {
    super(fileManager);
  }

  /**
   * This is where we use our in-memory {@link BytecodeObject} objects, whenever kind is equal to {@link JavaFileObject.Kind#CLASS}.
   * 

{@inheritDoc} */ @Override public JavaFileObject getJavaFileForOutput(final Location location, final String className, final JavaFileObject.Kind kind, final FileObject sibling) throws IOException { // special processing for class files only if (kind == JavaFileObject.Kind.CLASS) { BytecodeObject bytecode = classMap.get(className); if (bytecode == null) { bytecode = new BytecodeObject(className); classMap.put(className, bytecode); } return bytecode; } // otherwise delegate to the initial file manager return super.getJavaFileForOutput(location, className, kind, sibling); } /** * Get the bytecode of all classes compiled with this file manager. * @return a mapping of class names to the corresponding bytecode expressed as a byte[]. */ public Map getAllByteCodes() { Map result = new HashMap<>(); for (Map.Entry entry: classMap.entrySet()) { result.put(entry.getKey(), entry.getValue().getBytecode()); } return result; } /** * We cleanup the bytecode map, because {@link BytecodeObject} does not make a copy of its internal byte array, * which could prevent this file manager instance from being garbage-collected. *

This implies that a call to {@link #getAllByteCodes()} after this method has been invoked will * return an empty Map. * @throws IOException if any I/O error occurs. */ @Override public void close() throws IOException { classMap.clear(); super.close(); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy