KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > source > util > Trees


1 /**
2  * @(#)Trees.java 1.6 06/06/26
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  *
7  * Use and Distribution is subject to the Java Research License available
8  * at <http://wwws.sun.com/software/communitysource/jrl.html>.
9  */

10
11 package com.sun.source.util;
12
13 import java.lang.reflect.Method JavaDoc;
14 import javax.annotation.processing.ProcessingEnvironment;
15 import javax.lang.model.element.AnnotationMirror;
16 import javax.lang.model.element.AnnotationValue;
17 import javax.lang.model.element.Element;
18 import javax.lang.model.element.ExecutableElement;
19 import javax.lang.model.element.TypeElement;
20 import javax.lang.model.type.DeclaredType;
21 import javax.lang.model.type.TypeMirror;
22 import javax.tools.JavaCompiler.CompilationTask;
23
24 import com.sun.source.tree.ClassTree;
25 import com.sun.source.tree.CompilationUnitTree;
26 import com.sun.source.tree.MethodTree;
27 import com.sun.source.tree.Scope;
28 import com.sun.source.tree.Tree;
29
30 /**
31  * Bridges JSR 199, JSR 269, and the Tree API.
32  *
33  * @author Peter von der Ah&eacute;
34  */

35 public abstract class Trees {
36     /**
37      * Gets a Trees object for a given CompilationTask.
38      * @throws IllegalArgumentException if the task does not support the Trees API.
39      */

40     public static Trees instance(CompilationTask task) {
41         if (!task.getClass().getName().equals("com.sun.tools.javac.api.JavacTaskImpl"))
42             throw new IllegalArgumentException JavaDoc();
43         return getJavacTrees(CompilationTask.class, task);
44     }
45
46     /**
47      * Gets a Trees object for a given CompilationTask.
48      * @throws IllegalArgumentException if the env does not support the Trees API.
49      */

50     public static Trees instance(ProcessingEnvironment env) {
51         if (!env.getClass().getName().equals("com.sun.tools.javac.processing.JavacProcessingEnvironment"))
52             throw new IllegalArgumentException JavaDoc();
53         return getJavacTrees(ProcessingEnvironment.class, env);
54     }
55
56     private static Trees getJavacTrees(Class JavaDoc<?> argType, Object JavaDoc arg) {
57         try {
58             ClassLoader JavaDoc cl = arg.getClass().getClassLoader();
59             Class JavaDoc<?> c = Class.forName("com.sun.tools.javac.api.JavacTrees", false, cl);
60             argType = Class.forName(argType.getName(), false, cl);
61             Method JavaDoc m = c.getMethod("instance", new Class JavaDoc[] { argType });
62             return (Trees) m.invoke(null, new Object JavaDoc[] { arg });
63         } catch (Throwable JavaDoc e) {
64             throw new AssertionError JavaDoc(e);
65         }
66     }
67
68     /**
69      * Gets a utility object for obtaining source positions.
70      */

71     public abstract SourcePositions getSourcePositions();
72
73     /**
74      * Gets the Tree node for a given Element.
75      * Returns null if the node can not be found.
76      */

77     public abstract Tree getTree(Element element);
78
79     /**
80      * Gets the ClassTree node for a given TypeElement.
81      * Returns null if the node can not be found.
82      */

83     public abstract ClassTree getTree(TypeElement element);
84
85     /**
86      * Gets the MethodTree node for a given ExecutableElement.
87      * Returns null if the node can not be found.
88      */

89     public abstract MethodTree getTree(ExecutableElement method);
90
91     /**
92      * Gets the Tree node for an AnnotationMirror on a given Element.
93      * Returns null if the node can not be found.
94      */

95     public abstract Tree getTree(Element e, AnnotationMirror a);
96
97     /**
98      * Gets the Tree node for an AnnotationValue for an AnnotationMirror on a given Element.
99      * Returns null if the node can not be found.
100      */

101     public abstract Tree getTree(Element e, AnnotationMirror a, AnnotationValue v);
102
103     /**
104      * Gets the path to tree node within the specified compilation unit.
105      */

106     public abstract TreePath getPath(CompilationUnitTree unit, Tree node);
107
108     /**
109      * Gets the TreePath node for a given Element.
110      * Returns null if the node can not be found.
111      */

112     public abstract TreePath getPath(Element e);
113
114     /**
115      * Gets the TreePath node for an AnnotationMirror on a given Element.
116      * Returns null if the node can not be found.
117      */

118     public abstract TreePath getPath(Element e, AnnotationMirror a);
119
120     /**
121      * Gets the TreePath node for an AnnotationValue for an AnnotationMirror on a given Element.
122      * Returns null if the node can not be found.
123      */

124     public abstract TreePath getPath(Element e, AnnotationMirror a, AnnotationValue v);
125
126     /**
127      * Gets the Element for the Tree node identified by a given TreePath.
128      * Returns null if the element is not available.
129      * @throws IllegalArgumentException is the TreePath does not identify
130      * a Tree node that might have an associated Element.
131      */

132     public abstract Element getElement(TreePath path);
133
134     /**
135      * Gets the TypeMirror for the Tree node identified by a given TreePath.
136      * Returns null if the TypeMirror is not available.
137      * @throws IllegalArgumentException is the TreePath does not identify
138      * a Tree node that might have an associated TypeMirror.
139      */

140     public abstract TypeMirror getTypeMirror(TreePath path);
141
142     /**
143      * Gets the Scope for the Tree node identified by a given TreePath.
144      * Returns null if the Scope is not available.
145      */

146     public abstract Scope getScope(TreePath path);
147     
148     /**
149      * Checks whether a given type is accessible in a given scope.
150      * @param scope the scope to be checked
151      * @param type the type to be checked
152      * @return true if {@code type} is accessible
153      */

154     public abstract boolean isAccessible(Scope scope, TypeElement type);
155
156     /**
157      * Checks whether the given element is accessible as a member of the given
158      * type in a given scope.
159      * @param scope the scope to be checked
160      * @param member the member to be checked
161      * @param type the type for which to check if the member is accessible
162      * @return true if {@code member} is accessible in {@code type}
163      */

164     public abstract boolean isAccessible(Scope scope, Element member, DeclaredType type);
165 }
166
Popular Tags