Skip to main content
summaryrefslogtreecommitdiffstats
blob: f8f09b22e823d1255b2b4fd4c27dbd59c12fafae (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
/*******************************************************************************
 * 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.validation.internal.el.operators;

import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.jst.jsf.common.internal.types.BooleanLiteralType;
import org.eclipse.jst.jsf.common.internal.types.LiteralType;
import org.eclipse.jst.jsf.common.internal.types.TypeCoercer;
import org.eclipse.jst.jsf.common.internal.types.TypeCoercionException;
import org.eclipse.jst.jsf.common.internal.types.TypeConstants;
import org.eclipse.jst.jsf.common.internal.types.TypeTransformer;
import org.eclipse.jst.jsf.common.internal.types.ValueType;
import org.eclipse.jst.jsf.validation.internal.el.diagnostics.DiagnosticFactory;

/**
 * Represents te EL unary operators "!" and "not" on a ValueType
 * as described by JSP.2.3.6.2
 * 
 * @author cbateman
 *
 */
/*package*/ class NotUnaryOperator extends UnaryOperator 
{
    NotUnaryOperator(DiagnosticFactory diagnosticFactory) 
    {
        super(diagnosticFactory);
    }

    public Diagnostic validate(ValueType type)
    {
    	if (TypeConstants.TYPE_JAVAOBJECT.equals(type.getSignature())) {
    		return Diagnostic.OK_INSTANCE;
    	}
        boolean canCoerce =
            TypeCoercer.canCoerceToBoolean(TypeTransformer.transformBoxPrimitives(type.getSignature()));

        if (canCoerce)
        {
            // check for constant evaluation
            if (type instanceof LiteralType)
            {
                try
                {
                    Boolean coercedValue = ((LiteralType)type).coerceToBoolean();
                    
                    // we are logically notting, so coerced is true, then false
                    // if false then true
                    return _diagnosticFactory.create_UNARY_OP_CONSTANT_EXPRESSION_EVAL_SAME_ID(
                                 "not" //$NON-NLS-1$
                                 , Boolean.valueOf(!coercedValue.booleanValue()).toString()); 
                        
                }
                catch (TypeCoercionException tce)
                {
                    throw new AssertionError("coerce should not throw exception"); //$NON-NLS-1$
                }
            }
            
            return Diagnostic.OK_INSTANCE;
        }
        return _diagnosticFactory.create_UNARY_OP_CANNOT_COERCE_ARGUMENT_TO_BOOLEAN();
    }
    
    public ValueType performOperation(ValueType type)
    {
        boolean canCoerce =
            TypeCoercer.canCoerceToBoolean(TypeTransformer.transformBoxPrimitives(type.getSignature()));

        if (canCoerce)
        {
            if (type instanceof LiteralType)
            {
                try
                {
                    Boolean coercedValue = ((LiteralType)type).coerceToBoolean();
                    
                    // we are logically notting, so coerced is true, then false
                    // if false then true
                    return 
                        coercedValue.booleanValue() ? 
                                  BooleanLiteralType.FALSE :
                                      BooleanLiteralType.TRUE;
                }
                catch (TypeCoercionException tce)
                {
                    throw new AssertionError("coerce should not throw exception"); //$NON-NLS-1$
                }
            }
            
            return new ValueType(TypeConstants.TYPE_BOOLEAN, type.getAssignability());
        }
        return null;
    }

}

Back to the top