KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > SearchableEnvironmentRequestor


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.core;
12
13 import org.eclipse.jdt.core.IInitializer;
14 import org.eclipse.jdt.core.IJavaElement;
15 import org.eclipse.jdt.core.IJavaProject;
16 import org.eclipse.jdt.core.IPackageFragment;
17 import org.eclipse.jdt.core.IType;
18 import org.eclipse.jdt.core.JavaCore;
19 import org.eclipse.jdt.core.JavaModelException;
20 import org.eclipse.jdt.core.compiler.CharOperation;
21 import org.eclipse.jdt.internal.codeassist.ISearchRequestor;
22 import org.eclipse.jdt.internal.compiler.env.AccessRuleSet;
23 import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
24 import org.eclipse.jdt.internal.compiler.env.AccessRestriction;
25
26 /**
27  * Implements <code>IJavaElementRequestor</code>, wrappering and forwarding
28  * results onto a <code>org.eclipse.jdt.internal.codeassist.api.ISearchRequestor</code>.
29  */

30 class SearchableEnvironmentRequestor extends JavaElementRequestor {
31     /**
32      * The <code>ISearchRequestor</code> this JavaElementRequestor wraps
33      * and forwards results to.
34      */

35     protected ISearchRequestor requestor;
36     /**
37      * The <code>ICompilationUNit</code> this JavaElementRequestor will not
38      * accept types within.
39      */

40     protected ICompilationUnit unitToSkip;
41     
42     protected IJavaProject project;
43     
44     protected NameLookup nameLookup;
45     
46     protected boolean checkAccessRestrictions;
47 /**
48  * Constructs a SearchableEnvironmentRequestor that wraps the
49  * given SearchRequestor.
50  */

51 public SearchableEnvironmentRequestor(ISearchRequestor requestor) {
52     this.requestor = requestor;
53     this.unitToSkip= null;
54     this.project= null;
55     this.nameLookup= null;
56     this.checkAccessRestrictions = false;
57     
58 }
59 /**
60  * Constructs a SearchableEnvironmentRequestor that wraps the
61  * given SearchRequestor. The requestor will not accept types in
62  * the <code>unitToSkip</code>.
63  */

64 public SearchableEnvironmentRequestor(ISearchRequestor requestor, ICompilationUnit unitToSkip, IJavaProject project, NameLookup nameLookup) {
65     this.requestor = requestor;
66     this.unitToSkip= unitToSkip;
67     this.project= project;
68     this.nameLookup = nameLookup;
69     this.checkAccessRestrictions =
70         !JavaCore.IGNORE.equals(project.getOption(JavaCore.COMPILER_PB_FORBIDDEN_REFERENCE, true))
71         || !JavaCore.IGNORE.equals(project.getOption(JavaCore.COMPILER_PB_DISCOURAGED_REFERENCE, true));
72 }
73 /**
74  * Do nothing, a SearchRequestor does not accept initializers
75  * so there is no need to forward this results.
76  *
77  * @see IJavaElementRequestor
78  */

79 public void acceptInitializer(IInitializer initializer) {
80     // implements interface method
81
}
82 /**
83  * @see IJavaElementRequestor
84  */

85 public void acceptPackageFragment(IPackageFragment packageFragment) {
86     this.requestor.acceptPackage(packageFragment.getElementName().toCharArray());
87 }
88 /**
89  * @see IJavaElementRequestor
90  */

91 public void acceptType(IType type) {
92     try {
93         if (this.unitToSkip != null && this.unitToSkip.equals(type.getCompilationUnit())){
94             return;
95         }
96         char[] packageName = type.getPackageFragment().getElementName().toCharArray();
97         boolean isBinary = type instanceof BinaryType;
98         
99         // determine associated access restriction
100
AccessRestriction accessRestriction = null;
101         
102         if (this.checkAccessRestrictions && (isBinary || !type.getJavaProject().equals(this.project))) {
103             PackageFragmentRoot root = (PackageFragmentRoot)type.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
104             ClasspathEntry entry = (ClasspathEntry) this.nameLookup.rootToResolvedEntries.get(root);
105             if (entry != null) { // reverse map always contains resolved CP entry
106
AccessRuleSet accessRuleSet = entry.getAccessRuleSet();
107                 if (accessRuleSet != null) {
108                     // TODO (philippe) improve char[] <-> String conversions to avoid performing them on the fly
109
char[][] packageChars = CharOperation.splitOn('.', packageName);
110                     char[] fileWithoutExtension = type.getElementName().toCharArray();
111                     accessRestriction = accessRuleSet.getViolatedRestriction(CharOperation.concatWith(packageChars, fileWithoutExtension, '/'));
112                 }
113             }
114         }
115         this.requestor.acceptType(packageName, type.getElementName().toCharArray(), null, type.getFlags(), accessRestriction);
116     } catch (JavaModelException jme) {
117         // ignore
118
}
119 }
120 }
121
Popular Tags