use of javax.annotation.processing.FilerException in project graal by oracle.
the class OptionProcessor method generateOptionDescriptor.
private void generateOptionDescriptor(OptionsInfo info) {
Element element = info.type;
ProcessorContext context = ProcessorContext.getInstance();
CodeTypeElement unit = generateDescriptors(context, element, info);
DeclaredType overrideType = (DeclaredType) context.getType(Override.class);
DeclaredType suppressedWarnings = (DeclaredType) context.getType(SuppressWarnings.class);
unit.accept(new GenerateOverrideVisitor(overrideType), null);
unit.accept(new FixWarningsVisitor(context.getEnvironment(), suppressedWarnings, overrideType), null);
try {
unit.accept(new CodeWriter(context.getEnvironment(), element), null);
} catch (RuntimeException e) {
if (e.getCause() instanceof FilerException) {
// ignore spurious errors of source file already created in Eclipse.
if (e.getCause().getMessage().startsWith("Source file already created")) {
return;
}
}
}
}
use of javax.annotation.processing.FilerException 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.annotation.processing.FilerException in project graal by oracle.
the class AbstractCodeWriter method visitType.
@Override
public Void visitType(CodeTypeElement e, Void p) {
if (e.isTopLevelClass()) {
Writer w = null;
try {
imports = OrganizedImports.organize(e);
w = new TrimTrailingSpaceWriter(createWriter(e));
writer = w;
writeRootClass(e);
} catch (IOException ex) {
if (ex instanceof FilerException) {
if (ex.getMessage().startsWith("Source file already created")) {
// ignore source file already created errors
return null;
}
}
throw new RuntimeException(ex);
} finally {
if (w != null) {
try {
w.close();
} catch (Throwable e1) {
// see eclipse bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=361378
// TODO temporary suppress errors on close.
}
}
writer = null;
}
} else {
writeClassImpl(e);
}
return null;
}
use of javax.annotation.processing.FilerException in project graal by oracle.
the class InteropDSLProcessor method processElement.
private void processElement(Element e) throws IOException {
if (e.getKind() != ElementKind.CLASS) {
return;
}
MessageResolution messageImplementations = e.getAnnotation(MessageResolution.class);
if (messageImplementations == null) {
return;
}
// Check the receiver
final String receiverTypeFullClassName = Utils.getReceiverTypeFullClassName(messageImplementations);
if (isReceiverNonStaticInner(messageImplementations)) {
emitError(receiverTypeFullClassName + " cannot be used as a receiver as it is not a static inner class.", e);
return;
}
if (e.getModifiers().contains(Modifier.PRIVATE) || e.getModifiers().contains(Modifier.PROTECTED)) {
emitError("Class must be public or package protected", e);
return;
}
// check if there is a @LanguageCheck class
Element curr = e;
List<TypeElement> receiverChecks = new ArrayList<>();
for (Element innerClass : curr.getEnclosedElements()) {
if (innerClass.getKind() != ElementKind.CLASS) {
continue;
}
if (innerClass.getAnnotation(CanResolve.class) != null) {
receiverChecks.add((TypeElement) innerClass);
}
}
if (receiverChecks.size() == 0 && isInstanceMissing(receiverTypeFullClassName)) {
emitError("Missing isInstance method in class " + receiverTypeFullClassName, e);
return;
}
if (receiverChecks.size() == 0 && isInstanceHasWrongSignature(receiverTypeFullClassName)) {
emitError("Method isInstance in class " + receiverTypeFullClassName + " has an invalid signature: expected signature (object: TruffleObject).", e);
return;
}
if (receiverChecks.size() > 1) {
emitError("Only one @LanguageCheck element allowed", e);
return;
}
// Collect all inner classes with an @Resolve annotation
curr = e;
List<TypeElement> elements = new ArrayList<>();
for (Element innerClass : curr.getEnclosedElements()) {
if (innerClass.getKind() != ElementKind.CLASS) {
continue;
}
if (innerClass.getAnnotation(Resolve.class) != null) {
elements.add((TypeElement) innerClass);
}
}
ForeignAccessFactoryGenerator factoryGenerator = new ForeignAccessFactoryGenerator(processingEnv, messageImplementations, (TypeElement) e);
// Process inner classes with an @Resolve annotation
boolean generationSuccessfull = true;
for (TypeElement elem : elements) {
generationSuccessfull &= processResolveClass(elem.getAnnotation(Resolve.class), messageImplementations, elem, factoryGenerator);
}
if (!generationSuccessfull) {
return;
}
if (!receiverChecks.isEmpty()) {
generationSuccessfull &= processLanguageCheck(messageImplementations, receiverChecks.get(0), factoryGenerator);
}
if (!generationSuccessfull) {
return;
}
try {
factoryGenerator.generate();
} catch (FilerException ex) {
emitError("Foreign factory class with same name already exists", e);
return;
}
}
use of javax.annotation.processing.FilerException in project atlasdb by palantir.
the class AutoDelegateProcessor method process.
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
if (abortProcessing.get() == Boolean.TRUE) {
// Another instance of AutoDelegateProcessor is running in the current processing environment.
return false;
}
Set<String> generatedTypes = new HashSet<>();
for (Element annotatedElement : roundEnv.getElementsAnnotatedWith(AutoDelegate.class)) {
try {
validateAnnotatedElement(annotatedElement);
TypeElement typeElement = (TypeElement) annotatedElement;
AutoDelegate annotation = annotatedElement.getAnnotation(AutoDelegate.class);
TypeToExtend typeToExtend = validateAnnotationAndCreateTypeToExtend(annotation, typeElement);
if (generatedTypes.contains(typeToExtend.getCanonicalName())) {
continue;
}
generatedTypes.add(typeToExtend.getCanonicalName());
generateCode(typeToExtend);
} catch (FilerException e) {
// Happens when same file is written twice.
warn(annotatedElement, e.getMessage());
} catch (ProcessingException e) {
error(e.getElement(), e.getMessage());
} catch (IOException | RuntimeException e) {
error(annotatedElement, e.getMessage());
}
}
return false;
}
Aggregations