/******************************************************************************* * Copyright (c) 2000, 2008 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ package net.sourceforge.phpdt.core.dom; import java.util.ArrayList; import java.util.Iterator; import java.util.List; /** * Type declaration AST node type. A type declaration * is the union of a class declaration and an interface declaration. * For JLS2: *
* TypeDeclaration: * ClassDeclaration * InterfaceDeclaration * ClassDeclaration: * [ Javadoc ] { Modifier } class Identifier * [ extends Type] * [ implements Type { , Type } ] * { { ClassBodyDeclaration | ; } } * InterfaceDeclaration: * [ Javadoc ] { Modifier } interface Identifier * [ extends Type { , Type } ] * { { InterfaceBodyDeclaration | ; } } ** For JLS3, type parameters and reified modifiers * (and annotations) were added, and the superclass type name and superinterface * types names are generalized to type so that parameterized types can be * referenced: *
* TypeDeclaration: * ClassDeclaration * InterfaceDeclaration * ClassDeclaration: * [ Javadoc ] { ExtendedModifier } class Identifier * [ < TypeParameter { , TypeParameter } > ] * [ extends Type ] * [ implements Type { , Type } ] * { { ClassBodyDeclaration | ; } } * InterfaceDeclaration: * [ Javadoc ] { ExtendedModifier } interface Identifier * [ < TypeParameter { , TypeParameter } > ] * [ extends Type { , Type } ] * { { InterfaceBodyDeclaration | ; } } **
* When a Javadoc comment is present, the source * range begins with the first character of the "/**" comment delimiter. * When there is no Javadoc comment, the source range begins with the first * character of the first modifier or annotation (if any), or the * first character of the "class" or "interface" keyword (if no * modifiers or annotations). The source range extends through the last character of the "}" * token following the body declarations. *
* * @since 2.0 * @noinstantiate This class is not intended to be instantiated by clients. */ public class TypeDeclaration extends AbstractTypeDeclaration { /** * The "javadoc" structural property of this node type. * @since 3.0 */ public static final ChildPropertyDescriptor JAVADOC_PROPERTY = internalJavadocPropertyFactory(TypeDeclaration.class); /** * The "modifiers" structural property of this node type (JLS2 API only). * @since 3.0 */ public static final SimplePropertyDescriptor MODIFIERS_PROPERTY = internalModifiersPropertyFactory(TypeDeclaration.class); /** * The "modifiers" structural property of this node type (added in JLS3 API). * @since 3.1 */ public static final ChildListPropertyDescriptor MODIFIERS2_PROPERTY = internalModifiers2PropertyFactory(TypeDeclaration.class); /** * The "interface" structural property of this node type. * @since 3.0 */ public static final SimplePropertyDescriptor INTERFACE_PROPERTY = new SimplePropertyDescriptor(TypeDeclaration.class, "interface", boolean.class, MANDATORY); //$NON-NLS-1$ /** * The "name" structural property of this node type. * @since 3.0 */ public static final ChildPropertyDescriptor NAME_PROPERTY = internalNamePropertyFactory(TypeDeclaration.class); /** * The "superclass" structural property of this node type (JLS2 API only). * @since 3.0 */ public static final ChildPropertyDescriptor SUPERCLASS_PROPERTY = new ChildPropertyDescriptor(TypeDeclaration.class, "superclass", Name.class, OPTIONAL, NO_CYCLE_RISK); //$NON-NLS-1$ /** * The "superInterfaces" structural property of this node type (JLS2 API only). * @since 3.0 */ public static final ChildListPropertyDescriptor SUPER_INTERFACES_PROPERTY = new ChildListPropertyDescriptor(TypeDeclaration.class, "superInterfaces", Name.class, NO_CYCLE_RISK); //$NON-NLS-1$ /** * The "superclassType" structural property of this node type (added in JLS3 API). * @since 3.1 */ public static final ChildPropertyDescriptor SUPERCLASS_TYPE_PROPERTY = new ChildPropertyDescriptor(TypeDeclaration.class, "superclassType", Type.class, OPTIONAL, NO_CYCLE_RISK); //$NON-NLS-1$ /** * The "superInterfaceTypes" structural property of this node type (added in JLS3 API). * @since 3.1 */ public static final ChildListPropertyDescriptor SUPER_INTERFACE_TYPES_PROPERTY = new ChildListPropertyDescriptor(TypeDeclaration.class, "superInterfaceTypes", Type.class, NO_CYCLE_RISK); //$NON-NLS-1$ /** * The "typeParameters" structural property of this node type (added in JLS3 API). * @since 3.1 */ public static final ChildListPropertyDescriptor TYPE_PARAMETERS_PROPERTY = new ChildListPropertyDescriptor(TypeDeclaration.class, "typeParameters", TypeParameter.class, NO_CYCLE_RISK); //$NON-NLS-1$ /** * The "bodyDeclarations" structural property of this node type (added in JLS3 API). * @since 3.0 */ public static final ChildListPropertyDescriptor BODY_DECLARATIONS_PROPERTY = internalBodyDeclarationPropertyFactory(TypeDeclaration.class); /** * A list of property descriptors (element type: * {@link StructuralPropertyDescriptor}), * or null if uninitialized. * @since 3.0 */ private static final List PROPERTY_DESCRIPTORS_2_0; /** * A list of property descriptors (element type: * {@link StructuralPropertyDescriptor}), * or null if uninitialized. * @since 3.1 */ private static final List PROPERTY_DESCRIPTORS_3_0; static { List propertyList = new ArrayList(8); createPropertyList(TypeDeclaration.class, propertyList); addProperty(JAVADOC_PROPERTY, propertyList); addProperty(MODIFIERS_PROPERTY, propertyList); addProperty(INTERFACE_PROPERTY, propertyList); addProperty(NAME_PROPERTY, propertyList); addProperty(SUPERCLASS_PROPERTY, propertyList); addProperty(SUPER_INTERFACES_PROPERTY, propertyList); addProperty(BODY_DECLARATIONS_PROPERTY, propertyList); PROPERTY_DESCRIPTORS_2_0 = reapPropertyList(propertyList); propertyList = new ArrayList(9); createPropertyList(TypeDeclaration.class, propertyList); addProperty(JAVADOC_PROPERTY, propertyList); addProperty(MODIFIERS2_PROPERTY, propertyList); addProperty(INTERFACE_PROPERTY, propertyList); addProperty(NAME_PROPERTY, propertyList); addProperty(TYPE_PARAMETERS_PROPERTY, propertyList); addProperty(SUPERCLASS_TYPE_PROPERTY, propertyList); addProperty(SUPER_INTERFACE_TYPES_PROPERTY, propertyList); addProperty(BODY_DECLARATIONS_PROPERTY, propertyList); PROPERTY_DESCRIPTORS_3_0 = reapPropertyList(propertyList); } /** * Returns a list of structural property descriptors for this node type. * Clients must not modify the result. * * @param apiLevel the API level; one of the *AST.JLS*
constants
* @return a list of property descriptors (element type:
* {@link StructuralPropertyDescriptor})
* @since 3.0
*/
public static List propertyDescriptors(int apiLevel) {
if (apiLevel == AST.JLS2_INTERNAL) {
return PROPERTY_DESCRIPTORS_2_0;
} else {
return PROPERTY_DESCRIPTORS_3_0;
}
}
/**
* true
for an interface, false
for a class.
* Defaults to class.
*/
private boolean isInterface = false;
/**
* The type paramters (element type: TypeParameter
).
* Null in JLS2. Added in JLS3; defaults to an empty list
* (see constructor).
* @since 3.1
*/
private ASTNode.NodeList typeParameters = null;
/**
* The optional superclass name; null
if none.
* Defaults to none. Note that this field is not used for
* interface declarations. Not used in 3.0.
*/
private Name optionalSuperclassName = null;
/**
* The superinterface names (element type: Name
).
* JLS2 only; defaults to an empty list. Not used in JLS3.
* (see constructor).
*
*/
private ASTNode.NodeList superInterfaceNames = null;
/**
* The optional superclass type; null
if none.
* Defaults to none. Note that this field is not used for
* interface declarations. Null in JLS2. Added in JLS3.
* @since 3.1
*/
private Type optionalSuperclassType = null;
/**
* The superinterface types (element type: Type
).
* Null in JLS2. Added in JLS3; defaults to an empty list
* (see constructor).
* @since 3.1
*/
private ASTNode.NodeList superInterfaceTypes = null;
/**
* Creates a new AST node for a type declaration owned by the given
* AST. By default, the type declaration is for a class of an
* unspecified, but legal, name; no modifiers; no javadoc;
* no type parameters; no superclass or superinterfaces; and an empty list
* of body declarations.
* * N.B. This constructor is package-private; all subclasses must be * declared in the same package; clients are unable to declare * additional subclasses. *
* * @param ast the AST that is to own this node */ TypeDeclaration(AST ast) { super(ast); if (ast.apiLevel == AST.JLS2_INTERNAL) { this.superInterfaceNames = new ASTNode.NodeList(SUPER_INTERFACES_PROPERTY); } if (ast.apiLevel >= AST.JLS3) { this.typeParameters = new ASTNode.NodeList(TYPE_PARAMETERS_PROPERTY); this.superInterfaceTypes = new ASTNode.NodeList(SUPER_INTERFACE_TYPES_PROPERTY); } } /* (omit javadoc for this method) * Method declared on ASTNode. * @since 3.0 */ final List internalStructuralPropertiesForType(int apiLevel) { return propertyDescriptors(apiLevel); } /* (omit javadoc for this method) * Method declared on ASTNode. */ final int internalGetSetIntProperty(SimplePropertyDescriptor property, boolean get, int value) { if (property == MODIFIERS_PROPERTY) { if (get) { return getModifiers(); } else { internalSetModifiers(value); return 0; } } // allow default implementation to flag the error return super.internalGetSetIntProperty(property, get, value); } /* (omit javadoc for this method) * Method declared on ASTNode. */ final boolean internalGetSetBooleanProperty(SimplePropertyDescriptor property, boolean get, boolean value) { if (property == INTERFACE_PROPERTY) { if (get) { return isInterface(); } else { setInterface(value); return false; } } // allow default implementation to flag the error return super.internalGetSetBooleanProperty(property, get, value); } /* (omit javadoc for this method) * Method declared on ASTNode. */ final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) { if (property == JAVADOC_PROPERTY) { if (get) { return getJavadoc(); } else { setJavadoc((Javadoc) child); return null; } } if (property == NAME_PROPERTY) { if (get) { return getName(); } else { setName((SimpleName) child); return null; } } if (property == SUPERCLASS_PROPERTY) { if (get) { return getSuperclass(); } else { setSuperclass((Name) child); return null; } } if (property == SUPERCLASS_TYPE_PROPERTY) { if (get) { return getSuperclassType(); } else { setSuperclassType((Type) child); return null; } } // allow default implementation to flag the error return super.internalGetSetChildProperty(property, get, child); } /* (omit javadoc for this method) * Method declared on ASTNode. */ final List internalGetChildListProperty(ChildListPropertyDescriptor property) { if (property == MODIFIERS2_PROPERTY) { return modifiers(); } if (property == TYPE_PARAMETERS_PROPERTY) { return typeParameters(); } if (property == SUPER_INTERFACES_PROPERTY) { return superInterfaces(); } if (property == SUPER_INTERFACE_TYPES_PROPERTY) { return superInterfaceTypes(); } if (property == BODY_DECLARATIONS_PROPERTY) { return bodyDeclarations(); } // allow default implementation to flag the error return super.internalGetChildListProperty(property); } /* (omit javadoc for this method) * Method declared on BodyDeclaration. */ final ChildPropertyDescriptor internalJavadocProperty() { return JAVADOC_PROPERTY; } /* (omit javadoc for this method) * Method declared on BodyDeclaration. */ final ChildListPropertyDescriptor internalModifiers2Property() { return MODIFIERS2_PROPERTY; } /* (omit javadoc for this method) * Method declared on BodyDeclaration. */ final SimplePropertyDescriptor internalModifiersProperty() { return MODIFIERS_PROPERTY; } /* (omit javadoc for this method) * Method declared on AbstractTypeDeclaration. */ final ChildPropertyDescriptor internalNameProperty() { return NAME_PROPERTY; } /* (omit javadoc for this method) * Method declared on AbstractTypeDeclaration. */ final ChildListPropertyDescriptor internalBodyDeclarationsProperty() { return BODY_DECLARATIONS_PROPERTY; } /* (omit javadoc for this method) * Method declared on ASTNode. */ final int getNodeType0() { return TYPE_DECLARATION; } /* (omit javadoc for this method) * Method declared on ASTNode. */ ASTNode clone0(AST target) { TypeDeclaration result = new TypeDeclaration(target); result.setSourceRange(this.getStartPosition(), this.getLength()); result.setJavadoc( (Javadoc) ASTNode.copySubtree(target, getJavadoc())); if (this.ast.apiLevel == AST.JLS2_INTERNAL) { result.internalSetModifiers(getModifiers()); result.setSuperclass( (Name) ASTNode.copySubtree(target, getSuperclass())); result.superInterfaces().addAll( ASTNode.copySubtrees(target, superInterfaces())); } result.setInterface(isInterface()); result.setName((SimpleName) getName().clone(target)); if (this.ast.apiLevel >= AST.JLS3) { result.modifiers().addAll(ASTNode.copySubtrees(target, modifiers())); result.typeParameters().addAll( ASTNode.copySubtrees(target, typeParameters())); result.setSuperclassType( (Type) ASTNode.copySubtree(target, getSuperclassType())); result.superInterfaceTypes().addAll( ASTNode.copySubtrees(target, superInterfaceTypes())); } result.bodyDeclarations().addAll( ASTNode.copySubtrees(target, bodyDeclarations())); return result; } /* (omit javadoc for this method) * Method declared on ASTNode. */ final boolean subtreeMatch0(ASTMatcher matcher, Object other) { // dispatch to correct overloaded match method return matcher.match(this, other); } /* (omit javadoc for this method) * Method declared on ASTNode. */ void accept0(ASTVisitor visitor) { boolean visitChildren = visitor.visit(this); if (visitChildren) { // visit children in normal left to right reading order if (this.ast.apiLevel == AST.JLS2_INTERNAL) { acceptChild(visitor, getJavadoc()); acceptChild(visitor, getName()); acceptChild(visitor, getSuperclass()); acceptChildren(visitor, this.superInterfaceNames); acceptChildren(visitor, this.bodyDeclarations); } if (this.ast.apiLevel >= AST.JLS3) { acceptChild(visitor, getJavadoc()); acceptChildren(visitor, this.modifiers); acceptChild(visitor, getName()); acceptChildren(visitor, this.typeParameters); acceptChild(visitor, getSuperclassType()); acceptChildren(visitor, this.superInterfaceTypes); acceptChildren(visitor, this.bodyDeclarations); } } visitor.endVisit(this); } /** * Returns whether this type declaration declares a class or an * interface. * * @returntrue
if this is an interface declaration,
* and false
if this is a class declaration
*/
public boolean isInterface() {
return this.isInterface;
}
/**
* Sets whether this type declaration declares a class or an
* interface.
*
* @param isInterface true
if this is an interface
* declaration, and false
if this is a class
* declaration
*/
public void setInterface(boolean isInterface) {
preValueChange(INTERFACE_PROPERTY);
this.isInterface = isInterface;
postValueChange(INTERFACE_PROPERTY);
}
/**
* Returns the live ordered list of type parameters of this type
* declaration (added in JLS3 API). This list is non-empty for parameterized types.
*
* @return the live list of type parameters
* (element type: TypeParameter
)
* @exception UnsupportedOperationException if this operation is used in
* a JLS2 AST
* @since 3.1
*/
public List typeParameters() {
// more efficient than just calling unsupportedIn2() to check
if (this.typeParameters == null) {
unsupportedIn2();
}
return this.typeParameters;
}
/**
* Returns the name of the superclass declared in this type
* declaration, or null
if there is none (JLS2 API only).
* * Note that this child is not relevant for interface * declarations (although it does still figure in subtree * equality comparisons). *
* * @return the superclass name node, ornull
if
* there is none
* @exception UnsupportedOperationException if this operation is used in
* an AST later than JLS2
* @deprecated In the JLS3 API, this method is replaced by
* {@link #getSuperclassType()}, which returns a Type
* instead of a Name
.
*/
public Name getSuperclass() {
return internalGetSuperclass();
}
/**
* Internal synonym for deprecated method. Used to avoid
* deprecation warnings.
* @since 3.1
*/
/*package*/ final Name internalGetSuperclass() {
supportedOnlyIn2();
return this.optionalSuperclassName;
}
/**
* Returns the superclass declared in this type
* declaration, or null
if there is none (added in JLS3 API).
* * Note that this child is not relevant for interface * declarations (although it does still figure in subtree * equality comparisons). *
* * @return the superclass type node, ornull
if
* there is none
* @exception UnsupportedOperationException if this operation is used in
* a JLS2 AST
* @since 3.1
*/
public Type getSuperclassType() {
unsupportedIn2();
return this.optionalSuperclassType;
}
/**
* Sets or clears the name of the superclass declared in this type
* declaration (JLS2 API only).
* * Note that this child is not relevant for interface * declarations (although it does still figure in subtree * equality comparisons). *
* * @param superclassName the superclass name node, ornull
if
* there is none
* @exception IllegalArgumentException if:
* Type
instead of a Name
.
*/
public void setSuperclass(Name superclassName) {
internalSetSuperclass(superclassName);
}
/**
* Internal synonym for deprecated method. Used to avoid
* deprecation warnings.
* @since 3.1
*/
/*package*/ final void internalSetSuperclass(Name superclassName) {
supportedOnlyIn2();
ASTNode oldChild = this.optionalSuperclassName;
preReplaceChild(oldChild, superclassName, SUPERCLASS_PROPERTY);
this.optionalSuperclassName = superclassName;
postReplaceChild(oldChild, superclassName, SUPERCLASS_PROPERTY);
}
/**
* Sets or clears the superclass declared in this type
* declaration (added in JLS3 API).
* * Note that this child is not relevant for interface declarations * (although it does still figure in subtree equality comparisons). *
* * @param superclassType the superclass type node, ornull
if
* there is none
* @exception IllegalArgumentException if:
* Name
)
* @exception UnsupportedOperationException if this operation is used in
* an AST later than JLS2
* @deprecated In the JLS3 API, this method is replaced by
* {@link #superInterfaceTypes()}.
*/
public List superInterfaces() {
return internalSuperInterfaces();
}
/**
* Internal synonym for deprecated method. Used to avoid
* deprecation warnings.
* @since 3.1
*/
/*package*/ final List internalSuperInterfaces() {
// more efficient than just calling supportedOnlyIn2() to check
if (this.superInterfaceNames == null) {
supportedOnlyIn2();
}
return this.superInterfaceNames;
}
/**
* Returns the live ordered list of superinterfaces of this type
* declaration (added in JLS3 API). For a class declaration, these are the interfaces
* that this class implements; for an interface declaration,
* these are the interfaces that this interface extends.
*
* @return the live list of interface types
* (element type: Type
)
* @exception UnsupportedOperationException if this operation is used in
* a JLS2 AST
* @since 3.1
*/
public List superInterfaceTypes() {
// more efficient than just calling unsupportedIn2() to check
if (this.superInterfaceTypes == null) {
unsupportedIn2();
}
return this.superInterfaceTypes;
}
/**
* Returns the ordered list of field declarations of this type
* declaration. For a class declaration, these are the
* field declarations; for an interface declaration, these are
* the constant declarations.
*
* This convenience method returns this node's body declarations
* with non-fields filtered out. Unlike bodyDeclarations
,
* this method does not return a live result.
*
* This convenience method returns this node's body declarations
* with non-methods filtered out. Unlike bodyDeclarations
,
* this method does not return a live result.
*
* This convenience method returns this node's body declarations
* with non-types filtered out. Unlike bodyDeclarations
,
* this method does not return a live result.
*