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.integration.ide.events;
043:
044: import java.lang.reflect.Modifier;
045: import java.util.ArrayList;
046:
047: import org.netbeans.modules.uml.core.metamodel.core.foundation.IMultiplicity;
048: import org.netbeans.modules.uml.core.metamodel.core.foundation.IMultiplicityRange;
049: import org.netbeans.modules.uml.core.metamodel.infrastructure.coreinfrastructure.IBehavioralFeature;
050: import org.netbeans.modules.uml.core.metamodel.infrastructure.coreinfrastructure.IClassifier;
051: import org.netbeans.modules.uml.core.support.umlutils.ETList;
052: import org.netbeans.modules.uml.integration.ide.UMLSupport;
053: import org.netbeans.modules.uml.core.metamodel.infrastructure.coreinfrastructure.IParameter;
054: import org.netbeans.modules.uml.integration.ide.JavaClassUtils;
055: import org.netbeans.modules.uml.util.DummyCorePreference;
056: import org.openide.util.NbPreferences;
057:
058: /**
059: * The MethodParameterInfo is used to store the parameter information used
060: * to define the parameters for constructors and methods.
061: *
062: * @see ConstructorInfo
063: */
064: public class MethodParameterInfo implements Cloneable {
065: private String mOrigType = null;
066: private String mNewType = null;
067: private String mName = null;
068: private String fullyQualifiedType = null;
069: private IParameter parameterElement = null;
070: private MethodInfo parentMethodInfo = null;
071:
072: /**
073: * The number of multiplicty ranges that are associated with the parameter.
074: * In Java parameters can not define a range. So, all we need to know is
075: * how many ranges there are.
076: */
077: private int numberOfDimensions = 0;
078:
079: /**
080: * Constructs a MethodParameterInfo.
081: */
082: public MethodParameterInfo() {
083: this (null, null);
084: }
085:
086: /**
087: * Constructs a MethodParameterInfo and sets the type of the parameter.
088: */
089: public MethodParameterInfo(String type) {
090: this (type, null);
091: }
092:
093: /**
094: * Constructs a MethodParameterInfo and sets the type and name of the parameter.
095: */
096: public MethodParameterInfo(String type, String name) {
097: setType(type);
098: setName(name);
099: }
100:
101: public MethodParameterInfo(MethodInfo methInfo, IParameter param,
102: boolean collecOverride) {
103: setParentMethodInfo(methInfo);
104: setParameterElement(param);
105: setType(getType(param, collecOverride));
106: String paramName = param.getName();
107: if (paramName != null)
108: setName(paramName.trim());
109:
110: String type = getType();
111: if (type == null || type.trim().length() == 0)
112: setType("int");
113:
114: // conover
115: // have to check for null parameter type because there is a bug
116: // in the rev eng code that doesn't parse some types properly
117: // leaving them null in the model. Just need to guard agains this
118: if (param.getType() == null)
119: setFullyQualifiedType("error-type_was_null"); // NOI18N
120:
121: else
122: setFullyQualifiedType(param.getType()
123: .getFullyQualifiedName(false));
124:
125: IMultiplicity mult = param.getMultiplicity();
126: if (mult != null) {
127: ETList<IMultiplicityRange> ranges = mult.getRanges();
128: if (ranges != null) {
129: numberOfDimensions = ranges.size();
130: }
131: }
132: }
133:
134: /**
135: * Sets the original type of the parameter.
136: */
137: public void setType(String type) {
138: mOrigType = type;
139: }
140:
141: /**
142: * Retrieves the original type of the parameter.
143: */
144: public String getType() {
145: return mOrigType;
146: }
147:
148: /**
149: * Sets the new type of the parameter.
150: */
151: public void setNewType(String type) {
152: mNewType = type;
153: }
154:
155: /**
156: * Retrieves the new type of the parameter.
157: */
158: public String getNewType() {
159: return mNewType;
160: }
161:
162: /**
163: * Sets the name of the parameter.
164: */
165: public void setName(String name) {
166: mName = name;
167: }
168:
169: /**
170: * Gets the name of the parameter.
171: */
172: public String getName() {
173: return mName;
174: }
175:
176: public void update() {
177: }
178:
179: public Object clone() {
180: try {
181: return super .clone();
182: } catch (CloneNotSupportedException ignored) {
183: }
184: return null;
185: }
186:
187: private boolean equals(Object a, Object b) {
188: return a == null ? b == null : a.equals(b);
189: }
190:
191: public boolean equals(Object other) {
192: if (other instanceof MethodParameterInfo) {
193: MethodParameterInfo ot = (MethodParameterInfo) other;
194: return equals(getType(), ot.getType())
195: && equals(getName(), ot.getName());
196: }
197: return false;
198: }
199:
200: public static String getType(IParameter p) {
201: return getType(p, false);
202: }
203:
204: public static boolean isArray(String type) {
205: return type != null && type.indexOf('[') != -1;
206: }
207:
208: public static String getType(IParameter param,
209: boolean collectOverride) {
210: if (param != null) {
211: IBehavioralFeature feature = param.getBehavioralFeature();
212: String typeName = null;
213:
214: if (param.getType() != null)
215: typeName = param.getType().getName();
216:
217: if (feature != null) {
218: IClassifier clazz = feature.getFeaturingClassifier();
219:
220: typeName = JavaClassUtils
221: .replaceDollarSign(JavaClassUtils
222: .getFullyQualifiedName(param.getType()));
223: }
224:
225: int mul = MemberInfo.getMultiplicity(param
226: .getMultiplicity());
227:
228: if (mul > 0 && collectOverride) {
229: String override = UMLSupport.getUMLSupport()
230: .getCollectionOverride();
231:
232: if (override != null)
233: return override;
234: }
235:
236: typeName = MemberInfo.getArrayType(typeName, mul);
237: return typeName != null ? typeName.trim() : typeName;
238: }
239:
240: return "";
241: }
242:
243: public String getFullyQualifiedType() {
244: return fullyQualifiedType;
245: }
246:
247: public void setFullyQualifiedType(String fullyQualifiedType) {
248: this .fullyQualifiedType = fullyQualifiedType;
249: }
250:
251: public String getCodeGenType() {
252: return getCodeGenType(false);
253: }
254:
255: public String getCodeGenType(boolean fullyQualified) {
256: // if no return param, probably a Constructor; return empty string
257: if (getParameterElement() == null)
258: return ""; // NOI18N
259:
260: // if this is the parameter for "the" main method, then don't override
261: // the String[] type with a Collection type.
262: // public static void main(String[] args)
263: if (getParentMethodInfo().getName().equals("main")
264: && // NOI18N
265: getParentMethodInfo().getParameters().length == 1
266: && (getParentMethodInfo().getModifiers().intValue() & Modifier.PUBLIC) > 0
267: && (getParentMethodInfo().getModifiers().intValue() & Modifier.STATIC) > 0
268: && getType().equals("String[]") && // NOI18N
269: getParentMethodInfo().getReturnParameter().getType()
270: .equals("void")) // NOI18N
271: {
272: return getType();
273: }
274:
275: if (fullyQualified) {
276: if (codeGenTypeFullyQualified == null) {
277: codeGenTypeFullyQualified = GenCodeUtil.getCodeGenType(
278: getParameterElement().getType(), GenCodeUtil
279: .getCollectionOverrideDataTypes(
280: getParameterElement()
281: .getMultiplicity(),
282: fullyQualified),
283: isUseGenerics(), getParameterElement()
284: .getMultiplicity(), fullyQualified,
285: getParentMethodInfo().getContainingClass());
286: }
287: return codeGenTypeFullyQualified;
288: } else {
289: if (codeGenTypeShort == null) {
290: codeGenTypeShort = GenCodeUtil.getCodeGenType(
291: getParameterElement().getType(), GenCodeUtil
292: .getCollectionOverrideDataTypes(
293: getParameterElement()
294: .getMultiplicity(),
295: fullyQualified),
296: isUseGenerics(), getParameterElement()
297: .getMultiplicity(), fullyQualified,
298: getParentMethodInfo().getContainingClass());
299: }
300: return codeGenTypeShort;
301: }
302: }
303:
304: private String codeGenTypeFullyQualified = null;
305: private String codeGenTypeShort = null;
306:
307: public int getNumberOfDimensions() {
308: return numberOfDimensions;
309: }
310:
311: /*
312: public String getCollectionOverrideDataType()
313: {
314: // TODO: when Collection Override Data Type property is added at the
315: // Parameter element level, this will no longer use the global default
316:
317: return UMLSupport.getUMLSupport().getCollectionOverride();
318: }
319:
320:
321: public boolean isCollectionType()
322: {
323: return Util.isValidCollectionDataType(getFullyQualifiedType());
324: }
325:
326:
327: public boolean isUseCollectionOverride()
328: {
329: // TODO: conover - change this to use attribute level property
330: // rather than the global preference
331: return getParameterElement().getMultiplicity().getRangeCount() > 0 &&
332: Util.isValidCollectionDataType(getCollectionOverrideDataType());
333: }
334: */
335:
336: public boolean isUseGenerics() {
337: // TODO: conover - eventually, use the atribute level property
338: // instead of this global preference
339: //kris richards - made change to nbpreferences
340: return NbPreferences.forModule(DummyCorePreference.class)
341: .getBoolean("UML_USE_GENERICS_DEFAULT", true); // NOI18N
342: }
343:
344: public IParameter getParameterElement() {
345: return parameterElement;
346: }
347:
348: public void setParameterElement(IParameter parameterElement) {
349: this .parameterElement = parameterElement;
350: }
351:
352: public MethodInfo getParentMethodInfo() {
353: return parentMethodInfo;
354: }
355:
356: public void setParentMethodInfo(MethodInfo parentMethodInfo) {
357: this .parentMethodInfo = parentMethodInfo;
358: }
359:
360: //
361: // added for template codegen
362: //
363:
364: // see getCodeGenType() for how the type string is formed
365: public ArrayList<String[]> getReferredCodeGenTypes() {
366: return GenCodeUtil.getReferredCodeGenTypes(
367: getParameterElement().getType(),
368: GenCodeUtil.getCollectionOverrideDataTypes(
369: getParameterElement().getMultiplicity(), true),
370: isUseGenerics(), getParameterElement()
371: .getMultiplicity(), getParentMethodInfo()
372: .getContainingClass());
373: }
374:
375: }
|