1 /*
   2  * Copyright (c) 2003, 2004, 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 package com.sun.corba.se.impl.presentation.rmi ;
  27 
  28 /**
  29  * Holds information about the OMG IDL mapping of a Java type.
  30  */
  31 public class IDLType {
  32 
  33     private Class cl_;
  34 
  35     // terminology for OMG IDL type package name
  36     private String[] modules_;
  37 
  38     // name of element within module
  39     private String memberName_;
  40 
  41 
  42     public IDLType(Class cl, String[] modules, String memberName) {
  43         cl_ = cl;
  44         modules_ = modules;
  45         memberName_ = memberName;
  46     }
  47 
  48     public IDLType(Class cl, String memberName) {
  49         this( cl, new String[0], memberName ) ;
  50     }
  51 
  52     public Class getJavaClass() {
  53         return cl_;
  54     }
  55 
  56     public String[] getModules()
  57     {
  58         return modules_ ;
  59     }
  60 
  61     public String makeConcatenatedName( char separator, boolean fixIDLKeywords ) {
  62         StringBuffer sbuff = new StringBuffer() ;
  63         for (int ctr=0; ctr<modules_.length; ctr++) {
  64             String mod = modules_[ctr] ;
  65             if (ctr>0)
  66                 sbuff.append( separator ) ;
  67 
  68             if (fixIDLKeywords && IDLNameTranslatorImpl.isIDLKeyword(mod))
  69                 mod = IDLNameTranslatorImpl.mangleIDLKeywordClash( mod ) ;
  70 
  71             sbuff.append( mod ) ;
  72         }
  73 
  74         return sbuff.toString() ;
  75     }
  76 
  77     public String getModuleName() {
  78         // Note that this should probably be makeConcatenatedName( '/', true )
  79         // for spec compliance,
  80         // but rmic does it this way, so we'll leave this.
  81         // The effect is that an overloaded method like
  82         // void foo( bar.typedef.Baz )
  83         // will get an IDL name of foo__bar_typedef_Baz instead of
  84         // foo__bar__typedef_Baz (note the extra _ before typedef).
  85         return makeConcatenatedName( '_', false ) ;
  86     }
  87 
  88     public String getExceptionName() {
  89         // Here we will check for IDL keyword collisions (see bug 5010332).
  90         // This means that the repository ID for
  91         // foo.exception.SomeException is
  92         // "IDL:foo/_exception/SomeEx:1.0" (note the underscore in front
  93         // of the exception module name).
  94         String modName = makeConcatenatedName( '/', true ) ;
  95 
  96         String suffix = "Exception" ;
  97         String excName = memberName_ ;
  98         if (excName.endsWith( suffix )) {
  99             int last = excName.length() - suffix.length() ;
 100             excName = excName.substring( 0, last ) ;
 101         }
 102 
 103         // See bug 4989312: we must always add the Ex.
 104         excName += "Ex" ;
 105 
 106         if (modName.length() == 0)
 107             return "IDL:" + excName + ":1.0" ;
 108         else
 109             return "IDL:" + modName + '/' + excName + ":1.0" ;
 110     }
 111 
 112     public String getMemberName() {
 113         return memberName_;
 114     }
 115 
 116     /**
 117      * True if this type doesn't have a containing module.  This
 118      * would be true of a java type defined in the default package
 119      * or a primitive.
 120      */
 121     public boolean hasModule() {
 122         return (modules_.length > 0) ;
 123     }
 124 }