use of javax.annotation.processing.Filer 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.annotation.processing.Filer in project auto by google.
the class AutoServiceProcessor method generateConfigFiles.
private void generateConfigFiles() {
Filer filer = processingEnv.getFiler();
for (String providerInterface : providers.keySet()) {
String resourceFile = "META-INF/services/" + providerInterface;
log("Working on resource file: " + resourceFile);
try {
SortedSet<String> allServices = Sets.newTreeSet();
try {
// would like to be able to print the full path
// before we attempt to get the resource in case the behavior
// of filer.getResource does change to match the spec, but there's
// no good way to resolve CLASS_OUTPUT without first getting a resource.
FileObject existingFile = filer.getResource(StandardLocation.CLASS_OUTPUT, "", resourceFile);
log("Looking for existing resource file at " + existingFile.toUri());
Set<String> oldServices = ServicesFiles.readServiceFile(existingFile.openInputStream());
log("Existing service entries: " + oldServices);
allServices.addAll(oldServices);
} catch (IOException e) {
// According to the javadoc, Filer.getResource throws an exception
// if the file doesn't already exist. In practice this doesn't
// appear to be the case. Filer.getResource will happily return a
// FileObject that refers to a non-existent file but will throw
// IOException if you try to open an input stream for it.
log("Resource file did not already exist.");
}
Set<String> newServices = new HashSet<>(providers.get(providerInterface));
if (!allServices.addAll(newServices)) {
log("No new service entries being added.");
continue;
}
log("New service file contents: " + allServices);
FileObject fileObject = filer.createResource(StandardLocation.CLASS_OUTPUT, "", resourceFile);
try (OutputStream out = fileObject.openOutputStream()) {
ServicesFiles.writeServiceFile(allServices, out);
}
log("Wrote to: " + fileObject.toUri());
} catch (IOException e) {
fatalError("Unable to create " + resourceFile + ", " + e);
return;
}
}
}
use of javax.annotation.processing.Filer in project androidannotations by androidannotations.
the class FileHelper method findRootProjectHolder.
/**
* We use a dirty trick to find the AndroidManifest.xml file, since it's not
* available in the classpath. The idea is quite simple : create a fake class
* file, retrieve its URI, and start going up in parent folders to find the
* AndroidManifest.xml file. Any better solution will be appreciated.
*/
public static FileHolder findRootProjectHolder(ProcessingEnvironment processingEnv) throws FileNotFoundException {
FileHolder rootProjectHolder = findKaptRootProjectHolder(processingEnv);
if (rootProjectHolder != null) {
return rootProjectHolder;
}
Filer filer = processingEnv.getFiler();
FileObject dummySourceFile;
try {
dummySourceFile = filer.createResource(StandardLocation.SOURCE_OUTPUT, "", "dummy" + System.currentTimeMillis());
} catch (IOException ignored) {
throw new FileNotFoundException();
}
return createFileHolder(dummySourceFile.toUri().toString());
}
use of javax.annotation.processing.Filer in project Payara by payara.
the class RestModelExtensionProcessor method process.
@Override
public boolean process(Set<? extends TypeElement> elements, RoundEnvironment env) {
Messager messager = processingEnv.getMessager();
BufferedWriter bw = null;
try {
Map<String, List<String>> classes = new HashMap<String, List<String>>();
for (TypeElement te : elements) {
for (Element e : env.getElementsAnnotatedWith(te)) {
final RestModelExtension annotation = e.getAnnotation(RestModelExtension.class);
final String parent = annotation.parent();
List<String> list = classes.get(parent);
if (list == null) {
list = new ArrayList<String>();
classes.put(parent, list);
}
list.add(e.toString());
}
}
if (!classes.isEmpty()) {
final Filer filer = processingEnv.getFiler();
FileObject fo = filer.createResource(StandardLocation.CLASS_OUTPUT, "", "META-INF/restmodelextensions");
bw = new BufferedWriter(fo.openWriter());
// parent model:model extension
for (Map.Entry<String, List<String>> entry : classes.entrySet()) {
final String key = entry.getKey();
for (String ext : entry.getValue()) {
bw.write(key + ":" + ext + "\n");
}
}
bw.close();
}
} catch (IOException ex) {
messager.printMessage(Kind.ERROR, ex.getLocalizedMessage());
if (bw != null) {
try {
bw.close();
} catch (Exception e) {
}
}
}
return true;
}
use of javax.annotation.processing.Filer in project qpid-broker-j by apache.
the class SystemConfigFactoryGenerator method process.
@Override
public boolean process(final Set<? extends TypeElement> annotations, final RoundEnvironment roundEnv) {
if (roundEnv.processingOver()) {
return true;
}
Filer filer = processingEnv.getFiler();
try {
for (Element e : roundEnv.getElementsAnnotatedWith(SystemConfigFactoryConstructor.class)) {
if (e.getKind() == ElementKind.CONSTRUCTOR) {
ExecutableElement constructorElement = (ExecutableElement) e;
String factoryName = generateObjectFactory(filer, constructorElement);
}
}
} catch (Exception e) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Error: " + e.getLocalizedMessage());
}
return true;
}
Aggregations