use of javax.tools.FileObject in project graal by oracle.
the class LanguageRegistrationProcessor method generateFile.
private void generateFile(List<TypeElement> languages) {
String filename = "META-INF/truffle/language";
// sorted properties
Properties p = new SortedProperties();
int cnt = 0;
for (TypeElement l : languages) {
Registration annotation = l.getAnnotation(Registration.class);
if (annotation == null) {
continue;
}
String prefix = "language" + ++cnt + ".";
String className = processingEnv.getElementUtils().getBinaryName(l).toString();
String id = annotation.id();
if (id != null && !id.isEmpty()) {
p.setProperty(prefix + "id", id);
}
p.setProperty(prefix + "name", annotation.name());
p.setProperty(prefix + "implementationName", annotation.implementationName());
p.setProperty(prefix + "version", annotation.version());
p.setProperty(prefix + "className", className);
String[] mimes = annotation.mimeType();
for (int i = 0; i < mimes.length; i++) {
p.setProperty(prefix + "mimeType." + i, mimes[i]);
}
String[] dependencies = annotation.dependentLanguages();
Arrays.sort(dependencies);
for (int i = 0; i < dependencies.length; i++) {
p.setProperty(prefix + "dependentLanguage." + i, dependencies[i]);
}
p.setProperty(prefix + "interactive", Boolean.toString(annotation.interactive()));
p.setProperty(prefix + "internal", Boolean.toString(annotation.internal()));
}
if (cnt > 0) {
try {
FileObject file = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", filename, languages.toArray(new Element[0]));
try (OutputStream os = file.openOutputStream()) {
p.store(os, "Generated by " + LanguageRegistrationProcessor.class.getName());
}
} catch (IOException e) {
if (e instanceof FilerException) {
if (e.getMessage().startsWith("Source file already created")) {
// ignore source file already created errors
return;
}
}
processingEnv.getMessager().printMessage(Kind.ERROR, e.getMessage(), languages.get(0));
}
}
}
use of javax.tools.FileObject in project graal by oracle.
the class OptionProcessor method processElement.
private void processElement(Element element, OptionsInfo info) {
if (!element.getModifiers().contains(Modifier.STATIC)) {
processingEnv.getMessager().printMessage(Kind.ERROR, "Option field must be static", element);
return;
}
if (element.getModifiers().contains(Modifier.PRIVATE)) {
processingEnv.getMessager().printMessage(Kind.ERROR, "Option field cannot be private", element);
return;
}
Option annotation = element.getAnnotation(Option.class);
assert annotation != null;
assert element instanceof VariableElement;
assert element.getKind() == ElementKind.FIELD;
VariableElement field = (VariableElement) element;
String fieldName = field.getSimpleName().toString();
Elements elements = processingEnv.getElementUtils();
Types types = processingEnv.getTypeUtils();
TypeMirror fieldType = field.asType();
if (fieldType.getKind() != TypeKind.DECLARED) {
processingEnv.getMessager().printMessage(Kind.ERROR, "Option field must be of type " + OptionKey.class.getName(), element);
return;
}
DeclaredType declaredFieldType = (DeclaredType) fieldType;
TypeMirror optionKeyType = elements.getTypeElement(OptionKey.class.getName()).asType();
if (!types.isSubtype(fieldType, types.erasure(optionKeyType))) {
String msg = String.format("Option field type %s is not a subclass of %s", fieldType, optionKeyType);
processingEnv.getMessager().printMessage(Kind.ERROR, msg, element);
return;
}
if (!field.getModifiers().contains(Modifier.STATIC)) {
processingEnv.getMessager().printMessage(Kind.ERROR, "Option field must be static", element);
return;
}
if (field.getModifiers().contains(Modifier.PRIVATE)) {
processingEnv.getMessager().printMessage(Kind.ERROR, "Option field cannot be private", element);
return;
}
String optionName = annotation.name();
if (optionName.equals("")) {
optionName = fieldName;
}
if (!Character.isUpperCase(optionName.charAt(0))) {
processingEnv.getMessager().printMessage(Kind.ERROR, "Option name must start with an upper case letter", element);
return;
}
DeclaredType declaredOptionKeyType = declaredFieldType;
while (!types.isSameType(types.erasure(declaredOptionKeyType), types.erasure(optionKeyType))) {
List<? extends TypeMirror> directSupertypes = types.directSupertypes(declaredFieldType);
assert !directSupertypes.isEmpty();
declaredOptionKeyType = (DeclaredType) directSupertypes.get(0);
}
assert !declaredOptionKeyType.getTypeArguments().isEmpty();
String optionType = declaredOptionKeyType.getTypeArguments().get(0).toString();
if (optionType.startsWith("java.lang.")) {
optionType = optionType.substring("java.lang.".length());
}
Element enclosing = element.getEnclosingElement();
String declaringClass = "";
String separator = "";
Set<Element> originatingElementsList = info.originatingElements;
originatingElementsList.add(field);
PackageElement enclosingPackage = null;
while (enclosing != null) {
if (enclosing.getKind() == ElementKind.CLASS || enclosing.getKind() == ElementKind.INTERFACE) {
if (enclosing.getModifiers().contains(Modifier.PRIVATE)) {
String msg = String.format("Option field cannot be declared in a private %s %s", enclosing.getKind().name().toLowerCase(), enclosing);
processingEnv.getMessager().printMessage(Kind.ERROR, msg, element);
return;
}
originatingElementsList.add(enclosing);
declaringClass = enclosing.getSimpleName() + separator + declaringClass;
separator = ".";
} else if (enclosing.getKind() == ElementKind.PACKAGE) {
enclosingPackage = (PackageElement) enclosing;
}
enclosing = enclosing.getEnclosingElement();
}
if (enclosingPackage == null) {
processingEnv.getMessager().printMessage(Kind.ERROR, "Option field cannot be declared in the unnamed package", element);
return;
}
String[] helpValue = annotation.help();
String help = "";
String[] extraHelp = {};
if (helpValue.length == 1) {
help = helpValue[0];
if (help.startsWith("file:")) {
String path = help.substring("file:".length());
Filer filer = processingEnv.getFiler();
try {
FileObject file;
try {
file = filer.getResource(StandardLocation.SOURCE_PATH, enclosingPackage.getQualifiedName(), path);
} catch (IllegalArgumentException | IOException e) {
// Handle the case when a compiler doesn't support the SOURCE_PATH location
file = filer.getResource(StandardLocation.CLASS_OUTPUT, enclosingPackage.getQualifiedName(), path);
}
try (BufferedReader br = new BufferedReader(new InputStreamReader(file.openInputStream()))) {
help = br.readLine();
if (help == null) {
help = "";
}
String line = br.readLine();
List<String> lines = new ArrayList<>();
while (line != null) {
lines.add(line);
line = br.readLine();
}
extraHelp = lines.toArray(new String[lines.size()]);
}
} catch (IOException e) {
String msg = String.format("Error reading %s containing the help text for option field: %s", path, e);
processingEnv.getMessager().printMessage(Kind.ERROR, msg, element);
return;
}
}
} else if (helpValue.length > 1) {
help = helpValue[0];
extraHelp = Arrays.copyOfRange(helpValue, 1, helpValue.length);
}
if (help.length() != 0) {
char firstChar = help.charAt(0);
if (!Character.isUpperCase(firstChar)) {
processingEnv.getMessager().printMessage(Kind.ERROR, "Option help text must start with an upper case letter", element);
return;
}
}
info.options.add(new OptionInfo(optionName, annotation.type(), help, extraHelp, optionType, declaringClass, field));
}
use of javax.tools.FileObject in project vue-gwt by Axellience.
the class ComponentTemplateProcessor method getTemplateContent.
private Optional<String> getTemplateContent(ClassName componentTypeName, TypeElement componentTypeElement) {
String path = slashify(componentTypeName.reflectionName()) + ".html";
FileObject resource;
try {
resource = filer.getResource(StandardLocation.CLASS_OUTPUT, "", path);
} catch (IOException e) {
messager.printMessage(Kind.ERROR, "Couldn't find template for component: " + componentTypeName.simpleName() + ". Check our setup guide for help. On Eclipse add !vue-gwt-resources on your project in Properties > Maven > Active Maven Profile", componentTypeElement);
return Optional.empty();
}
// Get template content from HTML file
try {
return Optional.of(resource.getCharContent(true).toString());
} catch (IOException e) {
messager.printMessage(Kind.ERROR, "Failed to open template file for component: " + componentTypeName.simpleName() + ". Check our setup guide for help. On Eclipse add !vue-gwt-resources on your project in Properties > Maven > Active Maven Profile", componentTypeElement);
return Optional.empty();
}
}
use of javax.tools.FileObject in project hibernate-orm by hibernate.
the class XmlParserHelper method getInputStreamForResource.
/**
* Returns an input stream for the specified resource. First an attempt is made to load the resource
* via the {@code Filer} API and if that fails {@link Class#getResourceAsStream} is used.
*
* @param resource the resource to load
*
* @return an input stream for the specified resource or {@code null} in case resource cannot be loaded
*/
public InputStream getInputStreamForResource(String resource) {
// METAGEN-75
if (!resource.startsWith(RESOURCE_PATH_SEPARATOR)) {
resource = RESOURCE_PATH_SEPARATOR + resource;
}
String pkg = getPackage(resource);
String name = getRelativeName(resource);
InputStream ormStream;
try {
FileObject fileObject = context.getProcessingEnvironment().getFiler().getResource(StandardLocation.CLASS_OUTPUT, pkg, name);
ormStream = fileObject.openInputStream();
} catch (IOException e1) {
// TODO - METAGEN-12
// unfortunately, the Filer.getResource API seems not to be able to load from /META-INF. One gets a
// FilerException with the message with "Illegal name /META-INF". This means that we have to revert to
// using the classpath. This might mean that we find a persistence.xml which is 'part of another jar.
// Not sure what else we can do here
ormStream = this.getClass().getResourceAsStream(resource);
}
return ormStream;
}
use of javax.tools.FileObject in project ballerina by ballerina-lang.
the class ClassIndexProcessor method readOldIndexFile.
private FileObject readOldIndexFile(Set<String> entries, String resourceName) throws IOException {
Reader reader = null;
try {
final FileObject resource = filer.getResource(StandardLocation.CLASS_OUTPUT, "", resourceName);
reader = resource.openReader(true);
readOldIndexFile(entries, reader);
return resource;
} catch (FileNotFoundException e) {
/**
* Ugly hack for Intellij IDEA incremental compilation.
* The problem is that it throws FileNotFoundException on the files, if they were not created during the
* current session of compilation.
*/
final String realPath = e.getMessage();
if (new File(realPath).exists()) {
try (Reader fileReader = new InputStreamReader(new FileInputStream(realPath), "UTF-8")) {
readOldIndexFile(entries, fileReader);
}
}
} catch (IOException e) {
// Thrown by Eclipse JDT when not found
} catch (UnsupportedOperationException e) {
// Java6 does not support reading old index files
} finally {
if (reader != null) {
reader.close();
}
}
return null;
}
Aggregations