src/share/classes/sun/rmi/rmic/iiop/ImplementationType.java

Mon, 26 Mar 2012 14:01:40 +0100

author
coffeys
date
Mon, 26 Mar 2012 14:01:40 +0100
changeset 370
5222b7d658d4
parent 158
91006f157c46
child 748
6845b95cba6b
permissions
-rw-r--r--

7143851: Improve IIOP stub and tie generation in RMIC
7149048: Changes to corba rmic stubGenerator class are not used during jdk build process
Reviewed-by: mschoene, robm

     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  */
    26 /*
    27  * Licensed Materials - Property of IBM
    28  * RMI-IIOP v1.0
    29  * Copyright IBM Corp. 1998 1999  All Rights Reserved
    30  *
    31  */
    33 package sun.rmi.rmic.iiop;
    35 import java.util.Vector;
    36 import sun.tools.java.CompilerError;
    37 import sun.tools.java.ClassNotFound;
    38 import sun.tools.java.ClassDefinition;
    39 import sun.tools.java.MemberDefinition;
    41 /**
    42  * ImplementationType represents any non-special class which implements
    43  * one or more interfaces which inherit from java.rmi.Remote.
    44  * <p>
    45  * The static forImplementation(...) method must be used to obtain an instance,
    46  * and will return null if the ClassDefinition is non-conforming.
    47  *
    48  * @author      Bryan Atsatt
    49  */
    50 public class ImplementationType extends ClassType {
    52     //_____________________________________________________________________
    53     // Public Interfaces
    54     //_____________________________________________________________________
    56     /**
    57      * Create an ImplementationType for the given class.
    58      *
    59      * If the class is not a properly formed or if some other error occurs, the
    60      * return value will be null, and errors will have been reported to the
    61      * supplied BatchEnvironment.
    62      */
    63     public static ImplementationType forImplementation(ClassDefinition classDef,
    64                                                        ContextStack stack,
    65                                                        boolean quiet) {
    66         if (stack.anyErrors()) return null;
    68         boolean doPop = false;
    69         ImplementationType result = null;
    71         try {
    72             // Do we already have it?
    74             sun.tools.java.Type theType = classDef.getType();
    75             Type existing = getType(theType,stack);
    77             if (existing != null) {
    79                 if (!(existing instanceof ImplementationType)) return null; // False hit.
    81                                 // Yep, so return it...
    83                 return (ImplementationType) existing;
    85             }
    87             // Could this be an implementation?
    89             if (couldBeImplementation(quiet,stack,classDef)) {
    91                 // Yes, so check it...
    93                 ImplementationType it = new ImplementationType(stack, classDef);
    94                 putType(theType,it,stack);
    95                 stack.push(it);
    96                 doPop = true;
    98                 if (it.initialize(stack,quiet)) {
    99                     stack.pop(true);
   100                     result = it;
   101                 } else {
   102                     removeType(theType,stack);
   103                     stack.pop(false);
   104                 }
   105             }
   106         } catch (CompilerError e) {
   107             if (doPop) stack.pop(false);
   108         }
   110         return result;
   111     }
   113     /**
   114      * Return a string describing this type.
   115      */
   116     public String getTypeDescription () {
   117         return "Implementation";
   118     }
   121     //_____________________________________________________________________
   122     // Internal Interfaces
   123     //_____________________________________________________________________
   125     /**
   126      * Create a ImplementationType instance for the given class.  The resulting
   127      * object is not yet completely initialized.
   128      */
   129     private ImplementationType(ContextStack stack, ClassDefinition classDef) {
   130         super(TYPE_IMPLEMENTATION | TM_CLASS | TM_COMPOUND,classDef,stack); // Use special constructor.
   131     }
   134     private static boolean couldBeImplementation(boolean quiet, ContextStack stack,
   135                                                  ClassDefinition classDef) {
   136         boolean result = false;
   137         BatchEnvironment env = stack.getEnv();
   139         try {
   140             if (!classDef.isClass()) {
   141                 failedConstraint(17,quiet,stack,classDef.getName());
   142             } else {
   143                 result = env.defRemote.implementedBy(env, classDef.getClassDeclaration());
   144                 if (!result) failedConstraint(8,quiet,stack,classDef.getName());
   145             }
   146         } catch (ClassNotFound e) {
   147             classNotFound(stack,e);
   148         }
   150         return result;
   151     }
   154     /**
   155      * Initialize this instance.
   156      */
   157     private boolean initialize (ContextStack stack, boolean quiet) {
   159         boolean result = false;
   160         ClassDefinition theClass = getClassDefinition();
   162         if (initParents(stack)) {
   164             // Make up our collections...
   166             Vector directInterfaces = new Vector();
   167             Vector directMethods = new Vector();
   169             // Check interfaces...
   171             try {
   172                 if (addRemoteInterfaces(directInterfaces,true,stack) != null) {
   174                     boolean haveRemote = false;
   176                     // Get methods from all interfaces...
   178                     for (int i = 0; i < directInterfaces.size(); i++) {
   179                         InterfaceType theInt = (InterfaceType) directInterfaces.elementAt(i);
   180                         if (theInt.isType(TYPE_REMOTE) ||
   181                             theInt.isType(TYPE_JAVA_RMI_REMOTE)) {
   182                             haveRemote = true;
   183                         }
   185                         copyRemoteMethods(theInt,directMethods);
   186                     }
   188                     // Make sure we have at least one remote interface...
   190                     if (!haveRemote) {
   191                         failedConstraint(8,quiet,stack,getQualifiedName());
   192                         return false;
   193                     }
   195                     // Now check the methods to ensure we have the
   196                     // correct throws clauses...
   198                     if (checkMethods(theClass,directMethods,stack,quiet)) {
   200                         // We're ok, so pass 'em up...
   202                         result = initialize(directInterfaces,directMethods,null,stack,quiet);
   203                     }
   204                 }
   205             } catch (ClassNotFound e) {
   206                 classNotFound(stack,e);
   207             }
   208         }
   210         return result;
   211     }
   213     private static void copyRemoteMethods(InterfaceType type, Vector list) {
   215         if (type.isType(TYPE_REMOTE)) {
   217             // Copy all the unique methods from type...
   219             Method[] allMethods = type.getMethods();
   221             for (int i = 0; i < allMethods.length; i++) {
   222                 Method theMethod = allMethods[i];
   224                 if (!list.contains(theMethod)) {
   225                     list.addElement(theMethod);
   226                 }
   227             }
   229             // Now recurse thru all inherited interfaces...
   231             InterfaceType[] allInterfaces = type.getInterfaces();
   233             for (int i = 0; i < allInterfaces.length; i++) {
   234                 copyRemoteMethods(allInterfaces[i],list);
   235             }
   236         }
   237     }
   239     // Walk all methods of the class, and for each that is already in
   240     // the list, call setImplExceptions()...
   242     private boolean checkMethods(ClassDefinition theClass, Vector list,
   243                                  ContextStack stack, boolean quiet) {
   245         // Convert vector to array...
   247         Method[] methods = new Method[list.size()];
   248         list.copyInto(methods);
   250         for (MemberDefinition member = theClass.getFirstMember();
   251              member != null;
   252              member = member.getNextMember()) {
   254             if (member.isMethod() && !member.isConstructor()
   255                 && !member.isInitializer()) {
   257                 // It's a method...
   259                 if (!updateExceptions(member,methods,stack,quiet)) {
   260                     return false;
   261                 }
   262             }
   263         }
   264         return true;
   265     }
   267     private boolean updateExceptions (MemberDefinition implMethod, Method[] list,
   268                                       ContextStack stack, boolean quiet) {
   269         int length = list.length;
   270         String implMethodSig = implMethod.toString();
   272         for (int i = 0; i < length; i++) {
   273             Method existingMethod = list[i];
   274             MemberDefinition existing = existingMethod.getMemberDefinition();
   276             // Do we have a matching method?
   278             if (implMethodSig.equals(existing.toString())) {
   280                 // Yes, so create exception list...
   282                 try {
   283                     ValueType[] implExcept = getMethodExceptions(implMethod,quiet,stack);
   284                     existingMethod.setImplExceptions(implExcept);
   285                 } catch (Exception e) {
   286                     return false;
   287                 }
   288             }
   289         }
   290         return true;
   291     }
   292 }

mercurial