KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > util > TightSourceRangeComputer


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 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
12 package org.eclipse.jdt.internal.corext.refactoring.util;
13
14 import java.util.HashSet JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.jdt.core.dom.ASTNode;
19 import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor;
20 import org.eclipse.jdt.core.dom.rewrite.TargetSourceRangeComputer;
21
22 public class TightSourceRangeComputer extends TargetSourceRangeComputer {
23     private HashSet JavaDoc/*<ASTNode>*/ fTightSourceRangeNodes= new HashSet JavaDoc();
24     
25     public void addTightSourceNode(ASTNode reference) {
26         fTightSourceRangeNodes.add(reference);
27         
28         List JavaDoc properties= reference.structuralPropertiesForType();
29         for (Iterator JavaDoc iterator= properties.iterator(); iterator.hasNext();) {
30             StructuralPropertyDescriptor descriptor= (StructuralPropertyDescriptor)iterator.next();
31             if (descriptor.isChildProperty()) {
32                 ASTNode child= (ASTNode)reference.getStructuralProperty(descriptor);
33                 if (isExtending(child, reference)) {
34                     addTightSourceNode(child);
35                 }
36             } else if (descriptor.isChildListProperty()) {
37                 List JavaDoc childs= (List JavaDoc)reference.getStructuralProperty(descriptor);
38                 for (Iterator JavaDoc iterator2= childs.iterator(); iterator2.hasNext();) {
39                     ASTNode child= (ASTNode)iterator2.next();
40                     if (isExtending(child, reference)) {
41                         addTightSourceNode(child);
42                     }
43                 }
44             }
45         }
46     }
47
48     public SourceRange computeSourceRange(ASTNode node) {
49         if (fTightSourceRangeNodes.contains(node)) {
50             return new TargetSourceRangeComputer.SourceRange(node.getStartPosition(), node.getLength());
51         } else {
52             return super.computeSourceRange(node); // see bug 85850
53
}
54     }
55     
56     private boolean isExtending(ASTNode child, ASTNode parent) {
57         SourceRange extendedRange= super.computeSourceRange(child);
58         
59         int parentStart= parent.getStartPosition();
60         int extendedStart= extendedRange.getStartPosition();
61         if (parentStart > extendedStart)
62             return true;
63         
64         int parentEnd= parentStart + parent.getLength();
65         int extendedEnd= extendedStart + extendedRange.getLength();
66         if (parentEnd < extendedEnd)
67             return true;
68
69         return false;
70     }
71 }
72
Popular Tags