/
NodeInfoStore.java
169 lines (150 loc) · 5.26 KB
/
NodeInfoStore.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*******************************************************************************
* Copyright (c) 2000, 2013 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.internal.core.dom.rewrite;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Set;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.FieldDeclaration;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.ParameterizedType;
import org.eclipse.jdt.core.dom.TryStatement;
import org.eclipse.jdt.core.dom.VariableDeclarationExpression;
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
import org.eclipse.jdt.internal.core.dom.rewrite.RewriteEventStore.CopySourceInfo;
/**
*
*/
@SuppressWarnings({"rawtypes", "unchecked"})
public final class NodeInfoStore {
private AST ast;
private Map placeholderNodes;
private Set collapsedNodes;
public NodeInfoStore(AST ast) {
super();
this.ast= ast;
this.placeholderNodes= null;
this.collapsedNodes= null;
}
/**
* Marks a node as a placehoder for a plain string content. The type of the node should correspond to the
* code's code content.
* @param placeholder The placeholder node that acts for the string content.
* @param code The string content.
*/
public final void markAsStringPlaceholder(ASTNode placeholder, String code) {
StringPlaceholderData data= new StringPlaceholderData();
data.code= code;
setPlaceholderData(placeholder, data);
}
/**
* Marks a node as a copy or move target. The copy target represents a copied node at the target (copied) site.
* @param target The node at the target site. Can be a placeholder node but also the source node itself.
* @param copySource The info at the source site.
*/
public final void markAsCopyTarget(ASTNode target, CopySourceInfo copySource) {
CopyPlaceholderData data= new CopyPlaceholderData();
data.copySource= copySource;
setPlaceholderData(target, data);
}
/**
* Creates a placeholder node of the given type. <code>null</code> if the type is not supported
* @param nodeType Type of the node to create. Use the type constants in {@link NodeInfoStore}.
* @return Returns a place holder node.
*/
public final ASTNode newPlaceholderNode(int nodeType) {
try {
ASTNode node= this.ast.createInstance(nodeType);
switch (node.getNodeType()) {
case ASTNode.FIELD_DECLARATION:
((FieldDeclaration) node).fragments().add(this.ast.newVariableDeclarationFragment());
break;
case ASTNode.MODIFIER:
((Modifier) node).setKeyword(Modifier.ModifierKeyword.ABSTRACT_KEYWORD);
break;
case ASTNode.TRY_STATEMENT :
((TryStatement) node).setFinally(this.ast.newBlock()); // have to set at least a finally block to be legal code
break;
case ASTNode.VARIABLE_DECLARATION_EXPRESSION :
((VariableDeclarationExpression) node).fragments().add(this.ast.newVariableDeclarationFragment());
break;
case ASTNode.VARIABLE_DECLARATION_STATEMENT :
((VariableDeclarationStatement) node).fragments().add(this.ast.newVariableDeclarationFragment());
break;
case ASTNode.PARAMETERIZED_TYPE :
((ParameterizedType) node).typeArguments().add(this.ast.newWildcardType());
break;
}
return node;
} catch (IllegalArgumentException e) {
return null;
}
}
// collapsed nodes: in source: use one node that represents many; to be used as
// copy/move source or to replace at once.
// in the target: one block node that is not flattened.
public Block createCollapsePlaceholder() {
Block placeHolder= this.ast.newBlock();
if (this.collapsedNodes == null) {
this.collapsedNodes= new HashSet();
}
this.collapsedNodes.add(placeHolder);
return placeHolder;
}
public boolean isCollapsed(ASTNode node) {
if (this.collapsedNodes != null) {
return this.collapsedNodes.contains(node);
}
return false;
}
public Object getPlaceholderData(ASTNode node) {
if (this.placeholderNodes != null) {
return this.placeholderNodes.get(node);
}
return null;
}
private void setPlaceholderData(ASTNode node, PlaceholderData data) {
if (this.placeholderNodes == null) {
this.placeholderNodes= new IdentityHashMap();
}
this.placeholderNodes.put(node, data);
}
static class PlaceholderData {
// base class
}
protected static final class CopyPlaceholderData extends PlaceholderData {
public CopySourceInfo copySource;
@Override
public String toString() {
return "[placeholder " + this.copySource +"]"; //$NON-NLS-1$//$NON-NLS-2$
}
}
protected static final class StringPlaceholderData extends PlaceholderData {
public String code;
@Override
public String toString() {
return "[placeholder string: " + this.code +"]"; //$NON-NLS-1$ //$NON-NLS-2$
}
}
/**
*
*/
public void clear() {
this.placeholderNodes= null;
this.collapsedNodes= null;
}
}