Skip to content

Commit 24d77ad

Browse files
committed
8356057: PrintingProcessor (-Xprint) does not print type variable bounds and type annotations for Object supertypes
Reviewed-by: darcy, vromero
1 parent 4fc10a1 commit 24d77ad

File tree

6 files changed

+241
-21
lines changed

6 files changed

+241
-21
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/processing/PrintingProcessor.java

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -259,9 +259,7 @@ public PrintingElementVisitor visitType(TypeElement e, Boolean p) {
259259
if (kind == CLASS) {
260260
TypeMirror supertype = e.getSuperclass();
261261
if (supertype.getKind() != TypeKind.NONE) {
262-
TypeElement e2 = (TypeElement)
263-
((DeclaredType) supertype).asElement();
264-
if (e2.getSuperclass().getKind() != TypeKind.NONE)
262+
if (isImportantType(supertype))
265263
writer.print(" extends " + supertype);
266264
}
267265
}
@@ -524,13 +522,29 @@ private void printFormalTypeParameters(Parameterizable e,
524522
List<? extends TypeParameterElement> typeParams = e.getTypeParameters();
525523
if (!typeParams.isEmpty()) {
526524
writer.print(typeParams.stream()
527-
.map(tpe -> annotationsToString(tpe) + tpe.toString())
525+
.map(tpe -> annotationsToString(tpe) + tpe.toString() + printTypeVariableBoundsIfNeeded(tpe))
528526
.collect(Collectors.joining(", ", "<", ">")));
529527
if (pad)
530528
writer.print(" ");
531529
}
532530
}
533531

532+
private String printTypeVariableBoundsIfNeeded(TypeParameterElement tpe) {
533+
List<? extends TypeMirror> printableBounds =
534+
tpe.getBounds()
535+
.stream()
536+
.filter(type -> isImportantType(type))
537+
.toList();
538+
539+
if (printableBounds.isEmpty()) {
540+
return "";
541+
}
542+
543+
return " extends " + printableBounds.stream()
544+
.map(t -> t.toString())
545+
.collect(Collectors.joining(" & "));
546+
}
547+
534548
private String annotationsToString(Element e) {
535549
List<? extends AnnotationMirror> annotations = e.getAnnotationMirrors();
536550
return annotations.isEmpty() ?
@@ -770,5 +784,22 @@ private void indent() {
770784
writer.print(spaces[indentation]);
771785
}
772786

787+
/**{@return true if this type is either not {@code java.lang.Object},
788+
* or is annotated, and hence needs to be included in the output,
789+
* even for cases where there's implicit {@code java.lang.Object} type.}
790+
*
791+
* @param type the type to check.
792+
*/
793+
private boolean isImportantType(TypeMirror type) {
794+
if (!type.getAnnotationMirrors().isEmpty()) {
795+
return true;
796+
}
797+
TypeElement e2 = (TypeElement)
798+
((DeclaredType) type).asElement();
799+
if (!e2.getKind().isClass()) {
800+
return true;
801+
}
802+
return e2.getSuperclass().getKind() != TypeKind.NONE;
803+
}
773804
}
774805
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright (c) 2025, 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+
* @bug 8356057
27+
* @summary Verify annotated supertypes and type variable bounds are printed properly
28+
* @compile/ref=XprintTypeAnnotationsAndTypeVarBounds.out -Xprint XprintTypeAnnotationsAndTypeVarBounds.java
29+
*/
30+
31+
import java.lang.annotation.*;
32+
33+
class AnnotatedObjectSuperType extends @TA Object {
34+
}
35+
36+
class UnannotatedObjectSuperType extends Object {
37+
}
38+
39+
class TypeVariableWithAnnotation1<@TA T> {
40+
}
41+
42+
class TypeVariableWithAnnotation2<@TA T extends Object> {
43+
}
44+
45+
class TypeVariableWithBound1<T extends @TA Object> {
46+
}
47+
48+
class TypeVariableWithBound2<T extends @TA CharSequence> {
49+
}
50+
51+
class TypeVariableWithBound3<T extends @TA Object & CharSequence> {
52+
}
53+
54+
class TypeVariableWithBound4<T extends Object & @TA CharSequence> {
55+
}
56+
57+
class TypeVariableWithBound5<T extends CharSequence> {
58+
}
59+
60+
class TypeVariableWithBound6<T extends Object & CharSequence> {
61+
}
62+
63+
class TypeVariableWithBoundRecursive<T extends TypeVariableWithBoundRecursive<T>> {
64+
}
65+
66+
class TypeVariableBoundsOnMethods {
67+
public <@TA T> void test1() {}
68+
public <@TA T extends Object> void test2() {}
69+
public <T extends @TA Object> void test3() {}
70+
public <T extends @TA CharSequence> void test4() {}
71+
public <T extends @TA Object & CharSequence> void test5() {}
72+
public <T extends Object & @TA CharSequence> void test6() {}
73+
public <T extends CharSequence> void test7() {}
74+
public <T extends Object & CharSequence> void test8() {}
75+
}
76+
77+
class TypeVariableBoundsOnConstructors {
78+
public <@TA T> TypeVariableBoundsOnConstructors(boolean b) {}
79+
public <@TA T extends Object> TypeVariableBoundsOnConstructors(byte b) {}
80+
public <T extends @TA Object> TypeVariableBoundsOnConstructors(char c) {}
81+
public <T extends @TA CharSequence> TypeVariableBoundsOnConstructors(short s) {}
82+
public <T extends @TA Object & CharSequence> TypeVariableBoundsOnConstructors(int i) {}
83+
public <T extends Object & @TA CharSequence> TypeVariableBoundsOnConstructors(long l) {}
84+
public <T extends CharSequence> TypeVariableBoundsOnConstructors(float f) {}
85+
public <T extends Object & CharSequence> TypeVariableBoundsOnConstructors(double d) {}
86+
}
87+
88+
@Target(ElementType.TYPE_USE)
89+
@interface TA {
90+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
2+
class AnnotatedObjectSuperType extends java.lang.@TA Object {
3+
4+
AnnotatedObjectSuperType();
5+
}
6+
7+
class UnannotatedObjectSuperType {
8+
9+
UnannotatedObjectSuperType();
10+
}
11+
12+
class TypeVariableWithAnnotation1<@TA T> {
13+
14+
TypeVariableWithAnnotation1();
15+
}
16+
17+
class TypeVariableWithAnnotation2<@TA T> {
18+
19+
TypeVariableWithAnnotation2();
20+
}
21+
22+
class TypeVariableWithBound1<T extends java.lang.@TA Object> {
23+
24+
TypeVariableWithBound1();
25+
}
26+
27+
class TypeVariableWithBound2<T extends java.lang.@TA CharSequence> {
28+
29+
TypeVariableWithBound2();
30+
}
31+
32+
class TypeVariableWithBound3<T extends java.lang.@TA Object & java.lang.CharSequence> {
33+
34+
TypeVariableWithBound3();
35+
}
36+
37+
class TypeVariableWithBound4<T extends java.lang.@TA CharSequence> {
38+
39+
TypeVariableWithBound4();
40+
}
41+
42+
class TypeVariableWithBound5<T extends java.lang.CharSequence> {
43+
44+
TypeVariableWithBound5();
45+
}
46+
47+
class TypeVariableWithBound6<T extends java.lang.CharSequence> {
48+
49+
TypeVariableWithBound6();
50+
}
51+
52+
class TypeVariableWithBoundRecursive<T extends TypeVariableWithBoundRecursive<T>> {
53+
54+
TypeVariableWithBoundRecursive();
55+
}
56+
57+
class TypeVariableBoundsOnMethods {
58+
59+
TypeVariableBoundsOnMethods();
60+
61+
public <@TA T> void test1();
62+
63+
public <@TA T> void test2();
64+
65+
public <T extends java.lang.@TA Object> void test3();
66+
67+
public <T extends java.lang.@TA CharSequence> void test4();
68+
69+
public <T extends java.lang.@TA Object & java.lang.CharSequence> void test5();
70+
71+
public <T extends java.lang.@TA CharSequence> void test6();
72+
73+
public <T extends java.lang.CharSequence> void test7();
74+
75+
public <T extends java.lang.CharSequence> void test8();
76+
}
77+
78+
class TypeVariableBoundsOnConstructors {
79+
80+
public <@TA T> TypeVariableBoundsOnConstructors(boolean b);
81+
82+
public <@TA T> TypeVariableBoundsOnConstructors(byte b);
83+
84+
public <T extends java.lang.@TA Object> TypeVariableBoundsOnConstructors(char c);
85+
86+
public <T extends java.lang.@TA CharSequence> TypeVariableBoundsOnConstructors(short s);
87+
88+
public <T extends java.lang.@TA Object & java.lang.CharSequence> TypeVariableBoundsOnConstructors(int i);
89+
90+
public <T extends java.lang.@TA CharSequence> TypeVariableBoundsOnConstructors(long l);
91+
92+
public <T extends java.lang.CharSequence> TypeVariableBoundsOnConstructors(float f);
93+
94+
public <T extends java.lang.CharSequence> TypeVariableBoundsOnConstructors(double d);
95+
}
96+
97+
@java.lang.annotation.Target({TYPE_USE})
98+
@interface TA {
99+
}

test/langtools/tools/javac/processing/rounds/OverwriteBetweenCompilations_1.out

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ round: 1
22
round: 2
33

44
@java.lang.Deprecated
5-
public class GeneratedSource<T> extends java.util.ArrayList<java.lang.String> implements java.lang.Runnable {
5+
public class GeneratedSource<T extends java.lang.CharSequence> extends java.util.ArrayList<java.lang.String> implements java.lang.Runnable {
66

77
public GeneratedSource();
88

@@ -12,7 +12,7 @@ public class GeneratedSource<T> extends java.util.ArrayList<java.lang.String> im
1212
}
1313

1414
@java.lang.Deprecated
15-
public class GeneratedClass<T> extends java.util.ArrayList<java.lang.String> implements java.lang.Runnable {
15+
public class GeneratedClass<T extends java.lang.CharSequence> extends java.util.ArrayList<java.lang.String> implements java.lang.Runnable {
1616

1717
public GeneratedClass();
1818

@@ -23,7 +23,7 @@ public class GeneratedClass<T> extends java.util.ArrayList<java.lang.String> imp
2323
round: 3
2424

2525
@java.lang.Deprecated
26-
public class GeneratedSource<T> extends java.util.ArrayList<java.lang.String> implements java.lang.Runnable {
26+
public class GeneratedSource<T extends java.lang.CharSequence> extends java.util.ArrayList<java.lang.String> implements java.lang.Runnable {
2727

2828
public GeneratedSource();
2929

@@ -33,7 +33,7 @@ public class GeneratedSource<T> extends java.util.ArrayList<java.lang.String> im
3333
}
3434

3535
@java.lang.Deprecated
36-
public class GeneratedClass<T> extends java.util.ArrayList<java.lang.String> implements java.lang.Runnable {
36+
public class GeneratedClass<T extends java.lang.CharSequence> extends java.util.ArrayList<java.lang.String> implements java.lang.Runnable {
3737

3838
public GeneratedClass();
3939

test/langtools/tools/javac/processing/rounds/OverwriteBetweenCompilations_2.out

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
round: 1
22

33
@java.lang.Deprecated
4-
public class GeneratedSource<T> extends java.util.ArrayList<java.lang.String> implements java.lang.Runnable {
4+
public class GeneratedSource<T extends java.lang.CharSequence> extends java.util.ArrayList<java.lang.String> implements java.lang.Runnable {
55

66
public GeneratedSource();
77

@@ -11,7 +11,7 @@ public class GeneratedSource<T> extends java.util.ArrayList<java.lang.String> im
1111
}
1212

1313
@java.lang.Deprecated
14-
public class GeneratedClass<T> extends java.util.ArrayList<java.lang.String> implements java.lang.Runnable {
14+
public class GeneratedClass<T extends java.lang.CharSequence> extends java.util.ArrayList<java.lang.String> implements java.lang.Runnable {
1515

1616
public GeneratedClass();
1717

@@ -22,15 +22,15 @@ public class GeneratedClass<T> extends java.util.ArrayList<java.lang.String> imp
2222
round: 2
2323

2424
@javax.annotation.processing.SupportedAnnotationTypes({"*"})
25-
public abstract class GeneratedSource<E> extends java.util.LinkedList<java.lang.Number> implements java.lang.Runnable, java.lang.CharSequence {
25+
public abstract class GeneratedSource<E extends java.lang.Number> extends java.util.LinkedList<java.lang.Number> implements java.lang.Runnable, java.lang.CharSequence {
2626

2727
public GeneratedSource();
2828

2929
public void test(long a);
3030
}
3131

3232
@javax.annotation.processing.SupportedAnnotationTypes({"*"})
33-
public abstract class GeneratedClass<E> extends java.util.LinkedList<java.lang.Number> implements java.lang.Runnable, java.lang.CharSequence {
33+
public abstract class GeneratedClass<E extends java.lang.Number> extends java.util.LinkedList<java.lang.Number> implements java.lang.Runnable, java.lang.CharSequence {
3434

3535
public GeneratedClass();
3636

@@ -39,15 +39,15 @@ public abstract class GeneratedClass<E> extends java.util.LinkedList<java.lang.N
3939
round: 3
4040

4141
@javax.annotation.processing.SupportedAnnotationTypes({"*"})
42-
public abstract class GeneratedSource<E> extends java.util.LinkedList<java.lang.Number> implements java.lang.Runnable, java.lang.CharSequence {
42+
public abstract class GeneratedSource<E extends java.lang.Number> extends java.util.LinkedList<java.lang.Number> implements java.lang.Runnable, java.lang.CharSequence {
4343

4444
public GeneratedSource();
4545

4646
public void test(long a);
4747
}
4848

4949
@javax.annotation.processing.SupportedAnnotationTypes({"*"})
50-
public abstract class GeneratedClass<E> extends java.util.LinkedList<java.lang.Number> implements java.lang.Runnable, java.lang.CharSequence {
50+
public abstract class GeneratedClass<E extends java.lang.Number> extends java.util.LinkedList<java.lang.Number> implements java.lang.Runnable, java.lang.CharSequence {
5151

5252
public GeneratedClass();
5353

0 commit comments

Comments
 (0)