KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javacore > jmiimpl > javamodel > InfixExpressionImpl


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.javacore.jmiimpl.javamodel;
20
21 import java.util.*;
22 import org.netbeans.jmi.javamodel.*;
23 import org.netbeans.lib.java.parser.ASTree;
24 import org.netbeans.mdr.storagemodel.StorableObject;
25 import org.netbeans.modules.javacore.JMManager;
26 import org.netbeans.modules.javacore.parser.ASTProvider;
27
28 /**
29  *
30  * @author Martin Matula
31  */

32 public abstract class InfixExpressionImpl extends ExpressionImpl implements InfixExpression {
33     private Expression leftSide = null;
34     private Operator operator = null;
35     private Expression rightSide = null;
36     
37     /** Creates a new instance of InfixExpressionImpl */
38     public InfixExpressionImpl(StorableObject o) {
39         super(o);
40     }
41     
42     public void setLeftSide(Expression expression) {
43         objectChanged(CHANGED_LEFT_SIDE);
44         changeChild(getLeftSide(), expression);
45         this.leftSide = expression;
46     }
47     
48     public Expression getLeftSide() {
49         if (!childrenInited) {
50             initChildren();
51         }
52         return leftSide;
53     }
54     
55     public void setRightSide(Expression expression) {
56         objectChanged(CHANGED_RIGHT_SIDE);
57         changeChild(getRightSide(), expression);
58         this.rightSide = expression;
59     }
60     
61     public Expression getRightSide() {
62         if (!childrenInited) {
63             initChildren();
64         }
65         return rightSide;
66     }
67     
68     public void setOperator(Operator operator) {
69         objectChanged(CHANGED_OPERATOR);
70         this.operator = operator;
71     }
72     
73     public Operator getOperator() {
74         if (isChanged(CHANGED_OPERATOR)) {
75             return operator;
76         } else {
77             return getOperator(getASTree().getSubTrees()[1].getType());
78         }
79     }
80     
81     public List getChildren() {
82         List list = new ArrayList(2);
83         addIfNotNull(list, getLeftSide());
84         addIfNotNull(list, getRightSide());
85         return list;
86     }
87     
88     protected void initChildren() {
89         childrenInited = false;
90         ASTree tree = getASTree();
91         if (tree != null) {
92             ASTree[] parts = tree.getSubTrees();
93             leftSide = (Expression) initOrCreate(leftSide, parts[0]);
94             rightSide = (Expression) initOrCreate(rightSide, parts[2]);
95         }
96         childrenInited = true;
97     }
98     
99     public String JavaDoc getSourceText() {
100         String JavaDoc origElem;
101         if ((origElem = checkChange()) != null)
102             return origElem;
103         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
104         StatementImpl leftSide = (StatementImpl) getLeftSide();
105         StatementImpl rightSide = (StatementImpl) getRightSide();
106         Operator operator = getOperator();
107         buf.append(leftSide.getSourceText());
108         buf.append(" "); // NOI18N
109
buf.append(operatorToString(operator));
110         buf.append(" "); // NOI18N
111
buf.append(rightSide.getSourceText());
112         return buf.toString();
113     }
114
115     public void getDiff(List diff) {
116         ASTProvider parser = getParser();
117         ASTree tree = getASTree();
118         ASTree[] children = tree.getSubTrees();
119
120         getChildDiff(diff, parser, children[0], (MetadataElement) getLeftSide(), CHANGED_LEFT_SIDE);
121         if (isChanged(CHANGED_OPERATOR)) {
122             replaceNode(diff, parser, children[1], operatorToString(getOperator()), 0, null);
123         }
124         getChildDiff(diff, parser, children[2], (MetadataElement) getRightSide(), CHANGED_RIGHT_SIDE);
125     }
126     
127     private String JavaDoc operatorToString (Operator op) {
128         if (op == OperatorEnum.GT) return ">"; // NOI18N
129
if (op == OperatorEnum.LT) return "<"; // NOI18N
130
if (op == OperatorEnum.EQUAL) return "=="; // NOI18N
131
if (op == OperatorEnum.LTE) return "<="; // NOI18N
132
if (op == OperatorEnum.GTE) return ">="; // NOI18N
133
if (op == OperatorEnum.NEQUAL) return "!="; // NOI18N
134
if (op == OperatorEnum.PLUS) return "+"; // NOI18N
135
if (op == OperatorEnum.MINUS) return "-"; // NOI18N
136
if (op == OperatorEnum.MULTI) return "*"; // NOI18N
137
if (op == OperatorEnum.DIV) return "/"; // NOI18N
138
if (op == OperatorEnum.LAND) return "&&"; // NOI18N
139
if (op == OperatorEnum.LOR) return "||"; // NOI18N
140
if (op == OperatorEnum.AND) return "&"; // NOI18N
141
if (op == OperatorEnum.OR) return "|"; // NOI18N
142
if (op == OperatorEnum.XOR) return "^"; // NOI18N
143
if (op == OperatorEnum.MOD) return "%"; // NOI18N
144
if (op == OperatorEnum.LSHIFT) return "<<"; // NOI18N
145
if (op == OperatorEnum.RSHIFT) return ">>"; // NOI18N
146
if (op == OperatorEnum.URSHIFT) return ">>>"; // NOI18N
147
if (op == OperatorEnum.INSTANCEOF) return "instanceof"; // NOI18N
148

149         JMManager.getLog().log("Operator not mapped to string: " + op); // [PENDING]
150
return "???"; // NOI18N
151
}
152     
153     void setData(Expression leftSide, Operator operator, Expression rightSide) {
154         changeChild(null, leftSide);
155         this.leftSide = leftSide;
156         this.operator = operator;
157         changeChild(null, rightSide);
158         this.rightSide = rightSide;
159     }
160
161     protected void _delete() {
162         // --- delete components -------------------------------------------
163
if (childrenInited) {
164             deleteChild(leftSide);
165             deleteChild(rightSide);
166         }
167         // --- delete links -----------------------------------------------
168
// no links to delete
169
// --- call super ---------------------------------------
170
super._delete();
171     }
172     
173    public void replaceChild(Element oldElement,Element newElement) {
174         if (childrenInited) {
175             if (oldElement.equals(leftSide)) {
176                 setLeftSide((Expression)newElement);
177             } else if (oldElement.equals(rightSide)) {
178                 setRightSide((Expression)newElement);
179             }
180         }
181     }
182    
183     public Element duplicate(JavaModelPackage targetExtent) {
184         return targetExtent.getInfixExpression().createInfixExpression(
185                 (Expression) duplicateElement(getLeftSide(), targetExtent),
186                 getOperator(),
187                 (Expression) duplicateElement(getRightSide(), targetExtent)
188                );
189     }
190 }
191
Popular Tags