KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > code > OperatorPrecedence


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.refactoring.code;
12
13 import org.eclipse.jdt.core.dom.Assignment;
14 import org.eclipse.jdt.core.dom.ConditionalExpression;
15 import org.eclipse.jdt.core.dom.Expression;
16 import org.eclipse.jdt.core.dom.InfixExpression;
17 import org.eclipse.jdt.core.dom.InstanceofExpression;
18 import org.eclipse.jdt.core.dom.PostfixExpression;
19 import org.eclipse.jdt.core.dom.PrefixExpression;
20 import org.eclipse.jdt.core.dom.InfixExpression.Operator;
21
22 public class OperatorPrecedence {
23
24     private static final int ASSIGNMENT= 0;
25     private static final int CONDITIONAL= 1;
26     private static final int CONDITIONAL_OR= 2;
27     private static final int CONDITIONAL_AND= 3;
28     private static final int BITWISE_INCLUSIVE_OR= 4;
29     private static final int BITWISE_EXCLUSIVE_OR= 5;
30     private static final int BITWISE_AND= 6;
31     private static final int EQUALITY= 7;
32     private static final int RATIONAL= 8;
33     private static final int SHIFT= 9;
34     private static final int ADDITIVE= 10;
35     private static final int MULTIPLICATIVE= 11;
36     private static final int PREFIX= 12;
37     private static final int POSTFIX= 13;
38     
39     public static int getValue(Expression expression) {
40         if (expression instanceof InfixExpression) {
41             return getNormalizedValue((InfixExpression)expression);
42         } else if (expression instanceof PostfixExpression) {
43             return getNormalizedValue((PostfixExpression)expression);
44         } else if (expression instanceof PrefixExpression) {
45             return getNormalizedValue((PrefixExpression)expression);
46         } else if (expression instanceof Assignment) {
47             return getNormalizedValue((Assignment)expression);
48         } else if (expression instanceof ConditionalExpression) {
49             return getNormalizedValue((ConditionalExpression)expression);
50         } else if (expression instanceof InstanceofExpression) {
51             return getNormalizedValue((InstanceofExpression)expression);
52         }
53         return -1;
54     }
55     
56     private static int getNormalizedValue(Assignment ass) {
57         return ASSIGNMENT;
58     }
59     
60     private static int getNormalizedValue(ConditionalExpression exp) {
61         return CONDITIONAL;
62     }
63     
64     private static int getNormalizedValue(InfixExpression exp) {
65         Operator operator= exp.getOperator();
66         if (operator == Operator.CONDITIONAL_OR) {
67             return CONDITIONAL_OR;
68         } else if (operator == Operator.CONDITIONAL_AND) {
69             return CONDITIONAL_AND;
70         } else if (operator == Operator.OR) {
71             return BITWISE_INCLUSIVE_OR;
72         } else if (operator == Operator.XOR) {
73             return BITWISE_EXCLUSIVE_OR;
74         } else if (operator == Operator.AND) {
75             return BITWISE_AND;
76         } else if (operator == Operator.EQUALS || operator == Operator.NOT_EQUALS) {
77             return EQUALITY;
78         } else if (operator == Operator.LESS || operator == Operator.LESS_EQUALS || operator == Operator.GREATER || operator == Operator.GREATER_EQUALS) {
79             return RATIONAL;
80         } else if (operator == Operator.LEFT_SHIFT || operator == Operator.RIGHT_SHIFT_SIGNED || operator == Operator.RIGHT_SHIFT_UNSIGNED) {
81             return SHIFT;
82         } else if (operator == Operator.PLUS || operator == Operator.MINUS) {
83             return ADDITIVE;
84         } else if (operator == Operator.REMAINDER || operator == Operator.DIVIDE || operator == Operator.TIMES) {
85             return MULTIPLICATIVE;
86         }
87         return -1;
88     }
89     
90     private static int getNormalizedValue(InstanceofExpression exp) {
91         return RATIONAL;
92     }
93
94     private static int getNormalizedValue(PrefixExpression exp) {
95         return PREFIX;
96     }
97
98     private static int getNormalizedValue(PostfixExpression exp) {
99         return POSTFIX;
100     }
101 }
102
Popular Tags