001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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.codeassist.select;
011:
012: /*
013: * Selection node build by the parser in any case it was intending to
014: * reduce a message send containing the cursor.
015: * e.g.
016: *
017: * class X {
018: * void foo() {
019: * this.[start]bar[end](1, 2)
020: * }
021: * }
022: *
023: * ---> class X {
024: * void foo() {
025: * <SelectOnMessageSend:this.bar(1, 2)>
026: * }
027: * }
028: *
029: */
030:
031: import org.eclipse.jdt.internal.compiler.ast.MessageSend;
032: import org.eclipse.jdt.internal.compiler.lookup.Binding;
033: import org.eclipse.jdt.internal.compiler.lookup.BlockScope;
034: import org.eclipse.jdt.internal.compiler.lookup.MethodBinding;
035: import org.eclipse.jdt.internal.compiler.lookup.ProblemReasons;
036: import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
037: import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
038:
039: public class SelectionOnMessageSend extends MessageSend {
040:
041: /*
042: * Cannot answer default abstract match, iterate in superinterfaces of declaring class
043: * for a better match (default abstract match came from scope lookups).
044: */
045: private MethodBinding findNonDefaultAbstractMethod(
046: MethodBinding methodBinding) {
047:
048: ReferenceBinding[] itsInterfaces = methodBinding.declaringClass
049: .super Interfaces();
050: if (itsInterfaces != Binding.NO_SUPERINTERFACES) {
051: ReferenceBinding[] interfacesToVisit = itsInterfaces;
052: int nextPosition = interfacesToVisit.length;
053:
054: for (int i = 0; i < nextPosition; i++) {
055: ReferenceBinding currentType = interfacesToVisit[i];
056: MethodBinding[] methods = currentType
057: .getMethods(methodBinding.selector);
058: if (methods != null) {
059: for (int k = 0; k < methods.length; k++) {
060: if (methodBinding
061: .areParametersEqual(methods[k]))
062: return methods[k];
063: }
064: }
065:
066: if ((itsInterfaces = currentType.super Interfaces()) != Binding.NO_SUPERINTERFACES) {
067: int itsLength = itsInterfaces.length;
068: if (nextPosition + itsLength >= interfacesToVisit.length)
069: System
070: .arraycopy(
071: interfacesToVisit,
072: 0,
073: interfacesToVisit = new ReferenceBinding[nextPosition
074: + itsLength + 5], 0,
075: nextPosition);
076: nextInterface: for (int a = 0; a < itsLength; a++) {
077: ReferenceBinding next = itsInterfaces[a];
078: for (int b = 0; b < nextPosition; b++)
079: if (next == interfacesToVisit[b])
080: continue nextInterface;
081: interfacesToVisit[nextPosition++] = next;
082: }
083: }
084: }
085: }
086: return methodBinding;
087: }
088:
089: public StringBuffer printExpression(int indent, StringBuffer output) {
090:
091: output.append("<SelectOnMessageSend:"); //$NON-NLS-1$
092: if (!receiver.isImplicitThis())
093: receiver.printExpression(0, output).append('.');
094: output.append(this .selector).append('(');
095: if (arguments != null) {
096: for (int i = 0; i < arguments.length; i++) {
097: if (i > 0)
098: output.append(", "); //$NON-NLS-1$
099: arguments[i].printExpression(0, output);
100: }
101: }
102: return output.append(")>"); //$NON-NLS-1$
103: }
104:
105: public TypeBinding resolveType(BlockScope scope) {
106:
107: super .resolveType(scope);
108:
109: // tolerate some error cases
110: if (binding == null
111: || !(binding.isValidBinding()
112: || binding.problemId() == ProblemReasons.NotVisible
113: || binding.problemId() == ProblemReasons.InheritedNameHidesEnclosingName
114: || binding.problemId() == ProblemReasons.NonStaticReferenceInConstructorInvocation || binding
115: .problemId() == ProblemReasons.NonStaticReferenceInStaticContext)) {
116: throw new SelectionNodeFound();
117: } else {
118: if (binding.isDefaultAbstract()) {
119: throw new SelectionNodeFound(
120: findNonDefaultAbstractMethod(binding)); // 23594
121: } else {
122: throw new SelectionNodeFound(binding);
123: }
124: }
125: }
126: }
|