Skip to main content
summaryrefslogtreecommitdiffstats
blob: bc4dd09b7b481e817ea9a0e8e932acaa24206f6c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*******************************************************************************
 * Copyright (c) 2000, 2008 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.codeassist.select;

/*
 * Selection node build by the parser in any case it was intending to
 * reduce an allocation expression containing the cursor.
 * If the allocation expression is not qualified, the enclosingInstance field
 * is null.
 * e.g.
 *
 *	class X {
 *    void foo() {
 *      new [start]Bar[end](1, 2)
 *    }
 *  }
 *
 *	---> class X {
 *         void foo() {
 *           <SelectOnAllocationExpression:new Bar(1, 2)>
 *         }
 *       }
 *
 */

import org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration;
import org.eclipse.jdt.internal.compiler.ast.QualifiedAllocationExpression;
import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
import org.eclipse.jdt.internal.compiler.lookup.Binding;
import org.eclipse.jdt.internal.compiler.lookup.BlockScope;
import org.eclipse.jdt.internal.compiler.lookup.LocalTypeBinding;
import org.eclipse.jdt.internal.compiler.lookup.ProblemReasons;
import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;

public class SelectionOnQualifiedAllocationExpression extends QualifiedAllocationExpression {

	public SelectionOnQualifiedAllocationExpression() {
		// constructor without argument
	}

	public SelectionOnQualifiedAllocationExpression(TypeDeclaration anonymous) {
		super(anonymous);
	}

	@Override
	public StringBuffer printExpression(int indent, StringBuffer output) {
		if (this.enclosingInstance == null)
			output.append("<SelectOnAllocationExpression:");  //$NON-NLS-1$
		else
			output.append("<SelectOnQualifiedAllocationExpression:"); //$NON-NLS-1$

		return super.printExpression(indent, output).append('>');
	}

	@Override
	public TypeBinding resolveType(BlockScope scope) {
		super.resolveType(scope);

		if (this.binding == null) {
			throw new SelectionNodeFound();
		}

		// tolerate some error cases
		if (!this.binding.isValidBinding()) {
			switch (this.binding.problemId()) {
				case ProblemReasons.NotVisible:
					// visibility is ignored
					break;
				case ProblemReasons.NotFound:
					if (this.resolvedType != null && this.resolvedType.isValidBinding()) {
						throw new SelectionNodeFound(this.resolvedType);
					}
					throw new SelectionNodeFound();
				default:
					throw new SelectionNodeFound();
			}
		}

		if (this.anonymousType == null)
			throw new SelectionNodeFound(this.binding);

		// if selecting a type for an anonymous type creation, we have to
		// find its target super constructor (if extending a class) or its target
		// super interface (if extending an interface)
		if (this.anonymousType.binding != null) {
			LocalTypeBinding localType = (LocalTypeBinding) this.anonymousType.binding;
			if (localType.superInterfaces == Binding.NO_SUPERINTERFACES) {
				// find the constructor binding inside the super constructor call
				ConstructorDeclaration constructor = (ConstructorDeclaration) this.anonymousType.declarationOf(this.binding.original());
				if (constructor != null) {
					throw new SelectionNodeFound(constructor.constructorCall.binding);
				}
				throw new SelectionNodeFound(this.binding);
			}
			// open on the only super interface
			throw new SelectionNodeFound(localType.superInterfaces[0]);
		} else {
			if (this.resolvedType.isInterface()) {
				throw new SelectionNodeFound(this.resolvedType);
			}
			throw new SelectionNodeFound(this.binding);
		}
	}
}

Back to the top