| 1 | /******************************************************************************* |
|---|---|
| 2 | * Copyright (c) 2004, 2013 IBM Corporation and others. |
| 3 | * |
| 4 | * This program and the accompanying materials |
| 5 | * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | * which accompanies this distribution, and is available at |
| 7 | * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | * |
| 9 | * SPDX-License-Identifier: EPL-2.0 |
| 10 | * |
| 11 | * Contributors: |
| 12 | * IBM Corporation - initial API and implementation |
| 13 | *******************************************************************************/ |
| 14 | package org.eclipse.jdt.core.dom; |
| 15 | |
| 16 | /** |
| 17 | * Descriptor for a child property of an AST node. |
| 18 | * A child property is one whose value is an |
| 19 | * {@link ASTNode}. |
| 20 | * |
| 21 | * @see org.eclipse.jdt.core.dom.ASTNode#getStructuralProperty(StructuralPropertyDescriptor) |
| 22 | * @see org.eclipse.jdt.core.dom.ASTNode#setStructuralProperty(StructuralPropertyDescriptor, Object) |
| 23 | * @since 3.0 |
| 24 | * @noinstantiate This class is not intended to be instantiated by clients. |
| 25 | */ |
| 26 | @SuppressWarnings("rawtypes") |
| 27 | public final class ChildPropertyDescriptor extends StructuralPropertyDescriptor { |
| 28 | |
| 29 | /** |
| 30 | * Child type. For example, for a node type like |
| 31 | * CompilationUnit, the "package" property is PackageDeclaration.class |
| 32 | */ |
| 33 | private final Class childClass; |
| 34 | |
| 35 | /** |
| 36 | * Indicates whether the child is mandatory. A child property is allowed |
| 37 | * to be <code>null</code> only if it is not mandatory. |
| 38 | */ |
| 39 | private final boolean mandatory; |
| 40 | |
| 41 | /** |
| 42 | * Indicates whether a cycle is possible. |
| 43 | * Field is private, but marked package-visible for fast |
| 44 | * access from ASTNode. |
| 45 | */ |
| 46 | final boolean cycleRisk; |
| 47 | |
| 48 | /** |
| 49 | * Creates a new child property descriptor with the given property id. |
| 50 | * Note that this constructor is declared package-private so that |
| 51 | * property descriptors can only be created by the AST |
| 52 | * implementation. |
| 53 | * |
| 54 | * @param nodeClass concrete AST node type that owns this property |
| 55 | * @param propertyId the property id |
| 56 | * @param childType the child type of this property |
| 57 | * @param mandatory <code>true</code> if the property is mandatory, |
| 58 | * and <code>false</code> if it is may be <code>null</code> |
| 59 | * @param cycleRisk <code>true</code> if this property is at |
| 60 | * risk of cycles, and <code>false</code> if there is no worry about cycles |
| 61 | */ |
| 62 | ChildPropertyDescriptor(Class nodeClass, String propertyId, Class childType, boolean mandatory, boolean cycleRisk) { |
| 63 | super(nodeClass, propertyId); |
| 64 | if (childType == null || !ASTNode.class.isAssignableFrom(childType)) { |
| 65 | throw new IllegalArgumentException(); |
| 66 | } |
| 67 | this.childClass = childType; |
| 68 | this.mandatory = mandatory; |
| 69 | this.cycleRisk = cycleRisk; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Returns the child type of this property. |
| 74 | * <p> |
| 75 | * For example, for a node type like CompilationUnit, |
| 76 | * the "package" property returns <code>PackageDeclaration.class</code>. |
| 77 | * </p> |
| 78 | * |
| 79 | * @return the child type of the property |
| 80 | */ |
| 81 | public final Class getChildType() { |
| 82 | return this.childClass; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Returns whether this property is mandatory. A property value |
| 87 | * is not allowed to be <code>null</code> if it is mandatory. |
| 88 | * |
| 89 | * @return <code>true</code> if the property is mandatory, |
| 90 | * and <code>false</code> if it is may be <code>null</code> |
| 91 | */ |
| 92 | public final boolean isMandatory() { |
| 93 | return this.mandatory; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Returns whether this property is vulnerable to cycles. |
| 98 | * <p> |
| 99 | * A property is vulnerable to cycles if a node of the owning |
| 100 | * type (that is, the type that owns this property) could legally |
| 101 | * appear in the AST subtree below this property. For example, |
| 102 | * the body property of a |
| 103 | * {@link MethodDeclaration} node |
| 104 | * admits a body which might include statement that embeds |
| 105 | * another {@link MethodDeclaration} node. |
| 106 | * On the other hand, the name property of a |
| 107 | * MethodDeclaration node admits only names, and thereby excludes |
| 108 | * another MethodDeclaration node. |
| 109 | * </p> |
| 110 | * |
| 111 | * @return <code>true</code> if cycles are possible, |
| 112 | * and <code>false</code> if cycles are impossible |
| 113 | */ |
| 114 | public final boolean cycleRisk() { |
| 115 | return this.cycleRisk; |
| 116 | } |
| 117 | } |
| 118 |
Members