use of javax.tools.FileObject in project camel by apache.
the class AnnotationProcessorHelper method loadResource.
public static String loadResource(ProcessingEnvironment processingEnv, String packageName, String fileName) {
Filer filer = processingEnv.getFiler();
FileObject resource;
String relativeName = packageName + "/" + fileName;
try {
resource = filer.getResource(StandardLocation.CLASS_OUTPUT, "", relativeName);
} catch (Throwable e) {
return "Cannot load classpath resource: " + relativeName + " due: " + e.getMessage();
}
if (resource == null) {
return null;
}
try {
InputStream is = resource.openInputStream();
return loadText(is, true);
} catch (Exception e) {
warning(processingEnv, "APT cannot load file: " + packageName + "/" + fileName);
}
return null;
}
use of javax.tools.FileObject in project qpid-broker-j by apache.
the class PluggableProcessor method loadExistingServicesFile.
private String loadExistingServicesFile(final Filer filer, String serviceName) {
String relativeName = "META-INF/services/" + serviceName;
try {
FileObject existingFile = filer.getResource(StandardLocation.CLASS_OUTPUT, "", relativeName);
BufferedReader r = new BufferedReader(new InputStreamReader(existingFile.openInputStream(), "UTF-8"));
String line;
while ((line = r.readLine()) != null) {
if (!line.matches(" *#")) {
factoryImplementations.get(serviceName).add(line);
}
}
r.close();
} catch (FileNotFoundException e) {
// no existing file (ignore)
} catch (IOException e) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Error loading existing services file: " + relativeName + " - " + e.getLocalizedMessage());
}
return relativeName;
}
use of javax.tools.FileObject in project vertx-examples by vert-x3.
the class CodeTransProcessor method process.
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
if (roundEnv.processingOver()) {
if (log != null) {
log.close();
}
return false;
}
if (outputDir != null && (outputDir.exists() || outputDir.mkdirs())) {
List<ExecutableElement> methodElts = new ArrayList<>();
try {
PrintWriter log = getLogger();
// Process all verticles automatically
TypeMirror verticleType = processingEnv.getElementUtils().getTypeElement(Verticle.class.getName()).asType();
for (Element rootElt : roundEnv.getRootElements()) {
Set<Modifier> modifiers = rootElt.getModifiers();
if (rootElt.getKind() == ElementKind.CLASS && !modifiers.contains(Modifier.ABSTRACT) && modifiers.contains(Modifier.PUBLIC) && processingEnv.getTypeUtils().isSubtype(rootElt.asType(), verticleType)) {
TypeElement typeElt = (TypeElement) rootElt;
for (Element enclosedElt : typeElt.getEnclosedElements()) {
if (enclosedElt.getKind() == ElementKind.METHOD) {
ExecutableElement methodElt = (ExecutableElement) enclosedElt;
if (methodElt.getSimpleName().toString().equals("start") && methodElt.getParameters().isEmpty()) {
methodElts.add(methodElt);
}
}
}
}
}
// Process CodeTranslate annotations
roundEnv.getElementsAnnotatedWith(CodeTranslate.class).forEach(annotatedElt -> {
methodElts.add((ExecutableElement) annotatedElt);
});
// Generate
for (ExecutableElement methodElt : methodElts) {
TypeElement typeElt = (TypeElement) methodElt.getEnclosingElement();
FileObject obj = processingEnv.getFiler().getResource(StandardLocation.SOURCE_PATH, "", typeElt.getQualifiedName().toString().replace('.', '/') + ".java");
File srcFolder = new File(obj.toUri()).getParentFile();
String filename = Case.SNAKE.format(Case.CAMEL.parse(typeElt.getSimpleName().toString()));
for (Lang lang : langs) {
if (isSkipped(typeElt, lang)) {
log.write("Skipping " + lang.getExtension() + " translation for " + typeElt.getQualifiedName() + "#" + methodElt.getSimpleName());
continue;
}
String folderPath = processingEnv.getElementUtils().getPackageOf(typeElt).getQualifiedName().toString().replace('.', '/');
File dstFolder = new File(new File(outputDir, lang.getExtension()), folderPath);
if (dstFolder.exists() || dstFolder.mkdirs()) {
try {
String translation = translator.translate(methodElt, lang);
File f = new File(dstFolder, filename + "." + lang.getExtension());
Files.write(f.toPath(), translation.getBytes(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
log.println("Generated " + f.getAbsolutePath());
copyDirRec(srcFolder, dstFolder, log);
} catch (Exception e) {
log.println("Skipping generation of " + typeElt.getQualifiedName());
e.printStackTrace(log);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return true;
} else {
return false;
}
}
use of javax.tools.FileObject in project copybara by google.
the class MarkdownGenerator method processDoc.
private void processDoc(SetMultimap<Class<? extends Annotation>, Element> elementsByAnnotation) throws ElementException, IOException {
Multimap<String, String> docByElementType = ArrayListMultimap.create();
for (Element element : elementsByAnnotation.get(SkylarkModule.class)) {
TypeElement module = (TypeElement) element;
StringBuilder sb = new StringBuilder();
SkylarkModule skyModule = module.getAnnotation(SkylarkModule.class);
if (!skyModule.documented()) {
continue;
}
sb.append("## ").append(skyModule.name()).append("\n\n");
sb.append(skyModule.doc());
sb.append("\n\n");
// Generate flags associated with the whole module
sb.append(generateFlagsInfo(module));
for (Element member : module.getEnclosedElements()) {
sb.append(generateFunctionDocumentation(module, skyModule, member));
}
docByElementType.put(skyModule.name(), sb.toString());
}
for (String group : docByElementType.keySet()) {
FileObject resource = processingEnv.getFiler().createResource(StandardLocation.SOURCE_OUTPUT, "", group + ".copybara.md");
try (Writer writer = resource.openWriter()) {
for (String groupValues : docByElementType.get(group)) {
writer.append(groupValues).append("\n");
}
}
}
}
use of javax.tools.FileObject in project graal by oracle.
the class InstrumentRegistrationProcessor method loadExistingTypes.
static void loadExistingTypes(ProcessingEnvironment env, List<TypeElement> instruments, String filename, String pre) {
Set<String> typeNames = new HashSet<>();
for (TypeElement type : instruments) {
typeNames.add(ElementUtils.getQualifiedName(type));
}
Properties current = new Properties();
try {
FileObject object = env.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", filename);
current.load(object.openInputStream());
} catch (IOException e1) {
env.getMessager().printMessage(Kind.NOTE, filename + e1.getMessage(), null);
// does not exist yet.
// better way to detect this?
}
for (int cnt = 1; ; cnt++) {
String prefix = pre + cnt + ".";
String className = current.getProperty(prefix + "className");
if (className == null) {
break;
}
env.getMessager().printMessage(Kind.NOTE, filename + className, null);
TypeElement foundType = env.getElementUtils().getTypeElement(className);
if (foundType != null && !typeNames.contains(ElementUtils.getQualifiedName(foundType))) {
instruments.add(foundType);
}
}
}
Aggregations