本文整理汇总了Java中javax.lang.model.util.Elements.getPackageElement方法的典型用法代码示例。如果您正苦于以下问题:Java Elements.getPackageElement方法的具体用法?Java Elements.getPackageElement怎么用?Java Elements.getPackageElement使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.lang.model.util.Elements
的用法示例。
在下文中一共展示了Elements.getPackageElement方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: resolve
import javax.lang.model.util.Elements; //导入方法依赖的package包/类
public Element resolve(ASTService ast, Elements elements, String what, ModuleElement modle) {
Element result = null;
if (classes.containsKey(what)) {
result = createElement(ast, elements, what, null, modle);
}
if (result == null) {
result = modle != null ? elements.getTypeElement(modle, what) : elements.getTypeElement(what);
}
if (result == null) {
result = modle != null ? elements.getPackageElement(modle, what) : elements.getPackageElement(what);
}
result = createElement(ast, elements, what, result, modle);
return result;
}
示例2: createElement
import javax.lang.model.util.Elements; //导入方法依赖的package包/类
private Element createElement(ASTService ast, Elements elements, String name, Element original, ModuleElement modle) {
Element el = elementCache.get(name);
if (el == null) {
if (original != null) {
if (original.getKind().isClass() || original.getKind().isInterface()) {
elementCache.put(name, el = new TypeElementWrapper(ast, elements, (TypeElement) original));
return el;
}
if (original.getKind() == ElementKind.PACKAGE) {
elementCache.put(name, el = new PackageElementWrapper(ast, elements, (PackageElement) original));
return el;
}
return original;
}
int lastDot = name.lastIndexOf('.');
Name simpleName = elements.getName(name.substring(lastDot + 1));
Name fqnName = elements.getName(name);
if (classes.containsKey(name)) {
Element parent = lastDot > 0 ? resolve(ast, elements, name.substring(0, lastDot), modle) : elements.getPackageElement("");
elementCache.put(name, el = new FakeTypeElement(ast, elements, simpleName, fqnName, name, parent, classes.get(name), modle));
} else if (packages.contains(name)) {
elementCache.put(name, el = new FakePackageElement(ast, elements, fqnName, name, simpleName, modle));
} else {
return null;//XXX: handling of this null in callers!
}
}
return el;
}
示例3: getElementByFQN
import javax.lang.model.util.Elements; //导入方法依赖的package包/类
private Element getElementByFQN(CompilationUnitTree cut, String fqn) {
Elements elements = copy.getElements();
Element element = elements.getTypeElement(fqn);
if (element == null)
element = elements.getPackageElement(fqn);
if (element == null)
element = Symtab.instance(copy.impl.getJavacTask().getContext()).enterClass(
Modules.instance(copy.impl.getJavacTask().getContext()).getDefaultModule(),
(com.sun.tools.javac.util.Name)elements.getName(fqn));
return element;
}
示例4: run
import javax.lang.model.util.Elements; //导入方法依赖的package包/类
public void run(WorkingCopy copy) throws Exception {
copy.toPhase(JavaSource.Phase.RESOLVED);
CompilationUnitTree cut = copy.getCompilationUnit();
Elements elements = copy.getElements();
Set<Element> imports = new HashSet<Element>();
for (String imp : toImport) {
if (imp.endsWith(".*"))
imp = imp.substring(0, imp.length() - 2);
Element el = elements.getTypeElement(imp);
if (el == null)
el = elements.getPackageElement(imp);
if (el == null) {
int idx = imp.lastIndexOf('.');
if (idx >= 0) {
el = elements.getTypeElement(imp.substring(0, idx));
if (el != null) {
String name = imp.substring(idx + 1);
for (Element ee : el.getEnclosedElements()) {
if (name.contentEquals(ee.getSimpleName())) {
el = ee;
break;
}
}
}
}
}
assertNotNull(el);
imports.add(el);
}
GeneratorUtilities utilities = GeneratorUtilities.get(copy);
assertNotNull(utilities);
CompilationUnitTree newCut = utilities.addImports(cut, imports);
copy.rewrite(cut, newCut);
}
示例5: addPackages
import javax.lang.model.util.Elements; //导入方法依赖的package包/类
private void addPackages(Env env, String fqnPrefix, boolean srcOnly) {
if (fqnPrefix == null) {
fqnPrefix = EMPTY;
}
String prefix = env.getPrefix() != null ? fqnPrefix + env.getPrefix() : fqnPrefix;
CompilationController controller = env.getController();
Elements elements = controller.getElements();
Element el = controller.getTrees().getElement(new TreePath(controller.getCompilationUnit()));
ModuleElement moduleElement = el != null ? controller.getElements().getModuleOf(el) : null;
Set<String> seenPkgs = new HashSet<>();
EnumSet<ClassIndex.SearchScope> scope = srcOnly ? EnumSet.of(ClassIndex.SearchScope.SOURCE) : EnumSet.allOf(ClassIndex.SearchScope.class);
for (String pkgName : env.getController().getClasspathInfo().getClassIndex().getPackageNames(fqnPrefix, false, scope)) {
if (startsWith(env, pkgName, prefix) && !Utilities.isExcluded(pkgName + ".")
&& (moduleElement != null ? elements.getPackageElement(moduleElement, pkgName) : elements.getPackageElement(pkgName)) != null) { //NOI18N
if (fqnPrefix != null) {
pkgName = pkgName.substring(fqnPrefix.length());
}
int idx = pkgName.indexOf('.');
if (idx > 0) {
pkgName = pkgName.substring(0, idx);
}
if (seenPkgs.add(pkgName)) {
results.add(itemFactory.createPackageItem(pkgName, anchorOffset, srcOnly));
}
}
}
}
示例6: elementStuff
import javax.lang.model.util.Elements; //导入方法依赖的package包/类
static void elementStuff(Element e, Elements elems) {
// BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
e.getAnnotation(null);
// BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
elems.getPackageElement(null);
// BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
elems.getTypeElement(null);
// BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
elems.getDocComment(null);
}
示例7: process
import javax.lang.model.util.Elements; //导入方法依赖的package包/类
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
if (round++ != 0)
return false;
Elements elements = processingEnv.getElementUtils();
PackageElement pe = elements.getPackageElement("");
if (pe == null) {
System.out.println("null");
} else {
ModuleElement mod = (ModuleElement) pe.getEnclosingElement();
if (mod == null) {
System.out.println("noModule");
} else if (mod.isUnnamed()) {
System.out.println("unnamedModule");
} else {
System.out.println(mod);
}
}
ModuleElement m1x = elements.getModuleElement("m1x");
ModuleElement m2x = elements.getModuleElement("m2x");
if (m1x != null && m2x != null) {
System.out.println("m1x: " + (elements.getPackageElement(m1x, "") != null));
System.out.println("m2x: " + (elements.getPackageElement(m2x, "") != null));
}
return false;
}
示例8: completeThrowsOrPkg
import javax.lang.model.util.Elements; //导入方法依赖的package包/类
private void completeThrowsOrPkg(String fqn, String prefix, int substitutionOffset, JavadocContext jdctx) {
final Elements elements = jdctx.javac.getElements();
final Set<Element> excludes = new HashSet<Element>();
String pkgPrefix;
// add declared Throwables
ExecutableElement method = (ExecutableElement) jdctx.commentFor;
for (TypeMirror type : method.getThrownTypes()) {
if (type.getKind() != TypeKind.DECLARED) continue;
TypeElement clazz = (TypeElement) ((DeclaredType) type).asElement();
String typeName = clazz.getSimpleName().toString();
if (startsWith(typeName, prefix)) {
String qualTypeName = clazz.getQualifiedName().toString();
TypeElement typeElement = elements.getTypeElement(qualTypeName);
if (typeElement == null) {
continue;
}
items.add(JavaCompletionItem.createTypeItem(
jdctx.javac, typeElement, (DeclaredType) typeElement.asType(),
substitutionOffset, /*XXX:*/typeName != qualTypeName ? jdctx.getReferencesCount() : null,
elements.isDeprecated(typeElement), false, false, false, true, false, null));
excludes.add(typeElement);
}
}
// add other Throwables
if (fqn == null) {
pkgPrefix = prefix;
addTypes(EnumSet.<ElementKind>of(CLASS),
findDeclaredType("java.lang.Throwable", elements), // NOI18N
excludes, prefix, substitutionOffset, jdctx);
} else {
pkgPrefix = fqn + '.' + prefix;
PackageElement pkgElm = elements.getPackageElement(fqn);
if (pkgElm != null) {
addPackageContent(pkgElm,
EnumSet.<ElementKind>of(CLASS),
findDeclaredType("java.lang.Throwable", elements), // NOI18N
excludes, prefix, substitutionOffset, jdctx);
}
TypeElement typeElm = elements.getTypeElement(fqn);
if (typeElm != null) {
// inner classes
addInnerClasses(typeElm,
EnumSet.<ElementKind>of(CLASS),
findDeclaredType("java.lang.Throwable", elements), // NOI18N
excludes, prefix, substitutionOffset, jdctx);
}
}
// add packages
for (String pkgName : jdctx.javac.getClasspathInfo().getClassIndex().getPackageNames(pkgPrefix, true, EnumSet.allOf(ClassIndex.SearchScope.class)))
if (pkgName.length() > 0)
items.add(JavaCompletionItem.createPackageItem(pkgName, substitutionOffset, false));
}
示例9: run
import javax.lang.model.util.Elements; //导入方法依赖的package包/类
void run() throws Exception {
ToolBox tb = new ToolBox();
Path jar = createJar(tb);
Path src = Paths.get("src");
tb.writeJavaFiles(src, "class C { p1.C1 c1; }");
JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
PrintWriter out = new PrintWriter(System.err, true);
StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
List<String> options = Arrays.asList("-classpath", jar.toString(), "-proc:none");
Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(src.resolve("C.java"));
com.sun.source.util.JavacTask task =
(com.sun.source.util.JavacTask) comp.getTask(out, fm, null, options, null, files);
task.parse();
Elements elems = task.getElements();
try {
// Use p1, p1.C1 as a control to verify normal behavior
PackageElement p1 = elems.getPackageElement("p1");
TypeElement p1C1 = elems.getTypeElement("p1.C1");
System.err.println("p1: " + p1 + "; p1C1: " + p1C1);
if (p1C1 == null) {
throw new Exception("p1.C1 not found");
}
// Now repeat for p2, p2.C2, closing the file manager early
PackageElement p2 = elems.getPackageElement("p2");
System.err.println("closing file manager");
fm.close();
TypeElement p2C2 = elems.getTypeElement("p2.C2");
System.err.println("p2: " + p2 + "; p2C2: " + p2C2);
if (p2C2 != null) {
throw new Exception("p2.C2 found unexpectedly");
}
} catch (ClosedFileSystemException e) {
throw new Exception("unexpected exception thrown", e);
}
}