]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/jdt-before/core refactoring/org/eclipse/jdt/internal/corext/refactoring/code/LocalTypeAnalyzer.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / core refactoring / org / eclipse / jdt / internal / corext / refactoring / code / LocalTypeAnalyzer.java
CommitLineData
1b2798f6
EK
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 *******************************************************************************/
11package org.eclipse.jdt.internal.corext.refactoring.code;
12
13import java.util.ArrayList;
14import java.util.Iterator;
15import java.util.List;
16
17import org.eclipse.ltk.core.refactoring.RefactoringStatus;
18
19import org.eclipse.jdt.core.dom.ASTVisitor;
20import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
21import org.eclipse.jdt.core.dom.AnnotationTypeDeclaration;
22import org.eclipse.jdt.core.dom.BodyDeclaration;
23import org.eclipse.jdt.core.dom.EnumDeclaration;
24import org.eclipse.jdt.core.dom.IBinding;
25import org.eclipse.jdt.core.dom.ITypeBinding;
26import org.eclipse.jdt.core.dom.SimpleName;
27import org.eclipse.jdt.core.dom.TypeDeclaration;
28
29import org.eclipse.jdt.internal.corext.dom.Selection;
30import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
31
32public class LocalTypeAnalyzer extends ASTVisitor {
33
34 private Selection fSelection;
35 private List<AbstractTypeDeclaration> fTypeDeclarationsBefore= new ArrayList<AbstractTypeDeclaration>(2);
36 private List<AbstractTypeDeclaration> fTypeDeclarationsSelected= new ArrayList<AbstractTypeDeclaration>(2);
37 private String fBeforeTypeReferenced;
38 private String fSelectedTypeReferenced;
39
40 //---- Analyzing statements ----------------------------------------------------------------
41
42 public static RefactoringStatus perform(BodyDeclaration declaration, Selection selection) {
43 LocalTypeAnalyzer analyzer= new LocalTypeAnalyzer(selection);
44 declaration.accept(analyzer);
45 RefactoringStatus result= new RefactoringStatus();
46 analyzer.check(result);
47 return result;
48 }
49
50 private LocalTypeAnalyzer(Selection selection) {
51 fSelection= selection;
52 }
53
54 @Override
55 public boolean visit(SimpleName node) {
56 if (node.isDeclaration())
57 return true;
58 IBinding binding= node.resolveBinding();
59 if (binding instanceof ITypeBinding)
60 processLocalTypeBinding((ITypeBinding) binding, fSelection.getVisitSelectionMode(node));
61
62 return true;
63 }
64
65 @Override
66 public boolean visit(TypeDeclaration node) {
67 return visitType(node);
68 }
69
70 @Override
71 public boolean visit(AnnotationTypeDeclaration node) {
72 return visitType(node);
73 }
74
75 @Override
76 public boolean visit(EnumDeclaration node) {
77 return visitType(node);
78 }
79
80 private boolean visitType(AbstractTypeDeclaration node) {
81 int mode= fSelection.getVisitSelectionMode(node);
82 switch (mode) {
83 case Selection.BEFORE:
84 fTypeDeclarationsBefore.add(node);
85 break;
86 case Selection.SELECTED:
87 fTypeDeclarationsSelected.add(node);
88 break;
89 }
90 return true;
91 }
92
93 private void processLocalTypeBinding(ITypeBinding binding, int mode) {
94 switch (mode) {
95 case Selection.SELECTED:
96 if (fBeforeTypeReferenced != null)
97 break;
98 if (checkBinding(fTypeDeclarationsBefore, binding))
99 fBeforeTypeReferenced= RefactoringCoreMessages.LocalTypeAnalyzer_local_type_from_outside;
100 break;
101 case Selection.AFTER:
102 if (fSelectedTypeReferenced != null)
103 break;
104 if (checkBinding(fTypeDeclarationsSelected, binding))
105 fSelectedTypeReferenced= RefactoringCoreMessages.LocalTypeAnalyzer_local_type_referenced_outside;
106 break;
107 }
108 }
109
110 private boolean checkBinding(List<AbstractTypeDeclaration> declarations, ITypeBinding binding) {
111 for (Iterator<AbstractTypeDeclaration> iter= declarations.iterator(); iter.hasNext();) {
112 AbstractTypeDeclaration declaration= iter.next();
113 if (declaration.resolveBinding() == binding) {
114 return true;
115 }
116 }
117 return false;
118 }
119
120 private void check(RefactoringStatus status) {
121 if (fBeforeTypeReferenced != null)
122 status.addFatalError(fBeforeTypeReferenced);
123 if (fSelectedTypeReferenced != null)
124 status.addFatalError(fSelectedTypeReferenced);
125 }
126}