use of javax.tools.FileObject in project SpongeAPI by SpongePowered.
the class PluginProcessor method createWriter.
private BufferedWriter createWriter() throws IOException {
if (this.outputPath != null) {
getMessager().printMessage(NOTE, "Writing plugin metadata to " + this.outputPath);
return Files.newBufferedWriter(this.outputPath);
}
FileObject obj = this.processingEnv.getFiler().createResource(CLASS_OUTPUT, "", McModInfo.STANDARD_FILENAME);
getMessager().printMessage(NOTE, "Writing plugin metadata to " + obj.toUri());
return new BufferedWriter(obj.openWriter());
}
use of javax.tools.FileObject in project Nucleus by NucleusPowered.
the class StoreProcessor method process.
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
Map<Element, String> classes = new HashMap<>();
Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(Store.class);
for (Element element : elements) {
// Only storing classes.
if (element.getKind().isClass()) {
Store s = element.getAnnotation(Store.class);
classes.put(element, s.isRoot() ? null : s.value());
}
}
// Get the root elements
ClassElementVisitor cev = new ClassElementVisitor();
Map<String, String> conv = classes.entrySet().stream().filter(x -> x.getValue() == null).map(x -> cev.visit(x.getKey(), true)).filter(Objects::nonNull).collect(Collectors.toMap(x -> x.pa, x -> x.cl));
final Map<String, Map<String, List<String>>> result = conv.values().stream().collect(Collectors.toMap(x -> x, x -> new HashMap<>()));
classes.entrySet().stream().filter(x -> x.getValue() != null).map(x -> {
StringTuple st = cev.visit(x.getKey(), false);
if (st != null) {
return new StringTuple(x.getValue(), st.cl);
}
return null;
}).filter(Objects::nonNull).forEach(x -> {
// Check the class vs package name
conv.entrySet().stream().filter(y -> x.cl.startsWith(y.getKey())).distinct().findFirst().ifPresent(y -> result.get(y.getValue()).computeIfAbsent(x.pa, z -> new ArrayList<>()).add(x.cl));
});
if (!classes.isEmpty()) {
// Write this file
try {
FileObject fo = this.processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "assets.nucleus", "classes.json");
try (Writer os = fo.openWriter()) {
os.write(new GsonBuilder().setPrettyPrinting().create().toJson(result));
os.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
use of javax.tools.FileObject in project neo4j by neo4j.
the class PublicApiAnnotationProcessor method generateSignature.
private void generateSignature() throws IOException {
// only verify on request
if (!Boolean.getBoolean(VERIFY_TOGGLE)) {
return;
}
if (!publicElements.isEmpty()) {
StringBuilder sb = new StringBuilder();
for (final String element : publicElements) {
sb.append(element).append(newLine);
}
String newSignature = sb.toString();
// Write new signature
final FileObject file = processingEnv.getFiler().createResource(CLASS_OUTPUT, "", GENERATED_SIGNATURE_DESTINATION);
try (BufferedWriter writer = new BufferedWriter(file.openWriter())) {
writer.write(newSignature);
}
if (!testExecution) {
// Verify files
Path path = Path.of(file.toUri());
Path metaPath = getAndAssertParent(path, "META-INF");
Path classesPath = getAndAssertParent(metaPath, "classes");
Path targetPath = getAndAssertParent(classesPath, "target");
Path mavenModulePath = requireNonNull(targetPath.getParent());
Path oldSignaturePath = mavenModulePath.resolve("PublicApi.txt");
if (Boolean.getBoolean("overwrite")) {
info("Overwriting " + oldSignaturePath);
Files.writeString(oldSignaturePath, newSignature, UTF_8, WRITE, CREATE, TRUNCATE_EXISTING);
}
if (!Files.exists(oldSignaturePath)) {
error(format("Missing file %s, use `-Doverwrite` to create it.", oldSignaturePath));
return;
}
String oldSignature = Files.readString(oldSignaturePath, UTF_8);
if (!oldSignature.equals(newSignature)) {
oldSignature = oldSignature.replace(WINDOWS_NEW_LINE, DEFAULT_NEW_LINE);
newSignature = newSignature.replace(WINDOWS_NEW_LINE, DEFAULT_NEW_LINE);
if (!oldSignature.equals(newSignature)) {
StringBuilder diff = diff(oldSignaturePath);
error(format("Public API signature mismatch. The generated signature, %s, does not match the old signature in %s.%n" + "Specify `-Doverwrite` to maven to replace it. Changed public elements, compared to the committed PublicApi.txt:%n%s%n", path, oldSignaturePath, diff));
}
} else {
info("Public API signature matches. " + oldSignaturePath);
}
}
}
}
use of javax.tools.FileObject in project maven-plugins by apache.
the class SimpleAnnotationProcessor method process.
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
if (annotations.isEmpty()) {
return true;
}
// assert that commons-lang3 is on the classpath
try {
getClass().getClassLoader().loadClass("org.apache.commons.lang3.StringUtils");
} catch (ClassNotFoundException expected) {
throw new RuntimeException("Expected org.apache.commons.lang3.StringUtils to be on the processorpath," + "because it is a declared dependency of the annotation processor.");
}
// assert that commons-io is NOT on the classpath, as it is only a project dependency in "annotation-user"
try {
getClass().getClassLoader().loadClass("org.apache.commons.io.IOUtils");
throw new RuntimeException("Expected a ClassNotFoundException because " + "org.apache.commons.io.IOUtils is not supposed to be on the processorpath.");
} catch (ClassNotFoundException expected) {
// expected.
}
Filer filer = processingEnv.getFiler();
Elements elementUtils = processingEnv.getElementUtils();
Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(annotations.iterator().next());
for (Element element : elements) {
Name name = element.getSimpleName();
PackageElement packageElement = elementUtils.getPackageOf(element);
try {
Name packageName = packageElement.getQualifiedName();
FileObject resource = filer.createResource(StandardLocation.SOURCE_OUTPUT, packageName, name + ".txt", element);
Writer writer = resource.openWriter();
writer.write(name.toString());
writer.close();
String className = name + "Companion";
JavaFileObject javaFile = filer.createSourceFile(packageName + "." + className, element);
Writer javaWriter = javaFile.openWriter();
javaWriter.append("package ").append(packageName).append(";\n\n");
javaWriter.append("public class ").append(className).append(" {\n");
javaWriter.append(" public ").append(className).append("() {\n");
javaWriter.append(" System.out.println(\"Hey there!\");\n");
javaWriter.append(" }\n}\n");
javaWriter.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return !elements.isEmpty();
}
use of javax.tools.FileObject in project maven-plugins by apache.
the class ServiceProviderProcessor method writeServices.
private void writeServices() {
try {
FileObject out = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", "META-INF/one", new Element[0]);
OutputStream os = out.openOutputStream();
OutputStream os2 = processingEnv.getFiler().createSourceFile("org.Milos", new Element[0]).openOutputStream();
OutputStreamWriter osr = new OutputStreamWriter(os2);
try {
PrintWriter w = new PrintWriter(new OutputStreamWriter(os, "UTF-8"));
w.write("test");
w.flush();
String clazz = "package org;\n class Milos {}";
osr.write(clazz.toCharArray());
osr.flush();
} finally {
osr.close();
os.close();
}
} catch (IOException x) {
processingEnv.getMessager().printMessage(Kind.ERROR, "Failed to write to one: " + x.toString());
}
}
Aggregations