| 1 | /******************************************************************************* |
|---|---|
| 2 | * Copyright (c) 2021, 2022 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 | import java.util.ArrayList; |
| 17 | import java.util.List; |
| 18 | |
| 19 | import org.eclipse.jdt.internal.core.dom.util.DOMASTUtil; |
| 20 | |
| 21 | /** |
| 22 | * GuardedPattern pattern AST node type. |
| 23 | * |
| 24 | * <pre> |
| 25 | * GuardedPattern: |
| 26 | * Pattern && Expression |
| 27 | * </pre> |
| 28 | * |
| 29 | * @since 3.27 |
| 30 | * @noinstantiate This class is not intended to be instantiated by clients. |
| 31 | * @noreference This class is not intended to be referenced by clients. |
| 32 | */ |
| 33 | |
| 34 | @SuppressWarnings("rawtypes") |
| 35 | public class GuardedPattern extends Pattern{ |
| 36 | |
| 37 | GuardedPattern(AST ast) { |
| 38 | super(ast); |
| 39 | supportedOnlyIn18(); |
| 40 | unsupportedWithoutPreviewError(); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * The "pattern" structural property of this node type (child type: {@link Pattern}). (added in JEP 406). |
| 45 | */ |
| 46 | public static final ChildPropertyDescriptor PATTERN_PROPERTY = internalPatternPropertyFactory(GuardedPattern.class); |
| 47 | |
| 48 | /** |
| 49 | * The "expression" structural property of this node type (child type: {@link Expression}). (added in JEP 406). |
| 50 | */ |
| 51 | public static final ChildPropertyDescriptor EXPRESSION_PROPERTY = |
| 52 | new ChildPropertyDescriptor(GuardedPattern.class, "expression", Expression.class, MANDATORY, CYCLE_RISK); //$NON-NLS-1$); |
| 53 | |
| 54 | |
| 55 | /** |
| 56 | * A list of property descriptors (element type: |
| 57 | * {@link StructuralPropertyDescriptor}), |
| 58 | * or null if uninitialized. |
| 59 | */ |
| 60 | private static final List PROPERTY_DESCRIPTORS; |
| 61 | |
| 62 | static { |
| 63 | List propertyList = new ArrayList(3); |
| 64 | createPropertyList(GuardedPattern.class, propertyList); |
| 65 | addProperty(PATTERN_PROPERTY, propertyList); |
| 66 | addProperty(EXPRESSION_PROPERTY, propertyList); |
| 67 | PROPERTY_DESCRIPTORS = reapPropertyList(propertyList); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * The pattern; <code>null</code> for none |
| 72 | */ |
| 73 | private Pattern pattern = null; |
| 74 | |
| 75 | /** |
| 76 | * The expression; <code>null</code> for none; lazily initialized (but |
| 77 | * does <b>not</b> default to none). |
| 78 | */ |
| 79 | private Expression conditonalExpression = null; |
| 80 | |
| 81 | |
| 82 | |
| 83 | @Override |
| 84 | List internalStructuralPropertiesForType(int apiLevel) { |
| 85 | return propertyDescriptors(apiLevel); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | final List internalStructuralPropertiesForType(int apiLevel, boolean previewEnabled) { |
| 90 | return propertyDescriptors(apiLevel, previewEnabled); |
| 91 | } |
| 92 | @Override |
| 93 | final ASTNode internalGetSetChildProperty(ChildPropertyDescriptor property, boolean get, ASTNode child) { |
| 94 | if (property == EXPRESSION_PROPERTY) { |
| 95 | if (get) { |
| 96 | return getExpression(); |
| 97 | } else { |
| 98 | setExpression((Expression) child); |
| 99 | return null; |
| 100 | } |
| 101 | } else if (property == PATTERN_PROPERTY) { |
| 102 | if (get) { |
| 103 | return getPattern(); |
| 104 | } else { |
| 105 | setPattern((Pattern)child); |
| 106 | return null; |
| 107 | } |
| 108 | } |
| 109 | // allow default implementation to flag the error |
| 110 | return super.internalGetSetChildProperty(property, get, child); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | int getNodeType0() { |
| 115 | return GUARDED_PATTERN; |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | boolean subtreeMatch0(ASTMatcher matcher, Object other) { |
| 120 | return matcher.match(this, other); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | ASTNode clone0(AST target) { |
| 125 | GuardedPattern result = new GuardedPattern(target); |
| 126 | result.setSourceRange(getStartPosition(), getLength()); |
| 127 | result.setPattern((Pattern) getPattern().clone(target)); |
| 128 | result.setExpression((Expression) getExpression().clone(target)); |
| 129 | return result; |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | void accept0(ASTVisitor visitor) { |
| 134 | boolean visitChildren = visitor.visit(this); |
| 135 | if (visitChildren) { |
| 136 | // visit children in normal left to right reading order |
| 137 | acceptChild(visitor, getPattern()); |
| 138 | acceptChild(visitor, getExpression()); |
| 139 | } |
| 140 | visitor.endVisit(this); |
| 141 | |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | int memSize() { |
| 146 | return BASE_NODE_SIZE + 2 * 4; |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | int treeSize() { |
| 151 | return |
| 152 | memSize() |
| 153 | + (this.pattern == null ? 0 : getPattern().treeSize()) |
| 154 | + (this.conditonalExpression == null ? 0 : getExpression().treeSize()); |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public List<SingleVariableDeclaration> patternVariables() { |
| 159 | return null; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Returns a list of structural property descriptors for this node type. |
| 164 | * Clients must not modify the result. |
| 165 | * |
| 166 | * @param apiLevel the API level; one of the |
| 167 | * <code>AST.JLS*</code> constants |
| 168 | |
| 169 | * @return a list of property descriptors (element type: |
| 170 | * {@link StructuralPropertyDescriptor}) |
| 171 | */ |
| 172 | public static List propertyDescriptors(int apiLevel) { |
| 173 | return null; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Returns a list of structural property descriptors for this node type. |
| 178 | * Clients must not modify the result. |
| 179 | * |
| 180 | * @param apiLevel the API level; one of the |
| 181 | * <code>AST.JLS*</code> constants |
| 182 | * @param previewEnabled the previewEnabled flag |
| 183 | * @return a list of property descriptors (element type: |
| 184 | * {@link StructuralPropertyDescriptor}) |
| 185 | * @noreference This method is not intended to be referenced by clients. |
| 186 | */ |
| 187 | public static List propertyDescriptors(int apiLevel, boolean previewEnabled) { |
| 188 | if (DOMASTUtil.isPatternSupported(apiLevel, previewEnabled)) { |
| 189 | return PROPERTY_DESCRIPTORS; |
| 190 | } |
| 191 | return null; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Returns the conditional expression of this pattern, or |
| 196 | * <code>null</code> if there is none (the "default:" case). |
| 197 | * |
| 198 | * @return the expression node, or <code>null</code> if there is none |
| 199 | */ |
| 200 | public Expression getExpression() { |
| 201 | supportedOnlyIn18(); |
| 202 | unsupportedWithoutPreviewError(); |
| 203 | return this.conditonalExpression; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Returns the pattern of this Guarded Pattern, or |
| 208 | * <code>empty</code> if there is none. |
| 209 | * @return the pattern node |
| 210 | * (element type: {@link Pattern}) |
| 211 | * @exception UnsupportedOperationException if this operation is used other than JLS18 |
| 212 | * @exception UnsupportedOperationException if this expression is used with previewEnabled flag as false |
| 213 | * @noreference This method is not intended to be referenced by clients as it is a part of Java preview feature. |
| 214 | */ |
| 215 | public Pattern getPattern() { |
| 216 | supportedOnlyIn18(); |
| 217 | unsupportedWithoutPreviewError(); |
| 218 | return this.pattern; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Sets the conditional expression of this pattern, or clears it (turns it into |
| 223 | * the "default:" case). |
| 224 | * |
| 225 | * @param expression the expression node, or <code>null</code> to |
| 226 | * turn it into the "default:" case |
| 227 | * @exception IllegalArgumentException if: |
| 228 | * <ul> |
| 229 | * <li>the node belongs to a different AST</li> |
| 230 | * <li>the node already has a parent</li> |
| 231 | * <li>a cycle in would be created</li> |
| 232 | * </ul> |
| 233 | */ |
| 234 | public void setExpression(Expression expression) { |
| 235 | supportedOnlyIn18(); |
| 236 | unsupportedWithoutPreviewError(); |
| 237 | ASTNode oldChild = this.conditonalExpression; |
| 238 | preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY); |
| 239 | this.conditonalExpression = expression; |
| 240 | postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Sets the pattern of this switch case. |
| 245 | * @param pattern |
| 246 | * @noreference This method is not intended to be referenced by clients. |
| 247 | * @exception UnsupportedOperationException if this operation is used not for JLS18 |
| 248 | * @exception UnsupportedOperationException if this operation is used without previewEnabled |
| 249 | */ |
| 250 | public void setPattern(Pattern pattern) { |
| 251 | supportedOnlyIn18(); |
| 252 | unsupportedWithoutPreviewError(); |
| 253 | ASTNode oldChild = this.pattern; |
| 254 | preReplaceChild(oldChild, pattern, PATTERN_PROPERTY); |
| 255 | this.pattern = pattern; |
| 256 | postReplaceChild(oldChild, pattern, PATTERN_PROPERTY); |
| 257 | } |
| 258 | |
| 259 | } |
| 260 |
Members