KickJava   Java API By Example, From Geeks To Geeks.

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


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 an allocation expression containing the cursor.
16  * If the allocation expression is not qualified, the enclosingInstance field
17  * is null.
18  * e.g.
19  *
20  * class X {
21  * void foo() {
22  * new [start]Bar[end](1, 2)
23  * }
24  * }
25  *
26  * ---> class X {
27  * void foo() {
28  * <SelectOnAllocationExpression:new Bar(1, 2)>
29  * }
30  * }
31  *
32  */

33
34 import org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration;
35 import org.eclipse.jdt.internal.compiler.ast.QualifiedAllocationExpression;
36 import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
37 import org.eclipse.jdt.internal.compiler.lookup.Binding;
38 import org.eclipse.jdt.internal.compiler.lookup.BlockScope;
39 import org.eclipse.jdt.internal.compiler.lookup.ProblemReasons;
40 import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
41  
42 public class SelectionOnQualifiedAllocationExpression extends QualifiedAllocationExpression {
43
44     public SelectionOnQualifiedAllocationExpression() {
45         // constructor without argument
46
}
47     
48     public SelectionOnQualifiedAllocationExpression(TypeDeclaration anonymous) {
49         super(anonymous);
50     }
51     
52     public StringBuffer JavaDoc printExpression(int indent, StringBuffer JavaDoc output) {
53         if (this.enclosingInstance == null)
54             output.append("<SelectOnAllocationExpression:"); //$NON-NLS-1$
55
else
56             output.append("<SelectOnQualifiedAllocationExpression:"); //$NON-NLS-1$
57

58         return super.printExpression(indent, output).append('>');
59     }
60     
61     public TypeBinding resolveType(BlockScope scope) {
62         super.resolveType(scope);
63     
64         // tolerate some error cases
65
if (binding == null ||
66                 !(binding.isValidBinding() ||
67                     binding.problemId() == ProblemReasons.NotVisible))
68             throw new SelectionNodeFound();
69         if (anonymousType == null)
70             throw new SelectionNodeFound(binding);
71     
72         // if selecting a type for an anonymous type creation, we have to
73
// find its target super constructor (if extending a class) or its target
74
// super interface (if extending an interface)
75
if (anonymousType.binding.superInterfaces == Binding.NO_SUPERINTERFACES) {
76             // find the constructor binding inside the super constructor call
77
ConstructorDeclaration constructor = (ConstructorDeclaration) anonymousType.declarationOf(binding.original());
78             throw new SelectionNodeFound(constructor.constructorCall.binding);
79         } else {
80             // open on the only superinterface
81
throw new SelectionNodeFound(anonymousType.binding.superInterfaces[0]);
82         }
83     }
84 }
85
Popular Tags