changeset 3364:f34f96b386fc

Fixing a few Java model API bugs.
author jlahoda
date Fri, 23 Oct 2015 14:22:26 -0700
parents 7907e684cef1
children f5aa149f5bd0
files src/java.compiler/share/classes/javax/lang/model/type/TypeKind.java src/jdk.compiler/share/classes/com/sun/tools/javac/code/Symbol.java src/jdk.compiler/share/classes/com/sun/tools/javac/code/Symtab.java src/jdk.compiler/share/classes/com/sun/tools/javac/code/Type.java test/tools/javac/modules/AnnotationProcessing.java test/tools/javac/processing/model/TestSymtabItems.java test/tools/javac/tree/MakeTypeTest.java
diffstat 7 files changed, 110 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.compiler/share/classes/javax/lang/model/type/TypeKind.java	Fri Oct 23 13:54:30 2015 -0700
+++ b/src/java.compiler/share/classes/javax/lang/model/type/TypeKind.java	Fri Oct 23 14:22:26 2015 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2015, 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
@@ -151,7 +151,14 @@
       *
       * @since 1.8
       */
-    INTERSECTION;
+    INTERSECTION,
+
+    /**
+     * A pseudo-type corresponding to a package element.
+     * @see NoType
+     * @since 1.9
+     */
+    MODULE;
 
     /**
      * Returns {@code true} if this kind corresponds to a primitive
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Symbol.java	Fri Oct 23 13:54:30 2015 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Symbol.java	Fri Oct 23 14:22:26 2015 -0700
@@ -28,6 +28,7 @@
 import java.lang.annotation.Annotation;
 import java.lang.annotation.Inherited;
 import java.util.Collections;
+import java.util.HashSet;
 import java.util.Set;
 import java.util.concurrent.Callable;
 
@@ -871,6 +872,7 @@
 
         public PackageSymbol unnamedPackage;
         public Set<PackageSymbol> visiblePackages;
+        public List<Symbol> enclosedPackages = List.nil();
 
         public Completer usesProvidesCompleter = Completer.NULL_COMPLETER;
 
@@ -944,7 +946,17 @@
 
         @Override
         public <R, P> R accept(ElementVisitor<R, P> v, P p) {
-            return v.visitUnknown(this, p); // for now
+            return v.visitModule(this, p);
+        }
+
+        @Override
+        public List<Symbol> getEnclosedElements() {
+            List<Symbol> list = List.nil();
+            for (Symbol sym : enclosedPackages) {
+                if (sym.members().anyMatch(m -> m.kind == TYP))
+                    list = list.prepend(sym);
+            }
+            return list;
         }
 
         public void reset() {
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Symtab.java	Fri Oct 23 13:54:30 2015 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Symtab.java	Fri Oct 23 14:22:26 2015 -0700
@@ -704,6 +704,7 @@
 
     private void doEnterPackage(ModuleSymbol msym, PackageSymbol pack) {
         packages.computeIfAbsent(pack.fullname, n -> new HashMap<>()).put(msym, pack);
+        msym.enclosedPackages = msym.enclosedPackages.prepend(pack);
     }
 
     private void addRootPackageFor(ModuleSymbol module) {
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Type.java	Fri Oct 23 13:54:30 2015 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Type.java	Fri Oct 23 14:22:26 2015 -0700
@@ -1560,8 +1560,7 @@
 
         @Override
         public TypeKind getKind() {
-            throw new UnsupportedOperationException();
-//            return TypeKind.MODULE;
+            return TypeKind.MODULE;
         }
 
         @Override
--- a/test/tools/javac/modules/AnnotationProcessing.java	Fri Oct 23 13:54:30 2015 -0700
+++ b/test/tools/javac/modules/AnnotationProcessing.java	Fri Oct 23 14:22:26 2015 -0700
@@ -34,18 +34,28 @@
 
 import java.nio.file.Files;
 import java.nio.file.Path;
+import java.util.Arrays;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.Objects;
 import java.util.Set;
+import java.util.stream.Collectors;
+
 import javax.annotation.processing.AbstractProcessor;
 import javax.annotation.processing.RoundEnvironment;
 import javax.annotation.processing.SupportedAnnotationTypes;
+import javax.annotation.processing.SupportedOptions;
 import javax.lang.model.SourceVersion;
+import javax.lang.model.element.Element;
 import javax.lang.model.element.ModuleElement;
 import javax.lang.model.element.ModuleElement.ProvidesDirective;
 import javax.lang.model.element.ModuleElement.UsesDirective;
+import javax.lang.model.element.PackageElement;
 import javax.lang.model.element.TypeElement;
+import javax.lang.model.type.TypeKind;
 import javax.lang.model.util.ElementFilter;
+import javax.lang.model.util.ElementScanner9;
 
 public class AnnotationProcessing extends ModuleTestBase {
 
@@ -67,7 +77,9 @@
                           "package impl; public class Impl { }");
 
         String log = tb.new JavacTask()
-                .options("-modulesourcepath", moduleSrc.toString(), "-processor", AP.class.getName())
+                .options("-modulesourcepath", moduleSrc.toString(),
+                         "-processor", AP.class.getName(),
+                         "-AexpectedEnclosedElements=m1=>impl")
                 .outdir(classes)
                 .files(findJavaFiles(moduleSrc))
                 .run()
@@ -97,7 +109,9 @@
                           "package impl2; public class Impl2 { }");
 
         String log = tb.new JavacTask()
-                .options("-modulesourcepath", moduleSrc.toString(), "-processor", AP.class.getName())
+                .options("-modulesourcepath", moduleSrc.toString(),
+                         "-processor", AP.class.getName(),
+                         "-AexpectedEnclosedElements=m1=>impl1,m2=>impl2")
                 .outdir(classes)
                 .files(findJavaFiles(moduleSrc))
                 .run()
@@ -109,10 +123,64 @@
     }
 
     @SupportedAnnotationTypes("*")
+    @SupportedOptions("expectedEnclosedElements")
     public static final class AP extends AbstractProcessor {
 
+        private Map<String, List<String>> module2ExpectedEnclosedElements;
+
         @Override
         public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
+            if (module2ExpectedEnclosedElements == null) {
+                module2ExpectedEnclosedElements = new HashMap<>();
+
+                String expectedEnclosedElements =
+                        processingEnv.getOptions().get("expectedEnclosedElements");
+
+                for (String moduleDef : expectedEnclosedElements.split(",")) {
+                    String[] module2Packages = moduleDef.split("=>");
+
+                    module2ExpectedEnclosedElements.put(module2Packages[0],
+                                                        Arrays.asList(module2Packages[1].split(":")));
+                }
+            }
+
+            //verify ModuleType and ModuleSymbol behavior:
+            for (Element root : roundEnv.getRootElements()) {
+                ModuleElement module = processingEnv.getElementUtils().getModuleOf(root);
+
+                assertEquals(TypeKind.MODULE, module.asType().getKind());
+
+                boolean[] seenModule = new boolean[1];
+
+                module.accept(new ElementScanner9<Void, Void>() {
+                    @Override
+                    public Void visitModule(ModuleElement e, Void p) {
+                        seenModule[0] = true;
+                        return null;
+                    }
+                    @Override
+                    public Void scan(Element e, Void p) {
+                        throw new AssertionError("Shouldn't get here.");
+                    }
+                }, null);
+
+                assertEquals(true, seenModule[0]);
+
+                List<String> actualElements =
+                        module.getEnclosedElements()
+                              .stream()
+                              .map(s -> (PackageElement) s)
+                              .map(p -> p.getQualifiedName().toString())
+                              .collect(Collectors.toList());
+
+                assertEquals(module2ExpectedEnclosedElements.remove(module.getQualifiedName().toString()),
+                             actualElements);
+            }
+
+            if (roundEnv.processingOver()) {
+                assertEquals(true, module2ExpectedEnclosedElements.isEmpty());
+            }
+
             return false;
         }
 
--- a/test/tools/javac/processing/model/TestSymtabItems.java	Fri Oct 23 13:54:30 2015 -0700
+++ b/test/tools/javac/processing/model/TestSymtabItems.java	Fri Oct 23 14:22:26 2015 -0700
@@ -33,8 +33,10 @@
  */
 
 import java.lang.reflect.Field;
+
 import javax.lang.model.element.Element;
 import javax.lang.model.element.ExecutableElement;
+import javax.lang.model.element.ModuleElement;
 import javax.lang.model.element.PackageElement;
 import javax.lang.model.element.TypeElement;
 import javax.lang.model.element.TypeParameterElement;
@@ -83,6 +85,10 @@
             if (f.getName().toLowerCase().contains("methodhandle"))
                 continue;
 
+            //both noModule and unnamedModule claim the unnamed package, ignore noModule for now:
+            if (f.getName().equals("noModule"))
+                continue;
+
             f.setAccessible(true);
             Class<?> ft = f.getType();
             if (TypeMirror.class.isAssignableFrom(ft))
@@ -118,6 +124,15 @@
 
     class ElemPrinter extends ElementScanner9<Void, Void> {
         @Override
+        public Void visitModule(ModuleElement e, Void p) {
+            show("module", e);
+            indent(+1);
+            super.visitModule(e, p);
+            indent(-1);
+            return null;
+        }
+
+        @Override
         public Void visitPackage(PackageElement e, Void p) {
             show("package", e);
             indent(+1);
--- a/test/tools/javac/tree/MakeTypeTest.java	Fri Oct 23 13:54:30 2015 -0700
+++ b/test/tools/javac/tree/MakeTypeTest.java	Fri Oct 23 14:22:26 2015 -0700
@@ -134,7 +134,7 @@
         }.scan(path, null);
 
         unseenTypeKinds.removeAll(Arrays.asList(TypeKind.NONE, TypeKind.NULL, TypeKind.ERROR,
-                TypeKind.PACKAGE, TypeKind.EXECUTABLE, TypeKind.OTHER));
+                TypeKind.PACKAGE, TypeKind.EXECUTABLE, TypeKind.OTHER, TypeKind.MODULE));
 
         if (!unseenTypeKinds.isEmpty())
             throw new IllegalStateException("Unhandled types=" + unseenTypeKinds);