]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2005, 2011 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.corext.template.java; | |
12 | ||
13 | import java.util.List; | |
14 | ||
15 | import org.eclipse.jface.text.templates.TemplateContext; | |
16 | import org.eclipse.jface.text.templates.TemplateVariable; | |
17 | import org.eclipse.jface.text.templates.TemplateVariableResolver; | |
18 | ||
19 | import org.eclipse.jdt.core.Signature; | |
20 | ||
21 | import org.eclipse.jdt.internal.corext.template.java.CompilationUnitCompletion.Variable; | |
22 | ||
23 | import org.eclipse.jdt.internal.ui.text.template.contentassist.MultiVariable; | |
24 | ||
25 | /** | |
26 | * Resolves to the lower bound of a type argument of another template variable. | |
27 | * | |
28 | * @since 3.3 | |
29 | */ | |
30 | public class TypeVariableResolver extends TemplateVariableResolver { | |
31 | ||
32 | public TypeVariableResolver() { | |
33 | } | |
34 | ||
35 | /* | |
36 | * @see org.eclipse.jface.text.templates.TemplateVariableResolver#resolve(org.eclipse.jface.text.templates.TemplateVariable, org.eclipse.jface.text.templates.TemplateContext) | |
37 | * @since 3.3 | |
38 | */ | |
39 | @Override | |
40 | public void resolve(TemplateVariable variable, TemplateContext context) { | |
41 | if (!(variable instanceof MultiVariable)) { | |
42 | super.resolve(variable, context); | |
43 | return; | |
44 | } | |
45 | MultiVariable mv= (MultiVariable) variable; | |
46 | List<String> params= variable.getVariableType().getParams(); | |
47 | if (params.isEmpty()) { | |
48 | super.resolve(variable, context); | |
49 | return; | |
50 | } | |
51 | ||
52 | JavaContext jc= (JavaContext) context; | |
53 | String reference= params.get(0); | |
54 | int index= 0; | |
55 | if (params.size() > 1) { | |
56 | String indexParam= params.get(1); | |
57 | try { | |
58 | index= Integer.parseInt(indexParam); | |
59 | } catch (NumberFormatException x) { | |
60 | } | |
61 | } | |
62 | TemplateVariable refVar= jc.getTemplateVariable(reference); | |
63 | if (refVar instanceof JavaVariable) { | |
64 | JavaVariable jvar= (JavaVariable) refVar; | |
65 | resolve(mv, jvar, index, jc); | |
66 | ||
67 | return; | |
68 | } | |
69 | ||
70 | ||
71 | super.resolve(variable, context); | |
72 | } | |
73 | ||
74 | private void resolve(MultiVariable mv, JavaVariable master, int index, JavaContext context) { | |
75 | Object[] choices= master.getChoices(); | |
76 | if (choices instanceof Variable[]) { | |
77 | context.addDependency(master, mv); | |
78 | Variable[] variables= (Variable[]) choices; | |
79 | String type= master.getParamType(); | |
80 | for (int i= 0; i < choices.length; i++) { | |
81 | String[] bounds= variables[i].getTypeArgumentBoundSignatures(type, index); | |
82 | for (int j= 0; j < bounds.length; j++) | |
83 | bounds[j]= Signature.getSignatureSimpleName(bounds[j]); | |
84 | mv.setChoices(variables[i], bounds); | |
85 | } | |
86 | mv.setKey(master.getCurrentChoice()); | |
87 | } else { | |
88 | super.resolve(mv, context); | |
89 | return; | |
90 | } | |
91 | } | |
92 | ||
93 | } |