1 11 package org.eclipse.jdt.internal.core.dom.rewrite; 12 13 import java.util.HashSet ; 14 import java.util.IdentityHashMap ; 15 import java.util.Map ; 16 import java.util.Set ; 17 18 import org.eclipse.jdt.core.dom.AST; 19 import org.eclipse.jdt.core.dom.ASTNode; 20 import org.eclipse.jdt.core.dom.Block; 21 import org.eclipse.jdt.core.dom.FieldDeclaration; 22 import org.eclipse.jdt.core.dom.Modifier; 23 import org.eclipse.jdt.core.dom.ParameterizedType; 24 import org.eclipse.jdt.core.dom.TryStatement; 25 import org.eclipse.jdt.core.dom.VariableDeclarationExpression; 26 import org.eclipse.jdt.core.dom.VariableDeclarationStatement; 27 28 import org.eclipse.jdt.internal.core.dom.rewrite.RewriteEventStore.CopySourceInfo; 29 30 33 public final class NodeInfoStore { 34 private AST ast; 35 36 private Map placeholderNodes; 37 private Set collapsedNodes; 38 39 public NodeInfoStore(AST ast) { 40 super(); 41 this.ast= ast; 42 this.placeholderNodes= null; 43 this.collapsedNodes= null; 44 } 45 46 52 public final void markAsStringPlaceholder(ASTNode placeholder, String code) { 53 StringPlaceholderData data= new StringPlaceholderData(); 54 data.code= code; 55 setPlaceholderData(placeholder, data); 56 } 57 58 63 public final void markAsCopyTarget(ASTNode target, CopySourceInfo copySource) { 64 CopyPlaceholderData data= new CopyPlaceholderData(); 65 data.copySource= copySource; 66 setPlaceholderData(target, data); 67 } 68 69 74 public final ASTNode newPlaceholderNode(int nodeType) { 75 try { 76 ASTNode node= this.ast.createInstance(nodeType); 77 switch (node.getNodeType()) { 78 case ASTNode.FIELD_DECLARATION: 79 ((FieldDeclaration) node).fragments().add(this.ast.newVariableDeclarationFragment()); 80 break; 81 case ASTNode.MODIFIER: 82 ((Modifier) node).setKeyword(Modifier.ModifierKeyword.ABSTRACT_KEYWORD); 83 break; 84 case ASTNode.TRY_STATEMENT : 85 ((TryStatement) node).setFinally(this.ast.newBlock()); break; 87 case ASTNode.VARIABLE_DECLARATION_EXPRESSION : 88 ((VariableDeclarationExpression) node).fragments().add(this.ast.newVariableDeclarationFragment()); 89 break; 90 case ASTNode.VARIABLE_DECLARATION_STATEMENT : 91 ((VariableDeclarationStatement) node).fragments().add(this.ast.newVariableDeclarationFragment()); 92 break; 93 case ASTNode.PARAMETERIZED_TYPE : 94 ((ParameterizedType) node).typeArguments().add(this.ast.newWildcardType()); break; 96 } 97 return node; 98 } catch (IllegalArgumentException e) { 99 return null; 100 } 101 } 102 103 104 108 public Block createCollapsePlaceholder() { 109 Block placeHolder= this.ast.newBlock(); 110 if (this.collapsedNodes == null) { 111 this.collapsedNodes= new HashSet (); 112 } 113 this.collapsedNodes.add(placeHolder); 114 return placeHolder; 115 } 116 117 public boolean isCollapsed(ASTNode node) { 118 if (this.collapsedNodes != null) { 119 return this.collapsedNodes.contains(node); 120 } 121 return false; 122 } 123 124 public Object getPlaceholderData(ASTNode node) { 125 if (this.placeholderNodes != null) { 126 return this.placeholderNodes.get(node); 127 } 128 return null; 129 } 130 131 private void setPlaceholderData(ASTNode node, PlaceholderData data) { 132 if (this.placeholderNodes == null) { 133 this.placeholderNodes= new IdentityHashMap (); 134 } 135 this.placeholderNodes.put(node, data); 136 } 137 138 private static class PlaceholderData { 139 } 141 142 protected static final class CopyPlaceholderData extends PlaceholderData { 143 public CopySourceInfo copySource; 144 public String toString() { 145 return "[placeholder " + this.copySource +"]"; } 147 } 148 149 protected static final class StringPlaceholderData extends PlaceholderData { 150 public String code; 151 public String toString() { 152 return "[placeholder string: " + this.code +"]"; } 154 } 155 156 159 public void clear() { 160 this.placeholderNodes= null; 161 this.collapsedNodes= null; 162 } 163 } 164 | Popular Tags |