8166635: getEnclosedElements() on package causes BadClassFile error
authorjlahoda
Thu, 03 Nov 2016 11:46:20 +0100
changeset 41935 b1f606fff3ff
parent 41934 a4da50688dc7
child 41936 94139f291be4
8166635: getEnclosedElements() on package causes BadClassFile error Summary: Properly reading typevars from enclosing scopes for local classes. Reviewed-by: mcimadamore
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassReader.java
langtools/test/tools/javac/processing/model/nestedTypeVars/NestedTypeVars.java
langtools/test/tools/javac/processing/model/nestedTypeVars/NestedTypeVars.out
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassReader.java	Wed Nov 02 16:29:50 2016 -0700
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassReader.java	Thu Nov 03 11:46:20 2016 +0100
@@ -1328,7 +1328,7 @@
         } else {
             ((ClassType)sym.type).setEnclosingType(Type.noType);
         }
-        enterTypevars(self);
+        enterTypevars(self, self.type);
         if (!missingTypeVariables.isEmpty()) {
             ListBuffer<Type> typeVars =  new ListBuffer<>();
             for (Type typevar : missingTypeVariables) {
@@ -2353,19 +2353,17 @@
     /** Enter type variables of this classtype and all enclosing ones in
      *  `typevars'.
      */
-    protected void enterTypevars(Type t) {
-        if (t.getEnclosingType() != null && t.getEnclosingType().hasTag(CLASS))
-            enterTypevars(t.getEnclosingType());
-        for (List<Type> xs = t.getTypeArguments(); xs.nonEmpty(); xs = xs.tail)
+    protected void enterTypevars(Symbol sym, Type t) {
+        if (t.getEnclosingType() != null) {
+            if (!t.getEnclosingType().hasTag(TypeTag.NONE)) {
+                enterTypevars(sym.owner, t.getEnclosingType());
+            }
+        } else if (sym.kind == MTH && !sym.isStatic()) {
+            enterTypevars(sym.owner, sym.owner.type);
+        }
+        for (List<Type> xs = t.getTypeArguments(); xs.nonEmpty(); xs = xs.tail) {
             typevars.enter(xs.head.tsym);
-    }
-
-    protected void enterTypevars(Symbol sym) {
-        if (sym.owner.kind == MTH) {
-            enterTypevars(sym.owner);
-            enterTypevars(sym.owner.owner);
         }
-        enterTypevars(sym.type);
     }
 
     protected ClassSymbol enterClass(Name name) {
@@ -2388,7 +2386,7 @@
         // prepare type variable table
         typevars = typevars.dup(currentOwner);
         if (ct.getEnclosingType().hasTag(CLASS))
-            enterTypevars(ct.getEnclosingType());
+            enterTypevars(c.owner, ct.getEnclosingType());
 
         // read flags, or skip if this is an inner class
         long f = nextChar();
@@ -2545,6 +2543,11 @@
                     types.subst(ct.supertype_field, missing, found);
                 ct.interfaces_field =
                     types.subst(ct.interfaces_field, missing, found);
+                ct.typarams_field =
+                    types.substBounds(ct.typarams_field, missing, found);
+                for (List<Type> types = ct.typarams_field; types.nonEmpty(); types = types.tail) {
+                    types.head.tsym.type = types.head;
+                }
             } else if (missingTypeVariables.isEmpty() !=
                        foundTypeVariables.isEmpty()) {
                 Name name = missingTypeVariables.head.tsym.name;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/processing/model/nestedTypeVars/NestedTypeVars.java	Thu Nov 03 11:46:20 2016 +0100
@@ -0,0 +1,138 @@
+/*
+ * Copyright (c) 2016, 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
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * @test
+ * @modules jdk.compiler
+ * @build NestedTypeVars
+ * @compile/process/ref=NestedTypeVars.out -processor NestedTypeVars Test$1L1$L2$1L3$L4$L5 Test$1L1$CCheck Test$1L1 Test$1CCheck Test$CCheck Test
+ */
+
+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.lang.model.SourceVersion;
+import javax.lang.model.element.Element;
+import javax.lang.model.element.ExecutableElement;
+import javax.lang.model.element.TypeElement;
+import javax.lang.model.element.TypeParameterElement;
+import javax.lang.model.type.TypeKind;
+import javax.lang.model.type.TypeMirror;
+import javax.lang.model.type.TypeVariable;
+import javax.lang.model.util.ElementFilter;
+
+@SupportedAnnotationTypes("*")
+public class NestedTypeVars extends AbstractProcessor{
+
+    @Override
+    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
+        for (TypeElement te : ElementFilter.typesIn(roundEnv.getRootElements())) {
+            System.out.print(processingEnv.getElementUtils().getBinaryName(te));
+            System.out.print("<");
+            String separator = "";
+            for (TypeParameterElement tp : te.getTypeParameters()) {
+                System.out.print(separator);
+                separator = ", ";
+                System.out.print(tp.getSimpleName());
+                System.out.print(" extends ");
+                System.out.print(tp.getBounds().stream().map(b -> toString(b)).collect(Collectors.joining("&")));
+            }
+            System.out.println(">");
+            for (ExecutableElement m : ElementFilter.methodsIn(te.getEnclosedElements())) {
+                System.out.print("  <");
+                separator = "";
+                for (TypeParameterElement tp : m.getTypeParameters()) {
+                    System.out.print(separator);
+                    separator = ", ";
+                    System.out.print(tp.getSimpleName());
+                    System.out.print(" extends ");
+                    System.out.print(tp.getBounds().
+                            stream().
+                            map(b -> toString(b)).
+                            collect(Collectors.joining("&")));
+                }
+                System.out.print(">");
+                System.out.println(m.getSimpleName());
+            }
+        }
+
+        return false;
+    }
+
+    String toString(TypeMirror bound) {
+        if (bound.getKind() == TypeKind.TYPEVAR) {
+            TypeVariable var = (TypeVariable) bound;
+            return toString(var.asElement());
+        }
+        return bound.toString();
+    }
+
+    String toString(Element el) {
+        switch (el.getKind()) {
+            case METHOD:
+                return toString(el.getEnclosingElement()) + "." + el.getSimpleName();
+            case CLASS:
+                return processingEnv.getElementUtils().getBinaryName((TypeElement) el).toString();
+            case TYPE_PARAMETER:
+                return toString(((TypeParameterElement) el).getGenericElement()) + "." + el.getSimpleName();
+            default:
+                throw new IllegalStateException("Unexpected element: " + el + "(" + el.getKind() + ")");
+        }
+    }
+    @Override
+    public SourceVersion getSupportedSourceVersion() {
+        return SourceVersion.latestSupported();
+    }
+
+
+}
+
+class Test<T1, C> {
+    <T2, C> void m() {
+        class L1<T3, C> {
+            class L2<T4, C> {
+                <T5, C> void m() {
+                    class L3<T6, C> {
+                        class L4<T7, C> {
+                            class L5<T1a extends T1,
+                                     T2a extends T2,
+                                     T3a extends T3,
+                                     T4a extends T4,
+                                     T5a extends T5,
+                                     T6a extends T6,
+                                     T7a extends T7> {}
+                        }
+                    }
+                }
+            }
+            class CCheck<T extends C> {}
+            <T extends C> void test() {}
+        }
+        class CCheck<T extends C> {}
+    }
+    class CCheck<T extends C> {}
+    <T extends C> void test() {}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/processing/model/nestedTypeVars/NestedTypeVars.out	Thu Nov 03 11:46:20 2016 +0100
@@ -0,0 +1,9 @@
+Test$1L1$L2$1L3$L4$L5<T1a extends Test.T1, T2a extends Test.m.T2, T3a extends Test$1L1.T3, T4a extends Test$1L1$L2.T4, T5a extends Test$1L1$L2.m.T5, T6a extends Test$1L1$L2$1L3.T6, T7a extends Test$1L1$L2$1L3$L4.T7>
+Test$1L1$CCheck<T extends Test$1L1.C>
+Test$1L1<T3 extends java.lang.Object, C extends java.lang.Object>
+  <T extends Test$1L1.C>test
+Test$1CCheck<T extends Test.m.C>
+Test$CCheck<T extends Test.C>
+Test<T1 extends java.lang.Object, C extends java.lang.Object>
+  <T2 extends java.lang.Object, C extends java.lang.Object>m
+  <T extends Test.C>test