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.parser.ASTProvider; 26 27 31 public abstract class PrefixExpressionImpl extends ExpressionImpl implements PrefixExpression { 32 protected Operator operator = null; 33 protected UnaryExpression expression = null; 34 35 36 public PrefixExpressionImpl(StorableObject o) { 37 super(o); 38 } 39 40 public void setExpression(UnaryExpression expression) { 41 objectChanged(CHANGED_EXPRESSION); 42 changeChild(getExpression(), expression); 43 this.expression = expression; 44 } 45 46 public UnaryExpression getExpression() { 47 if (!childrenInited) { 48 initChildren(); 49 } 50 return expression; 51 } 52 53 public void setOperator(Operator operator) { 54 objectChanged(CHANGED_OPERATOR); 55 this.operator = operator; 56 } 57 58 public Operator getOperator() { 59 if (isChanged(CHANGED_OPERATOR)) { 60 return operator; 61 } else { 62 return extractOperator(); 63 } 64 } 65 66 protected Operator extractOperator() { 67 return getOperator(getASTree().getSubTrees()[0].getType()); 68 } 69 70 public List getChildren() { 71 List list = new ArrayList(1); 72 addIfNotNull(list, getExpression()); 73 return list; 74 } 75 76 protected void initChildren() { 77 childrenInited = false; 78 ASTree tree = getASTree(); 79 if (tree != null) { 80 expression = (UnaryExpression) initOrCreate(expression, tree.getSubTrees()[1]); 81 } 82 childrenInited = true; 83 } 84 85 public String getSourceText() { 86 String origElem; 87 if ((origElem = checkChange()) != null) 88 return origElem; 89 StringBuffer buf = new StringBuffer (); 90 StatementImpl expr = (StatementImpl) getExpression(); 91 Operator operator = getOperator(); 92 buf.append(operatorToString(operator)); 93 buf.append(expr.getSourceText()); 94 return buf.toString(); 95 } 96 97 public void getDiff(List diff) { 98 ASTProvider parser = getParser(); 99 ASTree tree = getASTree(); 100 ASTree[] children = tree.getSubTrees(); 101 102 if (isChanged(CHANGED_OPERATOR)) { 103 replaceNode(diff, parser, children[0], operatorToString(operator), 0, null); 104 } 105 getChildDiff(diff, parser, children[1], (MetadataElement) getExpression(), CHANGED_EXPRESSION); 106 } 107 108 protected String operatorToString (Operator op) { 109 if (op == OperatorEnum.INC) return "++"; if (op == OperatorEnum.DEC) return "--"; if (op == OperatorEnum.NOT) return "!"; if (op == OperatorEnum.COMP) return "~"; if (op == null) return ""; 114 return "???"; } 116 117 void setData(Operator operator, UnaryExpression expression) { 118 this.operator = operator; 119 changeChild(null, expression); 120 this.expression = expression; 121 } 122 123 protected void _delete() { 124 if (childrenInited) { 126 deleteChild(expression); 127 } 128 super._delete(); 132 } 133 134 public void replaceChild(Element oldElement,Element newElement) { 135 if (childrenInited) { 136 if (oldElement.equals(expression)) { 137 setExpression((UnaryExpression)newElement); 138 } 139 } 140 } 141 142 public Element duplicate(JavaModelPackage targetExtent) { 143 return targetExtent.getPrefixExpression().createPrefixExpression( 144 getOperator(), (UnaryExpression) duplicateElement(getExpression(), targetExtent)); 145 } 146 } 147 | Popular Tags |