1 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 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 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 getSourceText() { 100 String origElem; 101 if ((origElem = checkChange()) != null) 102 return origElem; 103 StringBuffer buf = new StringBuffer (); 104 StatementImpl leftSide = (StatementImpl) getLeftSide(); 105 StatementImpl rightSide = (StatementImpl) getRightSide(); 106 Operator operator = getOperator(); 107 buf.append(leftSide.getSourceText()); 108 buf.append(" "); buf.append(operatorToString(operator)); 110 buf.append(" "); 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 operatorToString (Operator op) { 128 if (op == OperatorEnum.GT) return ">"; if (op == OperatorEnum.LT) return "<"; if (op == OperatorEnum.EQUAL) return "=="; if (op == OperatorEnum.LTE) return "<="; if (op == OperatorEnum.GTE) return ">="; if (op == OperatorEnum.NEQUAL) return "!="; if (op == OperatorEnum.PLUS) return "+"; if (op == OperatorEnum.MINUS) return "-"; if (op == OperatorEnum.MULTI) return "*"; if (op == OperatorEnum.DIV) return "/"; if (op == OperatorEnum.LAND) return "&&"; if (op == OperatorEnum.LOR) return "||"; if (op == OperatorEnum.AND) return "&"; if (op == OperatorEnum.OR) return "|"; if (op == OperatorEnum.XOR) return "^"; if (op == OperatorEnum.MOD) return "%"; if (op == OperatorEnum.LSHIFT) return "<<"; if (op == OperatorEnum.RSHIFT) return ">>"; if (op == OperatorEnum.URSHIFT) return ">>>"; if (op == OperatorEnum.INSTANCEOF) return "instanceof"; 149 JMManager.getLog().log("Operator not mapped to string: " + op); return "???"; } 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 if (childrenInited) { 164 deleteChild(leftSide); 165 deleteChild(rightSide); 166 } 167 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 |