Skip to content

Commit

Permalink
8327824: Type annotation placed on incorrect array nesting levels
Browse files Browse the repository at this point in the history
Reviewed-by: prappo
  • Loading branch information
liach authored and pavelrappo committed Mar 13, 2024
1 parent be344e4 commit 35b00e6
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

package jdk.javadoc.internal.doclets.formats.html;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
Expand Down Expand Up @@ -101,24 +102,35 @@ protected Content defaultAction(TypeMirror type, HtmlLinkInfo linkInfo) {
return link;
}

int currentDepth = 0;
@Override
public Content visitArray(ArrayType type, HtmlLinkInfo linkInfo) {
// keep track of the dimension depth and replace the last dimension
// specifier with varargs, when the stack is fully unwound.
currentDepth++;
var componentType = type.getComponentType();
visit(componentType, linkInfo.forType(componentType));
currentDepth--;
if (utils.isAnnotated(type)) {
link.add(" ");
link.add(getTypeAnnotationLinks(linkInfo));
// int @A [] @B [] has @A on int[][] and @B on int[],
// encounter order is @A @B so print in FIFO order
var deque = new ArrayDeque<ArrayType>(1);
while (true) {
deque.add(type);
var component = type.getComponentType();
if (component instanceof ArrayType arrayType) {
type = arrayType;
} else {
visit(component, linkInfo.forType(component));
break;
}
}
// use vararg if required
if (linkInfo.isVarArg() && currentDepth == 0) {
link.add("...");
} else {
link.add("[]");

while (!deque.isEmpty()) {
var currentType = deque.remove();
if (utils.isAnnotated(currentType)) {
link.add(" ");
link.add(getTypeAnnotationLinks(linkInfo.forType(currentType)));
}

// use vararg if required
if (linkInfo.isVarArg() && deque.isEmpty()) {
link.add("...");
} else {
link.add("[]");
}
}
return link;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,17 @@ annotation interface in typeannos">@FldA</a> java.lang.String,<wbr><a href="FldB
ass="element-name">array1Deep</span></div>""",

"""
<div class="member-signature"><span class="return-type">java.lang.String <a href\
="FldB.html" title="annotation interface in typeannos">@FldB</a> [][]</span>&nbs\
<div class="member-signature"><span class="return-type">java.lang.String[] <a hr\
ef="FldB.html" title="annotation interface in typeannos">@FldB</a> []</span>&nbs\
p;<span class="element-name">array2SecondOld</span></div>""",

"""
<div class="member-signature"><span class="return-type"><a href="FldD.html" titl\
e="annotation interface in typeannos">@FldD</a> java.lang.String <a href="FldC.h\
tml" title="annotation interface in typeannos">@FldC</a> <a href="FldB.html" tit\
le="annotation interface in typeannos">@FldB</a> [] <a href="FldC.html" title="a\
nnotation interface in typeannos">@FldC</a> <a href="FldA.html" title="annotatio\
n interface in typeannos">@FldA</a> []</span>&nbsp;<span class="element-name">ar\
tml" title="annotation interface in typeannos">@FldC</a> <a href="FldA.html" tit\
le="annotation interface in typeannos">@FldA</a> [] <a href="FldC.html" title="a\
nnotation interface in typeannos">@FldC</a> <a href="FldB.html" title="annotatio\
n interface in typeannos">@FldB</a> []</span>&nbsp;<span class="element-name">ar\
ray2Deep</span></div>""",

"""
Expand Down Expand Up @@ -185,8 +185,8 @@ interface in typeannos">@FldB</a> java.lang.String&gt;</span>&nbsp;<span class=\
"""
<div class="member-signature"><span class="return-type"><a href="MRtnA.html" tit\
le="annotation interface in typeannos">@MRtnA</a> java.lang.String <a href="MRtn\
B.html" title="annotation interface in typeannos">@MRtnB</a> [] <a href="MRtnA.h\
tml" title="annotation interface in typeannos">@MRtnA</a> []</span>&nbsp;<span c\
A.html" title="annotation interface in typeannos">@MRtnA</a> [] <a href="MRtnB.h\
tml" title="annotation interface in typeannos">@MRtnB</a> []</span>&nbsp;<span c\
lass="element-name">array2Deep</span>()</div>""",

"""
Expand Down Expand Up @@ -274,8 +274,8 @@ tation interface in typeannos">@ParamB</a> java.lang.String&gt;&nbsp;a)</span></
<div class="member-signature"><span class="return-type">void</span>&nbsp;<span c\
lass="element-name">array2Deep</span><wbr><span class="parameters">(<a href="Par\
amA.html" title="annotation interface in typeannos">@ParamA</a> java.lang.String\
<a href="ParamB.html" title="annotation interface in typeannos">@ParamB</a> [] \
<a href="ParamA.html" title="annotation interface in typeannos">@ParamA</a> []&n\
<a href="ParamA.html" title="annotation interface in typeannos">@ParamA</a> [] \
<a href="ParamB.html" title="annotation interface in typeannos">@ParamB</a> []&n\
bsp;a)</span></div>""",

"""
Expand Down Expand Up @@ -862,5 +862,45 @@ ation interface in typeannos">@RepTypeUseA</a> <a href="RepTypeUseB.html" title=
<a href="RepMethodB.html" title="annotation interface in typeannos">@RepMethodB</a> <a hre\
f="RepMethodB.html" title="annotation interface in typeannos">@RepMethodB</a>
</span><span class="return-type">void</span>&nbsp;<span class="element-name">test</span>()""");

checkOutput("typeannos/DeepArrays.html", true,
"""
<div class="member-signature"><span class="return-type"><a href="ArrA.html" title="annotat\
ion interface in typeannos">@ArrA</a> java.lang.String <a href="ArrB.html" title="annotati\
on interface in typeannos">@ArrB</a> [] <a href="ArrC.html" title="annotation interface in \
typeannos">@ArrC</a> []</span>&nbsp;<span class="element-name">array2</span>()</div>""",

"""
<div class="member-signature"><span class="return-type">java.lang.String <a href="ArrA.htm\
l" title="annotation interface in typeannos">@ArrA</a> [] <a href="ArrB.html" title="annot\
ation interface in typeannos">@ArrB</a> [] <a href="ArrC.html" title="annotation interface \
in typeannos">@ArrC</a> [] <a href="ArrD.html" title="annotation interface in typeannos">@\
ArrD</a> []</span>&nbsp;<span class="element-name">array4</span>()</div>""",

"""
<div class="member-signature"><span class="return-type"><a href="ArrA.html" title="annotat\
ion interface in typeannos">@ArrA</a> <a href="ArrParameterized.html" title="class in type\
annos">ArrParameterized</a>&lt;<a href="ArrC.html" title="annotation interface in typeanno\
s">@ArrC</a> java.lang.String <a href="ArrA.html" title="annotation interface in typeannos\
">@ArrA</a> [] <a href="ArrB.html" title="annotation interface in typeannos">@ArrB</a> []&\
gt; <a href="ArrC.html" title="annotation interface in typeannos">@ArrC</a> [] <a href="Ar\
rD.html" title="annotation interface in typeannos">@ArrD</a> []</span>&nbsp;<span class="e\
lement-name">manyNested</span>()</div>""",

"""
<div class="member-signature"><span class="return-type">void</span>&nbsp;<span class="elem\
ent-name">varargs</span><wbr><span class="parameters">(<a href="ArrA.html" title="annotati\
on interface in typeannos">@ArrA</a> java.lang.String <a href="ArrB.html" title="annotatio\
n interface in typeannos">@ArrB</a> [] <a href="ArrC.html" title="annotation interface in \
typeannos">@ArrC</a> [] <a href="ArrD.html" title="annotation interface in typeannos">@Arr\
D</a> ...&nbsp;arg)</span></div>""",

"""
<div class="member-signature"><span class="return-type">int <a href="ArrB.html" title="ann\
otation interface in typeannos">@ArrB</a> [] <a href="ArrA.html" title="annotation interfa\
ce in typeannos">@ArrA</a> []</span>&nbsp;<span class="element-name">mixedStyles</span><wb\
r><span class="parameters">(int <a href="ArrB.html" title="annotation interface in typeann\
os">@ArrB</a> [] <a href="ArrA.html" title="annotation interface in typeannos">@ArrA</a> [\
]&nbsp;arg)</span></div>""");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2024, 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.
*/
package typeannos;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

public class DeepArrays {
@ArrA String @ArrB [] @ArrC [] array2() { return null; }
String @ArrA [] @ArrB [] @ArrC [] @ArrD [] array4() { return null; }
@ArrA ArrParameterized<@ArrC String @ArrA [] @ArrB []> @ArrC [] @ArrD [] manyNested() { return null; }
void varargs(@ArrA String @ArrB [] @ArrC [] @ArrD ... arg) {}
int @ArrA [] mixedStyles(int @ArrB [] @ArrA [] arg) @ArrB [] { return null; } // JLS example 10.2-2
}

class ArrParameterized<T> {}

@Target(ElementType.TYPE_USE)
@Documented
@interface ArrA { }
@Target(ElementType.TYPE_USE)
@Documented
@interface ArrB { }
@Target(ElementType.TYPE_USE)
@Documented
@interface ArrC { }
@Target(ElementType.TYPE_USE)
@Documented
@interface ArrD { }

1 comment on commit 35b00e6

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.