]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/core extension/org/eclipse/jdt/internal/corext/template/java/AbstractVariableResolver.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / core extension / org / eclipse / jdt / internal / corext / template / java / AbstractVariableResolver.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 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.ArrayList;
14 import java.util.Arrays;
15 import java.util.Iterator;
16 import java.util.List;
17
18 import org.eclipse.jface.text.templates.TemplateContext;
19 import org.eclipse.jface.text.templates.TemplateVariable;
20 import org.eclipse.jface.text.templates.TemplateVariableResolver;
21
22 import org.eclipse.jdt.internal.corext.template.java.CompilationUnitCompletion.Variable;
23
24
25 /**
26  * Resolves template variables to variables which are assignment-compatible with the variable
27  * instance class parameters.
28  *
29  * @since 3.4
30  */
31 public abstract class AbstractVariableResolver extends TemplateVariableResolver {
32
33         protected final String fDefaultType;
34         private Variable[] fVariables;
35
36         /**
37          * Create a variable resolver resolving to
38          * <code>defaultType</code> if no types specified
39          * as parameter
40          * @param defaultType the default type to resolve to
41          */
42         protected AbstractVariableResolver(String defaultType) {
43                 fDefaultType= defaultType;
44         }
45
46         /**
47          * Returns a set of variables of <code>type</code> visible in
48          * the given <code>context</code>.
49          *
50          * @param type the type name to search variables for
51          * @param context context within to search for variables
52          * @return the visible variables of <code>type</code> in <code>context</code>, empty array if no visible variables
53          */
54         protected abstract Variable[] getVisibleVariables(String type, JavaContext context);
55
56         /* (non-Javadoc)
57          * @see org.eclipse.jface.text.templates.TemplateVariableResolver#resolve(org.eclipse.jface.text.templates.TemplateVariable, org.eclipse.jface.text.templates.TemplateContext)
58          */
59         @Override
60         public void resolve(TemplateVariable variable, TemplateContext context) {
61
62                 if (variable instanceof JavaVariable) {
63                         JavaContext jc= (JavaContext) context;
64                         JavaVariable jv= (JavaVariable) variable;
65
66                         List<String> params= variable.getVariableType().getParams();
67                         if (params.size() == 0) {
68                                 fVariables= getVisibleVariables(fDefaultType, jc);
69                                 jv.setParamType(fDefaultType);
70                         } else if (params.size() == 1) {
71                                 String type= params.get(0);
72                                 fVariables= getVisibleVariables(type, jc);
73                                 jv.setParamType(type);
74                         } else {
75                                 ArrayList<Variable> variables= new ArrayList<Variable>();
76                                 for (Iterator<String> iterator= params.iterator(); iterator.hasNext();) {
77                                         variables.addAll(Arrays.asList(getVisibleVariables(iterator.next(), jc)));
78                                 }
79                                 fVariables= variables.toArray(new Variable[variables.size()]);
80
81                                 //set to default type, a template which references to the type
82                                 //of _the_ parameter will not correctly work anyway
83                                 jv.setParamType(fDefaultType);
84                         }
85
86                         if (fVariables.length > 0) {
87                                 jv.setChoices(fVariables);
88                                 jc.markAsUsed(jv.getDefaultValue());
89                         } else {
90                                 super.resolve(variable, context);
91                                 return;
92                         }
93                         if (fVariables.length > 1)
94                                 variable.setUnambiguous(false);
95                         else
96                                 variable.setUnambiguous(isUnambiguous(context));
97                 } else
98                         super.resolve(variable, context);
99         }
100
101         /* (non-Javadoc)
102          * @see org.eclipse.jface.text.templates.TemplateVariableResolver#resolveAll(org.eclipse.jface.text.templates.TemplateContext)
103          */
104         @Override
105         protected String[] resolveAll(TemplateContext context) {
106
107                 String[] names= new String[fVariables.length];
108             for (int i= 0; i < fVariables.length; i++)
109                         names[i]= fVariables[i].getName();
110
111             if (names.length > 0)
112                 ((JavaContext)context).markAsUsed(names[0]);
113
114                 return names;
115         }
116
117 }