Make FieldDeclaration not a TypeNode

This commit is contained in:
Clément Fournier
2020-01-07 22:05:28 +01:00
parent 1eab9448e7
commit 1fefbd2c4a
10 changed files with 77 additions and 86 deletions

View File

@ -1799,7 +1799,7 @@ void FormalParameter() :
{boolean isFinal = false;}
{
isFinal=LocalVarModifierList() {jjtThis.setFinal(isFinal);}
FormalParamType()
FormalParamType() ("|" FormalParamType())* // remove this stuff when #2202 is merged
VariableIdWithDims()
}

View File

@ -4,12 +4,8 @@
package net.sourceforge.pmd.lang.java.ast;
import java.util.Iterator;
import net.sourceforge.pmd.lang.ast.NodeStream;
import net.sourceforge.pmd.lang.ast.SignedNode;
import net.sourceforge.pmd.lang.java.multifile.signature.JavaFieldSignature;
import net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition;
/**
@ -35,10 +31,11 @@ import net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefin
*
* </pre>
*/
public final class ASTFieldDeclaration extends AbstractJavaAccessTypeNode
public final class ASTFieldDeclaration extends AbstractJavaAccessNode
implements SignedNode<ASTFieldDeclaration>,
Iterable<ASTVariableDeclaratorId>,
LeftRecursiveNode {
LeftRecursiveNode,
InternalInterfaces.MultiVariableIdOwner {
private JavaFieldSignature signature;
@ -171,46 +168,14 @@ public final class ASTFieldDeclaration extends AbstractJavaAccessTypeNode
return signature;
}
/**
* Returns the type node at the beginning of this field declaration.
* The type of this node is not necessarily the type of the variables,
* see {@link ASTVariableDeclaratorId#getType()}.
*/
@Override
public ASTType getTypeNode() {
return getFirstChildOfType(ASTType.class);
}
/**
* Returns a stream of IDs for the fields this node declares.
*/
public NodeStream<ASTVariableDeclaratorId> getVariables() {
return children(ASTVariableDeclarator.class).children(ASTVariableDeclaratorId.class);
}
/**
* Returns an iterator of IDs for the fields this node declares.
*
* @see #getVariables()
*/
@Override
public Iterator<ASTVariableDeclaratorId> iterator() {
return getVariables().iterator();
}
/**
* @deprecated FieldDeclaration may declare several variables with a different type
* It won't implement TypeNode anymore come 7.0.0
*/
@Override
@Deprecated
public Class<?> getType() {
return super.getType();
}
/**
* @deprecated FieldDeclaration may declare several variables with a different type
* It won't implement TypeNode anymore come 7.0.0
*/
@Override
@Deprecated
public JavaTypeDefinition getTypeDefinition() {
return super.getTypeDefinition();
}
}

View File

@ -94,11 +94,11 @@ public final class ASTFormalParameter extends AbstractJavaAccessTypeNode impleme
/**
* @deprecated use {@link #getVariableDeclaratorId()}
* @deprecated use {@link #getVarId()}
*/
@Deprecated
protected ASTVariableDeclaratorId getDecl() {
return getVariableDeclaratorId();
return getVarId();
}
/**

View File

@ -4,11 +4,6 @@
package net.sourceforge.pmd.lang.java.ast;
import java.util.Iterator;
import net.sourceforge.pmd.lang.ast.NodeStream;
/**
* Represents a local variable declaration. This is a {@linkplain ASTStatement statement},
* but the node is also used in {@linkplain ASTForInit for-loop initialisers} and
@ -27,7 +22,8 @@ import net.sourceforge.pmd.lang.ast.NodeStream;
// TODO extend AbstractStatement
public final class ASTLocalVariableDeclaration extends AbstractJavaAccessNode
implements Iterable<ASTVariableDeclaratorId>,
ASTStatement {
ASTStatement,
InternalInterfaces.MultiVariableIdOwner {
ASTLocalVariableDeclaration(int id) {
super(id);
@ -80,24 +76,10 @@ public final class ASTLocalVariableDeclaration extends AbstractJavaAccessNode
*
* @see #isTypeInferred()
*/
@Override
public ASTType getTypeNode() {
return getFirstChildOfType(ASTType.class);
}
/**
* Returns a stream of IDs for the fields this node declares.
*/
public NodeStream<ASTVariableDeclaratorId> getVarIds() {
return children(ASTVariableDeclarator.class).children(ASTVariableDeclaratorId.class);
}
/**
* Returns an iterator of IDs for the fields this node declares.
*
* @see #getVarIds()
*/
@Override
public Iterator<ASTVariableDeclaratorId> iterator() {
return getVarIds().iterator();
}
}

View File

@ -45,7 +45,6 @@ import net.sourceforge.pmd.lang.symboltable.NameOccurrence;
// @formatter:on
public final class ASTVariableDeclaratorId extends AbstractJavaTypeNode {
private int arrayDepth;
private VariableNameDeclaration nameDeclaration;
@InternalApi
@ -109,7 +108,7 @@ public final class ASTVariableDeclaratorId extends AbstractJavaTypeNode {
* a {@code catch} statement.
*/
public boolean isExceptionBlockParameter() {
return jjtGetParent() instanceof ASTCatchParameter;
return jjtGetParent() instanceof ASTFormalParameter && jjtGetParent().jjtGetParent() instanceof ASTCatchClause;
}
@ -179,9 +178,6 @@ public final class ASTVariableDeclaratorId extends AbstractJavaTypeNode {
if (jjtGetParent() instanceof ASTFormalParameter) {
// This accounts for exception parameters too for now
return ((ASTFormalParameter) jjtGetParent()).isFinal();
} else if (jjtGetParent() instanceof ASTCatchParameter) {
return ((ASTCatchParameter) jjtGetParent()).isMulticatch()
|| ((ASTCatchParameter) jjtGetParent()).isFinal();
}
Node grandpa = getNthParent(2);

View File

@ -5,9 +5,13 @@
package net.sourceforge.pmd.lang.java.ast;
import java.util.Iterator;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import net.sourceforge.pmd.lang.ast.NodeStream;
/**
* Those are some interfaces that are not published, but are used to keep
* uniform names on related concepts. Maybe it makes sense to publish some of
@ -92,4 +96,57 @@ final class InternalInterfaces {
}
}
/**
* Tags a node that has at least one child, then some methods never
* return null.
*/
interface AtLeastOneChildOfType<T extends JavaNode> extends JavaNode {
@Override
T jjtGetChild(int index);
/** Returns the first child of this node, never null. */
@Override
@NonNull
default T getFirstChild() {
assert jjtGetNumChildren() > 0;
return jjtGetChild(0);
}
/** Returns the last child of this node, never null. */
@Override
@NonNull
default T getLastChild() {
assert jjtGetNumChildren() > 0;
return jjtGetChild(jjtGetNumChildren() - 1);
}
}
interface VariableIdOwner extends JavaNode {
/** Returns the id of the declared variable. */
ASTVariableDeclaratorId getVarId();
}
interface MultiVariableIdOwner extends JavaNode, Iterable<ASTVariableDeclaratorId>, AccessNode {
/**
* Returns a stream of the variable ids declared
* by this node.
*/
default NodeStream<ASTVariableDeclaratorId> getVarIds() {
return children(ASTVariableDeclarator.class).children(ASTVariableDeclaratorId.class);
}
@Override
default Iterator<ASTVariableDeclaratorId> iterator() {
return getVarIds().iterator();
}
ASTType getTypeNode();
}
}

View File

@ -202,6 +202,7 @@ public class CommentRequiredRule extends AbstractCommentRule {
}
@SuppressWarnings("PMD.UnusedFormalParameter")
private boolean isSerialVersionUID(ASTFieldDeclaration field) {
return false; // FIXME, commented out because of incompatibility, needs typeres
// return "serialVersionUID".equals(field.getVariableName())
@ -220,6 +221,7 @@ public class CommentRequiredRule extends AbstractCommentRule {
* @return true if the field is a serialPersistentFields variable, otherwise false
* @see <a href="https://docs.oracle.com/javase/7/docs/platform/serialization/spec/serial-arch.html#6250">Oracle docs</a>
*/
@SuppressWarnings("PMD.UnusedFormalParameter")
private boolean isSerialPersistentFields(final ASTFieldDeclaration field) {
return false; // FIXME, commented out because of incompatibility, needs typeres
// return "serialPersistentFields".equals(field.getVariableName())

View File

@ -50,7 +50,6 @@ import net.sourceforge.pmd.lang.java.ast.ASTEqualityExpression;
import net.sourceforge.pmd.lang.java.ast.ASTExclusiveOrExpression;
import net.sourceforge.pmd.lang.java.ast.ASTExpression;
import net.sourceforge.pmd.lang.java.ast.ASTExtendsList;
import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTForStatement;
import net.sourceforge.pmd.lang.java.ast.ASTFormalParameter;
import net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration;
@ -594,13 +593,6 @@ public class ClassTypeResolver extends JavaParserVisitorAdapter {
}
@Override
public Object visit(ASTFieldDeclaration node, Object data) {
super.visit(node, data);
rollupTypeUnary(node);
return data;
}
@Override
public Object visit(ASTVariableDeclarator node, Object data) {
super.visit(node, data);

View File

@ -170,7 +170,6 @@ public class ClassTypeResolverTest {
assertEquals(ArrayList.class, acu.getFirstDescendantOfType(ASTType.class).getType());
assertEquals(ArrayList.class, acu.getFirstDescendantOfType(ASTVariableDeclaratorId.class).getType());
assertEquals(ArrayList.class, acu.getFirstDescendantOfType(ASTVariableDeclarator.class).getType());
assertEquals(ArrayList.class, acu.getFirstDescendantOfType(ASTFieldDeclaration.class).getType());
acu = parseAndTypeResolveForClass15(DefaultJavaLangImport.class);
assertEquals(String.class, acu.getFirstDescendantOfType(ASTClassOrInterfaceType.class).getType());

View File

@ -100,7 +100,6 @@ public class FullTypeAnnotations {
Document @Readonly [][] docs2 = new Document@Readonly[2][12]; // read-only array of arrays of documents
Document[] @Readonly [] docs3 = new Document[2]@Readonly[12]; // array of read-only arrays of documents
// TODO mixed array notation, for now syntax error
Document[] docs4@Readonly[] = new Document@Readonly[2][12]; // read-only array of arrays of documents
Document @Readonly [] docs5[] = new Document[2]@Readonly[12]; // array of read-only arrays of documents
@ -110,7 +109,6 @@ public class FullTypeAnnotations {
Document @Readonly [][] docs2 = new Document@Readonly[2][12]; // read-only array of arrays of documents
Document[] @Readonly [] docs3 = new Document[2]@Readonly[12]; // array of read-only arrays of documents
// TODO mixed array notation, for now syntax error
Document[] docs4@Readonly[] = new Document@Readonly[2][12]; // read-only array of arrays of documents
Document @Readonly [] docs5[] = new Document[2]@Readonly[12]; // array of read-only arrays of documents
@ -121,8 +119,8 @@ public class FullTypeAnnotations {
public String toString(@Readonly MyClass this) { }
public boolean equals(Object @Readonly ... other) { }
MyClass(Object @Readonly ... other) { }
public boolean equals(Object @Readonly ... other) @K[][]{ }
MyClass(Object @Readonly [] @ß... other) { }
}