Skip to main content
summaryrefslogtreecommitdiffstats
blob: 60451c389eafcb0178ac07d131ffff91318972a5 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*******************************************************************************
 * Copyright (c) 2008 Oracle Corporation.
 * 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:
 *    Matthias Fuessel -- extracted from https://bugs.eclipse.org/bugs/show_bug.cgi?id=215461
 *    Cameron Bateman/Oracle - integrated.
 * 
 ********************************************************************************/

package org.eclipse.jst.jsf.context.symbol;

import java.util.Arrays;
import java.util.Collections;

import org.eclipse.core.resources.IProject;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.Signature;
import org.eclipse.jst.jsf.common.internal.types.TypeConstants;
import org.eclipse.jst.jsf.common.internal.types.ValueType;
import org.eclipse.jst.jsf.common.util.TypeUtil;

/**
 * Creates purpose-built symbols and descriptors fully initialized (unlike the
 * EMF factory that simply creates empty instances.
 * 
 * This class is for convenience only and should not do anything that clients
 * could not do by hand (though with more work).
 * 
 * Clients may use or subclass.
 * 
 * @author cbateman
 * 
 */
public class InitializedSymbolFactory
{
    /**
     * If fullyQualifiedClass can be resolved to an IType, then a bean instance
     * symbol will be created. If the type cannot be resolved, then
     * createUnknownInstanceSymbol is called with the type descriptor on the
     * returned symbol forced to fullyQualifiedClass.
     * 
     * @param project
     * @param fullyQualifiedClass
     * @param symbolName
     * @param source
     * @return a symbol
     */
    public final ISymbol createBeanOrUnknownInstanceSymbol(
            final IProject project, final String fullyQualifiedClass,
            final String symbolName, final ERuntimeSource source)
    {
        final IJavaProject javaProject = JavaCore.create(project);
        try
        {
            final IType type = javaProject.findType(fullyQualifiedClass);

            // TODO: this is a high-bred since it consists of a java instance
            // but also has properties we can populate at designtime such as
            // the maps. Need to add the second part
            if (type != null)
            {
                final IJavaTypeDescriptor2 typeDesc = SymbolFactory.eINSTANCE
                        .createIJavaTypeDescriptor2();
                typeDesc.setType(type);
                final IBeanInstanceSymbol facesContextVar = SymbolFactory.eINSTANCE
                        .createIBeanInstanceSymbol();
                facesContextVar.setTypeDescriptor(typeDesc);
                facesContextVar.setName(symbolName);
                facesContextVar.setRuntimeSource(source);
                return facesContextVar;
            }
        }
        catch (final JavaModelException jme)
        {
            // fall-through and fail with unresolved map
        }

        final ISymbol symbol = createUnknownInstanceSymbol(symbolName, source);
        ((IInstanceSymbol) symbol)
                .getTypeDescriptor()
                .setTypeSignatureDelegate(
                        Signature
                                .createTypeSignature(fullyQualifiedClass, true));

        return symbol;
    }

    /**
     * @param symbolName
     * @param source
     * @return a symbol for a variable of unknown type
     */
    public final IComponentSymbol createUnknownComponentSymbol(
            final String symbolName, final ERuntimeSource source)
    {
        final IComponentSymbol symbol = SymbolFactory.eINSTANCE
                .createIComponentSymbol();
        populateUnknownInstanceSymbol(symbol, symbolName, source);
        return symbol;
    }

    /**
     * @param symbolName
     * @param source
     * @return the unknown instance symbol as an IInstanceSymbol
     */
    public final IInstanceSymbol createUnknownInstanceSymbol(
            final String symbolName, final ERuntimeSource source)
    {
        final IInstanceSymbol symbol = SymbolFactory.eINSTANCE
                .createIInstanceSymbol();
        populateUnknownInstanceSymbol(symbol, symbolName, source);
        return symbol;
    }

    /**
     * @param name
     *            may NOT be null.
     * @param typeDesc
     *            may NOT be null.
     * @param description
     *            may be null
     * @return a component symbol using the java type descriptor
     * @throws IllegalArgumentException
     *             if non-null argument is null
     */
    public final IComponentSymbol createJavaComponentSymbol(final String name,
            final IJavaTypeDescriptor2 typeDesc, final String description)
    {
        if (name == null || typeDesc == null)
        {
            throw new IllegalArgumentException(
                    "name and typeDesc must not be null");
        }

        final IComponentSymbol symbol = SymbolFactory.eINSTANCE
                .createIComponentSymbol();
        symbol.setName(name);
        symbol.setTypeDescriptor(typeDesc);
        symbol.setRuntimeSource(ERuntimeSource.TAG_INSTANTIATED_SYMBOL_LITERAL);
        return symbol;
    }

    /**
     * @param name
     * @param valueType
     * @param description
     * @param javaProject
     * @return an IComponentSymbol that uses valueType to derive the type
     * of its type descriptor
     */
    public final IComponentSymbol createJavaComponentSymbol(final String name,
            final ValueType valueType, final String description,
            final IJavaProject javaProject)
    {
        final IJavaTypeDescriptor2 typeDesc = createTypeDescriptorFromSignature(
                valueType.getSignature(), javaProject);
        return createJavaComponentSymbol(name, typeDesc, description);
    }

    private void populateUnknownInstanceSymbol(final IInstanceSymbol symbol,
            final String symbolName, final ERuntimeSource source)
    {
        final IMapTypeDescriptor typeDesc = SymbolFactory.eINSTANCE
                .createIBoundedMapTypeDescriptor();
        // empty map source
        typeDesc.setMapSource(Collections.emptyMap());
        symbol.setName(symbolName);
        symbol.setTypeDescriptor(typeDesc);
        symbol.setRuntimeSource(source);
    }

    /**
     * @param type
     * @return the signature of the element type of a collection/array,
     *         <code>null</code>, if untyped Collection or no container type
     *         at all.
     */
    public final String getElementSignatureFromContainerType(ValueType type)
    {
        if (type.isArray())
        {
            // TODO full signature
            String signature = type.getSignature();
            int arrayCount = Signature.getArrayCount(signature);
            String elementSig = Signature.getElementType(signature);
            return Signature.createArraySignature(elementSig, arrayCount - 1);
        }
        if (type.isInstanceOf(TypeConstants.TYPE_COLLECTION))
        {
            final String[] typeArguments = type.getTypeArguments();
            if (typeArguments.length > 0)
            {
                return typeArguments[0];
            }
        }
        return null;
    }

    /**
     * @param signature
     * @param javaProject
     * @return a java type descriptor based on the fully qualified type
     *         specified by signature using javaProject as the lookup classpath.
     *         If the IType for signature cannot be found, the descriptor's
     *         typeSignatureDelegate will be used.
     */
    public final IJavaTypeDescriptor2 createTypeDescriptorFromSignature(
            final String signature, final IJavaProject javaProject)
    {
        final String elementType = Signature.getElementType(signature);

        IJavaTypeDescriptor2 desc = SymbolFactory.eINSTANCE
                .createIJavaTypeDescriptor2();
        final int arrayCount = Signature.getArrayCount(signature);
        if (arrayCount > 0)
        {
            desc.setArrayCount(arrayCount);
        }

        IType type = TypeUtil.resolveType(javaProject, elementType);
        if (type != null)
        {
            desc.setType(type);
        }
        else
        {
            desc.setTypeSignatureDelegate(Signature.getTypeErasure(signature));
        }
        desc.getTypeParameterSignatures().addAll(
                Arrays.asList(Signature.getTypeArguments(signature)));
        return desc;
    }
}

Back to the top