
br.com.objectos.code.testing.CompilationJavaFileManager Maven / Gradle / Ivy
The newest version!
/*
* Copyright (C) 2014-2020 Objectos Software LTDA.
*
* 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 br.com.objectos.code.testing;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import javax.tools.FileObject;
import javax.tools.ForwardingJavaFileManager;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
import javax.tools.JavaFileObject.Kind;
import javax.tools.StandardLocation;
class CompilationJavaFileManager extends ForwardingJavaFileManager {
final Map generatedClassFiles = new LinkedHashMap<>();
final Map generatedJavaFiles = new LinkedHashMap<>();
final Map generatedResources = new LinkedHashMap<>();
CompilationJavaFileManager(JavaCompiler compiler) {
super(compiler.getStandardFileManager(null, Locale.getDefault(), StandardCharsets.UTF_8));
}
@Override
public final FileObject getFileForOutput(
Location location, String packageName, String relativeName, FileObject sibling)
throws IOException {
if (StandardLocation.CLASS_OUTPUT.equals(location)) {
String name = packageName.replace('.', '/') + relativeName;
return generatedResources.computeIfAbsent(name,
key -> new WritableByteArrayJavaFileObject(name, Kind.OTHER));
}
else {
return super.getFileForOutput(location, packageName, relativeName, sibling);
}
}
@Override
public final JavaFileObject getJavaFileForOutput(
Location location, String className, Kind kind, FileObject sibling)
throws IOException {
if (StandardLocation.SOURCE_OUTPUT.equals(location)) {
return generatedJavaFiles.computeIfAbsent(className,
key -> new WritableStringJavaFileObject(key, kind));
}
else if (StandardLocation.CLASS_OUTPUT.equals(location)) {
return generatedClassFiles.computeIfAbsent(className,
key -> new WritableByteArrayJavaFileObject(key, kind));
}
else {
return super.getJavaFileForOutput(location, className, kind, sibling);
}
}
@Override
public final boolean isSameFile(FileObject a, FileObject b) {
return a.toUri().equals(b.toUri());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy