diff options
Diffstat (limited to 'org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/TypeDeclaration.java')
-rw-r--r-- | org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/TypeDeclaration.java | 140 |
1 files changed, 138 insertions, 2 deletions
diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/TypeDeclaration.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/TypeDeclaration.java index b6071d5ab6..57c1325f53 100644 --- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/TypeDeclaration.java +++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/TypeDeclaration.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2013 IBM Corporation and others. + * Copyright (c) 2000, 2020 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -18,6 +18,8 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import org.eclipse.jdt.internal.core.dom.util.DOMASTUtil; + /** * Type declaration AST node type. A type declaration * is the union of a class declaration and an interface declaration. @@ -31,11 +33,13 @@ import java.util.List; * [ <b><</b> TypeParameter { <b>,</b> TypeParameter } <b>></b> ] * [ <b>extends</b> Type ] * [ <b>implements</b> Type { <b>,</b> Type } ] + * [ <b>permits</b> Type { <b>,</b> Type } ] * <b>{</b> { ClassBodyDeclaration | <b>;</b> } <b>}</b> * InterfaceDeclaration: * [ Javadoc ] { ExtendedModifier } <b>interface</b> Identifier * [ <b><</b> TypeParameter { <b>,</b> TypeParameter } <b>></b> ] * [ <b>extends</b> Type { <b>,</b> Type } ] + * [ <b>permits</b> Type { <b>,</b> Type } ] * <b>{</b> { InterfaceBodyDeclaration | <b>;</b> } <b>}</b> * </pre> * <p> @@ -135,6 +139,15 @@ public class TypeDeclaration extends AbstractTypeDeclaration { internalBodyDeclarationPropertyFactory(TypeDeclaration.class); /** + * The "permitsTypes" structural property of this node type (element type: {@link Type}) (added in JLS15 API). + * @since 3.23 + */ + public static final ChildListPropertyDescriptor PERMITS_TYPES_PROPERTY = + new ChildListPropertyDescriptor(TypeDeclaration.class, "permitsTypes", Type.class, NO_CYCLE_RISK); //$NON-NLS-1$ + + + + /** * A list of property descriptors (element type: * {@link StructuralPropertyDescriptor}), * or null if uninitialized. @@ -150,6 +163,14 @@ public class TypeDeclaration extends AbstractTypeDeclaration { */ private static final List PROPERTY_DESCRIPTORS_3_0; + /** + * A list of property descriptors (element type: + * {@link StructuralPropertyDescriptor}), + * or null if uninitialized. + * @since 3.22 + */ + private static final List PROPERTY_DESCRIPTORS_15; + static { List propertyList = new ArrayList(8); createPropertyList(TypeDeclaration.class, propertyList); @@ -173,6 +194,19 @@ public class TypeDeclaration extends AbstractTypeDeclaration { addProperty(SUPER_INTERFACE_TYPES_PROPERTY, propertyList); addProperty(BODY_DECLARATIONS_PROPERTY, propertyList); PROPERTY_DESCRIPTORS_3_0 = reapPropertyList(propertyList); + + propertyList = new ArrayList(10); + 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(PERMITS_TYPES_PROPERTY, propertyList); + addProperty(BODY_DECLARATIONS_PROPERTY, propertyList); + PROPERTY_DESCRIPTORS_15 = reapPropertyList(propertyList); } /** @@ -195,6 +229,27 @@ public class TypeDeclaration extends AbstractTypeDeclaration { } /** + * 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 + * <code>AST.JLS*</code> constants + * @param previewEnabled the previewEnabled flag + * @return a list of property descriptors (element type: + * {@link StructuralPropertyDescriptor}) + * @noreference This method is not intended to be referenced by clients. + * @since 3.22 + */ + public static List propertyDescriptors(int apiLevel, boolean previewEnabled) { + if (DOMASTUtil.isFeatureSupportedinAST(apiLevel, previewEnabled, Modifier.SEALED)) { + return PROPERTY_DESCRIPTORS_15; + } else if (apiLevel == AST.JLS2_INTERNAL) { + return PROPERTY_DESCRIPTORS_2_0; + } + return PROPERTY_DESCRIPTORS_3_0; + } + + /** * <code>true</code> for an interface, <code>false</code> for a class. * Defaults to class. */ @@ -240,6 +295,14 @@ public class TypeDeclaration extends AbstractTypeDeclaration { private ASTNode.NodeList superInterfaceTypes = null; /** + * The permits types (element type: {@link Type}). + * Not Null from Java 15 with oreview; defaults to an empty list + * (see constructor). + * @since 3.22 + */ + private ASTNode.NodeList permittedTypes = 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; @@ -262,6 +325,9 @@ public class TypeDeclaration extends AbstractTypeDeclaration { this.typeParameters = new ASTNode.NodeList(TYPE_PARAMETERS_PROPERTY); this.superInterfaceTypes = new ASTNode.NodeList(SUPER_INTERFACE_TYPES_PROPERTY); } + if (DOMASTUtil.isFeatureSupportedinAST(ast, Modifier.SEALED)) { + this.permittedTypes = new ASTNode.NodeList(PERMITS_TYPES_PROPERTY); + } } /* (omit javadoc for this method) @@ -274,6 +340,11 @@ public class TypeDeclaration extends AbstractTypeDeclaration { } @Override + final List internalStructuralPropertiesForType(int apiLevel, boolean previewEnabled) { + return propertyDescriptors(apiLevel, previewEnabled); + } + + @Override final int internalGetSetIntProperty(SimplePropertyDescriptor property, boolean get, int value) { if (property == MODIFIERS_PROPERTY) { if (get) { @@ -353,6 +424,9 @@ public class TypeDeclaration extends AbstractTypeDeclaration { if (property == SUPER_INTERFACE_TYPES_PROPERTY) { return superInterfaceTypes(); } + if (property == PERMITS_TYPES_PROPERTY) { + return permittedTypes(); + } if (property == BODY_DECLARATIONS_PROPERTY) { return bodyDeclarations(); } @@ -414,6 +488,11 @@ public class TypeDeclaration extends AbstractTypeDeclaration { result.superInterfaceTypes().addAll( ASTNode.copySubtrees(target, superInterfaceTypes())); } + if (DOMASTUtil.isFeatureSupportedinAST(this.ast, Modifier.SEALED)) { + result.permittedTypes().addAll( + ASTNode.copySubtrees(target, permittedTypes())); + result.restrictedIdentifierStartPosition = getRestrictedIdentifierStartPosition(); + } result.bodyDeclarations().addAll( ASTNode.copySubtrees(target, bodyDeclarations())); return result; @@ -446,6 +525,9 @@ public class TypeDeclaration extends AbstractTypeDeclaration { acceptChildren(visitor, this.superInterfaceTypes); acceptChildren(visitor, this.bodyDeclarations); } + if (DOMASTUtil.isFeatureSupportedinAST(getAST(), Modifier.SEALED)) { + acceptChildren(visitor, this.permittedTypes); + } } visitor.endVisit(this); } @@ -662,6 +744,26 @@ public class TypeDeclaration extends AbstractTypeDeclaration { } /** + * Returns the live ordered list of permits of this type + * declaration (added in JLS15 API). For a type declaration, these are the + * permitted types which can implement/extend this sealed type. + * + * + * @return the live list of types + * (element type: {@link Type}) + * @exception UnsupportedOperationException if this operation is not used with Java 15 and preview enabled + * @since 3.23 + */ + public List permittedTypes() { + // more efficient than just calling unsupportedIn2() to check + if (this.permittedTypes == null) { + supportedOnlyIn15(); + unsupportedWithoutPreviewError(); + } + return this.permittedTypes; + } + + /** * 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 @@ -761,7 +863,8 @@ public class TypeDeclaration extends AbstractTypeDeclaration { @Override int memSize() { - return super.memSize() + 6 * 4; + // there are 7 fields that are either int or pointer and one boolean type + return super.memSize() + 1 + (7 * 4) ; } @Override @@ -775,7 +878,40 @@ public class TypeDeclaration extends AbstractTypeDeclaration { + (this.optionalSuperclassType == null ? 0 : getSuperclassType().treeSize()) + (this.superInterfaceNames == null ? 0 : this.superInterfaceNames.listSize()) + (this.superInterfaceTypes == null ? 0 : this.superInterfaceTypes.listSize()) + + (this.permittedTypes == null ? 0 : this.permittedTypes.listSize()) + this.bodyDeclarations.listSize(); } + + /** + * A character index into the original restricted identifier source string, or <code>-1</code> if no restricted + * identifier source position information is available for this node; <code>-1</code> by default. + */ + private int restrictedIdentifierStartPosition = -1; + + /** + * A character index into the original restricted identifier source string, or <code>-1</code> if no restricted + * identifier source position information is available for this node; <code>-1</code> by default. + * + * @noreference This method is not intended to be referenced by clients. + */ + public void setRestrictedIdentifierStartPosition(int restrictedIdentifierStartPosition) { + if (restrictedIdentifierStartPosition < 0) { + throw new IllegalArgumentException(); + } + // restrictedIdentifierStartPosition is not considered a structural property + // but we protect it nevertheless + checkModifiable(); + this.restrictedIdentifierStartPosition = restrictedIdentifierStartPosition; + } + + /** + * A character index into the original restricted identifier source string, or <code>-1</code> if no restricted + * identifier source position information is available for this node; <code>-1</code> by default. + * + * @noreference This method is not intended to be referenced by clients. + */ + public int getRestrictedIdentifierStartPosition() { + return this.restrictedIdentifierStartPosition; + } } |