Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2000, 2010 IBM Corporation and others. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html Contributors: IBM Corporation - initial API and implementation /
 
 package org.eclipse.jdt.internal.core.search.matching;
 
 import  org.eclipse.core.runtime.CoreException;
 
 
 public class PackageReferenceLocator extends PatternLocator {
 
 
 // check that referenced type is actually defined in this package fragment
 public static boolean isDeclaringPackageFragment(IPackageFragment packageFragmentReferenceBinding typeBinding) {
 	char[] fileName = typeBinding.getFileName();
 	if (fileName != null) {
 		// retrieve the actual file name from the full path (sources are generally only containing it already)
 		fileName = CharOperation.replaceOnCopy(fileName'/''\\'); // ensure to not do any side effect on file name (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=136016)
 		fileName = CharOperation.lastSegment(fileName'\\');
 
 		try {
 			switch (packageFragment.getKind()) {
 					if (!org.eclipse.jdt.internal.core.util.Util.isJavaLikeFileName(fileName) || !packageFragment.getCompilationUnit(new String(fileName)).exists()) {
 						return false// unit doesn't live in selected package
 					}
 					break;
 //					if (Util.isJavaFileName(fileName)) { // binary with attached source
 //						int length = fileName.length;
 //						System.arraycopy(fileName, 0, fileName = new char[length], 0, length - 4); // copy all but extension
 //						System.arraycopy(SuffixConstants.SUFFIX_class, 0, fileName, length - 4, 4);
 //					}
 					if (!Util.isClassFileName(fileName) || !packageFragment.getClassFile(new String(fileName)).exists()) {
 						return false// classfile doesn't live in selected package
 					}
 					break;
 			}
 		} catch(JavaModelException e) {
 			// unable to determine kind; tolerate this match
 		}
 	}
 	return true// by default, do not eliminate
 }
 
 	super(pattern);
 
 	this. = pattern;
 }
 public int match(Annotation nodeMatchingNodeSet nodeSet) {
 	return match(node.typenodeSet);
 }
 public int match(ASTNode nodeMatchingNodeSet nodeSet) { // interested in ImportReference
 	if (!(node instanceof ImportReference)) return ;
 
 	return nodeSet.addMatch(nodematchLevel((ImportReferencenode));
 }
 //public int match(ConstructorDeclaration node, MatchingNodeSet nodeSet) - SKIP IT
 //public int match(Expression node, MatchingNodeSet nodeSet) - SKIP IT
 //public int match(FieldDeclaration node, MatchingNodeSet nodeSet) - SKIP IT
 //public int match(MethodDeclaration node, MatchingNodeSet nodeSet) - SKIP IT
 //public int match(MessageSend node, MatchingNodeSet nodeSet) - SKIP IT
 public int match(Reference nodeMatchingNodeSet nodeSet) { // interested in QualifiedNameReference
 	if (!(node instanceof QualifiedNameReference)) return ;
 
 	return nodeSet.addMatch(nodematchLevelForTokens(((QualifiedNameReferencenode).));
 }
 //public int match(TypeDeclaration node, MatchingNodeSet nodeSet) - SKIP IT
 public int match(TypeReference nodeMatchingNodeSet nodeSet) { // interested in QualifiedTypeReference only
 	if (node instanceof JavadocSingleTypeReference) {
 		char[][] tokens = new char[][] { ((JavadocSingleTypeReferencenode). };
 		return nodeSet.addMatch(nodematchLevelForTokens(tokens));
 	}
 	if (!(node instanceof QualifiedTypeReference)) return ;
 	return nodeSet.addMatch(nodematchLevelForTokens(((QualifiedTypeReferencenode).));
 }
 
 protected int matchLevel(ImportReference importRef) {
 	return matchLevelForTokens(importRef.tokens);
 }
 protected int matchLevelForTokens(char[][] tokens) {
	if (this.. == nullreturn ;
	switch (this.) {
			if (CharOperation.prefixEquals(this.., CharOperation.concatWith(tokens'.'), this.)) {
			}
			break;
			char[] patternName = this..[this...length - 1] == '*'
				: CharOperation.concat(this..".*".toCharArray()); //$NON-NLS-1$
			if (CharOperation.match(patternName, CharOperation.concatWith(tokens'.'), this.)) {
			}
			break;
			// TODO (frederic) implement regular expression match
			break;
			char[] packageName = CharOperation.concatWith(tokens'.');
			if (CharOperation.camelCaseMatch(this..packageNamefalse)) {
			}
			// only test case insensitive as CamelCase already verified prefix case sensitive
			if (!this. && CharOperation.prefixEquals(this..packageNamefalse)) {
			}
			break;
			if (CharOperation.camelCaseMatch(this.., CharOperation.concatWith(tokens'.'), true)) {
			}
			break;
	}
/* (non-Javadoc)
 * @see org.eclipse.jdt.internal.core.search.matching.PatternLocator#matchLevelAndReportImportRef(org.eclipse.jdt.internal.compiler.ast.ImportReference, org.eclipse.jdt.internal.compiler.lookup.Binding, org.eclipse.jdt.internal.core.search.matching.MatchLocator)
 */
protected void matchLevelAndReportImportRef(ImportReference importRefBinding bindingMatchLocator locatorthrows CoreException {
	Binding refBinding = binding;
	if (importRef.isStatic()) {
		// for static import, binding can be a field binding or a member type binding
		// verify that in this case binding is static and use declaring class for fields
		if (binding instanceof FieldBinding) {
			FieldBinding fieldBinding = (FieldBindingbinding;
			if (!fieldBinding.isStatic()) return;
			refBinding = fieldBinding.declaringClass;
else if (binding instanceof MethodBinding) {
			MethodBinding methodBinding = (MethodBindingbinding;
			if (!methodBinding.isStatic()) return;
			refBinding = methodBinding.declaringClass;
else if (binding instanceof MemberTypeBinding) {
			MemberTypeBinding memberBinding = (MemberTypeBindingbinding;
			if (!memberBinding.isStatic()) return;
		}
	}
	super.matchLevelAndReportImportRef(importRefrefBindinglocator);
protected void matchReportImportRef(ImportReference importRefBinding bindingIJavaElement elementint accuracyMatchLocator locatorthrows CoreException {
	if (binding == null) {
		this.matchReportReference(importRefelementnull/*no binding*/accuracylocator);
else {
		if (locator.encloses(element)) {
			long[] positions = importRef.sourcePositions;
			int last = positions.length - 1;
			if (binding instanceof ProblemReferenceBinding)
				binding = ((ProblemReferenceBindingbinding).closestMatch();
			if (binding instanceof ReferenceBinding) {
				PackageBinding pkgBinding = ((ReferenceBindingbinding).;
				if (pkgBinding != null)
					last = pkgBinding.compoundName.length;
			}
			if (binding instanceof PackageBinding)
				last = ((PackageBindingbinding)..length;
			int start = (int) (positions[0] >>> 32);
			int end = (intpositions[last - 1];
			this. = locator.newPackageReferenceMatch(elementaccuracystartend-start+1, importRef);
			locator.report(this.);
		}
	}
protected void matchReportReference(ASTNode referenceIJavaElement elementBinding elementBindingint accuracyMatchLocator locatorthrows CoreException {
	matchReportReference(referenceelementnullnullelementBindingaccuracylocator);
protected void matchReportReference(ASTNode referenceIJavaElement elementIJavaElement localElementIJavaElement[] otherElementsBinding elementBindingint accuracyMatchLocator locatorthrows CoreException {
	long[] positions = null;
	int last = -1;
	if (reference instanceof ImportReference) {
		ImportReference importRef = (ImportReferencereference;
		positions = importRef.sourcePositions;
		last = (importRef.bits & .) != 0 ? positions.length : positions.length - 1;
else {
		TypeBinding typeBinding = null;
		if (reference instanceof QualifiedNameReference) {
			positions = qNameRef.sourcePositions;
			switch (qNameRef.bits & .) {
				case . : // reading a field
					typeBinding = qNameRef.actualReceiverType;
					break;
				case . : //=============only type ==============
					if (qNameRef.binding instanceof TypeBinding)
						typeBinding = (TypeBindingqNameRef.binding;
					break;
				case . : //============unbound cases===========
					Binding binding = qNameRef.binding;
					if (binding instanceof TypeBinding) {
						typeBinding = (TypeBindingbinding;
else if (binding instanceof ProblemFieldBinding) {
						typeBinding = qNameRef.actualReceiverType;
						last = qNameRef.tokens.length - (qNameRef.otherBindings == null ? 2 : qNameRef.otherBindings.length + 2);
else if (binding instanceof ProblemBinding) {
						ProblemBinding pbBinding = (ProblemBindingbinding;
						typeBinding = pbBinding.searchType;
						last = CharOperation.occurencesOf('.'pbBinding.name);
					}
					break;
			}
else if (reference instanceof QualifiedTypeReference) {
			positions = qTypeRef.sourcePositions;
			typeBinding = qTypeRef.resolvedType;
else if (reference instanceof JavadocSingleTypeReference) {
			positions = new long[1];
			positions[0] = (((long)jsTypeRef.sourceStart) << 32) + jsTypeRef.sourceEnd;
			typeBinding = jsTypeRef.resolvedType;
		}
		if (positions == nullreturn;
		if (typeBinding instanceof ArrayBinding)
			typeBinding = ((ArrayBindingtypeBinding).;
		if (typeBinding instanceof ProblemReferenceBinding)
			typeBinding = ((ProblemReferenceBindingtypeBinding).closestMatch();
		if (typeBinding instanceof ReferenceBinding) {
			PackageBinding pkgBinding = ((ReferenceBindingtypeBinding).;
			if (pkgBinding != null)
				last = pkgBinding.compoundName.length;
		}
		// Do not report qualified references which are only enclosing type
		// (see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=91078)
		ReferenceBinding enclosingType = typeBinding == null ? nulltypeBinding.enclosingType();
		if (enclosingType != null) {
			int length = positions.length;
			while (enclosingType != null && length > 0) {
				length--;
				enclosingType = enclosingType.enclosingType();
			}
			if (length <= 1) return;
		}
	}
	if (last == -1) {
		last = this...length;
	}
	if (last == 0) return;
	if (last > positions.lengthlast = positions.length;
	int sourceStart = (int) (positions[0] >>> 32);
	int sourceEnd = ((intpositions[last - 1]);
	PackageReferenceMatch packageReferenceMatch = locator.newPackageReferenceMatch(elementaccuracysourceStartsourceEnd-sourceStart+1, reference);
	packageReferenceMatch.setLocalElement(localElement);
	this. = packageReferenceMatch;
	locator.report(this.);
protected int referenceType() {
public int resolveLevel(ASTNode node) {
	if (node instanceof JavadocQualifiedTypeReference) {
		if (qualifRef.packageBinding != null)
			return resolveLevel(qualifRef.packageBinding);
		return resolveLevel(qualifRef.resolvedType);
	}
	if (node instanceof JavadocSingleTypeReference) {
		if (singleRef.packageBinding != null)
			return resolveLevel(singleRef.packageBinding);
	}
	if (node instanceof QualifiedTypeReference)
	if (node instanceof QualifiedNameReference)
		return this.resolveLevel((QualifiedNameReferencenode);
//	if (node instanceof ImportReference) - Not called when resolve is true, see MatchingNodeSet.reportMatching(unit)
public int resolveLevel(Binding binding) {
	if (binding == nullreturn ;
	char[][] compoundName = null;
	if (binding instanceof ImportBinding) {
		compoundName = ((ImportBindingbinding).;
else if (binding instanceof PackageBinding) {
		compoundName = ((PackageBindingbinding).;
else {
		if (binding instanceof ArrayBinding)
			binding = ((ArrayBindingbinding).;
		if (binding instanceof ProblemReferenceBinding)
			binding = ((ProblemReferenceBindingbinding).closestMatch();
		if (binding == nullreturn ;
		if (binding instanceof ReferenceBinding) {
			PackageBinding pkgBinding = ((ReferenceBindingbinding).;
			if (pkgBinding == nullreturn ;
			compoundName = pkgBinding.compoundName;
		}
	}
	if (compoundName != null && matchesName(this.., CharOperation.concatWith(compoundName'.'))) {
		if (this.. instanceof IPackageFragment && binding instanceof ReferenceBinding) {
			// check that type is located inside this instance of a package fragment
		}
	}
protected int resolveLevel(QualifiedNameReference qNameRef) {
	TypeBinding typeBinding = null;
	switch (qNameRef.bits & .) {
		case . : // reading a field
			if (qNameRef.tokens.length < (qNameRef.otherBindings == null ? 3 : qNameRef.otherBindings.length + 3))
				return // must be at least p1.A.x
			typeBinding = qNameRef.actualReceiverType;
			break;
		case . : // reading a local variable
			return // no package match in it
		case . : //=============only type ==============
			if (qNameRef.binding instanceof TypeBinding)
				typeBinding = (TypeBindingqNameRef.binding;
			break;
		/*
		 * Handling of unbound qualified name references. The match may reside in the resolved fragment,
		 * which is recorded inside the problem binding, along with the portion of the name until it became a problem.
		 */
		case . : //============unbound cases===========
			Binding binding = qNameRef.binding;
			if (binding instanceof ProblemReferenceBinding) {
				typeBinding = (TypeBindingbinding;
else if (binding instanceof ProblemFieldBinding) {
				if (qNameRef.tokens.length < (qNameRef.otherBindings == null ? 3 : qNameRef.otherBindings.length + 3))
					return // must be at least p1.A.x
				typeBinding = qNameRef.actualReceiverType;
else if (binding instanceof ProblemBinding) {
				ProblemBinding pbBinding = (ProblemBindingbinding;
				if (CharOperation.occurencesOf('.'pbBinding.name) <= 0) // index of last bound token is one before the pb token
				typeBinding = pbBinding.searchType;
			}
			break;
	}
	return resolveLevel(typeBinding);
public String toString() {
	return "Locator for " + this..toString(); //$NON-NLS-1$
New to GrepCode? Check out our FAQ X