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.resources.IResource;
 import  org.eclipse.core.runtime.CoreException;
 
 public class FieldLocator extends VariableLocator {
 
 
 public FieldLocator(FieldPattern pattern) {
 	super(pattern);
 
 }
 protected int fineGrain() {
 	return this..;
 }
 public int match(ASTNode nodeMatchingNodeSet nodeSet) {
 	int declarationsLevel = ;
 	if (this..) {
 		if (node instanceof ImportReference) {
 			// With static import, we can have static field reference in import reference
 			ImportReference importRef = (ImportReferencenode;
 			int length = importRef.tokens.length-1;
 			if (importRef.isStatic() && ((importRef.bits & .) == 0) && matchesName(this..importRef.tokens[length])) {
 				char[][] compoundName = new char[length][];
 				System.arraycopy(importRef.tokens, 0, compoundName, 0, length);
 				FieldPattern fieldPattern = (FieldPatternthis.;
 				char[] declaringType = CharOperation.concat(fieldPattern.declaringQualificationfieldPattern.declaringSimpleName'.');
 				if (matchesName(declaringType, CharOperation.concatWith(compoundName'.'))) {
 					declarationsLevel = this.. ?  : ;
 				}
 			}
 		}
 	}
 	return nodeSet.addMatch(nodedeclarationsLevel);
 }
 //public int match(ConstructorDeclaration node, MatchingNodeSet nodeSet) - SKIP IT
 public int match(FieldDeclaration nodeMatchingNodeSet nodeSet) {
 	int referencesLevel = ;
 		// must be a write only access with an initializer
 		if (this.. && !this.. && node.initialization != null)
 			if (matchesName(this..node.name))
 				referencesLevel = this.. ?  : ;
 
 	int declarationsLevel = ;
 	if (this..) {
 		switch (node.getKind()) {
 				if (matchesName(this..node.name))
 						declarationsLevel = this.. ?  : ;
 				break;
 		}
 	}
 	return nodeSet.addMatch(nodereferencesLevel >= declarationsLevel ? referencesLevel : declarationsLevel); // use the stronger match
 }
 //public int match(MethodDeclaration node, MatchingNodeSet nodeSet) - SKIP IT
 //public int match(MessageSend node, MatchingNodeSet nodeSet) - SKIP IT
 //public int match(TypeDeclaration node, MatchingNodeSet nodeSet) - SKIP IT
 //public int match(TypeReference node, MatchingNodeSet nodeSet) - SKIP IT
 
 protected int matchContainer() {
 	if (this.. || this.. != 0) {
 		// need to look everywhere to find in javadocs and static import
 		return ;
 	}
 	return ;
 }
 protected int matchField(FieldBinding fieldboolean matchName) {
 	if (field == nullreturn ;
 
 	if (matchName && !matchesName(this..field.readableName())) return ;
 
 	FieldPattern fieldPattern = (FieldPattern)this.;
 	ReferenceBinding receiverBinding = field.declaringClass;
 	if (receiverBinding == null) {
 		if (field == .)
 			// optimized case for length field of an array
			return fieldPattern.declaringQualification == null && fieldPattern.declaringSimpleName == null
	}
	// Note there is no dynamic lookup for field access
	int declaringLevel = resolveLevelForType(fieldPattern.declaringSimpleNamefieldPattern.declaringQualificationreceiverBinding);
	if (declaringLevel == return ;
	// look at field type only if declaring type is not specified
	if (fieldPattern.declaringSimpleName == nullreturn declaringLevel;
	// get real field binding
	FieldBinding fieldBinding = field;
	if (field instanceof ParameterizedFieldBinding) {
		fieldBinding = ((ParameterizedFieldBindingfield).;
	}
	int typeLevel = resolveLevelForType(fieldBinding.type);
	return declaringLevel > typeLevel ? typeLevel : declaringLevel// return the weaker match
/* (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)
 * Accept to report match of static field on static import
 */
protected void matchLevelAndReportImportRef(ImportReference importRefBinding bindingMatchLocator locatorthrows CoreException {
	if (importRef.isStatic() && binding instanceof FieldBinding) {
		super.matchLevelAndReportImportRef(importRefbindinglocator);
	}
protected int matchReference(Reference nodeMatchingNodeSet nodeSetboolean writeOnlyAccess) {
	if (node instanceof FieldReference) {
			return nodeSet.addMatch(nodethis.. ?  : );
	}
	return super.matchReference(nodenodeSetwriteOnlyAccess);
protected void matchReportReference(ASTNode referenceIJavaElement elementBinding elementBindingint accuracyMatchLocator locatorthrows CoreException {
	matchReportReference(referenceelementnullnullelementBindingaccuracylocator);
protected void matchReportReference(ASTNode referenceIJavaElement elementIJavaElement localElementIJavaElement[] otherElements,Binding elementBindingint accuracyMatchLocator locatorthrows CoreException {
		// need exact match to be able to open on type ref
		if (accuracy != .return;
		// element that references the field must be included in the enclosing element
		while (element != null && !declPattern.enclosingElement.equals(element))
			element = element.getParent();
		if (element != null) {
			if (reference instanceof FieldReference) {
				reportDeclaration(((FieldReferencereference).locatordeclPattern.knownFields);
else if (reference instanceof QualifiedNameReference) {
				Binding nameBinding = qNameRef.binding;
				if (nameBinding instanceof FieldBinding)
					reportDeclaration((FieldBinding)nameBindinglocatordeclPattern.knownFields);
				int otherMax = qNameRef.otherBindings == null ? 0 : qNameRef.otherBindings.length;
				for (int i = 0; i < otherMaxi++)
					reportDeclaration(qNameRef.otherBindings[i], locatordeclPattern.knownFields);
else if (reference instanceof SingleNameReference) {
				reportDeclaration((FieldBinding)((SingleNameReferencereference).locatordeclPattern.knownFields);
			}
		}
else if (reference instanceof ImportReference) {
		ImportReference importRef = (ImportReferencereference;
		long[] positions = importRef.sourcePositions;
		int lastIndex = importRef.tokens.length - 1;
		int start = (int) ((positions[lastIndex]) >>> 32);
		int end = (intpositions[lastIndex];
		this. = locator.newFieldReferenceMatch(elementlocalElementelementBindingaccuracystartend-start+1, importRef);
		locator.report(this.);
else if (reference instanceof FieldReference) {
		FieldReference fieldReference = (FieldReferencereference;
		long position = fieldReference.nameSourcePosition;
		int start = (int) (position >>> 32);
		int end = (intposition;
		this. = locator.newFieldReferenceMatch(elementlocalElementelementBindingaccuracystartend-start+1, fieldReference);
		locator.report(this.);
else if (reference instanceof SingleNameReference) {
		int offset = reference.sourceStart;
		this. = locator.newFieldReferenceMatch(elementlocalElementelementBindingaccuracyoffsetreference.sourceEnd-offset+1, reference);
		locator.report(this.);
else if (reference instanceof QualifiedNameReference) {
		int length = qNameRef.tokens.length;
		SearchMatch[] matches = new SearchMatch[length];
		Binding nameBinding = qNameRef.binding;
		int indexOfFirstFieldBinding = qNameRef.indexOfFirstFieldBinding > 0 ? qNameRef.indexOfFirstFieldBinding-1 : 0;
		// first token
		if (matchesName(this..qNameRef.tokens[indexOfFirstFieldBinding]) && !(nameBinding instanceof LocalVariableBinding)) {
			FieldBinding fieldBinding = nameBinding instanceof FieldBinding ? (FieldBindingnameBinding : null;
			if (fieldBinding == null) {
				matches[indexOfFirstFieldBinding] = locator.newFieldReferenceMatch(elementlocalElementelementBindingaccuracy, -1, -1, reference);
else {
				switch (matchField(fieldBindingfalse)) {
						matches[indexOfFirstFieldBinding] = locator.newFieldReferenceMatch(elementlocalElementelementBinding., -1, -1, reference);
						break;
						this. = locator.newFieldReferenceMatch(elementlocalElementelementBinding., -1, -1, reference);
						if (fieldBinding.type != null && fieldBinding.type.isParameterizedType() && this..hasTypeArguments()) {
							updateMatch((ParameterizedTypeBindingfieldBinding.typethis..getTypeArguments(), locator);
						}
						matches[indexOfFirstFieldBinding] = this.;
						break;
				}
			}
		}
		// other tokens
		for (int i = indexOfFirstFieldBinding+1; i < lengthi++) {
			char[] token = qNameRef.tokens[i];
			if (matchesName(this..token)) {
				FieldBinding otherBinding = qNameRef.otherBindings == null ? null : qNameRef.otherBindings[i-(indexOfFirstFieldBinding+1)];
				if (otherBinding == null) {
					matches[i] = locator.newFieldReferenceMatch(elementlocalElementelementBindingaccuracy, -1, -1, reference);
else {
					switch (matchField(otherBindingfalse)) {
							matches[i] = locator.newFieldReferenceMatch(elementlocalElementelementBinding., -1, -1, reference);
							break;
							this. = locator.newFieldReferenceMatch(elementlocalElementelementBinding., -1, -1, reference);
							if (otherBinding.type != null && otherBinding.type.isParameterizedType() && this..hasTypeArguments()) {
								updateMatch((ParameterizedTypeBindingotherBinding.typethis..getTypeArguments(), locator);
							}
							matches[i] = this.;
							break;
					}
				}
			}
		}
		locator.reportAccurateFieldReference(matchesqNameRef);
	}
/* (non-Javadoc)
 * Overridden to reject unexact matches.
 * @see org.eclipse.jdt.internal.core.search.matching.PatternLocator#updateMatch(org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding, char[][][], org.eclipse.jdt.internal.core.search.matching.MatchLocator)
 *
 */
protected void updateMatch(ParameterizedTypeBinding parameterizedBindingchar[][][] patternTypeArgumentsMatchLocator locator) {
	// We can only refine if locator has an unit scope.
	if (locator.unitScope == nullreturn;
	updateMatch(parameterizedBindingpatternTypeArgumentsfalse, 0, locator);
	if (!this..isExact()) {
		// cannot accept neither erasure nor compatible match
		this..setRule(0);
	}
protected void reportDeclaration(FieldBinding fieldBindingMatchLocator locatorSimpleSet knownFieldsthrows CoreException {
	// ignore length field
	if (fieldBinding == .return;
	ReferenceBinding declaringClass = fieldBinding.declaringClass;
	IType type = locator.lookupType(declaringClass);
	if (type == nullreturn// case of a secondary type
	char[] bindingName = fieldBinding.name;
	IField field = type.getField(new String(bindingName));
	if (knownFields.addIfNotIncluded(field) == nullreturn;
	IResource resource = type.getResource();
	boolean isBinary = type.isBinary();
	IBinaryType info = null;
	if (isBinary) {
		if (resource == null)
			resource = type.getJavaProject().getProject();
		info = locator.getBinaryInfo((org.eclipse.jdt.internal.core.ClassFiletype.getClassFile(), resource);
		locator.reportBinaryMemberDeclaration(resourcefieldfieldBindinginfo.);
else {
		if (declaringClass instanceof ParameterizedTypeBinding)
			declaringClass = ((ParameterizedTypeBindingdeclaringClass).genericType();
		ClassScope scope = ((SourceTypeBindingdeclaringClass).;
		if (scope != null) {
			TypeDeclaration typeDecl = scope.referenceContext;
			FieldDeclaration fieldDecl = null;
			FieldDeclaration[] fieldDecls = typeDecl.fields;
			int length = fieldDecls == null ? 0 : fieldDecls.length;
			for (int i = 0; i < lengthi++) {
				if (CharOperation.equals(bindingNamefieldDecls[i].)) {
					fieldDecl = fieldDecls[i];
					break;
				}
			}
			if (fieldDecl != null) {
				int offset = fieldDecl.sourceStart;
				this. = new FieldDeclarationMatch(((JavaElementfield).resolved(fieldBinding), .offsetfieldDecl.sourceEnd-offset+1, locator.getParticipant(), resource);
				locator.report(this.);
			}
		}
	}
protected int referenceType() {
public int resolveLevel(ASTNode possiblelMatchingNode) {
	if (this.. || this.. != 0) {
		if (possiblelMatchingNode instanceof FieldReference)
			return matchField(((FieldReferencepossiblelMatchingNode).true);
		else if (possiblelMatchingNode instanceof NameReference)
			return resolveLevel((NameReferencepossiblelMatchingNode);
	}
	if (possiblelMatchingNode instanceof FieldDeclaration)
		return matchField(((FieldDeclarationpossiblelMatchingNode).true);
public int resolveLevel(Binding binding) {
	if (binding == nullreturn ;
	if (!(binding instanceof FieldBinding)) return ;
	return matchField((FieldBindingbindingtrue);
protected int resolveLevel(NameReference nameRef) {
	if (nameRef instanceof SingleNameReference)
		return resolveLevel(nameRef.binding);
	Binding binding = nameRef.binding;
	FieldBinding fieldBinding = null;
	if (binding instanceof FieldBinding) {
		fieldBinding = (FieldBindingbinding;
		char[] bindingName = fieldBinding.name;
		int lastDot = CharOperation.lastIndexOf('.'bindingName);
		if (lastDot > -1)
			bindingName = CharOperation.subarray(bindingNamelastDot+1, bindingName.length);
		if (matchesName(this..bindingName)) {
			int level = matchField(fieldBindingfalse);
			if (level != return level;
		}
	}
	int otherMax = qNameRef.otherBindings == null ? 0 : qNameRef.otherBindings.length;
	for (int i = 0; i < otherMaxi++) {
		char[] token = qNameRef.tokens[i + qNameRef.indexOfFirstFieldBinding];
		if (matchesName(this..token)) {
			FieldBinding otherBinding = qNameRef.otherBindings[i];
			int level = matchField(otherBindingfalse);
			if (level != return level;
		}
	}
/* (non-Javadoc)
 * Resolve level for type with a given binding.
 */
protected int resolveLevelForType(TypeBinding typeBinding) {
	FieldPattern fieldPattern = (FieldPatternthis.;
	TypeBinding fieldTypeBinding = typeBinding;
	if (fieldTypeBinding != null && fieldTypeBinding.isParameterizedType()) {
		fieldTypeBinding = typeBinding.erasure();
	}
			fieldPattern.typeSimpleName,
			fieldPattern.typeQualification,
			fieldPattern.getTypeArguments(),
			0,
			fieldTypeBinding);
New to GrepCode? Check out our FAQ X