SpecialInterfaceType.java revision 608:7e06bf1dcb09
1220497Smarkm/*
2220497Smarkm * Copyright (c) 1998, 2007, Oracle and/or its affiliates. All rights reserved.
3220497Smarkm * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4220497Smarkm *
5220497Smarkm * This code is free software; you can redistribute it and/or modify it
6220497Smarkm * under the terms of the GNU General Public License version 2 only, as
7220497Smarkm * published by the Free Software Foundation.  Oracle designates this
8220497Smarkm * particular file as subject to the "Classpath" exception as provided
9220497Smarkm * by Oracle in the LICENSE file that accompanied this code.
10220497Smarkm *
11220497Smarkm * This code is distributed in the hope that it will be useful, but WITHOUT
12220497Smarkm * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13220497Smarkm * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14220497Smarkm * version 2 for more details (a copy is included in the LICENSE file that
15220497Smarkm * accompanied this code).
16220497Smarkm *
17220497Smarkm * You should have received a copy of the GNU General Public License version
18220497Smarkm * 2 along with this work; if not, write to the Free Software Foundation,
19220497Smarkm * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20220497Smarkm *
21220497Smarkm * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22220497Smarkm * or visit www.oracle.com if you need additional information or have any
23220497Smarkm * questions.
24220497Smarkm */
25220497Smarkm
26220497Smarkm/*
27220497Smarkm * Licensed Materials - Property of IBM
28220497Smarkm * RMI-IIOP v1.0
29220497Smarkm * Copyright IBM Corp. 1998 1999  All Rights Reserved
30220497Smarkm *
31220497Smarkm */
32220497Smarkm
33220497Smarkmpackage sun.rmi.rmic.iiop;
34220497Smarkm
35220497Smarkmimport sun.tools.java.ClassNotFound;
36220497Smarkmimport sun.tools.java.CompilerError;
37220497Smarkmimport sun.tools.java.Identifier;
38220497Smarkmimport sun.tools.java.ClassDeclaration;
39220497Smarkmimport sun.tools.java.ClassDefinition;
40220497Smarkm
41220497Smarkm/**
42220497Smarkm * SpecialInterfaceType represents any one of the following types:
43220497Smarkm * <pre>
44220497Smarkm *    java.rmi.Remote
45220497Smarkm *    java.io.Serializable
46220497Smarkm *    java.io.Externalizable
47220497Smarkm *    org.omg.CORBA.Object
48220497Smarkm *    org.omg.CORBA.portable.IDLEntity
49220497Smarkm * </pre>
50220497Smarkm * all of which are treated as special cases. For all but CORBA.Object,
51220497Smarkm * the type must match exactly. For CORBA.Object, the type must either be
52220497Smarkm * CORBA.Object or inherit from it.
53220497Smarkm * <p>
54220497Smarkm * The static forSpecial(...) method must be used to obtain an instance, and
55220497Smarkm * will return null if the type is non-conforming.
56220497Smarkm *
57220497Smarkm * @author  Bryan Atsatt
58220497Smarkm */
59220497Smarkmpublic class SpecialInterfaceType extends InterfaceType {
60220497Smarkm
61220497Smarkm    //_____________________________________________________________________
62220497Smarkm    // Public Interfaces
63221471Sobrien    //_____________________________________________________________________
64220497Smarkm
65220497Smarkm    /**
66220497Smarkm     * Create a SpecialInterfaceType object for the given class.
67220497Smarkm     *
68220497Smarkm     * If the class is not a properly formed or if some other error occurs, the
69220497Smarkm     * return value will be null, and errors will have been reported to the
70220497Smarkm     * supplied BatchEnvironment.
71220497Smarkm     */
72220497Smarkm    public static SpecialInterfaceType forSpecial ( ClassDefinition theClass,
73220497Smarkm                                                    ContextStack stack) {
74220497Smarkm
75220497Smarkm        if (stack.anyErrors()) return null;
76220497Smarkm
77220497Smarkm        // Do we already have it?
78220497Smarkm
79220497Smarkm        sun.tools.java.Type type = theClass.getType();
80220497Smarkm        Type existing = getType(type,stack);
81220497Smarkm
82220497Smarkm        if (existing != null) {
83220497Smarkm
84220497Smarkm            if (!(existing instanceof SpecialInterfaceType)) return null; // False hit.
85220497Smarkm
86220497Smarkm            // Yep, so return it...
87220497Smarkm
88220497Smarkm            return (SpecialInterfaceType) existing;
89220497Smarkm        }
90220497Smarkm
91220497Smarkm        // Is it special?
92220497Smarkm
93220497Smarkm        if (isSpecial(type,theClass,stack)) {
94220497Smarkm
95220497Smarkm            // Yes...
96220497Smarkm
97220497Smarkm            SpecialInterfaceType result = new SpecialInterfaceType(stack,0,theClass);
98220497Smarkm            putType(type,result,stack);
99220497Smarkm            stack.push(result);
100220497Smarkm
101220497Smarkm            if (result.initialize(type,stack)) {
102220497Smarkm                stack.pop(true);
103220497Smarkm                return result;
104220497Smarkm            } else {
105220497Smarkm                removeType(type,stack);
106220497Smarkm                stack.pop(false);
107220497Smarkm                return null;
108220497Smarkm            }
109220497Smarkm        }
110220497Smarkm        return null;
111220497Smarkm    }
112220497Smarkm
113220497Smarkm    /**
114220497Smarkm     * Return a string describing this type.
115220497Smarkm     */
116220497Smarkm    public String getTypeDescription () {
117220497Smarkm        return "Special interface";
118220497Smarkm    }
119220497Smarkm
120220497Smarkm    //_____________________________________________________________________
121220497Smarkm    // Subclass/Internal Interfaces
122220497Smarkm    //_____________________________________________________________________
123220497Smarkm
124220497Smarkm    /**
125220497Smarkm     * Create an SpecialInterfaceType instance for the given class.
126220497Smarkm     */
127220497Smarkm    private SpecialInterfaceType(ContextStack stack, int typeCode,
128220497Smarkm                                 ClassDefinition theClass) {
129220497Smarkm        super(stack,typeCode | TM_SPECIAL_INTERFACE | TM_INTERFACE | TM_COMPOUND, theClass);
130220497Smarkm        setNames(theClass.getName(),null,null); // Fixed in initialize.
131220497Smarkm    }
132220497Smarkm
133220497Smarkm    private static boolean isSpecial(sun.tools.java.Type type,
134220497Smarkm                                     ClassDefinition theClass,
135220497Smarkm                                     ContextStack stack) {
136220497Smarkm        if (type.isType(TC_CLASS)) {
137220497Smarkm            Identifier id = type.getClassName();
138220497Smarkm
139220497Smarkm            if (id.equals(idRemote)) return true;
140220497Smarkm            if (id == idJavaIoSerializable) return true;
141220497Smarkm            if (id == idJavaIoExternalizable) return true;
142220497Smarkm            if (id == idCorbaObject) return true;
143220497Smarkm            if (id == idIDLEntity) return true;
144220497Smarkm            BatchEnvironment env = stack.getEnv();
145220497Smarkm            try {
146220497Smarkm                if (env.defCorbaObject.implementedBy(env,theClass.getClassDeclaration())) return true;
147220497Smarkm            } catch (ClassNotFound e) {
148220497Smarkm                classNotFound(stack,e);
149220497Smarkm            }
150220497Smarkm        }
151220497Smarkm        return false;
152220497Smarkm    }
153220497Smarkm
154220497Smarkm    private boolean initialize(sun.tools.java.Type type, ContextStack stack) {
155220497Smarkm
156220497Smarkm        int typeCode = TYPE_NONE;
157220497Smarkm        Identifier id = null;
158220497Smarkm        String idlName = null;
159220497Smarkm        String[] idlModuleName = null;
160220497Smarkm        boolean constant = stack.size() > 0 && stack.getContext().isConstant();
161220497Smarkm
162220497Smarkm        if (type.isType(TC_CLASS)) {
163220497Smarkm            id = type.getClassName();
164220497Smarkm
165220497Smarkm            if (id.equals(idRemote)) {
166220497Smarkm                typeCode = TYPE_JAVA_RMI_REMOTE;
167220497Smarkm                idlName = IDL_JAVA_RMI_REMOTE;
168220497Smarkm                idlModuleName = IDL_JAVA_RMI_MODULE;
169220497Smarkm            } else if (id == idJavaIoSerializable) {
170220497Smarkm                typeCode = TYPE_ANY;
171220497Smarkm                idlName = IDL_SERIALIZABLE;
172220497Smarkm                idlModuleName = IDL_JAVA_IO_MODULE;
173220497Smarkm            } else if (id == idJavaIoExternalizable) {
174220497Smarkm                typeCode = TYPE_ANY;
175220497Smarkm                idlName = IDL_EXTERNALIZABLE;
176220497Smarkm                idlModuleName = IDL_JAVA_IO_MODULE;
177220497Smarkm            } else if (id == idIDLEntity) {
178220497Smarkm                typeCode = TYPE_ANY;
179220497Smarkm                idlName = IDL_IDLENTITY;
180220497Smarkm                idlModuleName = IDL_ORG_OMG_CORBA_PORTABLE_MODULE;
181220497Smarkm            } else {
182220497Smarkm
183220497Smarkm                typeCode = TYPE_CORBA_OBJECT;
184220497Smarkm
185220497Smarkm                // Is it exactly org.omg.CORBA.Object?
186220497Smarkm
187220497Smarkm                if (id == idCorbaObject) {
188220497Smarkm
189220497Smarkm                    // Yes, so special case...
190220497Smarkm
191220497Smarkm                    idlName = IDLNames.getTypeName(typeCode,constant);
192220497Smarkm                    idlModuleName = null;
193220497Smarkm
194220497Smarkm                } else {
195220497Smarkm
196220497Smarkm                    // No, so get the correct names...
197220497Smarkm
198220497Smarkm                    try {
199220497Smarkm
200220497Smarkm                        // These can fail if we get case-sensitive name matches...
201220497Smarkm
202220497Smarkm                        idlName = IDLNames.getClassOrInterfaceName(id,env);
203220497Smarkm                        idlModuleName = IDLNames.getModuleNames(id,isBoxed(),env);
204220497Smarkm
205220497Smarkm                    } catch (Exception e) {
206220497Smarkm                        failedConstraint(7,false,stack,id.toString(),e.getMessage());
207220497Smarkm                        throw new CompilerError("");
208220497Smarkm                    }
209220497Smarkm                }
210220497Smarkm            }
211220497Smarkm        }
212220497Smarkm
213220497Smarkm        if (typeCode == TYPE_NONE) {
214220497Smarkm            return false;
215220497Smarkm        }
216220497Smarkm
217220497Smarkm        // Reset type code...
218220497Smarkm
219220497Smarkm        setTypeCode(typeCode | TM_SPECIAL_INTERFACE | TM_INTERFACE | TM_COMPOUND);
220220497Smarkm
221220497Smarkm        // Set names
222220497Smarkm
223220497Smarkm        if (idlName == null) {
224220497Smarkm            throw new CompilerError("Not a special type");
225220497Smarkm        }
226220497Smarkm
227220497Smarkm        setNames(id,idlModuleName,idlName);
228220497Smarkm
229220497Smarkm        // Initialize CompoundType...
230220497Smarkm
231220497Smarkm        return initialize(null,null,null,stack,false);
232220497Smarkm    }
233220497Smarkm}
234220497Smarkm