Search in sources :

Example 1 with FilerException

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;
            }
        }
    }
}
Also used : CodeTypeElement(com.oracle.truffle.dsl.processor.java.model.CodeTypeElement) PackageElement(javax.lang.model.element.PackageElement) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) CodeExecutableElement(com.oracle.truffle.dsl.processor.java.model.CodeExecutableElement) CodeTypeElement(com.oracle.truffle.dsl.processor.java.model.CodeTypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) GenerateOverrideVisitor(com.oracle.truffle.dsl.processor.java.transform.GenerateOverrideVisitor) FixWarningsVisitor(com.oracle.truffle.dsl.processor.java.transform.FixWarningsVisitor) FilerException(javax.annotation.processing.FilerException) DeclaredType(javax.lang.model.type.DeclaredType)

Example 2 with FilerException

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));
        }
    }
}
Also used : TypeElement(javax.lang.model.element.TypeElement) Registration(com.oracle.truffle.api.TruffleLanguage.Registration) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) OutputStream(java.io.OutputStream) FileObject(javax.tools.FileObject) IOException(java.io.IOException) Properties(java.util.Properties) FilerException(javax.annotation.processing.FilerException)

Example 3 with FilerException

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;
}
Also used : IOException(java.io.IOException) FilerException(javax.annotation.processing.FilerException) Writer(java.io.Writer)

Example 4 with FilerException

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;
    }
}
Also used : MessageResolution(com.oracle.truffle.api.interop.MessageResolution) CanResolve(com.oracle.truffle.api.interop.CanResolve) TypeElement(javax.lang.model.element.TypeElement) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) ArrayList(java.util.ArrayList) FilerException(javax.annotation.processing.FilerException) CanResolve(com.oracle.truffle.api.interop.CanResolve) Resolve(com.oracle.truffle.api.interop.Resolve)

Example 5 with FilerException

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;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) IOException(java.io.IOException) FilerException(javax.annotation.processing.FilerException) HashSet(java.util.HashSet)

Aggregations

FilerException (javax.annotation.processing.FilerException)15 Element (javax.lang.model.element.Element)12 TypeElement (javax.lang.model.element.TypeElement)11 ArrayList (java.util.ArrayList)8 IOException (java.io.IOException)7 FileObject (javax.tools.FileObject)7 MainInfo (com.predic8.membrane.annot.model.MainInfo)5 BufferedWriter (java.io.BufferedWriter)5 ExecutableElement (javax.lang.model.element.ExecutableElement)5 ChildElementInfo (com.predic8.membrane.annot.model.ChildElementInfo)4 ElementInfo (com.predic8.membrane.annot.model.ElementInfo)4 ClassName (com.squareup.javapoet.ClassName)3 CodeBlock (com.squareup.javapoet.CodeBlock)3 JavaFile (com.squareup.javapoet.JavaFile)3 MethodSpec (com.squareup.javapoet.MethodSpec)3 TypeSpec (com.squareup.javapoet.TypeSpec)3 VariableElement (javax.lang.model.element.VariableElement)3 AttributeInfo (com.predic8.membrane.annot.model.AttributeInfo)2 ChildElementDeclarationInfo (com.predic8.membrane.annot.model.ChildElementDeclarationInfo)2 OutputStream (java.io.OutputStream)2