Mercurial > hg > truffle
view graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/LanguageRegistrationProcessor.java @ 21554:b1530a6cce8c
renamed com.oracle.graal.[debug|options|hotspotvmconfig]* modules to com.oracle.jvmci.[debug|options|hotspotvmconfig]* modules (JBS:GRAAL-53)
author | Doug Simon <doug.simon@oracle.com> |
---|---|
date | Tue, 26 May 2015 23:21:15 +0200 |
parents | bb51b9a142b3 |
children | 31fc2fce38f3 |
line wrap: on
line source
/* * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package com.oracle.truffle.dsl.processor; import java.io.*; import java.util.*; import javax.annotation.processing.*; import javax.lang.model.*; import javax.lang.model.element.*; import javax.lang.model.type.*; import javax.tools.Diagnostic.Kind; import javax.tools.*; import com.oracle.truffle.api.*; import com.oracle.truffle.api.TruffleLanguage.Registration; import com.oracle.truffle.api.dsl.*; @SupportedAnnotationTypes("com.oracle.truffle.api.*") public final class LanguageRegistrationProcessor extends AbstractProcessor { @Override public SourceVersion getSupportedSourceVersion() { return SourceVersion.latest(); } private void createProviderFile(TypeElement language, Registration annotation) { String filename = "META-INF/truffle/language"; try { FileObject file = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", filename, language); Properties p = new Properties(); String className = processingEnv.getElementUtils().getBinaryName(language).toString(); p.setProperty("name", annotation.name()); p.setProperty("className", className); String[] mimes = annotation.mimeType(); for (int i = 0; i < mimes.length; i++) { p.setProperty("mimeType." + i, mimes[i]); } try (OutputStream os = file.openOutputStream()) { p.store(os, "Generated by " + LanguageRegistrationProcessor.class.getName()); } } catch (IOException e) { processingEnv.getMessager().printMessage(Kind.ERROR, e.getMessage(), language); } } @Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { for (Element e : roundEnv.getElementsAnnotatedWith(Registration.class)) { Registration annotation = e.getAnnotation(Registration.class); if (annotation != null && e.getKind() == ElementKind.CLASS) { if (!e.getModifiers().contains(Modifier.PUBLIC)) { emitError("Registered language class must be public", e); continue; } if (e.getEnclosingElement().getKind() != ElementKind.PACKAGE && !e.getModifiers().contains(Modifier.STATIC)) { emitError("Registered language inner-class must be static", e); continue; } TypeMirror truffleLang = processingEnv.getElementUtils().getTypeElement(TruffleLanguage.class.getName()).asType(); if (!processingEnv.getTypeUtils().isAssignable(e.asType(), truffleLang)) { emitError("Registered language class must subclass TruffleLanguage", e); continue; } boolean found = false; for (Element mem : e.getEnclosedElements()) { if (mem.getKind() != ElementKind.CONSTRUCTOR) { continue; } ExecutableElement ee = (ExecutableElement) mem; if (ee.getParameters().size() != 1) { continue; } if (!ee.getModifiers().contains(Modifier.PUBLIC)) { continue; } TypeMirror env = processingEnv.getElementUtils().getTypeElement(TruffleLanguage.Env.class.getCanonicalName()).asType(); if (processingEnv.getTypeUtils().isSameType(ee.getParameters().get(0).asType(), env)) { found = true; break; } } if (!found) { emitError("Language must have a public constructor accepting TruffleLanguage.Env as parameter", e); continue; } assertNoErrorExpected(e); createProviderFile((TypeElement) e, annotation); } } return true; } void assertNoErrorExpected(Element e) { TypeElement eee = processingEnv.getElementUtils().getTypeElement(ExpectError.class.getName()); for (AnnotationMirror am : e.getAnnotationMirrors()) { if (am.getAnnotationType().asElement().equals(eee)) { processingEnv.getMessager().printMessage(Kind.ERROR, "Expected an error, but none found!", e); } } } void emitError(String msg, Element e) { TypeElement eee = processingEnv.getElementUtils().getTypeElement(ExpectError.class.getName()); for (AnnotationMirror am : e.getAnnotationMirrors()) { if (am.getAnnotationType().asElement().equals(eee)) { Map<? extends ExecutableElement, ? extends AnnotationValue> vals = am.getElementValues(); if (vals.size() == 1) { AnnotationValue av = vals.values().iterator().next(); if (av.getValue() instanceof List) { List<?> arr = (List<?>) av.getValue(); for (Object o : arr) { if (o instanceof AnnotationValue) { AnnotationValue ov = (AnnotationValue) o; if (msg.equals(ov.getValue())) { return; } } } } } } } processingEnv.getMessager().printMessage(Kind.ERROR, msg, e); } }