comparison graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/java/compiler/JavaCCompiler.java @ 16759:23415229349b

Truffle-DSL: new package structure.
author Christian Humer <christian.humer@gmail.com>
date Mon, 11 Aug 2014 15:57:14 +0200
parents graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/compiler/JavaCCompiler.java@f78eafd5ba9e
children
comparison
equal deleted inserted replaced
16758:c5f8eeb3cbc8 16759:23415229349b
1 /*
2 * Copyright (c) 2012, 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.java.compiler;
24
25 import java.util.*;
26
27 import javax.annotation.processing.*;
28 import javax.lang.model.element.*;
29
30 import com.oracle.truffle.dsl.processor.java.*;
31
32 public class JavaCCompiler extends AbstractCompiler {
33
34 public static boolean isValidElement(Element currentElement) {
35 try {
36 Class<?> elementClass = Class.forName("com.sun.tools.javac.code.Symbol");
37 return elementClass.isAssignableFrom(currentElement.getClass());
38 } catch (ClassNotFoundException e) {
39 return false;
40 }
41 }
42
43 public List<? extends Element> getEnclosedElementsInDeclarationOrder(TypeElement type) {
44 return type.getEnclosedElements();
45 }
46
47 public List<? extends Element> getAllMembersInDeclarationOrder(ProcessingEnvironment environment, TypeElement type) {
48 return environment.getElementUtils().getAllMembers(type);
49 }
50
51 private static final Class<?>[] getTreeAndTopLevelSignature = new Class[]{Element.class, AnnotationMirror.class, AnnotationValue.class};
52 private static final Class<?>[] getCharContentSignature = new Class[]{boolean.class};
53
54 @Override
55 public String getMethodBody(ProcessingEnvironment env, ExecutableElement method) {
56 try {
57 /*
58 * if (false) { Pair<JCTree, JCCompilationUnit> treeAndTopLevel = ((JavacElements)
59 * env.getElementUtils()).getTreeAndTopLevel(method, null, null); JCBlock block =
60 * ((JCMethodDecl) treeAndTopLevel.fst).getBody(); int startPos = block.pos; int endPos
61 * = block.endpos; String methodBody =
62 * treeAndTopLevel.snd.getSourceFile().getCharContent(true).subSequence(startPos + 1,
63 * endPos).toString(); return methodBody; }
64 */
65
66 Object treeAndTopLevel = getTreeAndTopLevel(env, method);
67 Object block = method(field(treeAndTopLevel, "fst"), "getBody");
68 int startPos = (int) field(block, "pos");
69 int endPos = (int) field(block, "endpos");
70 return getContent(treeAndTopLevel).subSequence(startPos + 1, endPos).toString();
71 } catch (Exception e) {
72 return ElementUtils.printException(e);
73 }
74 }
75
76 private static CharSequence getContent(Object treeAndTopLevel) throws Exception {
77 /*
78 * CharSequence content = treeAndTopLevel.snd.getSourceFile().getCharContent(true);
79 */
80 return (CharSequence) method(method(field(treeAndTopLevel, "snd"), "getSourceFile"), "getCharContent", getCharContentSignature, true);
81 }
82
83 private static Object getTreeAndTopLevel(ProcessingEnvironment env, Element element) throws Exception {
84 /*
85 * Pair<JCTree, JCCompilationUnit> treeAndTopLevel = ((JavacElements)
86 * env.getElementUtils()).getTreeAndTopLevel(method, null, null);
87 */
88 return method(method(env, "getElementUtils"), "getTreeAndTopLevel", getTreeAndTopLevelSignature, element, null, null);
89 }
90
91 @Override
92 public String getHeaderComment(ProcessingEnvironment env, Element type) {
93 try {
94 String content = getContent(getTreeAndTopLevel(env, type)).toString();
95 return parseHeader(content);
96 } catch (Exception e) {
97 return ElementUtils.printException(e);
98 }
99 }
100
101 }