8173776: More javax.lang.model improvements to support modules

Reviewed-by: jjg, jlahoda
This commit is contained in:
Joe Darcy 2017-02-01 17:04:24 -08:00
parent 3a1a49f453
commit 20529706dc
5 changed files with 56 additions and 11 deletions

View File

@ -139,7 +139,7 @@ public interface Element extends javax.lang.model.AnnotatedConstruct {
*
* <li> If this is a {@linkplain
* PackageElement#getEnclosingElement package}, its module is
* returned.
* returned if such a module exists. Otherwise, {@code null} is returned.
*
* <li> If this is a {@linkplain
* TypeParameterElement#getEnclosingElement type parameter},

View File

@ -83,9 +83,16 @@ public interface PackageElement extends Element, QualifiedNameable {
boolean isUnnamed();
/**
* Returns the enclosing module.
* Returns the enclosing module if such a module exists; otherwise
* returns {@code null}.
*
* @return the enclosing module
* One situation where a module does not exist for a package is if
* the environment does not include modules, such as an annotation
* processing environment configured for a {@linkplain
* javax.annotation.processing.ProcessingEnvironment#getSourceVersion
* source version} without modules.
*
* @return the enclosing module or {@code null} if no such module exists
*/
@Override
Element getEnclosingElement();

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -34,6 +34,7 @@ import javax.lang.model.element.ExecutableElement;
* <ul>
* <li>{@link TypeKind#VOID VOID} - corresponds to the keyword {@code void}.
* <li>{@link TypeKind#PACKAGE PACKAGE} - the pseudo-type of a package element.
* <li>{@link TypeKind#MODULE MODULE} - the pseudo-type of a module element.
* <li>{@link TypeKind#NONE NONE} - used in other cases
* where no actual type is appropriate; for example, the superclass
* of {@code java.lang.Object}.

View File

@ -59,12 +59,17 @@ public interface Elements {
/**
* Returns a package given its fully qualified name, as seen from the given module.
*
* @implSpec The default implementation of this method returns
* {@code null}.
*
* @param name fully qualified package name, or an empty string for an unnamed package
* @param module module relative to which the lookup should happen
* @return the specified package, or {@code null} if it cannot be found
* @since 9
*/
PackageElement getPackageElement(ModuleElement module, CharSequence name);
default PackageElement getPackageElement(ModuleElement module, CharSequence name) {
return null;
}
/**
* Returns a type element given its canonical name if the type element is unique in the environment.
@ -79,12 +84,17 @@ public interface Elements {
/**
* Returns a type element given its canonical name, as seen from the given module.
*
* @implSpec The default implementation of this method returns
* {@code null}.
*
* @param name the canonical name
* @param module module relative to which the lookup should happen
* @return the named type element, or {@code null} if it cannot be found
* @since 9
*/
TypeElement getTypeElement(ModuleElement module, CharSequence name);
default TypeElement getTypeElement(ModuleElement module, CharSequence name) {
return null;
}
/**
* Returns a module element given its fully qualified name.
@ -95,11 +105,16 @@ public interface Elements {
* javax.annotation.processing.ProcessingEnvironment#getSourceVersion
* source version} without modules.
*
* @implSpec The default implementation of this method returns
* {@code null}.
*
* @param name the name
* @return the named module element, or {@code null} if it cannot be found
* @since 9
*/
ModuleElement getModuleElement(CharSequence name);
default ModuleElement getModuleElement(CharSequence name) {
return null;
}
/**
* Returns the values of an annotation's elements, including defaults.
@ -337,11 +352,16 @@ public interface Elements {
* javax.annotation.processing.ProcessingEnvironment#getSourceVersion
* source version} without modules.
*
* @implSpec The default implementation of this method returns
* {@code null}.
*
* @param type the element being examined
* @return the module of an element
* @since 9
*/
ModuleElement getModuleOf(Element type);
default ModuleElement getModuleOf(Element type) {
return null;
}
/**
* Returns all members of a type element, whether inherited or

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2006, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2006, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -23,16 +23,18 @@
/*
* @test
* @bug 6449798 6399404
* @bug 6449798 6399404 8173776
* @summary Test basic workings of PackageElement
* @author Joseph D. Darcy
* @library /tools/javac/lib
* @modules java.compiler
* jdk.compiler
* @build JavacTestingAbstractProcessor TestPackageElement
* @compile -processor TestPackageElement -proc:only TestPackageElement.java
* @compile -processor TestPackageElement -proc:only TestPackageElement.java
* @compile -processor TestPackageElement -proc:only --release 8 TestPackageElement.java
*/
import java.util.Objects;
import java.util.Set;
import javax.annotation.processing.*;
import javax.lang.model.SourceVersion;
@ -67,7 +69,22 @@ public class TestPackageElement extends JavacTestingAbstractProcessor {
PackageElement javaLang = eltUtils.getPackageElement("java.lang");
if (javaLang.isUnnamed())
throw new RuntimeException("Package java.lang is unnamed!");
testEnclosingElement(javaLang);
}
return true;
}
void testEnclosingElement(PackageElement javaLang) {
SourceVersion version = processingEnv.getSourceVersion();
Element enclosing = javaLang.getEnclosingElement();
Element expectedEnclosing =
(version.compareTo(RELEASE_9) < 0) ? // No modules
null :
eltUtils.getModuleElement("java.base");
if (!Objects.equals(enclosing, expectedEnclosing))
throw new RuntimeException("Unexpected enclosing element under source version " +
version);
}
}