protected int resolveLevel(NameReference nameRef) { if (nameRef instanceof SingleNameReference) return resolveLevel(nameRef.binding); Binding binding = nameRef.binding; QualifiedNameReference qNameRef = (QualifiedNameReference) nameRef; FieldBinding fieldBinding = null; if (binding instanceof FieldBinding) { fieldBinding = (FieldBinding) binding; char[] bindingName = fieldBinding.name; int lastDot = CharOperation.lastIndexOf('.', bindingName); if (lastDot > -1) bindingName = CharOperation.subarray(bindingName, lastDot+1, bindingName.length); if (matchesName(this.pattern.name, bindingName)) { int level = matchField(fieldBinding, false); if (level != IMPOSSIBLE_MATCH) return level; } } int otherMax = qNameRef.otherBindings == null ? 0 : qNameRef.otherBindings.length; for (int i = 0; i < otherMax; i++) { char[] token = qNameRef.tokens[i + qNameRef.indexOfFirstFieldBinding]; if (matchesName(this.pattern.name, token)) { FieldBinding otherBinding = qNameRef.otherBindings[i]; int level = matchField(otherBinding, false); if (level != IMPOSSIBLE_MATCH) return level; } } return IMPOSSIBLE_MATCH; } /* (non-Javadoc)
@Override protected void matchReportReference(ASTNode reference, IJavaElement element, Binding elementBinding, int accuracy, MatchLocator locator) throws CoreException { matchReportReference(reference, element, null, null, elementBinding, accuracy, locator); } @Override
protected int matchField(FieldBinding field, boolean matchName) { if (field == null) return INACCURATE_MATCH; if (matchName && !matchesName(this.pattern.name, field.readableName())) return IMPOSSIBLE_MATCH; int declaringLevel = resolveLevelForType(fieldPattern.declaringSimpleName, fieldPattern.declaringQualification, receiverBinding); if (declaringLevel == IMPOSSIBLE_MATCH) return IMPOSSIBLE_MATCH; int typeLevel = resolveLevelForType(fieldBinding.type); return declaringLevel > typeLevel ? typeLevel : declaringLevel; // return the weaker match
public int resolveLevel(ASTNode possiblelMatchingNode) { if (this.pattern.findReferences || this.pattern.fineGrain != 0) { if (possiblelMatchingNode instanceof FieldReference) return matchField(((FieldReference) possiblelMatchingNode).binding, true); else if (possiblelMatchingNode instanceof NameReference) return resolveLevel((NameReference) possiblelMatchingNode); } if (possiblelMatchingNode instanceof FieldDeclaration) return matchField(((FieldDeclaration) possiblelMatchingNode).binding, true); return IMPOSSIBLE_MATCH; } public int resolveLevel(Binding binding) {
if (element != null) { if (reference instanceof FieldReference) { reportDeclaration(((FieldReference) reference).binding, locator, declPattern.knownFields); } else if (reference instanceof QualifiedNameReference) { QualifiedNameReference qNameRef = (QualifiedNameReference) reference; Binding nameBinding = qNameRef.binding; if (nameBinding instanceof FieldBinding) reportDeclaration((FieldBinding)nameBinding, locator, declPattern.knownFields); int otherMax = qNameRef.otherBindings == null ? 0 : qNameRef.otherBindings.length; for (int i = 0; i < otherMax; i++) reportDeclaration(qNameRef.otherBindings[i], locator, declPattern.knownFields); } else if (reference instanceof SingleNameReference) { reportDeclaration((FieldBinding)((SingleNameReference) reference).binding, locator, declPattern.knownFields); if (matchesName(this.pattern.name, qNameRef.tokens[indexOfFirstFieldBinding]) && !(nameBinding instanceof LocalVariableBinding)) { FieldBinding fieldBinding = nameBinding instanceof FieldBinding ? (FieldBinding) nameBinding : null; if (fieldBinding == null) { matches[indexOfFirstFieldBinding] = locator.newFieldReferenceMatch(element, localElement, elementBinding, accuracy, -1, -1, reference); } else { switch (matchField(fieldBinding, false)) { case ACCURATE_MATCH: matches[indexOfFirstFieldBinding] = locator.newFieldReferenceMatch(element, localElement, elementBinding, SearchMatch.A_ACCURATE, -1, -1, reference); this.match = locator.newFieldReferenceMatch(element, localElement, elementBinding, SearchMatch.A_INACCURATE, -1, -1, reference); if (fieldBinding.type != null && fieldBinding.type.isParameterizedType() && this.pattern.hasTypeArguments()) { updateMatch((ParameterizedTypeBinding) fieldBinding.type, this.pattern.getTypeArguments(), locator); if (matchesName(this.pattern.name, token)) { FieldBinding otherBinding = qNameRef.otherBindings == null ? null : qNameRef.otherBindings[i-(indexOfFirstFieldBinding+1)];
public int match(FieldDeclaration node, MatchingNodeSet nodeSet) { int referencesLevel = IMPOSSIBLE_MATCH; if (this.pattern.findReferences) // must be a write only access with an initializer if (this.pattern.writeAccess && !this.pattern.readAccess && node.initialization != null) if (matchesName(this.pattern.name, node.name)) referencesLevel = this.pattern.mustResolve ? POSSIBLE_MATCH : ACCURATE_MATCH; int declarationsLevel = IMPOSSIBLE_MATCH; if (this.pattern.findDeclarations) { switch (node.getKind()) { case AbstractVariableDeclaration.FIELD : case AbstractVariableDeclaration.ENUM_CONSTANT : if (matchesName(this.pattern.name, node.name)) if (matchesTypeReference(((FieldPattern)this.pattern).typeSimpleName, node.type)) declarationsLevel = this.pattern.mustResolve ? POSSIBLE_MATCH : ACCURATE_MATCH; break; } } return nodeSet.addMatch(node, referencesLevel >= declarationsLevel ? referencesLevel : declarationsLevel); // use the stronger match } //public int match(MethodDeclaration node, MatchingNodeSet nodeSet) - SKIP IT
@Override protected int matchReference(Reference node, MatchingNodeSet nodeSet, boolean writeOnlyAccess) { if (node instanceof FieldReference) { if (matchesName(this.pattern.name, ((FieldReference) node).token)) return nodeSet.addMatch(node, this.pattern.mustResolve ? POSSIBLE_MATCH : ACCURATE_MATCH); return IMPOSSIBLE_MATCH; } return super.matchReference(node, nodeSet, writeOnlyAccess); } @Override
@Override protected void updateMatch(ParameterizedTypeBinding parameterizedBinding, char[][][] patternTypeArguments, MatchLocator locator) { // We can only refine if locator has an unit scope. if (locator.unitScope == null) return; updateMatch(parameterizedBinding, patternTypeArguments, false, 0, locator); if (!this.match.isExact()) { // cannot accept neither erasure nor compatible match this.match.setRule(0); } } protected void reportDeclaration(FieldBinding fieldBinding, MatchLocator locator, SimpleSet knownFields) throws CoreException {
@Override public int resolveLevel(Binding binding) { if (binding == null) return INACCURATE_MATCH; if (!(binding instanceof FieldBinding)) return IMPOSSIBLE_MATCH; return matchField((FieldBinding) binding, true); } protected int resolveLevel(NameReference nameRef) {
protected int resolveLevelForType(TypeBinding typeBinding) { FieldPattern fieldPattern = (FieldPattern) this.pattern; TypeBinding fieldTypeBinding = typeBinding; if (fieldTypeBinding != null && fieldTypeBinding.isParameterizedType()) { fieldTypeBinding = typeBinding.erasure(); } return resolveLevelForType( fieldPattern.typeSimpleName, fieldPattern.typeQualification, fieldPattern.getTypeArguments(), 0, fieldTypeBinding); } }
public static PatternLocator patternLocator(SearchPattern pattern) { switch (pattern.kind) { case IIndexConstants.PKG_REF_PATTERN : return new PackageReferenceLocator((PackageReferencePattern) pattern); case IIndexConstants.PKG_DECL_PATTERN : return new PackageDeclarationLocator((PackageDeclarationPattern) pattern); case IIndexConstants.TYPE_REF_PATTERN : return new TypeReferenceLocator((TypeReferencePattern) pattern); case IIndexConstants.TYPE_DECL_PATTERN : return new TypeDeclarationLocator((TypeDeclarationPattern) pattern); case IIndexConstants.SUPER_REF_PATTERN : return new SuperTypeReferenceLocator((SuperTypeReferencePattern) pattern); case IIndexConstants.CONSTRUCTOR_PATTERN : return new ConstructorLocator((ConstructorPattern) pattern); case IIndexConstants.FIELD_PATTERN : return new FieldLocator((FieldPattern) pattern); case IIndexConstants.METHOD_PATTERN : return new MethodLocator((MethodPattern) pattern); case IIndexConstants.OR_PATTERN : return new OrLocator((OrPattern) pattern); case IIndexConstants.AND_PATTERN : return new AndLocator((AndPattern) pattern); case IIndexConstants.LOCAL_VAR_PATTERN : return new LocalVariableLocator((LocalVariablePattern) pattern); case IIndexConstants.TYPE_PARAM_PATTERN: return new TypeParameterLocator((TypeParameterPattern) pattern); } return null; } public static char[] qualifiedPattern(char[] simpleNamePattern, char[] qualificationPattern) {
if (element != null) { if (reference instanceof FieldReference) { reportDeclaration(((FieldReference) reference).binding, locator, declPattern.knownFields); } else if (reference instanceof QualifiedNameReference) { QualifiedNameReference qNameRef = (QualifiedNameReference) reference; Binding nameBinding = qNameRef.binding; if (nameBinding instanceof FieldBinding) reportDeclaration((FieldBinding)nameBinding, locator, declPattern.knownFields); int otherMax = qNameRef.otherBindings == null ? 0 : qNameRef.otherBindings.length; for (int i = 0; i < otherMax; i++) reportDeclaration(qNameRef.otherBindings[i], locator, declPattern.knownFields); } else if (reference instanceof SingleNameReference) { reportDeclaration((FieldBinding)((SingleNameReference) reference).binding, locator, declPattern.knownFields); if (matchesName(this.pattern.name, qNameRef.tokens[indexOfFirstFieldBinding]) && !(nameBinding instanceof LocalVariableBinding)) { FieldBinding fieldBinding = nameBinding instanceof FieldBinding ? (FieldBinding) nameBinding : null; if (fieldBinding == null) { matches[indexOfFirstFieldBinding] = locator.newFieldReferenceMatch(element, localElement, elementBinding, accuracy, -1, -1, reference); } else { switch (matchField(fieldBinding, false)) { case ACCURATE_MATCH: matches[indexOfFirstFieldBinding] = locator.newFieldReferenceMatch(element, localElement, elementBinding, SearchMatch.A_ACCURATE, -1, -1, reference); this.match = locator.newFieldReferenceMatch(element, localElement, elementBinding, SearchMatch.A_INACCURATE, -1, -1, reference); if (fieldBinding.type != null && fieldBinding.type.isParameterizedType() && this.pattern.hasTypeArguments()) { updateMatch((ParameterizedTypeBinding) fieldBinding.type, this.pattern.getTypeArguments(), locator); if (matchesName(this.pattern.name, token)) { FieldBinding otherBinding = qNameRef.otherBindings == null ? null : qNameRef.otherBindings[i-(indexOfFirstFieldBinding+1)];
public int resolveLevel(ASTNode possiblelMatchingNode) { if (this.pattern.findReferences || this.pattern.fineGrain != 0) { if (possiblelMatchingNode instanceof FieldReference) return matchField(((FieldReference) possiblelMatchingNode).binding, true); else if (possiblelMatchingNode instanceof NameReference) return resolveLevel((NameReference) possiblelMatchingNode); } if (possiblelMatchingNode instanceof FieldDeclaration) return matchField(((FieldDeclaration) possiblelMatchingNode).binding, true); return IMPOSSIBLE_MATCH; } public int resolveLevel(Binding binding) {
public int match(FieldDeclaration node, MatchingNodeSet nodeSet) { int referencesLevel = IMPOSSIBLE_MATCH; if (this.pattern.findReferences) // must be a write only access with an initializer if (this.pattern.writeAccess && !this.pattern.readAccess && node.initialization != null) if (matchesName(this.pattern.name, node.name)) referencesLevel = this.pattern.mustResolve ? POSSIBLE_MATCH : ACCURATE_MATCH; int declarationsLevel = IMPOSSIBLE_MATCH; if (this.pattern.findDeclarations) { switch (node.getKind()) { case AbstractVariableDeclaration.FIELD : case AbstractVariableDeclaration.ENUM_CONSTANT : if (matchesName(this.pattern.name, node.name)) if (matchesTypeReference(((FieldPattern)this.pattern).typeSimpleName, node.type)) declarationsLevel = this.pattern.mustResolve ? POSSIBLE_MATCH : ACCURATE_MATCH; break; } } return nodeSet.addMatch(node, referencesLevel >= declarationsLevel ? referencesLevel : declarationsLevel); // use the stronger match } //public int match(MethodDeclaration node, MatchingNodeSet nodeSet) - SKIP IT
protected int matchReference(Reference node, MatchingNodeSet nodeSet, boolean writeOnlyAccess) { if (node instanceof FieldReference) { if (matchesName(this.pattern.name, ((FieldReference) node).token)) return nodeSet.addMatch(node, this.pattern.mustResolve ? POSSIBLE_MATCH : ACCURATE_MATCH); return IMPOSSIBLE_MATCH; } return super.matchReference(node, nodeSet, writeOnlyAccess); } protected void matchReportReference(ASTNode reference, IJavaElement element, Binding elementBinding, int accuracy, MatchLocator locator) throws CoreException {
protected void updateMatch(ParameterizedTypeBinding parameterizedBinding, char[][][] patternTypeArguments, MatchLocator locator) { // We can only refine if locator has an unit scope. if (locator.unitScope == null) return; updateMatch(parameterizedBinding, patternTypeArguments, false, 0, locator); if (!this.match.isExact()) { // cannot accept neither erasure nor compatible match this.match.setRule(0); } } protected void reportDeclaration(FieldBinding fieldBinding, MatchLocator locator, SimpleSet knownFields) throws CoreException {
public int resolveLevel(Binding binding) { if (binding == null) return INACCURATE_MATCH; if (!(binding instanceof FieldBinding)) return IMPOSSIBLE_MATCH; return matchField((FieldBinding) binding, true); } protected int resolveLevel(NameReference nameRef) {
protected int resolveLevelForType(TypeBinding typeBinding) { FieldPattern fieldPattern = (FieldPattern) this.pattern; TypeBinding fieldTypeBinding = typeBinding; if (fieldTypeBinding != null && fieldTypeBinding.isParameterizedType()) { fieldTypeBinding = typeBinding.erasure(); } return resolveLevelForType( fieldPattern.typeSimpleName, fieldPattern.typeQualification, fieldPattern.getTypeArguments(), 0, fieldTypeBinding); } }
public static PatternLocator patternLocator(SearchPattern pattern) { switch (pattern.kind) { case IIndexConstants.PKG_REF_PATTERN : return new PackageReferenceLocator((PackageReferencePattern) pattern); case IIndexConstants.PKG_DECL_PATTERN : return new PackageDeclarationLocator((PackageDeclarationPattern) pattern); case IIndexConstants.TYPE_REF_PATTERN : return new TypeReferenceLocator((TypeReferencePattern) pattern); case IIndexConstants.TYPE_DECL_PATTERN : return new TypeDeclarationLocator((TypeDeclarationPattern) pattern); case IIndexConstants.SUPER_REF_PATTERN : return new SuperTypeReferenceLocator((SuperTypeReferencePattern) pattern); case IIndexConstants.CONSTRUCTOR_PATTERN : return new ConstructorLocator((ConstructorPattern) pattern); case IIndexConstants.FIELD_PATTERN : return new FieldLocator((FieldPattern) pattern); case IIndexConstants.METHOD_PATTERN : return new MethodLocator((MethodPattern) pattern); case IIndexConstants.OR_PATTERN : return new OrLocator((OrPattern) pattern); case IIndexConstants.AND_PATTERN : return new AndLocator((AndPattern) pattern); case IIndexConstants.LOCAL_VAR_PATTERN : return new LocalVariableLocator((LocalVariablePattern) pattern); case IIndexConstants.TYPE_PARAM_PATTERN: return new TypeParameterLocator((TypeParameterPattern) pattern); } return null; } public static char[] qualifiedPattern(char[] simpleNamePattern, char[] qualificationPattern) {
if (element != null) { if (reference instanceof FieldReference) { reportDeclaration(((FieldReference) reference).binding, locator, declPattern.knownFields); } else if (reference instanceof QualifiedNameReference) { QualifiedNameReference qNameRef = (QualifiedNameReference) reference; Binding nameBinding = qNameRef.binding; if (nameBinding instanceof FieldBinding) reportDeclaration((FieldBinding)nameBinding, locator, declPattern.knownFields); int otherMax = qNameRef.otherBindings == null ? 0 : qNameRef.otherBindings.length; for (int i = 0; i < otherMax; i++) reportDeclaration(qNameRef.otherBindings[i], locator, declPattern.knownFields); } else if (reference instanceof SingleNameReference) { reportDeclaration((FieldBinding)((SingleNameReference) reference).binding, locator, declPattern.knownFields); if (matchesName(this.pattern.name, qNameRef.tokens[indexOfFirstFieldBinding]) && !(nameBinding instanceof LocalVariableBinding)) { FieldBinding fieldBinding = nameBinding instanceof FieldBinding ? (FieldBinding) nameBinding : null; if (fieldBinding == null) { matches[indexOfFirstFieldBinding] = locator.newFieldReferenceMatch(element, localElement, elementBinding, accuracy, -1, -1, reference); } else { switch (matchField(fieldBinding, false)) { case ACCURATE_MATCH: matches[indexOfFirstFieldBinding] = locator.newFieldReferenceMatch(element, localElement, elementBinding, SearchMatch.A_ACCURATE, -1, -1, reference); this.match = locator.newFieldReferenceMatch(element, localElement, elementBinding, SearchMatch.A_INACCURATE, -1, -1, reference); if (fieldBinding.type != null && fieldBinding.type.isParameterizedType() && this.pattern.hasTypeArguments()) { updateMatch((ParameterizedTypeBinding) fieldBinding.type, this.pattern.getTypeArguments(), locator); if (matchesName(this.pattern.name, token)) { FieldBinding otherBinding = qNameRef.otherBindings == null ? null : qNameRef.otherBindings[i-(indexOfFirstFieldBinding+1)];