]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2011 IBM Corporation and others. | |
3 | * All rights reserved. This program and the accompanying materials | |
4 | * are made available under the terms of the Eclipse Public License v1.0 | |
5 | * which accompanies this distribution, and is available at | |
6 | * http://www.eclipse.org/legal/epl-v10.html | |
7 | * | |
8 | * Contributors: | |
9 | * IBM Corporation - initial API and implementation | |
10 | *******************************************************************************/ | |
11 | package org.eclipse.jdt.internal.corext.dom.fragments; | |
12 | ||
13 | import org.eclipse.core.runtime.Assert; | |
14 | ||
15 | import org.eclipse.text.edits.TextEditGroup; | |
16 | ||
17 | import org.eclipse.jdt.core.dom.ASTNode; | |
18 | import org.eclipse.jdt.core.dom.Name; | |
19 | import org.eclipse.jdt.core.dom.ParenthesizedExpression; | |
20 | import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; | |
21 | ||
22 | import org.eclipse.jdt.internal.corext.dom.JdtASTMatcher; | |
23 | ||
24 | class SimpleFragment extends ASTFragment { | |
25 | private final ASTNode fNode; | |
26 | ||
27 | SimpleFragment(ASTNode node) { | |
28 | Assert.isNotNull(node); | |
29 | fNode= node; | |
30 | } | |
31 | ||
32 | @Override | |
33 | public IASTFragment[] getMatchingFragmentsWithNode(ASTNode node) { | |
34 | if (! JdtASTMatcher.doNodesMatch(getAssociatedNode(), node)) | |
35 | return new IASTFragment[0]; | |
36 | ||
37 | IASTFragment match= ASTFragmentFactory.createFragmentForFullSubtree(node); | |
38 | Assert.isTrue(match.matches(this) || this.matches(match)); | |
39 | return new IASTFragment[] { match }; | |
40 | } | |
41 | ||
42 | public boolean matches(IASTFragment other) { | |
43 | return other.getClass().equals(getClass()) && JdtASTMatcher.doNodesMatch(other.getAssociatedNode(), getAssociatedNode()); | |
44 | } | |
45 | ||
46 | public IASTFragment[] getSubFragmentsMatching(IASTFragment toMatch) { | |
47 | return ASTMatchingFragmentFinder.findMatchingFragments(getAssociatedNode(), (ASTFragment) toMatch); | |
48 | } | |
49 | ||
50 | public int getStartPosition() { | |
51 | return fNode.getStartPosition(); | |
52 | } | |
53 | ||
54 | public int getLength() { | |
55 | return fNode.getLength(); | |
56 | } | |
57 | ||
58 | public ASTNode getAssociatedNode() { | |
59 | return fNode; | |
60 | } | |
61 | ||
62 | public void replace(ASTRewrite rewrite, ASTNode replacement, TextEditGroup textEditGroup) { | |
63 | if (replacement instanceof Name && fNode.getParent() instanceof ParenthesizedExpression) { | |
64 | // replace including the parenthesized expression around it | |
65 | rewrite.replace(fNode.getParent(), replacement, textEditGroup); | |
66 | } else { | |
67 | rewrite.replace(fNode, replacement, textEditGroup); | |
68 | } | |
69 | } | |
70 | ||
71 | @Override | |
72 | public int hashCode() { | |
73 | return fNode.hashCode(); | |
74 | } | |
75 | ||
76 | @Override | |
77 | public boolean equals(Object obj) { | |
78 | if (this == obj) | |
79 | return true; | |
80 | if (obj == null) | |
81 | return false; | |
82 | if (getClass() != obj.getClass()) | |
83 | return false; | |
84 | SimpleFragment other= (SimpleFragment) obj; | |
85 | return other.generated_7872377811361121460(this); | |
86 | } | |
87 | ||
88 | public boolean generated_7872377811361121460(SimpleFragment simplefragment) { | |
89 | return simplefragment.fNode.equals(fNode); | |
90 | } | |
91 | ||
92 | } |