net.openhft.compiler.MyJavaFileManager Maven / Gradle / Ivy
/*
* Copyright 2014 Higher Frequency Trading
*
* http://www.higherfrequencytrading.com
*
* 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 net.openhft.compiler;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URI;
import java.util.*;
import javax.tools.*;
import javax.tools.JavaFileObject.Kind;
import org.jetbrains.annotations.NotNull;
class MyJavaFileManager implements JavaFileManager {
private final StandardJavaFileManager fileManager;
// synchronizing due to ConcurrentModificationException
private final Map buffers = Collections.synchronizedMap(new LinkedHashMap<>());
MyJavaFileManager(StandardJavaFileManager fileManager) {
this.fileManager = fileManager;
}
public Iterable> listLocationsForModules(final Location location) {
return invokeNamedMethodIfAvailable(location, "listLocationsForModules");
}
public String inferModuleName(final Location location) {
return invokeNamedMethodIfAvailable(location, "inferModuleName");
}
@Override
public ClassLoader getClassLoader(Location location) {
return fileManager.getClassLoader(location);
}
@Override
public Iterable list(Location location, String packageName, Set kinds, boolean recurse) throws IOException {
return fileManager.list(location, packageName, kinds, recurse);
}
@Override
public String inferBinaryName(Location location, JavaFileObject file) {
return fileManager.inferBinaryName(location, file);
}
@Override
public boolean isSameFile(FileObject a, FileObject b) {
return fileManager.isSameFile(a, b);
}
@Override
public boolean handleOption(String current, Iterator remaining) {
return fileManager.handleOption(current, remaining);
}
@Override
public boolean hasLocation(Location location) {
return fileManager.hasLocation(location);
}
@Override
public JavaFileObject getJavaFileForInput(Location location, String className, Kind kind) throws IOException {
if (location == StandardLocation.CLASS_OUTPUT) {
boolean success = false;
final byte[] bytes;
synchronized (buffers) {
success = buffers.containsKey(className) && kind == Kind.CLASS;
bytes = buffers.get(className).toByteArray();
}
if (success) {
return new SimpleJavaFileObject(URI.create(className), kind) {
@NotNull
@Override
public InputStream openInputStream() {
return new ByteArrayInputStream(bytes);
}
};
}
}
return fileManager.getJavaFileForInput(location, className, kind);
}
@NotNull
@Override
public JavaFileObject getJavaFileForOutput(Location location, final String className, Kind kind, FileObject sibling) {
return new SimpleJavaFileObject(URI.create(className), kind) {
@NotNull
@Override
public OutputStream openOutputStream() {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
buffers.put(className, baos);
return baos;
}
};
}
@Override
public FileObject getFileForInput(Location location, String packageName, String relativeName) throws IOException {
return fileManager.getFileForInput(location, packageName, relativeName);
}
@Override
public FileObject getFileForOutput(Location location, String packageName, String relativeName, FileObject sibling) throws IOException {
return fileManager.getFileForOutput(location, packageName, relativeName, sibling);
}
@Override
public void flush() {
// Do nothing
}
@Override
public void close() throws IOException {
fileManager.close();
}
@Override
public int isSupportedOption(String option) {
return fileManager.isSupportedOption(option);
}
public void clearBuffers() {
buffers.clear();
}
@NotNull
public Map getAllBuffers() {
synchronized (buffers) {
Map ret = new LinkedHashMap<>(buffers.size() * 2);
for (Map.Entry entry : buffers.entrySet()) {
ret.put(entry.getKey(), entry.getValue().toByteArray());
}
return ret;
}
}
@SuppressWarnings("unchecked")
private T invokeNamedMethodIfAvailable(final Location location, final String name) {
final Method[] methods = fileManager.getClass().getDeclaredMethods();
for (Method method : methods) {
if (method.getName().equals(name) && method.getParameterTypes().length == 1
&& method.getParameterTypes()[0] == Location.class) {
try {
// unsafe.putBoolean(method, OVERRIDE_OFFSET, true);
return (T) method.invoke(fileManager, location);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new UnsupportedOperationException("Unable to invoke method " + name);
}
}
}
throw new UnsupportedOperationException("Unable to find method " + name);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy