001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.core;
011:
012: import org.eclipse.jdt.core.IInitializer;
013: import org.eclipse.jdt.core.IJavaElement;
014: import org.eclipse.jdt.core.IJavaProject;
015: import org.eclipse.jdt.core.IPackageFragment;
016: import org.eclipse.jdt.core.IType;
017: import org.eclipse.jdt.core.JavaCore;
018: import org.eclipse.jdt.core.JavaModelException;
019: import org.eclipse.jdt.core.compiler.CharOperation;
020: import org.eclipse.jdt.internal.codeassist.ISearchRequestor;
021: import org.eclipse.jdt.internal.compiler.env.AccessRuleSet;
022: import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
023: import org.eclipse.jdt.internal.compiler.env.AccessRestriction;
024:
025: /**
026: * Implements <code>IJavaElementRequestor</code>, wrappering and forwarding
027: * results onto a <code>org.eclipse.jdt.internal.codeassist.api.ISearchRequestor</code>.
028: */
029: class SearchableEnvironmentRequestor extends JavaElementRequestor {
030: /**
031: * The <code>ISearchRequestor</code> this JavaElementRequestor wraps
032: * and forwards results to.
033: */
034: protected ISearchRequestor requestor;
035: /**
036: * The <code>ICompilationUNit</code> this JavaElementRequestor will not
037: * accept types within.
038: */
039: protected ICompilationUnit unitToSkip;
040:
041: protected IJavaProject project;
042:
043: protected NameLookup nameLookup;
044:
045: protected boolean checkAccessRestrictions;
046:
047: /**
048: * Constructs a SearchableEnvironmentRequestor that wraps the
049: * given SearchRequestor.
050: */
051: public SearchableEnvironmentRequestor(ISearchRequestor requestor) {
052: this .requestor = requestor;
053: this .unitToSkip = null;
054: this .project = null;
055: this .nameLookup = null;
056: this .checkAccessRestrictions = false;
057:
058: }
059:
060: /**
061: * Constructs a SearchableEnvironmentRequestor that wraps the
062: * given SearchRequestor. The requestor will not accept types in
063: * the <code>unitToSkip</code>.
064: */
065: public SearchableEnvironmentRequestor(ISearchRequestor requestor,
066: ICompilationUnit unitToSkip, IJavaProject project,
067: NameLookup nameLookup) {
068: this .requestor = requestor;
069: this .unitToSkip = unitToSkip;
070: this .project = project;
071: this .nameLookup = nameLookup;
072: this .checkAccessRestrictions = !JavaCore.IGNORE.equals(project
073: .getOption(JavaCore.COMPILER_PB_FORBIDDEN_REFERENCE,
074: true))
075: || !JavaCore.IGNORE.equals(project.getOption(
076: JavaCore.COMPILER_PB_DISCOURAGED_REFERENCE,
077: true));
078: }
079:
080: /**
081: * Do nothing, a SearchRequestor does not accept initializers
082: * so there is no need to forward this results.
083: *
084: * @see IJavaElementRequestor
085: */
086: public void acceptInitializer(IInitializer initializer) {
087: // implements interface method
088: }
089:
090: /**
091: * @see IJavaElementRequestor
092: */
093: public void acceptPackageFragment(IPackageFragment packageFragment) {
094: this .requestor.acceptPackage(packageFragment.getElementName()
095: .toCharArray());
096: }
097:
098: /**
099: * @see IJavaElementRequestor
100: */
101: public void acceptType(IType type) {
102: try {
103: if (this .unitToSkip != null
104: && this .unitToSkip
105: .equals(type.getCompilationUnit())) {
106: return;
107: }
108: char[] packageName = type.getPackageFragment()
109: .getElementName().toCharArray();
110: boolean isBinary = type instanceof BinaryType;
111:
112: // determine associated access restriction
113: AccessRestriction accessRestriction = null;
114:
115: if (this .checkAccessRestrictions
116: && (isBinary || !type.getJavaProject().equals(
117: this .project))) {
118: PackageFragmentRoot root = (PackageFragmentRoot) type
119: .getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
120: ClasspathEntry entry = (ClasspathEntry) this .nameLookup.rootToResolvedEntries
121: .get(root);
122: if (entry != null) { // reverse map always contains resolved CP entry
123: AccessRuleSet accessRuleSet = entry
124: .getAccessRuleSet();
125: if (accessRuleSet != null) {
126: // TODO (philippe) improve char[] <-> String conversions to avoid performing them on the fly
127: char[][] packageChars = CharOperation.splitOn(
128: '.', packageName);
129: char[] fileWithoutExtension = type
130: .getElementName().toCharArray();
131: accessRestriction = accessRuleSet
132: .getViolatedRestriction(CharOperation
133: .concatWith(packageChars,
134: fileWithoutExtension,
135: '/'));
136: }
137: }
138: }
139: this .requestor.acceptType(packageName, type
140: .getElementName().toCharArray(), null, type
141: .getFlags(), accessRestriction);
142: } catch (JavaModelException jme) {
143: // ignore
144: }
145: }
146: }
|