jdk/src/share/classes/com/sun/tools/jdi/InterfaceTypeImpl.java
author alanb
Fri, 02 Nov 2012 15:50:11 +0000
changeset 14342 8435a30053c1
parent 11277 e3a1c90dd439
child 22049 15b9d7db4ad7
permissions -rw-r--r--
7197491: update copyright year to match last edit in jdk8 jdk repository Reviewed-by: chegar, ksrini
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 11277
diff changeset
     2
 * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.jdi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import com.sun.jdi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.lang.ref.SoftReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class InterfaceTypeImpl extends ReferenceTypeImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
                               implements InterfaceType {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private SoftReference<List<InterfaceType>> superinterfacesRef = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    protected InterfaceTypeImpl(VirtualMachine aVm,long aRef) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        super(aVm, aRef);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    public List<InterfaceType> superinterfaces() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        List<InterfaceType> superinterfaces = (superinterfacesRef == null) ? null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
                                     superinterfacesRef.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        if (superinterfaces == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            superinterfaces = getInterfaces();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            superinterfaces = Collections.unmodifiableList(superinterfaces);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            superinterfacesRef = new SoftReference<List<InterfaceType>>(superinterfaces);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        return superinterfaces;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public List<InterfaceType> subinterfaces() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        List<InterfaceType> subs = new ArrayList<InterfaceType>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        for (ReferenceType refType : vm.allClasses()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            if (refType instanceof InterfaceType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                InterfaceType interfaze = (InterfaceType)refType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                if (interfaze.isPrepared() && interfaze.superinterfaces().contains(this)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                    subs.add(interfaze);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        return subs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public List<ClassType> implementors() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        List<ClassType> implementors = new ArrayList<ClassType>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        for (ReferenceType refType : vm.allClasses()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            if (refType instanceof ClassType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                ClassType clazz = (ClassType)refType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                if (clazz.isPrepared() && clazz.interfaces().contains(this)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                    implementors.add(clazz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        return implementors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    void addVisibleMethods(Map<String, Method> methodMap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
         * Add methods from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
         * parent types first, so that the methods in this class will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
         * overwrite them in the hash table
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        for (InterfaceType interfaze : superinterfaces()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            ((InterfaceTypeImpl)interfaze).addVisibleMethods(methodMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        addToMethodMap(methodMap, methods());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public List<Method> allMethods() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        ArrayList<Method> list = new ArrayList<Method>(methods());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
         * It's more efficient if don't do this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
         * recursively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        for (InterfaceType interfaze : allSuperinterfaces()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            list.addAll(interfaze.methods());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    List<InterfaceType> allSuperinterfaces() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        ArrayList<InterfaceType> list = new ArrayList<InterfaceType>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        addSuperinterfaces(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    void addSuperinterfaces(List<InterfaceType> list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
         * This code is a little strange because it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
         * builds the list with a more suitable order than the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
         * depth-first approach a normal recursive solution would
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
         * take. Instead, all direct superinterfaces precede all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
         * indirect ones.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
         * Get a list of direct superinterfaces that's not already in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
         * list being built.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        List<InterfaceType> immediate = new ArrayList<InterfaceType>(superinterfaces());
11277
e3a1c90dd439 7117053: Fix build warnings in com/sun/tools/jdi/*
jjh
parents: 5506
diff changeset
   131
        Iterator<InterfaceType> iter = immediate.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        while (iter.hasNext()) {
11277
e3a1c90dd439 7117053: Fix build warnings in com/sun/tools/jdi/*
jjh
parents: 5506
diff changeset
   133
            InterfaceType interfaze = iter.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            if (list.contains(interfaze)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                iter.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
         * Add all new direct superinterfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        list.addAll(immediate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
         * Recurse for all new direct superinterfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        iter = immediate.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            InterfaceTypeImpl interfaze = (InterfaceTypeImpl)iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            interfaze.addSuperinterfaces(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    boolean isAssignableTo(ReferenceType type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        // Exact match?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if (this.equals(type)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            // Try superinterfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            for (InterfaceType interfaze : superinterfaces()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                if (((InterfaceTypeImpl)interfaze).isAssignableTo(type)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    List<InterfaceType> inheritedTypes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        return superinterfaces();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public boolean isInitialized() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        return isPrepared();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
       return "interface " + name() + " (" + loaderString() + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
}