001: /*******************************************************************************
002: * Copyright (c) 2004, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.core.dom;
011:
012: /**
013: * Descriptor for a child property of an AST node.
014: * A child property is one whose value is an
015: * {@link ASTNode}.
016: *
017: * @see org.eclipse.jdt.core.dom.ASTNode#getStructuralProperty(StructuralPropertyDescriptor)
018: * @see org.eclipse.jdt.core.dom.ASTNode#setStructuralProperty(StructuralPropertyDescriptor, Object)
019: * @since 3.0
020: */
021: public final class ChildPropertyDescriptor extends
022: StructuralPropertyDescriptor {
023:
024: /**
025: * Child type. For example, for a node type like
026: * CompilationUnit, the "package" property is PackageDeclaration.class
027: */
028: private final Class childClass;
029:
030: /**
031: * Indicates whether the child is mandatory. A child property is allowed
032: * to be <code>null</code> only if it is not mandatory.
033: */
034: private final boolean mandatory;
035:
036: /**
037: * Indicates whether a cycle is possible.
038: * Field is private, but marked package-visible for fast
039: * access from ASTNode.
040: */
041: final boolean cycleRisk;
042:
043: /**
044: * Creates a new child property descriptor with the given property id.
045: * Note that this constructor is declared package-private so that
046: * property descriptors can only be created by the AST
047: * implementation.
048: *
049: * @param nodeClass concrete AST node type that owns this property
050: * @param propertyId the property id
051: * @param childType the child type of this property
052: * @param mandatory <code>true</code> if the property is mandatory,
053: * and <code>false</code> if it is may be <code>null</code>
054: * @param cycleRisk <code>true</code> if this property is at
055: * risk of cycles, and <code>false</code> if there is no worry about cycles
056: */
057: ChildPropertyDescriptor(Class nodeClass, String propertyId,
058: Class childType, boolean mandatory, boolean cycleRisk) {
059: super (nodeClass, propertyId);
060: if (childType == null
061: || !ASTNode.class.isAssignableFrom(childType)) {
062: throw new IllegalArgumentException();
063: }
064: this .childClass = childType;
065: this .mandatory = mandatory;
066: this .cycleRisk = cycleRisk;
067: }
068:
069: /**
070: * Returns the child type of this property.
071: * <p>
072: * For example, for a node type like CompilationUnit,
073: * the "package" property returns <code>PackageDeclaration.class</code>.
074: * </p>
075: *
076: * @return the child type of the property
077: */
078: public final Class getChildType() {
079: return this .childClass;
080: }
081:
082: /**
083: * Returns whether this property is mandatory. A property value
084: * is not allowed to be <code>null</code> if it is mandatory.
085: *
086: * @return <code>true</code> if the property is mandatory,
087: * and <code>false</code> if it is may be <code>null</code>
088: */
089: public final boolean isMandatory() {
090: return this .mandatory;
091: }
092:
093: /**
094: * Returns whether this property is vulnerable to cycles.
095: * <p>
096: * A property is vulnerable to cycles if a node of the owning
097: * type (that is, the type that owns this property) could legally
098: * appear in the AST subtree below this property. For example,
099: * the body property of a
100: * {@link MethodDeclaration} node
101: * admits a body which might include statement that embeds
102: * another {@link MethodDeclaration} node.
103: * On the other hand, the name property of a
104: * MethodDeclaration node admits only names, and thereby excludes
105: * another MethodDeclaration node.
106: * </p>
107: *
108: * @return <code>true</code> if cycles are possible,
109: * and <code>false</code> if cycles are impossible
110: */
111: public final boolean cycleRisk() {
112: return this.cycleRisk;
113: }
114: }
|