KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > code > LocalTypeAnalyzer


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.refactoring.code;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.jdt.core.dom.ASTVisitor;
18 import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
19 import org.eclipse.jdt.core.dom.AnnotationTypeDeclaration;
20 import org.eclipse.jdt.core.dom.BodyDeclaration;
21 import org.eclipse.jdt.core.dom.EnumDeclaration;
22 import org.eclipse.jdt.core.dom.IBinding;
23 import org.eclipse.jdt.core.dom.ITypeBinding;
24 import org.eclipse.jdt.core.dom.SimpleName;
25 import org.eclipse.jdt.core.dom.TypeDeclaration;
26
27 import org.eclipse.jdt.internal.corext.dom.Selection;
28 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
29 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
30
31 public class LocalTypeAnalyzer extends ASTVisitor {
32
33     private Selection fSelection;
34     private List JavaDoc fTypeDeclarationsBefore= new ArrayList JavaDoc(2);
35     private List JavaDoc fTypeDeclarationsSelected= new ArrayList JavaDoc(2);
36     private String JavaDoc fBeforeTypeReferenced;
37     private String JavaDoc fSelectedTypeReferenced;
38
39     //---- Analyzing statements ----------------------------------------------------------------
40

41     public static RefactoringStatus perform(BodyDeclaration declaration, Selection selection) {
42         LocalTypeAnalyzer analyzer= new LocalTypeAnalyzer(selection);
43         declaration.accept(analyzer);
44         RefactoringStatus result= new RefactoringStatus();
45         analyzer.check(result);
46         return result;
47     }
48
49     private LocalTypeAnalyzer(Selection selection) {
50         fSelection= selection;
51     }
52
53     public boolean visit(SimpleName node) {
54         if (node.isDeclaration())
55             return true;
56         IBinding binding= node.resolveBinding();
57         if (binding instanceof ITypeBinding)
58             processLocalTypeBinding((ITypeBinding) binding, fSelection.getVisitSelectionMode(node));
59
60         return true;
61     }
62
63     public boolean visit(TypeDeclaration node) {
64         return visitType(node);
65     }
66
67     public boolean visit(AnnotationTypeDeclaration node) {
68         return visitType(node);
69     }
70
71     public boolean visit(EnumDeclaration node) {
72         return visitType(node);
73     }
74
75     private boolean visitType(AbstractTypeDeclaration node) {
76         int mode= fSelection.getVisitSelectionMode(node);
77         switch (mode) {
78             case Selection.BEFORE:
79                 fTypeDeclarationsBefore.add(node);
80                 break;
81             case Selection.SELECTED:
82                 fTypeDeclarationsSelected.add(node);
83                 break;
84         }
85         return true;
86     }
87
88     private void processLocalTypeBinding(ITypeBinding binding, int mode) {
89         switch (mode) {
90             case Selection.SELECTED:
91                 if (fBeforeTypeReferenced != null)
92                     break;
93                 if (checkBinding(fTypeDeclarationsBefore, binding))
94                     fBeforeTypeReferenced= RefactoringCoreMessages.LocalTypeAnalyzer_local_type_from_outside;
95                 break;
96             case Selection.AFTER:
97                 if (fSelectedTypeReferenced != null)
98                     break;
99                 if (checkBinding(fTypeDeclarationsSelected, binding))
100                     fSelectedTypeReferenced= RefactoringCoreMessages.LocalTypeAnalyzer_local_type_referenced_outside;
101                 break;
102         }
103     }
104     
105     private boolean checkBinding(List JavaDoc declarations, ITypeBinding binding) {
106         for (Iterator JavaDoc iter= declarations.iterator(); iter.hasNext();) {
107             AbstractTypeDeclaration declaration= (AbstractTypeDeclaration)iter.next();
108             if (declaration.resolveBinding() == binding) {
109                 return true;
110             }
111         }
112         return false;
113     }
114     
115     private void check(RefactoringStatus status) {
116         if (fBeforeTypeReferenced != null)
117             status.addFatalError(fBeforeTypeReferenced);
118         if (fSelectedTypeReferenced != null)
119             status.addFatalError(fSelectedTypeReferenced);
120     }
121 }
122
Popular Tags