1 /******************************************************************************* 2 * Copyright (c) 2000, 2019 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 * Microsoft Corporation - copied to jdt.core.manipulation 14 *******************************************************************************/ 15 package org.eclipse.jdt.internal.corext.dom.fragments; 16 17 import org.eclipse.text.edits.TextEditGroup; 18 19 import org.eclipse.jdt.core.dom.ASTNode; 20 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; 21 22 /** 23 * An IASTFragment represents 'part' of an AST, but 24 * not necessarily a subtree of the AST. A fragment 25 * may simply be an instance of some sort of pattern 26 * or formation in an AST. 27 * Such "fragments", however, do correspond to a 28 * contiguous source code region, and, thus, posses a 29 * source start position, and a source length. Every 30 * fragment maps to an ASTNode, although this mapping is 31 * not necessarily straightforward, and more than one 32 * fragment may map to a given node. 33 * 34 * Fragments support abstract operations, which 35 * support the notion of 'matching' fragments. 36 * One operation determines whether a fragment 'matches' 37 * a given fragment. Another operation finds all 38 * sub-fragments (fragments contained within a 39 * parent fragment, including the parent itself) 40 * which 'match' another given fragment. 41 * 42 */ 43 public interface IASTFragment { 44 45 /** 46 * Determines whether <code> other </code> 47 * 'matches' <code> this </code>. 48 * This binary operation should be reflexive, 49 * symmetric, and transitive. 50 * 51 * That two node match does not imply that their source ranges 52 * are the same, or that they map (via getAssociatedNode()) to the 53 * same node. 54 * @param other the element to test with 55 * @return return <code> true if the passed element matches the current element. 56 */ matches(IASTFragment other)57 public boolean matches(IASTFragment other); 58 59 /** 60 * Returns (at least some approximation of) a maximal set of 61 * sub-fragments of this fragment which match <code> toMatch </code> 62 * @param toMatch the fragment to match 63 * @return set of sub fragments that match 64 */ getSubFragmentsMatching(IASTFragment toMatch)65 public IASTFragment[] getSubFragmentsMatching(IASTFragment toMatch); 66 67 /** 68 * Every fragment maps to a node. 69 * Multiple fragments can map to the same node. 70 * 71 * @return ASTNode The node to which this fragment maps. 72 */ getAssociatedNode()73 public ASTNode getAssociatedNode(); 74 75 /** 76 * Every fragment has a source start position. 77 * 78 * @return int The source start position. 79 */ getStartPosition()80 public int getStartPosition(); 81 82 /** 83 * Every fragment has a source length. 84 * 85 * @return int The source length. 86 */ getLength()87 public int getLength(); 88 89 /** 90 * Replaces this fragment with the given replacement node. 91 * 92 * @param rewrite an ASTRewrite 93 * @param replacement replacement for this fragment 94 * @param textEditGroup a description or <code>null</code> 95 */ replace(ASTRewrite rewrite, ASTNode replacement, TextEditGroup textEditGroup)96 public void replace(ASTRewrite rewrite, ASTNode replacement, TextEditGroup textEditGroup); 97 } 98