Skip to main content
summaryrefslogtreecommitdiffstats
blob: fa75a12f9419523e292a06862bedf4725b95104b (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
/*******************************************************************************
 * Copyright (c) 2006 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:
 *    Cameron Bateman/Oracle - initial API and implementation
 *    
 ********************************************************************************/

package org.eclipse.jst.jsf.common.internal.types;

import org.eclipse.jdt.core.Signature;

/**
 * Coercer for simple type signatures
 * 
 * @author cbateman
 *
 */
public class TypeCoercer 
{
    
    /**
     * Based on JSP.2.8.3 in JSP 2.0 specification
     * 
     * @param boxedTypeSignature -- an auto-boxed type signature
     * @return the coerced type or null if cannot be resolved.  No unboxing
     * is performed on the return.
     * @throws TypeCoercionException if boxedTypeSignature is
     */
    public static String coerceToNumber(final String boxedTypeSignature)
                             throws TypeCoercionException
    {
        String boxedTypeSignature_ = boxedTypeSignature;
        
        // can't coerce arrays to numbers
        if (Signature.getTypeSignatureKind(boxedTypeSignature_)
                == Signature.ARRAY_TYPE_SIGNATURE)
        {
            throw new TypeCoercionException("Cannot coerce arrays to numbers"); //$NON-NLS-1$
        }
        // if it's character, pre-coerce to short per step 2
        if (TypeConstants.SIGNATURE_BOXED_CHARACTER.equals(boxedTypeSignature))
        {
            boxedTypeSignature_ = TypeConstants.TYPE_BOXED_SHORT;
        }

        if (TypeCoercer.typeIsNumeric(boxedTypeSignature_)
                || TypeCoercer.typeIsNull(boxedTypeSignature_))
        {
            return boxedTypeSignature_;
        }
        else if (typeIsString(boxedTypeSignature_))
        {
            // undetermined a string may or not resolve to a number
            // depending on its runtime value
            return null;
        }
        else
        {
            throw new TypeCoercionException();
        }
    }
    
    /**
     * @param boxedTypeSignature
     * @return true if type can be coerced to boolean; null if indeterminate
     */
    public static boolean canCoerceToBoolean(String boxedTypeSignature)
    {
        // JSP.2.8.5 -- boolean is always boolean; string is converted by Boolean.valueOf(String)
        if (typeIsBoolean(boxedTypeSignature)
                || typeIsString(boxedTypeSignature)
                || typeIsNull(boxedTypeSignature))
        {
            return true;
        }
        // nothing else really convertible besides null
        return false;
    }
    
    /**
     * @param typeSignature -- boxed type signature
     * @return true if the typeSignature is numeric
     */
    public static boolean typeIsNumeric(final String typeSignature)
    {
        return (TypeConstants.TYPE_BOXED_BYTE.equals(typeSignature) ||
                TypeConstants.TYPE_BOXED_SHORT.equals(typeSignature) ||
                TypeConstants.TYPE_BOXED_INTEGER.equals(typeSignature) ||
                TypeConstants.TYPE_BOXED_LONG.equals(typeSignature) ||
                TypeConstants.TYPE_BOXED_FLOAT.equals(typeSignature) ||
                TypeConstants.TYPE_BOXED_DOUBLE.equals(typeSignature) ||
                TypeConstants.TYPE_BIG_INTEGER.equals(typeSignature) ||
                TypeConstants.TYPE_BIG_DOUBLE.equals(typeSignature));
    }
    
    /**
     * @param typeSignature
     * @return true if the typeSignature represents a String
     */
    public static boolean typeIsString(final String typeSignature)
    {
        return (TypeConstants.TYPE_STRING.equals(typeSignature));
    }
    
    /**
     * @param typeSignature -- boxed type signature
     * @return true if the typeSignature represents a boxed boolean
     */
    public static boolean typeIsBoolean(final String typeSignature)
    {
        return (TypeConstants.TYPE_BOXED_BOOLEAN.equals(typeSignature));
    }
    
    /**
     * @param typeSignature
     * @return true if type is the EL null type
     */
    public static boolean typeIsNull(final String typeSignature)
    {
        return (TypeConstants.TYPE_NULL.equals(typeSignature));
    }
}

Back to the top