diff --git a/pmd-java/etc/grammar/Java.jjt b/pmd-java/etc/grammar/Java.jjt index b2d4e15280..efcd789fdf 100644 --- a/pmd-java/etc/grammar/Java.jjt +++ b/pmd-java/etc/grammar/Java.jjt @@ -1578,7 +1578,7 @@ void TypeArgument() #void: void WildcardType(): {} { - "?" [ ("extends" {jjtThis.setUpperBound(true);}| "super") AnnotatedRefType() ] + "?" [ ("extends" | "super" {jjtThis.setLowerBound(true);}) AnnotatedRefType() ] } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTWildcardType.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTWildcardType.java index c66913fd3d..dc97754a65 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTWildcardType.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTWildcardType.java @@ -18,14 +18,14 @@ import org.checkerframework.checker.nullness.qual.Nullable; */ public final class ASTWildcardType extends AbstractJavaTypeNode implements ASTReferenceType { - private boolean isUpperBound; + private boolean isLowerBound; ASTWildcardType(int id) { super(id); } - void setUpperBound(boolean upperBound) { - isUpperBound = upperBound; + void setLowerBound(boolean lowerBound) { + isLowerBound = lowerBound; } @Override @@ -34,11 +34,12 @@ public final class ASTWildcardType extends AbstractJavaTypeNode implements ASTRe } /** - * Returns true if this is an upper type bound, e.g. - * in {@code }. + * Return true if this is an upper type bound, e.g. + * {@code }, or the unbounded + * wildcard {@code }. */ - public boolean hasUpperBound() { - return isUpperBound && getNumChildren() > 0; + public boolean isUpperBound() { + return !isLowerBound; } @@ -46,18 +47,18 @@ public final class ASTWildcardType extends AbstractJavaTypeNode implements ASTRe * Returns true if this is a lower type bound, e.g. * in {@code }. */ - public boolean hasLowerBound() { - return !isUpperBound && getNumChildren() > 0; + public boolean isLowerBound() { + return isLowerBound; } /** * Returns the type node representing the bound, e.g. - * the {@code Node} in {@code }, or null. + * the {@code Node} in {@code }, or null in + * the unbounded wildcard {@code }. */ - @Nullable - public ASTReferenceType getTypeBoundNode() { - return getFirstChildOfType(ASTReferenceType.class); + public @Nullable ASTReferenceType getTypeBoundNode() { + return firstChild(ASTReferenceType.class); } @Override diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/TypesFromAst.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/TypesFromAst.java index 57e2bd98a5..8a664dc6b3 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/TypesFromAst.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/TypesFromAst.java @@ -64,13 +64,10 @@ final class TypesFromAst { ASTWildcardType wild = (ASTWildcardType) node; @Nullable JTypeMirror bound = fromAst(ts, lexicalSubst, wild.getTypeBoundNode()); - boolean isUpperBound = true; if (bound == null) { bound = ts.OBJECT; - } else { - isUpperBound = wild.hasUpperBound(); } - return ts.wildcard(isUpperBound, bound).withAnnotations(getTypeAnnotations(node)); + return ts.wildcard(wild.isUpperBound(), bound).withAnnotations(getTypeAnnotations(node)); } else if (node instanceof ASTIntersectionType) { @@ -264,7 +261,6 @@ final class TypesFromAst { PSet annotsOnType = getSymbolicAnnotations(type); Annotatable parent = getEnclosingAnnotationGiver(type); if (parent != null) { - // todo parent annots should be filtered by target TYPE_USE PSet parentAnnots = getSymbolicAnnotations(parent); for (SymAnnot parentAnnot : parentAnnots) { if (parentAnnot.appliesToTypeUse()) { diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/internal/PrettyPrintingUtil.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/internal/PrettyPrintingUtil.java index ad12ee78bd..0e1945919c 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/internal/PrettyPrintingUtil.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/internal/PrettyPrintingUtil.java @@ -117,7 +117,7 @@ public final class PrettyPrintingUtil { sb.append("?"); ASTReferenceType bound = ((ASTWildcardType) t).getTypeBoundNode(); if (bound != null) { - sb.append(((ASTWildcardType) t).hasLowerBound() ? " super " : " extends "); + sb.append(((ASTWildcardType) t).isLowerBound() ? " super " : " extends "); prettyPrintTypeNode(sb, bound); } } else if (t instanceof ASTUnionType) { diff --git a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ASTWildcardTypeTest.kt b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ASTWildcardTypeTest.kt index 2c8042e136..7a30b86ed3 100644 --- a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ASTWildcardTypeTest.kt +++ b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ASTWildcardTypeTest.kt @@ -23,8 +23,8 @@ class ASTWildcardTypeTest : ParserTestSpec({ classType("List") { typeArgList { child { - it::hasUpperBound shouldBe true - it::hasLowerBound shouldBe false + it::isUpperBound shouldBe true + it::isLowerBound shouldBe false it::getTypeBoundNode shouldBe classType("B") } @@ -37,8 +37,8 @@ class ASTWildcardTypeTest : ParserTestSpec({ classType("List") { typeArgList { child { - it::hasUpperBound shouldBe false - it::hasLowerBound shouldBe true + it::isUpperBound shouldBe false + it::isLowerBound shouldBe true it::getTypeBoundNode shouldBe classType("B") } @@ -51,8 +51,8 @@ class ASTWildcardTypeTest : ParserTestSpec({ classType("List") { typeArgList { child { - it::hasUpperBound shouldBe false - it::hasLowerBound shouldBe false + it::isUpperBound shouldBe true + it::isLowerBound shouldBe false it::getTypeBoundNode shouldBe null } @@ -76,8 +76,8 @@ class ASTWildcardTypeTest : ParserTestSpec({ annotation("A") annotation("B") - it::hasUpperBound shouldBe true - it::hasLowerBound shouldBe false + it::isUpperBound shouldBe true + it::isLowerBound shouldBe false it::getTypeBoundNode shouldBe classType("B") { annotation("C")