PageRenderTime 25ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/eclipse_SDK-3.7.1/plugins/org.eclipse.jdt.ui.source_3.7.1.r371_v20110824-0800/org/eclipse/jdt/internal/corext/refactoring/structure/ASTNodeSearchUtil.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 268 lines | 218 code | 36 blank | 14 comment | 47 complexity | 59a613616f9d4f7284bc999246d4e7fc MD5 | raw file
  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.refactoring.structure;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. import org.eclipse.core.runtime.Assert;
  15. import org.eclipse.jdt.core.IField;
  16. import org.eclipse.jdt.core.IImportContainer;
  17. import org.eclipse.jdt.core.IImportDeclaration;
  18. import org.eclipse.jdt.core.IInitializer;
  19. import org.eclipse.jdt.core.IJavaElement;
  20. import org.eclipse.jdt.core.IMember;
  21. import org.eclipse.jdt.core.IMethod;
  22. import org.eclipse.jdt.core.IPackageDeclaration;
  23. import org.eclipse.jdt.core.ISourceRange;
  24. import org.eclipse.jdt.core.IType;
  25. import org.eclipse.jdt.core.JavaModelException;
  26. import org.eclipse.jdt.core.dom.ASTNode;
  27. import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
  28. import org.eclipse.jdt.core.dom.AnnotationTypeDeclaration;
  29. import org.eclipse.jdt.core.dom.AnnotationTypeMemberDeclaration;
  30. import org.eclipse.jdt.core.dom.Block;
  31. import org.eclipse.jdt.core.dom.BodyDeclaration;
  32. import org.eclipse.jdt.core.dom.ClassInstanceCreation;
  33. import org.eclipse.jdt.core.dom.CompilationUnit;
  34. import org.eclipse.jdt.core.dom.ConstructorInvocation;
  35. import org.eclipse.jdt.core.dom.EnumConstantDeclaration;
  36. import org.eclipse.jdt.core.dom.EnumDeclaration;
  37. import org.eclipse.jdt.core.dom.FieldDeclaration;
  38. import org.eclipse.jdt.core.dom.ImportDeclaration;
  39. import org.eclipse.jdt.core.dom.Initializer;
  40. import org.eclipse.jdt.core.dom.MethodDeclaration;
  41. import org.eclipse.jdt.core.dom.NodeFinder;
  42. import org.eclipse.jdt.core.dom.PackageDeclaration;
  43. import org.eclipse.jdt.core.dom.SimpleName;
  44. import org.eclipse.jdt.core.dom.SuperConstructorInvocation;
  45. import org.eclipse.jdt.core.dom.TypeDeclaration;
  46. import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
  47. import org.eclipse.jdt.core.search.SearchMatch;
  48. import org.eclipse.jdt.internal.corext.dom.ASTNodes;
  49. import org.eclipse.jdt.internal.corext.dom.Selection;
  50. import org.eclipse.jdt.internal.corext.dom.SelectionAnalyzer;
  51. import org.eclipse.jdt.internal.corext.util.JdtFlags;
  52. public class ASTNodeSearchUtil {
  53. private ASTNodeSearchUtil() {
  54. //no instance
  55. }
  56. public static ASTNode[] getAstNodes(SearchMatch[] searchResults, CompilationUnit cuNode) {
  57. List<ASTNode> result= new ArrayList<ASTNode>(searchResults.length);
  58. for (int i= 0; i < searchResults.length; i++) {
  59. ASTNode node= getAstNode(searchResults[i], cuNode);
  60. if (node != null)
  61. result.add(node);
  62. }
  63. return result.toArray(new ASTNode[result.size()]);
  64. }
  65. public static ASTNode getAstNode(SearchMatch searchResult, CompilationUnit cuNode) {
  66. ASTNode selectedNode= getAstNode(cuNode, searchResult.getOffset(), searchResult.getLength());
  67. if (selectedNode == null)
  68. return null;
  69. if (selectedNode.getParent() == null)
  70. return null;
  71. return selectedNode;
  72. }
  73. public static ASTNode getAstNode(CompilationUnit cuNode, int start, int length){
  74. SelectionAnalyzer analyzer= new SelectionAnalyzer(Selection.createFromStartLength(start, length), true);
  75. cuNode.accept(analyzer);
  76. //XXX workaround for jdt core feature 23527
  77. ASTNode node= analyzer.getFirstSelectedNode();
  78. if (node == null && analyzer.getLastCoveringNode() instanceof SuperConstructorInvocation)
  79. node= analyzer.getLastCoveringNode().getParent();
  80. else if (node == null && analyzer.getLastCoveringNode() instanceof ConstructorInvocation)
  81. node= analyzer.getLastCoveringNode().getParent();
  82. if (node == null)
  83. return null;
  84. ASTNode parentNode= node.getParent();
  85. if (parentNode instanceof MethodDeclaration){
  86. MethodDeclaration md= (MethodDeclaration)parentNode;
  87. if (!(node instanceof SimpleName)
  88. && md.isConstructor()
  89. && md.getBody() != null
  90. && md.getBody().statements().size() > 0
  91. &&(md.getBody().statements().get(0) instanceof ConstructorInvocation || md.getBody().statements().get(0) instanceof SuperConstructorInvocation)
  92. &&((ASTNode)md.getBody().statements().get(0)).getLength() == length + 1)
  93. return (ASTNode)md.getBody().statements().get(0);
  94. }
  95. if (parentNode instanceof SuperConstructorInvocation){
  96. if (parentNode.getLength() == length + 1)
  97. return parentNode;
  98. }
  99. if (parentNode instanceof ConstructorInvocation){
  100. if (parentNode.getLength() == length + 1)
  101. return parentNode;
  102. }
  103. return node;
  104. }
  105. public static MethodDeclaration getMethodDeclarationNode(IMethod iMethod, CompilationUnit cuNode) throws JavaModelException {
  106. return (MethodDeclaration)ASTNodes.getParent(getNameNode(iMethod, cuNode), MethodDeclaration.class);
  107. }
  108. public static AnnotationTypeMemberDeclaration getAnnotationTypeMemberDeclarationNode(IMethod iMethod, CompilationUnit cuNode) throws JavaModelException {
  109. return (AnnotationTypeMemberDeclaration) ASTNodes.getParent(getNameNode(iMethod, cuNode), AnnotationTypeMemberDeclaration.class);
  110. }
  111. public static BodyDeclaration getMethodOrAnnotationTypeMemberDeclarationNode(IMethod iMethod, CompilationUnit cuNode) throws JavaModelException {
  112. if (JdtFlags.isAnnotation(iMethod.getDeclaringType()))
  113. return getAnnotationTypeMemberDeclarationNode(iMethod, cuNode);
  114. else
  115. return getMethodDeclarationNode(iMethod, cuNode);
  116. }
  117. public static VariableDeclarationFragment getFieldDeclarationFragmentNode(IField iField, CompilationUnit cuNode) throws JavaModelException {
  118. ASTNode node= getNameNode(iField, cuNode);
  119. if (node instanceof VariableDeclarationFragment)
  120. return (VariableDeclarationFragment)node;
  121. return (VariableDeclarationFragment)ASTNodes.getParent(node, VariableDeclarationFragment.class);
  122. }
  123. public static FieldDeclaration getFieldDeclarationNode(IField iField, CompilationUnit cuNode) throws JavaModelException {
  124. return (FieldDeclaration) ASTNodes.getParent(getNameNode(iField, cuNode), FieldDeclaration.class);
  125. }
  126. public static EnumConstantDeclaration getEnumConstantDeclaration(IField iField, CompilationUnit cuNode) throws JavaModelException {
  127. return (EnumConstantDeclaration) ASTNodes.getParent(getNameNode(iField, cuNode), EnumConstantDeclaration.class);
  128. }
  129. public static BodyDeclaration getFieldOrEnumConstantDeclaration(IField iField, CompilationUnit cuNode) throws JavaModelException {
  130. if (JdtFlags.isEnum(iField))
  131. return getEnumConstantDeclaration(iField, cuNode);
  132. else
  133. return getFieldDeclarationNode(iField, cuNode);
  134. }
  135. public static EnumDeclaration getEnumDeclarationNode(IType iType, CompilationUnit cuNode) throws JavaModelException {
  136. return (EnumDeclaration) ASTNodes.getParent(getNameNode(iType, cuNode), EnumDeclaration.class);
  137. }
  138. public static AnnotationTypeDeclaration getAnnotationTypeDeclarationNode(IType iType, CompilationUnit cuNode) throws JavaModelException {
  139. return (AnnotationTypeDeclaration) ASTNodes.getParent(getNameNode(iType, cuNode), AnnotationTypeDeclaration.class);
  140. }
  141. public static BodyDeclaration getBodyDeclarationNode(IMember iMember, CompilationUnit cuNode) throws JavaModelException {
  142. return (BodyDeclaration) ASTNodes.getParent(getNameNode(iMember, cuNode), BodyDeclaration.class);
  143. }
  144. public static AbstractTypeDeclaration getAbstractTypeDeclarationNode(IType iType, CompilationUnit cuNode) throws JavaModelException {
  145. return (AbstractTypeDeclaration) ASTNodes.getParent(getNameNode(iType, cuNode), AbstractTypeDeclaration.class);
  146. }
  147. public static TypeDeclaration getTypeDeclarationNode(IType iType, CompilationUnit cuNode) throws JavaModelException {
  148. return (TypeDeclaration) ASTNodes.getParent(getNameNode(iType, cuNode), TypeDeclaration.class);
  149. }
  150. public static ClassInstanceCreation getClassInstanceCreationNode(IType iType, CompilationUnit cuNode) throws JavaModelException {
  151. return (ClassInstanceCreation) ASTNodes.getParent(getNameNode(iType, cuNode), ClassInstanceCreation.class);
  152. }
  153. public static List<BodyDeclaration> getBodyDeclarationList(IType iType, CompilationUnit cuNode) throws JavaModelException {
  154. if (iType.isAnonymous())
  155. return getClassInstanceCreationNode(iType, cuNode).getAnonymousClassDeclaration().bodyDeclarations();
  156. else
  157. return getAbstractTypeDeclarationNode(iType, cuNode).bodyDeclarations();
  158. }
  159. //returns an array because of the import container, which does not represent 1 node but many
  160. //for fields, it returns the whole declaration node
  161. public static ASTNode[] getDeclarationNodes(IJavaElement element, CompilationUnit cuNode) throws JavaModelException {
  162. switch(element.getElementType()){
  163. case IJavaElement.FIELD:
  164. return new ASTNode[]{getFieldOrEnumConstantDeclaration((IField) element, cuNode)};
  165. case IJavaElement.IMPORT_CONTAINER:
  166. return getImportNodes((IImportContainer)element, cuNode);
  167. case IJavaElement.IMPORT_DECLARATION:
  168. return new ASTNode[]{getImportDeclarationNode((IImportDeclaration)element, cuNode)};
  169. case IJavaElement.INITIALIZER:
  170. return new ASTNode[]{getInitializerNode((IInitializer)element, cuNode)};
  171. case IJavaElement.METHOD:
  172. return new ASTNode[]{getMethodOrAnnotationTypeMemberDeclarationNode((IMethod) element, cuNode)};
  173. case IJavaElement.PACKAGE_DECLARATION:
  174. return new ASTNode[]{getPackageDeclarationNode((IPackageDeclaration)element, cuNode)};
  175. case IJavaElement.TYPE:
  176. return new ASTNode[]{getAbstractTypeDeclarationNode((IType) element, cuNode)};
  177. default:
  178. Assert.isTrue(false, String.valueOf(element.getElementType()));
  179. return null;
  180. }
  181. }
  182. private static ASTNode getNameNode(IMember iMember, CompilationUnit cuNode) throws JavaModelException {
  183. return NodeFinder.perform(cuNode, iMember.getNameRange());
  184. }
  185. public static PackageDeclaration getPackageDeclarationNode(IPackageDeclaration reference, CompilationUnit cuNode) throws JavaModelException {
  186. return (PackageDeclaration) findNode(reference.getSourceRange(), cuNode);
  187. }
  188. public static ImportDeclaration getImportDeclarationNode(IImportDeclaration reference, CompilationUnit cuNode) throws JavaModelException {
  189. return (ImportDeclaration) findNode(reference.getSourceRange(), cuNode);
  190. }
  191. public static ASTNode[] getImportNodes(IImportContainer reference, CompilationUnit cuNode) throws JavaModelException {
  192. IJavaElement[] imps= reference.getChildren();
  193. ASTNode[] result= new ASTNode[imps.length];
  194. for (int i= 0; i < imps.length; i++) {
  195. result[i]= getImportDeclarationNode((IImportDeclaration)imps[i], cuNode);
  196. }
  197. return result;
  198. }
  199. public static Initializer getInitializerNode(IInitializer initializer, CompilationUnit cuNode) throws JavaModelException {
  200. ASTNode node= findNode(initializer.getSourceRange(), cuNode);
  201. if (node instanceof Initializer)
  202. return (Initializer) node;
  203. if (node instanceof Block && node.getParent() instanceof Initializer)
  204. return (Initializer) node.getParent();
  205. return null;
  206. }
  207. private static ASTNode findNode(ISourceRange range, CompilationUnit cuNode){
  208. NodeFinder nodeFinder= new NodeFinder(cuNode, range.getOffset(), range.getLength());
  209. ASTNode coveredNode= nodeFinder.getCoveredNode();
  210. if (coveredNode != null)
  211. return coveredNode;
  212. else
  213. return nodeFinder.getCoveringNode();
  214. }
  215. public static ASTNode[] findNodes(SearchMatch[] searchResults, CompilationUnit cuNode) {
  216. List<ASTNode> result= new ArrayList<ASTNode>(searchResults.length);
  217. for (int i= 0; i < searchResults.length; i++) {
  218. ASTNode node= findNode(searchResults[i], cuNode);
  219. if (node != null)
  220. result.add(node);
  221. }
  222. return result.toArray(new ASTNode[result.size()]);
  223. }
  224. public static ASTNode findNode(SearchMatch searchResult, CompilationUnit cuNode) {
  225. ASTNode selectedNode= NodeFinder.perform(cuNode, searchResult.getOffset(), searchResult.getLength());
  226. if (selectedNode == null)
  227. return null;
  228. if (selectedNode.getParent() == null)
  229. return null;
  230. return selectedNode;
  231. }
  232. }