1 /*
   2  * Copyright (c) 1998, 2007, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * Licensed Materials - Property of IBM
  28  * RMI-IIOP v1.0
  29  * Copyright IBM Corp. 1998 1999  All Rights Reserved
  30  *
  31  */
  32 
  33 package sun.rmi.rmic.iiop;
  34 
  35 import sun.tools.java.CompilerError;
  36 import sun.tools.java.Identifier;
  37 import sun.tools.java.ClassDefinition;
  38 
  39 /**
  40  * PrimitiveType wraps primitive types and void.
  41  * <p>
  42  * The static forPrimitive(...) method must be used to obtain an instance, and
  43  * will return null if the type is non-conforming.
  44  *
  45  * @author      Bryan Atsatt
  46  */
  47 public class PrimitiveType extends Type {
  48 
  49     //_____________________________________________________________________
  50     // Public Interfaces
  51     //_____________________________________________________________________
  52 
  53     /**
  54      * Create a PrimitiveType object for the given type.
  55      *
  56      * If the type is not a properly formed or if some other error occurs, the
  57      * return value will be null, and errors will have been reported to the
  58      * supplied BatchEnvironment.
  59      */
  60     public static PrimitiveType forPrimitive(sun.tools.java.Type type,
  61                                              ContextStack stack) {
  62 
  63         if (stack.anyErrors()) return null;
  64 
  65         // Do we already have it?
  66 
  67         Type existing = getType(type,stack);
  68 
  69         if (existing != null) {
  70 
  71             if (!(existing instanceof PrimitiveType)) return null; // False hit.
  72 
  73             // Yep, so return it...
  74 
  75             return (PrimitiveType) existing;
  76         }
  77 
  78         int typeCode;
  79 
  80         switch (type.getTypeCode()) {
  81         case TC_VOID:           typeCode = TYPE_VOID; break;
  82         case TC_BOOLEAN:        typeCode = TYPE_BOOLEAN; break;
  83         case TC_BYTE:           typeCode = TYPE_BYTE; break;
  84         case TC_CHAR:           typeCode = TYPE_CHAR; break;
  85         case TC_SHORT:          typeCode = TYPE_SHORT; break;
  86         case TC_INT:            typeCode = TYPE_INT; break;
  87         case TC_LONG:           typeCode = TYPE_LONG; break;
  88         case TC_FLOAT:          typeCode = TYPE_FLOAT; break;
  89         case TC_DOUBLE:         typeCode = TYPE_DOUBLE; break;
  90         default: return null;
  91         }
  92 
  93         PrimitiveType it = new PrimitiveType(stack,typeCode);
  94 
  95         // Add it...
  96 
  97         putType(type,it,stack);
  98 
  99         // Do the stack thing in case tracing on...
 100 
 101         stack.push(it);
 102         stack.pop(true);
 103 
 104         return it;
 105     }
 106 
 107     /**
 108      * Return signature for this type  (e.g. com.acme.Dynamite
 109      * would return "com.acme.Dynamite", byte = "B")
 110      */
 111     public String getSignature() {
 112         switch (getTypeCode()) {
 113         case TYPE_VOID:         return SIG_VOID;
 114         case TYPE_BOOLEAN:      return SIG_BOOLEAN;
 115         case TYPE_BYTE:         return SIG_BYTE;
 116         case TYPE_CHAR:         return SIG_CHAR;
 117         case TYPE_SHORT:    return SIG_SHORT;
 118         case TYPE_INT:          return SIG_INT;
 119         case TYPE_LONG:         return SIG_LONG;
 120         case TYPE_FLOAT:        return SIG_FLOAT;
 121         case TYPE_DOUBLE:       return SIG_DOUBLE;
 122         default:            return null;
 123         }
 124     }
 125 
 126     /**
 127      * Return a string describing this type.
 128      */
 129     public String getTypeDescription () {
 130         return "Primitive";
 131     }
 132 
 133     /**
 134      * IDL_Naming
 135      * Return the fully qualified IDL name for this type (e.g. com.acme.Dynamite would
 136      * return "com::acme::Dynamite").
 137      * @param global If true, prepends "::".
 138      */
 139     public String getQualifiedIDLName(boolean global) {
 140         return super.getQualifiedIDLName(false);
 141     }
 142 
 143     //_____________________________________________________________________
 144     // Subclass/Internal Interfaces
 145     //_____________________________________________________________________
 146 
 147     /*
 148      * Load a Class instance. Return null if fail.
 149      */
 150     protected Class loadClass() {
 151         switch (getTypeCode()) {
 152         case TYPE_VOID:         return Null.class;
 153         case TYPE_BOOLEAN:      return boolean.class;
 154         case TYPE_BYTE:         return byte.class;
 155         case TYPE_CHAR:         return char.class;
 156         case TYPE_SHORT:        return short.class;
 157         case TYPE_INT:          return int.class;
 158         case TYPE_LONG:         return long.class;
 159         case TYPE_FLOAT:        return float.class;
 160         case TYPE_DOUBLE:       return double.class;
 161         default:            throw new CompilerError("Not a primitive type");
 162         }
 163     }
 164 
 165     /**
 166      * IDL_Naming
 167      * Create an PrimitiveType instance for the given class.
 168      */
 169     private PrimitiveType(ContextStack stack, int typeCode) {
 170         super(stack,typeCode | TM_PRIMITIVE);
 171 
 172         // Validate type and set names...
 173 
 174         String idlName = IDLNames.getTypeName(typeCode,false);
 175         Identifier id = null;
 176 
 177         switch (typeCode) {
 178         case TYPE_VOID:         id = idVoid; break;
 179         case TYPE_BOOLEAN:      id = idBoolean; break;
 180         case TYPE_BYTE:         id = idByte; break;
 181         case TYPE_CHAR:         id = idChar; break;
 182         case TYPE_SHORT:        id = idShort; break;
 183         case TYPE_INT:          id = idInt; break;
 184         case TYPE_LONG:         id = idLong; break;
 185         case TYPE_FLOAT:        id = idFloat; break;
 186         case TYPE_DOUBLE:       id = idDouble; break;
 187         default:            throw new CompilerError("Not a primitive type");
 188         }
 189 
 190         setNames(id,null,idlName);
 191         setRepositoryID();
 192     }
 193 }
 194 
 195 class Null {}