jdk/src/share/classes/com/sun/tools/jdi/InterfaceTypeImpl.java
author sla
Mon, 16 Dec 2013 11:04:59 +0100
changeset 22049 15b9d7db4ad7
parent 14342 8435a30053c1
child 24125 b85eeaae56c7
permissions -rw-r--r--
8028430: JDI: ReferenceType.visibleMethods() return wrong visible methods Reviewed-by: mchung
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;
22049
15b9d7db4ad7 8028430: JDI: ReferenceType.visibleMethods() return wrong visible methods
sla
parents: 14342
diff changeset
    35
import java.util.Set;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.lang.ref.SoftReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class InterfaceTypeImpl extends ReferenceTypeImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
                               implements InterfaceType {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private SoftReference<List<InterfaceType>> superinterfacesRef = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    protected InterfaceTypeImpl(VirtualMachine aVm,long aRef) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        super(aVm, aRef);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    public List<InterfaceType> superinterfaces() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        List<InterfaceType> superinterfaces = (superinterfacesRef == null) ? null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                                     superinterfacesRef.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        if (superinterfaces == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            superinterfaces = getInterfaces();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            superinterfaces = Collections.unmodifiableList(superinterfaces);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            superinterfacesRef = new SoftReference<List<InterfaceType>>(superinterfaces);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        return superinterfaces;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public List<InterfaceType> subinterfaces() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        List<InterfaceType> subs = new ArrayList<InterfaceType>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        for (ReferenceType refType : vm.allClasses()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            if (refType instanceof InterfaceType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                InterfaceType interfaze = (InterfaceType)refType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                if (interfaze.isPrepared() && interfaze.superinterfaces().contains(this)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                    subs.add(interfaze);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        return subs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public List<ClassType> implementors() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        List<ClassType> implementors = new ArrayList<ClassType>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        for (ReferenceType refType : vm.allClasses()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            if (refType instanceof ClassType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                ClassType clazz = (ClassType)refType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                if (clazz.isPrepared() && clazz.interfaces().contains(this)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                    implementors.add(clazz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        return implementors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
22049
15b9d7db4ad7 8028430: JDI: ReferenceType.visibleMethods() return wrong visible methods
sla
parents: 14342
diff changeset
    84
    @Override
15b9d7db4ad7 8028430: JDI: ReferenceType.visibleMethods() return wrong visible methods
sla
parents: 14342
diff changeset
    85
    void addVisibleMethods(Map<String, Method> methodMap, Set<InterfaceType> seenInterfaces) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
         * Add methods from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
         * parent types first, so that the methods in this class will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
         * overwrite them in the hash table
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        for (InterfaceType interfaze : superinterfaces()) {
22049
15b9d7db4ad7 8028430: JDI: ReferenceType.visibleMethods() return wrong visible methods
sla
parents: 14342
diff changeset
    93
            if (!seenInterfaces.contains(interfaze)) {
15b9d7db4ad7 8028430: JDI: ReferenceType.visibleMethods() return wrong visible methods
sla
parents: 14342
diff changeset
    94
                ((InterfaceTypeImpl)interfaze).addVisibleMethods(methodMap, seenInterfaces);
15b9d7db4ad7 8028430: JDI: ReferenceType.visibleMethods() return wrong visible methods
sla
parents: 14342
diff changeset
    95
                seenInterfaces.add(interfaze);
15b9d7db4ad7 8028430: JDI: ReferenceType.visibleMethods() return wrong visible methods
sla
parents: 14342
diff changeset
    96
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        addToMethodMap(methodMap, methods());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public List<Method> allMethods() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        ArrayList<Method> list = new ArrayList<Method>(methods());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
         * It's more efficient if don't do this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
         * recursively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        for (InterfaceType interfaze : allSuperinterfaces()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            list.addAll(interfaze.methods());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    List<InterfaceType> allSuperinterfaces() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        ArrayList<InterfaceType> list = new ArrayList<InterfaceType>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        addSuperinterfaces(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    void addSuperinterfaces(List<InterfaceType> list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
         * This code is a little strange because it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
         * builds the list with a more suitable order than the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
         * depth-first approach a normal recursive solution would
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
         * take. Instead, all direct superinterfaces precede all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
         * indirect ones.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
         * Get a list of direct superinterfaces that's not already in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
         * list being built.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        List<InterfaceType> immediate = new ArrayList<InterfaceType>(superinterfaces());
11277
e3a1c90dd439 7117053: Fix build warnings in com/sun/tools/jdi/*
jjh
parents: 5506
diff changeset
   136
        Iterator<InterfaceType> iter = immediate.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        while (iter.hasNext()) {
11277
e3a1c90dd439 7117053: Fix build warnings in com/sun/tools/jdi/*
jjh
parents: 5506
diff changeset
   138
            InterfaceType interfaze = iter.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            if (list.contains(interfaze)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                iter.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
         * Add all new direct superinterfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        list.addAll(immediate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
         * Recurse for all new direct superinterfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        iter = immediate.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            InterfaceTypeImpl interfaze = (InterfaceTypeImpl)iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            interfaze.addSuperinterfaces(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    boolean isAssignableTo(ReferenceType type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        // Exact match?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (this.equals(type)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            // Try superinterfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            for (InterfaceType interfaze : superinterfaces()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                if (((InterfaceTypeImpl)interfaze).isAssignableTo(type)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    List<InterfaceType> inheritedTypes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        return superinterfaces();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public boolean isInitialized() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        return isPrepared();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
       return "interface " + name() + " (" + loaderString() + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
}