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-2006 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: package org.netbeans.modules.vmd.midp.serialization;
042:
043: import org.netbeans.modules.vmd.api.model.PropertyDescriptor;
044: import org.netbeans.modules.vmd.api.model.PropertyValue;
045: import org.netbeans.modules.vmd.api.model.TypeID;
046: import org.netbeans.modules.vmd.api.model.Versionable;
047: import org.netbeans.modules.vmd.midp.components.MidpTypes;
048:
049: import javax.lang.model.element.VariableElement;
050: import javax.lang.model.element.TypeElement;
051: import javax.lang.model.type.DeclaredType;
052: import javax.lang.model.type.TypeMirror;
053:
054: /**
055: * @author David Kaspar
056: */
057: public final class MidpTypesConvertor {
058:
059: public static PropertyDescriptor createPropertyDescriptorForParameter(
060: String name, boolean usedInConstructor,
061: VariableElement element) {
062: TypeMirror type = element.asType();
063: switch (type.getKind()) {
064: case BOOLEAN:
065: return createProperty(name, usedInConstructor,
066: MidpTypes.TYPEID_BOOLEAN, PropertyValue
067: .createUserCode("false")); // NOI18N
068: case BYTE:
069: return createProperty(name, usedInConstructor,
070: MidpTypes.TYPEID_BYTE, PropertyValue
071: .createUserCode("(byte) 0")); // NOI18N
072: case CHAR:
073: return createProperty(name, usedInConstructor,
074: MidpTypes.TYPEID_CHAR, PropertyValue
075: .createUserCode("(char) 0")); // NOI18N
076: case SHORT:
077: return createProperty(name, usedInConstructor,
078: MidpTypes.TYPEID_SHORT, PropertyValue
079: .createUserCode("(short) 0")); // NOI18N
080: case DOUBLE:
081: return createProperty(name, usedInConstructor,
082: MidpTypes.TYPEID_DOUBLE, PropertyValue
083: .createUserCode("0.0")); // NOI18N
084: case FLOAT:
085: return createProperty(name, usedInConstructor,
086: MidpTypes.TYPEID_FLOAT, PropertyValue
087: .createUserCode("0.0f")); // NOI18N
088: case INT:
089: return createProperty(name, usedInConstructor,
090: MidpTypes.TYPEID_INT, PropertyValue
091: .createUserCode("0")); // NOI18N
092: case LONG:
093: return createProperty(name, usedInConstructor,
094: MidpTypes.TYPEID_LONG, PropertyValue
095: .createUserCode("0l")); // NOI18N
096: case DECLARED:
097: String className = ((TypeElement) ((DeclaredType) type)
098: .asElement()).getQualifiedName().toString();
099: if ("java.lang.String".equals(className)) // NOI18N
100: return new PropertyDescriptor(name,
101: MidpTypes.TYPEID_JAVA_LANG_STRING,
102: PropertyValue.createNull(), true, true,
103: Versionable.FOREVER);
104: // TODO - component references like Image...
105: default:
106: return new PropertyDescriptor(name,
107: MidpTypes.TYPEID_JAVA_CODE, MidpTypes
108: .createJavaCodeValue("null"), false, true,
109: Versionable.FOREVER); // NOI18N
110: }
111: }
112:
113: private static PropertyDescriptor createProperty(String name,
114: boolean usedInConstructor, TypeID typeID,
115: PropertyValue defaultValue) {
116: return new PropertyDescriptor(name, typeID,
117: usedInConstructor ? defaultValue : PropertyValue
118: .createNull(), !usedInConstructor, true,
119: Versionable.FOREVER);
120: }
121:
122: }
|