KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > codeassist > select > SelectionOnMessageSend


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.codeassist.select;
12
13 /*
14  * Selection node build by the parser in any case it was intending to
15  * reduce a message send containing the cursor.
16  * e.g.
17  *
18  * class X {
19  * void foo() {
20  * this.[start]bar[end](1, 2)
21  * }
22  * }
23  *
24  * ---> class X {
25  * void foo() {
26  * <SelectOnMessageSend:this.bar(1, 2)>
27  * }
28  * }
29  *
30  */

31
32 import org.eclipse.jdt.internal.compiler.ast.MessageSend;
33 import org.eclipse.jdt.internal.compiler.lookup.Binding;
34 import org.eclipse.jdt.internal.compiler.lookup.BlockScope;
35 import org.eclipse.jdt.internal.compiler.lookup.MethodBinding;
36 import org.eclipse.jdt.internal.compiler.lookup.ProblemReasons;
37 import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
38 import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
39
40 public class SelectionOnMessageSend extends MessageSend {
41
42     /*
43      * Cannot answer default abstract match, iterate in superinterfaces of declaring class
44      * for a better match (default abstract match came from scope lookups).
45      */

46     private MethodBinding findNonDefaultAbstractMethod(MethodBinding methodBinding) {
47
48         ReferenceBinding[] itsInterfaces = methodBinding.declaringClass.superInterfaces();
49         if (itsInterfaces != Binding.NO_SUPERINTERFACES) {
50             ReferenceBinding[] interfacesToVisit = itsInterfaces;
51             int nextPosition = interfacesToVisit.length;
52
53             for (int i = 0; i < nextPosition; i++) {
54                 ReferenceBinding currentType = interfacesToVisit[i];
55                 MethodBinding[] methods = currentType.getMethods(methodBinding.selector);
56                 if(methods != null) {
57                     for (int k = 0; k < methods.length; k++) {
58                         if(methodBinding.areParametersEqual(methods[k]))
59                             return methods[k];
60                     }
61                 }
62
63                 if ((itsInterfaces = currentType.superInterfaces()) != Binding.NO_SUPERINTERFACES) {
64                     int itsLength = itsInterfaces.length;
65                     if (nextPosition + itsLength >= interfacesToVisit.length)
66                         System.arraycopy(interfacesToVisit, 0, interfacesToVisit = new ReferenceBinding[nextPosition + itsLength + 5], 0, nextPosition);
67                     nextInterface : for (int a = 0; a < itsLength; a++) {
68                         ReferenceBinding next = itsInterfaces[a];
69                         for (int b = 0; b < nextPosition; b++)
70                             if (next == interfacesToVisit[b]) continue nextInterface;
71                         interfacesToVisit[nextPosition++] = next;
72                     }
73                 }
74             }
75         }
76         return methodBinding;
77     }
78     
79     public StringBuffer JavaDoc printExpression(int indent, StringBuffer JavaDoc output) {
80
81         output.append("<SelectOnMessageSend:"); //$NON-NLS-1$
82
if (!receiver.isImplicitThis()) receiver.printExpression(0, output).append('.');
83         output.append(this.selector).append('(');
84         if (arguments != null) {
85             for (int i = 0; i < arguments.length; i++) {
86                 if (i > 0) output.append(", "); //$NON-NLS-1$
87
arguments[i].printExpression(0, output);
88             }
89         }
90         return output.append(")>"); //$NON-NLS-1$
91
}
92     
93     public TypeBinding resolveType(BlockScope scope) {
94
95         super.resolveType(scope);
96
97         // tolerate some error cases
98
if(binding == null ||
99                     !(binding.isValidBinding() ||
100                         binding.problemId() == ProblemReasons.NotVisible
101                         || binding.problemId() == ProblemReasons.InheritedNameHidesEnclosingName
102                         || binding.problemId() == ProblemReasons.NonStaticReferenceInConstructorInvocation
103                         || binding.problemId() == ProblemReasons.NonStaticReferenceInStaticContext)) {
104             throw new SelectionNodeFound();
105         } else {
106             if(binding.isDefaultAbstract()) {
107                 throw new SelectionNodeFound(findNonDefaultAbstractMethod(binding)); // 23594
108
} else {
109                 throw new SelectionNodeFound(binding);
110             }
111         }
112     }
113 }
114
Popular Tags