use of javax.tools.FileObject in project graal by oracle.
the class InstrumentRegistrationProcessor method generateFile.
private void generateFile(List<TypeElement> instruments) {
String filename = "META-INF/truffle/instrument";
Properties p = new SortedProperties();
int numInstruments = loadIfFileAlreadyExists(filename, p);
for (TypeElement l : instruments) {
Registration annotation = l.getAnnotation(Registration.class);
if (annotation == null) {
continue;
}
int instNum = findInstrument(annotation.id(), p);
if (instNum == 0) {
// not found
numInstruments += 1;
instNum = numInstruments;
}
String prefix = "instrument" + instNum + ".";
String className = processingEnv.getElementUtils().getBinaryName(l).toString();
p.setProperty(prefix + "id", annotation.id());
p.setProperty(prefix + "name", annotation.name());
p.setProperty(prefix + "version", annotation.version());
p.setProperty(prefix + "className", className);
p.setProperty(prefix + "internal", Boolean.toString(annotation.internal()));
int serviceCounter = 0;
for (AnnotationMirror anno : l.getAnnotationMirrors()) {
final String annoName = anno.getAnnotationType().asElement().toString();
if (Registration.class.getCanonicalName().equals(annoName)) {
for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : anno.getElementValues().entrySet()) {
final Name attrName = entry.getKey().getSimpleName();
if (attrName.contentEquals("services")) {
AnnotationValue attrValue = entry.getValue();
List<?> classes = (List<?>) attrValue.getValue();
for (Object clazz : classes) {
AnnotationValue clazzValue = (AnnotationValue) clazz;
p.setProperty(prefix + "service" + serviceCounter++, clazzValue.getValue().toString());
}
}
}
}
}
}
if (numInstruments > 0) {
try {
FileObject file = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", filename);
try (OutputStream os = file.openOutputStream()) {
p.store(os, "Generated by " + InstrumentRegistrationProcessor.class.getName());
}
} catch (IOException e) {
processingEnv.getMessager().printMessage(Kind.ERROR, e.getMessage(), instruments.get(0));
}
}
}
use of javax.tools.FileObject in project service-proxy by membrane.
the class SpringConfigurationXSDGeneratingAnnotationProcessor method read.
@SuppressWarnings("unchecked")
private void read() {
if (cache != null)
return;
cache = new HashMap<Class<? extends Annotation>, HashSet<Element>>();
try {
FileObject o = processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", "META-INF/membrane.cache");
BufferedReader r = new BufferedReader(o.openReader(false));
try {
if (!CACHE_FILE_FORMAT_VERSION.equals(r.readLine()))
return;
HashSet<Element> currentSet = null;
Class<? extends Annotation> annotationClass = null;
while (true) {
String line = r.readLine();
if (line == null)
break;
if (line.startsWith(" ")) {
line = line.substring(1);
TypeElement element = null;
try {
element = processingEnv.getElementUtils().getTypeElement(line);
} catch (RuntimeException e) {
// do nothing (Eclipse)
}
if (element != null) {
if (element.getAnnotation(annotationClass) != null)
currentSet.add(element);
}
} else {
try {
annotationClass = (Class<? extends Annotation>) getClass().getClassLoader().loadClass(line);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
currentSet = new HashSet<Element>();
cache.put(annotationClass, currentSet);
}
}
} finally {
r.close();
}
} catch (FileNotFoundException e) {
// do nothing (Maven)
} catch (IOException e) {
// do nothing (Eclipse)
}
for (Set<Element> e : cache.values()) {
String status = "read " + e.size();
log(status);
}
}
use of javax.tools.FileObject in project service-proxy by membrane.
the class BlueprintParsers method writeParserDefinitior.
public void writeParserDefinitior(Model m) throws IOException {
for (MainInfo main : m.getMains()) {
List<Element> sources = new ArrayList<Element>();
sources.addAll(main.getInterceptorElements());
sources.add(main.getElement());
try {
FileObject o = processingEnv.getFiler().createSourceFile(main.getAnnotation().outputPackage() + ".blueprint" + ".BlueprintNamespaceParser", sources.toArray(new Element[0]));
BufferedWriter bw = new BufferedWriter(o.openWriter());
try {
bw.write("/* Copyright 2014 predic8 GmbH, www.predic8.com\r\n" + "\r\n" + " Licensed under the Apache License, Version 2.0 (the \"License\");\r\n" + " you may not use this file except in compliance with the License.\r\n" + " You may obtain a copy of the License at\r\n" + "\r\n" + " http://www.apache.org/licenses/LICENSE-2.0\r\n" + "\r\n" + " Unless required by applicable law or agreed to in writing, software\r\n" + " distributed under the License is distributed on an \"AS IS\" BASIS,\r\n" + " WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r\n" + " See the License for the specific language governing permissions and\r\n" + " limitations under the License. */\r\n" + "\r\n" + "package " + main.getAnnotation().outputPackage() + ".blueprint;\r\n" + "\r\n" + "/**\r\n" + " * Automatically generated by " + BlueprintParsers.class.getName() + ".\r\n" + " */\r\n" + "public class BlueprintNamespaceParser extends com.predic8.membrane.annot.parser.BlueprintNamespaceParser {\r\n" + "\r\n" + " public void init() {\r\n");
for (ElementInfo i : main.getIis()) {
if (i.getAnnotation().topLevel()) {
bw.write(" registerGlobalBeanDefinitionParser(\"" + i.getAnnotation().name() + "\", new " + i.getParserClassSimpleName() + "());\r\n");
} else {
for (ChildElementDeclarationInfo cedi : i.getUsedBy()) {
for (ChildElementInfo cei : cedi.getUsedBy()) {
TypeElement element = cei.getEi().getElement();
String clazz = AnnotUtils.getRuntimeClassName(element);
bw.write(" registerLocalBeanDefinitionParser(\"" + clazz + "\", \"" + i.getAnnotation().name() + "\", new " + i.getParserClassSimpleName() + "());\r\n");
}
}
}
}
bw.write(" }\r\n" + "}\r\n" + "");
} finally {
bw.close();
}
} catch (FilerException e) {
if (e.getMessage().contains("Source file already created"))
return;
throw e;
}
}
}
use of javax.tools.FileObject in project service-proxy by membrane.
the class Parsers method writeParserDefinitior.
public void writeParserDefinitior(Model m) throws IOException {
for (MainInfo main : m.getMains()) {
List<Element> sources = new ArrayList<Element>();
sources.addAll(main.getInterceptorElements());
sources.add(main.getElement());
try {
FileObject o = processingEnv.getFiler().createSourceFile(main.getAnnotation().outputPackage() + ".NamespaceHandlerAutoGenerated", sources.toArray(new Element[0]));
BufferedWriter bw = new BufferedWriter(o.openWriter());
try {
bw.write("/* Copyright 2012,2013 predic8 GmbH, www.predic8.com\r\n" + "\r\n" + " Licensed under the Apache License, Version 2.0 (the \"License\");\r\n" + " you may not use this file except in compliance with the License.\r\n" + " You may obtain a copy of the License at\r\n" + "\r\n" + " http://www.apache.org/licenses/LICENSE-2.0\r\n" + "\r\n" + " Unless required by applicable law or agreed to in writing, software\r\n" + " distributed under the License is distributed on an \"AS IS\" BASIS,\r\n" + " WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r\n" + " See the License for the specific language governing permissions and\r\n" + " limitations under the License. */\r\n" + "\r\n" + "package " + main.getAnnotation().outputPackage() + ";\r\n" + "\r\n" + "/**\r\n" + " * Automatically generated by " + Parsers.class.getName() + ".\r\n" + " */\r\n" + "public class NamespaceHandlerAutoGenerated {\r\n" + "\r\n" + " public static void registerBeanDefinitionParsers(NamespaceHandler nh) {\r\n");
for (ElementInfo i : main.getIis()) {
if (i.getAnnotation().topLevel()) {
bw.write(" nh.registerGlobalBeanDefinitionParser(\"" + i.getAnnotation().name() + "\", new " + i.getParserClassSimpleName() + "());\r\n");
} else {
for (ChildElementDeclarationInfo cedi : i.getUsedBy()) {
for (ChildElementInfo cei : cedi.getUsedBy()) {
TypeElement element = cei.getEi().getElement();
String clazz = AnnotUtils.getRuntimeClassName(element);
bw.write(" nh.registerLocalBeanDefinitionParser(\"" + clazz + "\", \"" + i.getAnnotation().name() + "\", new " + i.getParserClassSimpleName() + "());\r\n");
}
}
}
}
bw.write(" }\r\n" + "}\r\n" + "");
} finally {
bw.close();
}
} catch (FilerException e) {
if (e.getMessage().contains("Source file already created"))
return;
throw e;
}
}
}
use of javax.tools.FileObject in project service-proxy by membrane.
the class Schemas method writeXSD.
public void writeXSD(Model m) throws IOException {
try {
for (MainInfo main : m.getMains()) {
List<Element> sources = new ArrayList<Element>();
sources.add(main.getElement());
sources.addAll(main.getInterceptorElements());
FileObject o = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, main.getAnnotation().outputPackage(), main.getAnnotation().outputName(), sources.toArray(new Element[0]));
BufferedWriter bw = new BufferedWriter(o.openWriter());
try {
assembleXSD(bw, m, main);
} finally {
bw.close();
}
}
} catch (FilerException e) {
if (e.getMessage().contains("Source file already created"))
return;
throw e;
}
}
Aggregations