001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.uml.core.reverseengineering.reframework;
043:
044: import java.util.List;
045:
046: import org.dom4j.Node;
047:
048: import org.netbeans.modules.uml.core.metamodel.core.foundation.BaseElement;
049: import org.netbeans.modules.uml.core.metamodel.core.foundation.IExpression;
050: import org.netbeans.modules.uml.core.metamodel.infrastructure.coreinfrastructure.IClassifier;
051: import org.netbeans.modules.uml.core.metamodel.infrastructure.coreinfrastructure.IOperation;
052: import org.netbeans.modules.uml.core.metamodel.infrastructure.coreinfrastructure.IParameter;
053: import org.netbeans.modules.uml.core.support.umlsupport.IStrings;
054: import org.netbeans.modules.uml.core.support.umlsupport.Strings;
055: import org.netbeans.modules.uml.core.support.umlsupport.XMLManip;
056: import org.netbeans.modules.uml.core.support.umlutils.ETArrayList;
057: import org.netbeans.modules.uml.core.support.umlutils.ETList;
058:
059: /**
060: */
061: public class REOperation extends REClassFeature implements IREOperation {
062: /**
063: * Retrieves the operations parameters
064: * @param pVal The parameters.
065: */
066: public ETList<IREParameter> getParameters() {
067: REXMLCollection<IREParameter> coll = new REXMLCollection<IREParameter>(
068: REParameter.class,
069: "UML:Element.ownedElement/UML:Parameter");
070: try {
071: coll.setDOMNode(getEventData());
072: } catch (Exception e) {
073: e.printStackTrace();
074: }
075: return coll;
076: }
077:
078: /**
079: * Specifies if the operation return type is a primitive or an object type.
080: *
081: * @param *pVal [out] True if primitive, False otherwise.
082: */
083: public boolean getIsPrimitive() {
084: IREParameter par = getReturnParameter();
085: return par != null && par.getIsPrimitive();
086: }
087:
088: /**
089: * Specifies if the operation is a constructor of the owner class.
090: *
091: * @param pVal [out] True if the operationis a constructor.
092: */
093: public boolean getIsConstructor() {
094: return XMLManip.getAttributeBooleanValue(getEventData(),
095: "isConstructor");
096: }
097:
098: /**
099: * Specifies whether the operation must be defined by a descendent.
100: * True indicates that the operation must be defined by a descendent.
101: * False indicates that a descendent is not required to define the operation.
102: * @param pVal [out] True if the operationis abstract.
103: */
104: public boolean getIsAbstract() {
105: return XMLManip.getAttributeBooleanValue(getEventData(),
106: "isAbstract");
107: }
108:
109: /* (non-Javadoc)
110: * @see org.netbeans.modules.uml.core.reverseengineering.reframework.IREOperation#clone(java.lang.Object, java.lang.Object)
111: */
112: public IOperation clone(IClassifier c) {
113: IOperation op = c.createOperation(getType(), getName());
114: if (op != null) {
115: op.removeAllParameters();
116: c.addOperation(op);
117: cloneParametersToOperation(op);
118: }
119: return op;
120: }
121:
122: private void cloneParametersToOperation(IOperation op) {
123: ETList<IParameter> params = op.getParameters();
124: ETList<IREParameter> repars = getParameters();
125: if (params != null && repars != null) {
126: for (int i = 0, count = repars.size(); i < count; ++i) {
127: IREParameter repar = repars.get(i);
128: IParameter p = cloneParameter(repar, op);
129: if (p != null)
130: op.addParameter(p);
131: }
132: }
133: }
134:
135: private IParameter cloneParameter(IREParameter rep, IOperation op) {
136: IParameter p = op.createParameter(rep.getType(), rep.getName());
137: if (p != null) {
138: p.setDirection(rep.getKind());
139: IExpression def = p.getDefault();
140: if (def != null)
141: def.setBody(rep.getDefaultValue());
142: }
143: return p;
144: }
145:
146: /**
147: * Return a list of the exceptions that this operation raises
148: *
149: * @param pExceptions[out] list of exceptions that this operation raises
150: */
151: public IStrings getRaisedExceptions() {
152: IStrings ret = new Strings();
153: Node oen = getXMLNode("UML:Element.ownedElement");
154: if (oen != null) {
155: List nodes = XMLManip.selectNodeList(oen, "UML:Exception");
156: if (nodes != null && nodes.size() > 0) {
157: for (int i = 0, count = nodes.size(); i < count; ++i) {
158: Node n = (Node) nodes.get(i);
159: ret.add(XMLManip.getAttributeValue(n, "name"));
160: }
161: }
162: }
163: return ret;
164: }
165:
166: /**
167: * Determines if this operation is strictfp (java only)
168: */
169: public boolean getIsStrictFP() {
170: return XMLManip.getAttributeBooleanValue(getEventData(),
171: "isStrictFP");
172: }
173:
174: /**
175: * Determines if this operation is native
176: */
177: public boolean getIsNative() {
178: return XMLManip.getAttributeBooleanValue(getEventData(),
179: "isNative");
180: }
181:
182: /**
183: * Determines the concurrency of this operation
184: */
185: public int getConcurrency() {
186: String con = XMLManip.getAttributeValue(getEventData(),
187: "concurrency");
188: if ("guarded".equals(con))
189: return BaseElement.CCK_GUARDED;
190: else if ("concurrent".equals(con))
191: return BaseElement.CCK_CONCURRENT;
192: else
193: return BaseElement.CCK_SEQUENTIAL;
194: }
195:
196: /**
197: * Retrieves the type of the class feature. Example the type of a
198: * attribute or the return type of a operation.
199: * @param pVal The features type.
200: */
201: public String getType() {
202: IREParameter par = getReturnParameter();
203: return par != null ? par.getType() : null;
204: }
205:
206: public ETList<IREMultiplicityRange> getMultiplicity() {
207: REXMLCollection<IREMultiplicityRange> mul = new REXMLCollection<IREMultiplicityRange>(
208: REMultiplicityRange.class,
209: "UML:TypedElement.multiplicity/UML:Multiplicity"
210: + "/UML:Multiplicity.range/UML:MultiplicityRange");
211: try {
212: mul.setDOMNode(getEventData());
213: } catch (Exception e) {
214: e.printStackTrace();
215: }
216: return mul;
217: }
218:
219: public void setMultiplicity(ETList<IREMultiplicityRange> mul) {
220: throw new UnsupportedOperationException(
221: "Can't set multiplicity");
222: }
223:
224: protected IREParameter getReturnParameter() {
225: // Since the operation type is represented as a parameter with a
226: // kind of "return", I have to search the parameters for the return type.
227: ETList<IREParameter> params = getParameters();
228: if (params != null) {
229: for (int i = 0, count = params.size(); i < count; ++i) {
230: // Now retrieve the current parameter and check if it represents
231: // the return value of the operation. If it is the return type
232: // retrieve the type from the parameter.
233: IREParameter par = params.get(i);
234: if (par == null)
235: continue;
236:
237: if (par.getKind() == IREParameter.PDK_RESULT)
238: return par;
239: }
240: }
241: return null;
242: }
243: }
|