1 /* 2 * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * @test 26 * @modules jdk.compiler 27 * @library /tools/javac/lib 28 * @build NestedTypeVars JavacTestingAbstractProcessor 29 * @compile/process/ref=NestedTypeVars.out -processor NestedTypeVars Test$1L1$L2$1L3$L4$L5 Test$1L1$CCheck Test$1L1 Test$1CCheck Test$CCheck Test 30 */ 31 32 import java.util.Set; 33 import java.util.stream.Collectors; 34 35 import javax.annotation.processing.AbstractProcessor; 36 import javax.annotation.processing.RoundEnvironment; 37 import javax.annotation.processing.SupportedAnnotationTypes; 38 import javax.lang.model.SourceVersion; 39 import javax.lang.model.element.Element; 40 import javax.lang.model.element.ExecutableElement; 41 import javax.lang.model.element.TypeElement; 42 import javax.lang.model.element.TypeParameterElement; 43 import javax.lang.model.type.TypeKind; 44 import javax.lang.model.type.TypeMirror; 45 import javax.lang.model.type.TypeVariable; 46 import javax.lang.model.util.ElementFilter; 47 48 public class NestedTypeVars extends JavacTestingAbstractProcessor { 49 50 @Override 51 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { 52 for (TypeElement te : ElementFilter.typesIn(roundEnv.getRootElements())) { 53 System.out.print(processingEnv.getElementUtils().getBinaryName(te)); 54 System.out.print("<"); 55 String separator = ""; 56 for (TypeParameterElement tp : te.getTypeParameters()) { 57 System.out.print(separator); 58 separator = ", "; 59 System.out.print(tp.getSimpleName()); 60 System.out.print(" extends "); 61 System.out.print(tp.getBounds().stream().map(b -> toString(b)).collect(Collectors.joining("&"))); 62 } 63 System.out.println(">"); 64 for (ExecutableElement m : ElementFilter.methodsIn(te.getEnclosedElements())) { 65 System.out.print(" <"); 66 separator = ""; 67 for (TypeParameterElement tp : m.getTypeParameters()) { 68 System.out.print(separator); 69 separator = ", "; 70 System.out.print(tp.getSimpleName()); 71 System.out.print(" extends "); 72 System.out.print(tp.getBounds(). 73 stream(). 74 map(b -> toString(b)). 75 collect(Collectors.joining("&"))); 76 } 77 System.out.print(">"); 78 System.out.println(m.getSimpleName()); 79 } 80 } 81 82 return false; 83 } 84 85 String toString(TypeMirror bound) { 86 if (bound.getKind() == TypeKind.TYPEVAR) { 87 TypeVariable var = (TypeVariable) bound; 88 return toString(var.asElement()); 89 } 90 return bound.toString(); 91 } 92 93 String toString(Element el) { 94 switch (el.getKind()) { 95 case METHOD: 96 return toString(el.getEnclosingElement()) + "." + el.getSimpleName(); 97 case CLASS: 98 return processingEnv.getElementUtils().getBinaryName((TypeElement) el).toString(); 99 case TYPE_PARAMETER: 100 return toString(((TypeParameterElement) el).getGenericElement()) + "." + el.getSimpleName(); 101 default: 102 throw new IllegalStateException("Unexpected element: " + el + "(" + el.getKind() + ")"); 103 } 104 } 105 } 106 107 class Test<T1, C> { 108 <T2, C> void m() { 109 class L1<T3, C> { 110 class L2<T4, C> { 111 <T5, C> void m() { 112 class L3<T6, C> { 113 class L4<T7, C> { 114 class L5<T1a extends T1, 115 T2a extends T2, 116 T3a extends T3, 117 T4a extends T4, 118 T5a extends T5, 119 T6a extends T6, 120 T7a extends T7> {} 121 } 122 } 123 } 124 } 125 class CCheck<T extends C> {} 126 <T extends C> void test() {} 127 } 128 class CCheck<T extends C> {} 129 } 130 class CCheck<T extends C> {} 131 <T extends C> void test() {} 132 }