KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > api > jms > selector > parser > TypeChecker


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Coridan.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46 package org.mr.api.jms.selector.parser;
47
48 import org.mr.api.jms.selector.syntax.Identifiers;
49 import org.mr.api.jms.selector.syntax.Type;
50 import org.mr.api.jms.selector.syntax.TypeMismatchException;
51 /**
52  * This class represents a utility class for performing type checking
53  *
54  */

55 final class TypeChecker {
56
57     
58     /**
59      * Private constructor,we don't want to create a new instance of the TypeChecker
60      */

61     private TypeChecker() {
62     }//TypeChecker
63

64     
65     /**
66      * Checks the return type of an expression against the expected type
67      *
68      * @param node the expression node
69      * @param expected the expected type of the expression
70      * @throws TypeMismatchException if the return type doesn't match that expected
71      */

72     public static void check(final SelectorAST node, final Type expected) throws TypeMismatchException {
73
74         Type type = node.getReturnType();
75         if (type != expected && type != Type.UNDEFINED) {
76             String JavaDoc msg = "expecting a " + expected + " expression, found a " + type;
77             throw new TypeMismatchException(msg,node.getContext());
78         }//if
79
}//check
80

81
82     /**
83      * Checks the return type of an expression against the expected type
84      *
85      * @param operator the expression operator
86      * @param node the expression node
87      * @param expected the expected type of the expression
88      * @throws TypeMismatchException if the return type doesn't match that
89      * expected
90      */

91     public static void check(final String JavaDoc operator, final SelectorAST node, final Type expected) throws TypeMismatchException {
92
93         Type type = node.getReturnType();
94         if (type != expected && type != Type.UNDEFINED) {
95             String JavaDoc msg = "expecting a " + expected + " expression for operator " + operator + ", found a " + type;
96             throw new TypeMismatchException(msg,node.getContext());
97         }//if
98
}//check
99

100
101     /**
102      * Checks the types of left and right hand sides of an expression against
103      * the expected type
104      *
105      * @param operator the expression operator
106      * @param left the left hand side of the expression
107      * @param right the right hand side of the expression
108      * @param expected the expected type of the expression
109      * @throws TypeMismatchException if a type doesn't match that expected
110      */

111     public static void check(final String JavaDoc operator, final SelectorAST left, final SelectorAST right, final Type expected) throws TypeMismatchException {
112
113         check(operator, left, expected);
114         check(operator, right, expected);
115     }//check
116

117     
118     /**
119      * Verifies if two expressions can be compared
120      *
121      * @param operator the comparison operator
122      * @param left the left hand side of the expression
123      * @param right the right hand side of the expression
124      * @throws TypeMismatchException if the expressions can't be compared
125      */

126     public static void checkComparison(final String JavaDoc operator, final SelectorAST left,final SelectorAST right) throws TypeMismatchException {
127
128         Type leftHand = left.getReturnType();
129         Type rightHand = right.getReturnType();
130
131         if (leftHand == Type.UNDEFINED || rightHand == Type.UNDEFINED) {
132             // can't evaluate this at parse time.
133
}//if
134
else if (leftHand == Type.STRING && rightHand == Type.STRING) {
135             checkStringComparison(operator, left, right);
136         }//else if
137
else if ((leftHand == Type.STRING && rightHand != Type.STRING) || (leftHand == Type.BOOLEAN && rightHand != Type.BOOLEAN) || (leftHand == Type.NUMERIC && rightHand != Type.NUMERIC)) {
138             String JavaDoc msg = "expecting a " + leftHand + " expression for operator " + operator + ", found a " + rightHand;
139             throw new TypeMismatchException(msg,right.getContext());
140         }//else if
141
}//checkComparison
142

143     /**
144      * Verifies if two string expressions can be compared
145      *
146      * @param operator the comparison operator
147      * @param left the left hand side of the expression
148      * @param right the right hand side of the expression
149      * @throws TypeMismatchException if the expressions can't be compared
150      */

151     public static void checkStringComparison(final String JavaDoc operator, final SelectorAST left, final SelectorAST right) throws TypeMismatchException {
152
153         if (left.getType() == SelectorTokenTypes.IDENT && right.getType() == SelectorTokenTypes.STRING_LITERAL) {
154             checkIdentifierComparison(left, right);
155         }//if
156
else if (left.getType() == SelectorTokenTypes.STRING_LITERAL && right.getType() == SelectorTokenTypes.IDENT) {
157             checkIdentifierComparison(right, left);
158         }//else if
159
}//checkStringComparison
160

161
162     /**
163      * Verifies that an identifier may be compared to a string literal
164      *
165      * @param identifier the identifier
166      * @param literal the string literal
167      * @throws TypeMismatchException if the expressions can't be compared
168      */

169     public static void checkIdentifierComparison(final SelectorAST identifier, final SelectorAST literal) throws TypeMismatchException {
170
171         if (identifier.getText().equals(Identifiers.JMS_DELIVERY_MODE)) {
172             String JavaDoc value = literal.getText();
173             if (!value.equals(Identifiers.PERSISTENT) && !value.equals(Identifiers.NON_PERSISTENT)) {
174                 String JavaDoc msg = "Cannot compare JMSDeliveryMode with '" + value + "'";
175                 throw new TypeMismatchException(msg,identifier.getContext());
176             }//if
177
}//if
178
}//checkIdentifierComparison
179
}//TypeChecker
Popular Tags