KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > dev > jdt > TypeRefVisitor


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.dev.jdt;
17
18 import org.eclipse.jdt.internal.compiler.ASTVisitor;
19 import org.eclipse.jdt.internal.compiler.ast.ArrayQualifiedTypeReference;
20 import org.eclipse.jdt.internal.compiler.ast.ArrayTypeReference;
21 import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
22 import org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference;
23 import org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference;
24 import org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference;
25 import org.eclipse.jdt.internal.compiler.ast.SingleTypeReference;
26 import org.eclipse.jdt.internal.compiler.ast.Wildcard;
27 import org.eclipse.jdt.internal.compiler.lookup.ArrayBinding;
28 import org.eclipse.jdt.internal.compiler.lookup.BlockScope;
29 import org.eclipse.jdt.internal.compiler.lookup.ClassScope;
30 import org.eclipse.jdt.internal.compiler.lookup.CompilationUnitScope;
31 import org.eclipse.jdt.internal.compiler.lookup.Scope;
32 import org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding;
33 import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
34
35 /**
36  * Walks the AST to determine every location from which a type is referenced.
37  */

38 public abstract class TypeRefVisitor extends ASTVisitor {
39
40   public void endVisit(ArrayQualifiedTypeReference x, BlockScope scope) {
41     maybeDispatch(scope, x.resolvedType);
42   }
43
44   public void endVisit(ArrayQualifiedTypeReference x, ClassScope scope) {
45     maybeDispatch(scope, x.resolvedType);
46   }
47
48   public void endVisit(ArrayTypeReference x, BlockScope scope) {
49     maybeDispatch(scope, x.resolvedType);
50   }
51
52   public void endVisit(ArrayTypeReference x, ClassScope scope) {
53     maybeDispatch(scope, x.resolvedType);
54   }
55
56   public void endVisit(ParameterizedQualifiedTypeReference x, BlockScope scope) {
57     maybeDispatch(scope, x.resolvedType);
58   }
59
60   public void endVisit(ParameterizedQualifiedTypeReference x, ClassScope scope) {
61     maybeDispatch(scope, x.resolvedType);
62   }
63
64   public void endVisit(ParameterizedSingleTypeReference x, BlockScope scope) {
65     maybeDispatch(scope, x.resolvedType);
66   }
67
68   public void endVisit(ParameterizedSingleTypeReference x, ClassScope scope) {
69     maybeDispatch(scope, x.resolvedType);
70   }
71
72   public void endVisit(QualifiedTypeReference x, BlockScope scope) {
73     maybeDispatch(scope, x.resolvedType);
74   }
75
76   public void endVisit(QualifiedTypeReference x, ClassScope scope) {
77     maybeDispatch(scope, x.resolvedType);
78   }
79
80   public void endVisit(SingleTypeReference x, BlockScope scope) {
81     maybeDispatch(scope, x.resolvedType);
82   }
83
84   public void endVisit(SingleTypeReference x, ClassScope scope) {
85     maybeDispatch(scope, x.resolvedType);
86   }
87
88   public void endVisit(Wildcard x, BlockScope scope) {
89     maybeDispatch(scope, x.resolvedType);
90   }
91
92   public void endVisit(Wildcard x, ClassScope scope) {
93     maybeDispatch(scope, x.resolvedType);
94   }
95
96   protected abstract void onTypeRef(SourceTypeBinding referencedType,
97       CompilationUnitDeclaration unitOfReferrer);
98
99   private CompilationUnitScope findUnitScope(Scope referencedFrom) {
100     assert (referencedFrom != null);
101     Scope scope = referencedFrom;
102     while (scope.parent != null) {
103       scope = scope.parent;
104     }
105     assert (scope instanceof CompilationUnitScope);
106     return (CompilationUnitScope) scope;
107   }
108
109   private void maybeDispatch(Scope referencedFrom, TypeBinding binding) {
110     if (binding instanceof SourceTypeBinding) {
111       SourceTypeBinding type = (SourceTypeBinding) binding;
112       CompilationUnitScope from = findUnitScope(referencedFrom);
113       onTypeRef(type, from.referenceContext);
114     } else if (binding instanceof ArrayBinding) {
115       maybeDispatch(referencedFrom, ((ArrayBinding) binding).leafComponentType);
116     } else {
117       // We don't care about other cases.
118
}
119   }
120 }
121
Popular Tags