001: /*
002: * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.source.util;
027:
028: import java.lang.reflect.Method;
029: import javax.annotation.processing.ProcessingEnvironment;
030: import javax.lang.model.element.AnnotationMirror;
031: import javax.lang.model.element.AnnotationValue;
032: import javax.lang.model.element.Element;
033: import javax.lang.model.element.ExecutableElement;
034: import javax.lang.model.element.TypeElement;
035: import javax.lang.model.type.DeclaredType;
036: import javax.lang.model.type.TypeMirror;
037: import javax.tools.JavaCompiler.CompilationTask;
038:
039: import com.sun.source.tree.ClassTree;
040: import com.sun.source.tree.CompilationUnitTree;
041: import com.sun.source.tree.MethodTree;
042: import com.sun.source.tree.Scope;
043: import com.sun.source.tree.Tree;
044:
045: /**
046: * Bridges JSR 199, JSR 269, and the Tree API.
047: *
048: * @author Peter von der Ahé
049: */
050: public abstract class Trees {
051: /**
052: * Gets a Trees object for a given CompilationTask.
053: * @throws IllegalArgumentException if the task does not support the Trees API.
054: */
055: public static Trees instance(CompilationTask task) {
056: if (!task.getClass().getName().equals(
057: "com.sun.tools.javac.api.JavacTaskImpl"))
058: throw new IllegalArgumentException();
059: return getJavacTrees(CompilationTask.class, task);
060: }
061:
062: /**
063: * Gets a Trees object for a given CompilationTask.
064: * @throws IllegalArgumentException if the env does not support the Trees API.
065: */
066: public static Trees instance(ProcessingEnvironment env) {
067: if (!env
068: .getClass()
069: .getName()
070: .equals(
071: "com.sun.tools.javac.processing.JavacProcessingEnvironment"))
072: throw new IllegalArgumentException();
073: return getJavacTrees(ProcessingEnvironment.class, env);
074: }
075:
076: private static Trees getJavacTrees(Class<?> argType, Object arg) {
077: try {
078: ClassLoader cl = arg.getClass().getClassLoader();
079: Class<?> c = Class.forName(
080: "com.sun.tools.javac.api.JavacTrees", false, cl);
081: argType = Class.forName(argType.getName(), false, cl);
082: Method m = c.getMethod("instance", new Class[] { argType });
083: return (Trees) m.invoke(null, new Object[] { arg });
084: } catch (Throwable e) {
085: throw new AssertionError(e);
086: }
087: }
088:
089: /**
090: * Gets a utility object for obtaining source positions.
091: */
092: public abstract SourcePositions getSourcePositions();
093:
094: /**
095: * Gets the Tree node for a given Element.
096: * Returns null if the node can not be found.
097: */
098: public abstract Tree getTree(Element element);
099:
100: /**
101: * Gets the ClassTree node for a given TypeElement.
102: * Returns null if the node can not be found.
103: */
104: public abstract ClassTree getTree(TypeElement element);
105:
106: /**
107: * Gets the MethodTree node for a given ExecutableElement.
108: * Returns null if the node can not be found.
109: */
110: public abstract MethodTree getTree(ExecutableElement method);
111:
112: /**
113: * Gets the Tree node for an AnnotationMirror on a given Element.
114: * Returns null if the node can not be found.
115: */
116: public abstract Tree getTree(Element e, AnnotationMirror a);
117:
118: /**
119: * Gets the Tree node for an AnnotationValue for an AnnotationMirror on a given Element.
120: * Returns null if the node can not be found.
121: */
122: public abstract Tree getTree(Element e, AnnotationMirror a,
123: AnnotationValue v);
124:
125: /**
126: * Gets the path to tree node within the specified compilation unit.
127: */
128: public abstract TreePath getPath(CompilationUnitTree unit, Tree node);
129:
130: /**
131: * Gets the TreePath node for a given Element.
132: * Returns null if the node can not be found.
133: */
134: public abstract TreePath getPath(Element e);
135:
136: /**
137: * Gets the TreePath node for an AnnotationMirror on a given Element.
138: * Returns null if the node can not be found.
139: */
140: public abstract TreePath getPath(Element e, AnnotationMirror a);
141:
142: /**
143: * Gets the TreePath node for an AnnotationValue for an AnnotationMirror on a given Element.
144: * Returns null if the node can not be found.
145: */
146: public abstract TreePath getPath(Element e, AnnotationMirror a,
147: AnnotationValue v);
148:
149: /**
150: * Gets the Element for the Tree node identified by a given TreePath.
151: * Returns null if the element is not available.
152: * @throws IllegalArgumentException is the TreePath does not identify
153: * a Tree node that might have an associated Element.
154: */
155: public abstract Element getElement(TreePath path);
156:
157: /**
158: * Gets the TypeMirror for the Tree node identified by a given TreePath.
159: * Returns null if the TypeMirror is not available.
160: * @throws IllegalArgumentException is the TreePath does not identify
161: * a Tree node that might have an associated TypeMirror.
162: */
163: public abstract TypeMirror getTypeMirror(TreePath path);
164:
165: /**
166: * Gets the Scope for the Tree node identified by a given TreePath.
167: * Returns null if the Scope is not available.
168: */
169: public abstract Scope getScope(TreePath path);
170:
171: /**
172: * Checks whether a given type is accessible in a given scope.
173: * @param scope the scope to be checked
174: * @param type the type to be checked
175: * @return true if {@code type} is accessible
176: */
177: public abstract boolean isAccessible(Scope scope, TypeElement type);
178:
179: /**
180: * Checks whether the given element is accessible as a member of the given
181: * type in a given scope.
182: * @param scope the scope to be checked
183: * @param member the member to be checked
184: * @param type the type for which to check if the member is accessible
185: * @return true if {@code member} is accessible in {@code type}
186: */
187: public abstract boolean isAccessible(Scope scope, Element member,
188: DeclaredType type);
189: }
|