29 import java.util.Collections;
30 import java.util.HashMap;
31 import java.util.HashSet;
32 import java.util.LinkedHashSet;
33 import java.util.Map;
34 import java.util.Optional;
35 import java.util.Set;
36 import java.util.function.BiFunction;
37 import java.util.stream.Collectors;
38
39 import javax.lang.model.AnnotatedConstruct;
40 import javax.lang.model.SourceVersion;
41 import javax.lang.model.element.*;
42 import javax.lang.model.type.DeclaredType;
43 import javax.lang.model.util.Elements;
44 import javax.tools.JavaFileObject;
45 import static javax.lang.model.util.ElementFilter.methodsIn;
46
47 import com.sun.source.util.JavacTask;
48 import com.sun.tools.javac.api.JavacTaskImpl;
49 import com.sun.tools.javac.code.*;
50 import com.sun.tools.javac.code.Attribute.Compound;
51 import com.sun.tools.javac.code.Directive.ExportsDirective;
52 import com.sun.tools.javac.code.Directive.ExportsFlag;
53 import com.sun.tools.javac.code.Directive.OpensDirective;
54 import com.sun.tools.javac.code.Directive.OpensFlag;
55 import com.sun.tools.javac.code.Directive.RequiresDirective;
56 import com.sun.tools.javac.code.Directive.RequiresFlag;
57 import com.sun.tools.javac.code.Scope.WriteableScope;
58 import com.sun.tools.javac.code.Source.Feature;
59 import com.sun.tools.javac.code.Symbol.*;
60 import com.sun.tools.javac.comp.AttrContext;
61 import com.sun.tools.javac.comp.Enter;
62 import com.sun.tools.javac.comp.Env;
63 import com.sun.tools.javac.main.JavaCompiler;
64 import com.sun.tools.javac.processing.PrintingProcessor;
65 import com.sun.tools.javac.tree.DocCommentTable;
66 import com.sun.tools.javac.tree.JCTree;
67 import com.sun.tools.javac.tree.JCTree.*;
68 import com.sun.tools.javac.tree.TreeInfo;
69 import com.sun.tools.javac.tree.TreeScanner;
70 import com.sun.tools.javac.util.*;
71 import com.sun.tools.javac.util.DefinedBy.Api;
72 import com.sun.tools.javac.util.Name;
73 import static com.sun.tools.javac.code.Kinds.Kind.*;
74 import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE;
75 import static com.sun.tools.javac.code.TypeTag.CLASS;
76 import com.sun.tools.javac.comp.Attr;
77 import com.sun.tools.javac.comp.Modules;
78 import com.sun.tools.javac.comp.Resolve;
79 import com.sun.tools.javac.resources.CompilerProperties.Notes;
80 import static com.sun.tools.javac.tree.JCTree.Tag.*;
81
82 /**
83 * Utility methods for operating on program elements.
84 *
85 * <p><b>This is NOT part of any supported API.
86 * If you write code that depends on this, you do so at your own
87 * risk. This code and its internal interfaces are subject to change
88 * or deletion without notice.</b></p>
89 */
90 public class JavacElements implements Elements {
91
92 private final JavaCompiler javaCompiler;
93 private final Symtab syms;
94 private final Modules modules;
95 private final Names names;
96 private final Types types;
97 private final Enter enter;
98 private final Attr attr;
99 private final Resolve resolve;
100 private final JavacTaskImpl javacTaskImpl;
101 private final Log log;
102 private final boolean allowModules;
103
104 public static JavacElements instance(Context context) {
105 JavacElements instance = context.get(JavacElements.class);
106 if (instance == null)
107 instance = new JavacElements(context);
108 return instance;
109 }
110
111 @SuppressWarnings("this-escape")
112 protected JavacElements(Context context) {
113 context.put(JavacElements.class, this);
114 javaCompiler = JavaCompiler.instance(context);
115 syms = Symtab.instance(context);
116 modules = Modules.instance(context);
117 names = Names.instance(context);
118 types = Types.instance(context);
119 enter = Enter.instance(context);
120 attr = Attr.instance(context);
121 resolve = Resolve.instance(context);
122 JavacTask t = context.get(JavacTask.class);
123 javacTaskImpl = t instanceof JavacTaskImpl taskImpl ? taskImpl : null;
124 log = Log.instance(context);
125 Source source = Source.instance(context);
126 allowModules = Feature.MODULES.allowedInSource(source);
127 }
128
129 @Override @DefinedBy(Api.LANGUAGE_MODEL)
130 public Set<? extends ModuleElement> getAllModuleElements() {
131 if (allowModules)
132 return Collections.unmodifiableSet(modules.allModules());
133 else
134 return Collections.emptySet();
135 }
136
137 @Override @DefinedBy(Api.LANGUAGE_MODEL)
138 public ModuleSymbol getModuleElement(CharSequence name) {
139 ensureEntered("getModuleElement");
140 if (modules.getDefaultModule() == syms.noModule)
141 return null;
142 String strName = name.toString();
143 if (strName.equals(""))
144 return syms.unnamedModule;
|
29 import java.util.Collections;
30 import java.util.HashMap;
31 import java.util.HashSet;
32 import java.util.LinkedHashSet;
33 import java.util.Map;
34 import java.util.Optional;
35 import java.util.Set;
36 import java.util.function.BiFunction;
37 import java.util.stream.Collectors;
38
39 import javax.lang.model.AnnotatedConstruct;
40 import javax.lang.model.SourceVersion;
41 import javax.lang.model.element.*;
42 import javax.lang.model.type.DeclaredType;
43 import javax.lang.model.util.Elements;
44 import javax.tools.JavaFileObject;
45 import static javax.lang.model.util.ElementFilter.methodsIn;
46
47 import com.sun.source.util.JavacTask;
48 import com.sun.tools.javac.api.JavacTaskImpl;
49 import com.sun.tools.javac.api.JavacTrees;
50 import com.sun.tools.javac.code.*;
51 import com.sun.tools.javac.code.Attribute.Compound;
52 import com.sun.tools.javac.code.Directive.ExportsDirective;
53 import com.sun.tools.javac.code.Directive.ExportsFlag;
54 import com.sun.tools.javac.code.Directive.OpensDirective;
55 import com.sun.tools.javac.code.Directive.OpensFlag;
56 import com.sun.tools.javac.code.Directive.RequiresDirective;
57 import com.sun.tools.javac.code.Directive.RequiresFlag;
58 import com.sun.tools.javac.code.Scope.WriteableScope;
59 import com.sun.tools.javac.code.Source.Feature;
60 import com.sun.tools.javac.code.Symbol.*;
61 import com.sun.tools.javac.comp.Attr;
62 import com.sun.tools.javac.comp.AttrContext;
63 import com.sun.tools.javac.comp.Enter;
64 import com.sun.tools.javac.comp.Env;
65 import com.sun.tools.javac.main.JavaCompiler;
66 import com.sun.tools.javac.processing.PrintingProcessor;
67 import com.sun.tools.javac.tree.DocCommentTable;
68 import com.sun.tools.javac.tree.JCTree;
69 import com.sun.tools.javac.tree.JCTree.*;
70 import com.sun.tools.javac.tree.TreeInfo;
71 import com.sun.tools.javac.tree.TreeMaker;
72 import com.sun.tools.javac.tree.TreeScanner;
73 import com.sun.tools.javac.util.*;
74 import com.sun.tools.javac.util.DefinedBy.Api;
75 import com.sun.tools.javac.util.Name;
76 import static com.sun.tools.javac.code.Kinds.Kind.*;
77 import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE;
78 import static com.sun.tools.javac.code.TypeTag.CLASS;
79
80 import com.sun.tools.javac.comp.Modules;
81 import com.sun.tools.javac.resources.CompilerProperties.Notes;
82
83 import static com.sun.tools.javac.tree.JCTree.Tag.*;
84
85 /**
86 * Utility methods for operating on program elements.
87 *
88 * <p><b>This is NOT part of any supported API.
89 * If you write code that depends on this, you do so at your own
90 * risk. This code and its internal interfaces are subject to change
91 * or deletion without notice.</b></p>
92 */
93 public class JavacElements implements Elements {
94
95 private final JavaCompiler javaCompiler;
96 private final Symtab syms;
97 private final Modules modules;
98 private final Names names;
99 private final Types types;
100 private final Enter enter;
101 private final JavacTrees javacTrees;
102 private final Attr attr;
103 private final JavacTaskImpl javacTaskImpl;
104 private final Log log;
105 private final TreeMaker make;
106 private final boolean allowModules;
107
108 public static JavacElements instance(Context context) {
109 JavacElements instance = context.get(JavacElements.class);
110 if (instance == null)
111 instance = new JavacElements(context);
112 return instance;
113 }
114
115 @SuppressWarnings("this-escape")
116 protected JavacElements(Context context) {
117 context.put(JavacElements.class, this);
118 javaCompiler = JavaCompiler.instance(context);
119 syms = Symtab.instance(context);
120 modules = Modules.instance(context);
121 names = Names.instance(context);
122 types = Types.instance(context);
123 enter = Enter.instance(context);
124 attr = Attr.instance(context);
125 javacTrees = JavacTrees.instance(context);
126 JavacTask t = context.get(JavacTask.class);
127 javacTaskImpl = t instanceof JavacTaskImpl taskImpl ? taskImpl : null;
128 log = Log.instance(context);
129 make = TreeMaker.instance(context);
130 Source source = Source.instance(context);
131 allowModules = Feature.MODULES.allowedInSource(source);
132 }
133
134 @Override @DefinedBy(Api.LANGUAGE_MODEL)
135 public Set<? extends ModuleElement> getAllModuleElements() {
136 if (allowModules)
137 return Collections.unmodifiableSet(modules.allModules());
138 else
139 return Collections.emptySet();
140 }
141
142 @Override @DefinedBy(Api.LANGUAGE_MODEL)
143 public ModuleSymbol getModuleElement(CharSequence name) {
144 ensureEntered("getModuleElement");
145 if (modules.getDefaultModule() == syms.noModule)
146 return null;
147 String strName = name.toString();
148 if (strName.equals(""))
149 return syms.unnamedModule;
|