KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > dom > JavaElementMapper


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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;
12
13 import org.eclipse.jdt.core.ICompilationUnit;
14 import org.eclipse.jdt.core.IMember;
15 import org.eclipse.jdt.core.ISourceRange;
16 import org.eclipse.jdt.core.JavaModelException;
17
18 import org.eclipse.jdt.core.dom.AST;
19 import org.eclipse.jdt.core.dom.ASTNode;
20 import org.eclipse.jdt.core.dom.ASTParser;
21 import org.eclipse.jdt.core.dom.CompilationUnit;
22
23 import org.eclipse.jdt.internal.corext.Assert;
24
25 public class JavaElementMapper extends GenericVisitor {
26
27     private IMember fElement;
28     private int fStart;
29     private int fLength;
30     private int fEnd;
31     private ASTNode fResult;
32     
33     private JavaElementMapper(IMember element) throws JavaModelException {
34         super(true);
35         Assert.isNotNull(element);
36         fElement= element;
37         ISourceRange sourceRange= fElement.getNameRange();
38         fStart= sourceRange.getOffset();
39         fLength= sourceRange.getLength();
40         fEnd= fStart + fLength;
41     }
42
43     public static ASTNode perform(IMember member, Class JavaDoc type) throws JavaModelException {
44         JavaElementMapper mapper= new JavaElementMapper(member);
45         ICompilationUnit unit= member.getCompilationUnit();
46         ASTParser parser= ASTParser.newParser(AST.JLS3);
47         parser.setSource(unit);
48         parser.setResolveBindings(true);
49         CompilationUnit node= (CompilationUnit) parser.createAST(null);
50         node.accept(mapper);
51         ASTNode result= mapper.fResult;
52         while(result != null && !type.isInstance(result)) {
53             result= result.getParent();
54         }
55         return result;
56     }
57     
58     protected boolean visitNode(ASTNode node) {
59         if (fResult != null) {
60             return false;
61         }
62         int nodeStart= node.getStartPosition();
63         int nodeLength= node.getLength();
64         int nodeEnd= nodeStart + nodeLength;
65         if (nodeStart == fStart && nodeLength == fLength) {
66             fResult= node;
67             return false;
68         } else if ( nodeStart <= fStart && fEnd <= nodeEnd) {
69             return true;
70         }
71         return false;
72     }
73 }
74
75
Popular Tags