comparison graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/LanguageRegistrationProcessor.java @ 21468:99942eac9c6d

Introducing TruffleVM - a central place to invoke code in any registered TruffleLanguage.
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Fri, 22 May 2015 13:41:10 +0200
parents
children bb51b9a142b3
comparison
equal deleted inserted replaced
21467:d4db9d812c8d 21468:99942eac9c6d
1 /*
2 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23 package com.oracle.truffle.dsl.processor;
24
25 import com.oracle.truffle.api.TruffleLanguage.Registration;
26 import java.io.IOException;
27 import java.io.OutputStream;
28 import java.util.Properties;
29 import java.util.Set;
30 import javax.annotation.processing.AbstractProcessor;
31 import javax.annotation.processing.RoundEnvironment;
32 import javax.annotation.processing.SupportedAnnotationTypes;
33 import javax.lang.model.SourceVersion;
34 import javax.lang.model.element.Element;
35 import javax.lang.model.element.ElementKind;
36 import javax.lang.model.element.Modifier;
37 import javax.lang.model.element.TypeElement;
38 import javax.tools.Diagnostic.Kind;
39 import javax.tools.FileObject;
40 import javax.tools.StandardLocation;
41
42 @SupportedAnnotationTypes("com.oracle.truffle.api.*")
43 public final class LanguageRegistrationProcessor extends AbstractProcessor {
44 @Override
45 public SourceVersion getSupportedSourceVersion() {
46 return SourceVersion.latest();
47 }
48
49 private void createProviderFile(TypeElement language, Registration annotation) {
50 String filename = "META-INF/truffle/language";
51 try {
52 FileObject file = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", filename, language);
53 Properties p = new Properties();
54 String className = processingEnv.getElementUtils().getBinaryName(language).toString();
55 p.setProperty("name", annotation.name());
56 p.setProperty("className", className);
57 String[] mimes = annotation.mimeType();
58 for (int i = 0; i < mimes.length; i++) {
59 p.setProperty("mimeType." + i, mimes[i]);
60 }
61 try (OutputStream os = file.openOutputStream()) {
62 p.store(os, "Generated by " + LanguageRegistrationProcessor.class.getName());
63 }
64 } catch (IOException e) {
65 processingEnv.getMessager().printMessage(Kind.ERROR, e.getMessage(), language);
66 }
67 }
68
69 @Override
70 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
71 for (Element e : roundEnv.getElementsAnnotatedWith(Registration.class)) {
72 Registration annotation = e.getAnnotation(Registration.class);
73 if (annotation != null && e.getKind() == ElementKind.CLASS) {
74 if (!e.getModifiers().contains(Modifier.PUBLIC)) {
75 processingEnv.getMessager().printMessage(Kind.ERROR, "Registered language class must be public", e);
76 }
77 createProviderFile((TypeElement) e, annotation);
78 }
79 }
80
81 return true;
82 }
83 }