jdk/src/jdk.jdi/share/classes/com/sun/tools/jdi/InterfaceTypeImpl.java
changeset 25859 3317bb8137f4
parent 24195 705325a63a58
child 30350 c055af074935
equal deleted inserted replaced
25858:836adbf7a2cd 25859:3317bb8137f4
       
     1 /*
       
     2  * Copyright (c) 1998, 2011, 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.tools.jdi;
       
    27 
       
    28 import com.sun.jdi.*;
       
    29 
       
    30 import java.util.List;
       
    31 import java.util.ArrayList;
       
    32 import java.util.Collections;
       
    33 import java.util.Set;
       
    34 import java.lang.ref.SoftReference;
       
    35 
       
    36 final public class InterfaceTypeImpl extends InvokableTypeImpl
       
    37                                      implements InterfaceType {
       
    38 
       
    39     private static class IResult implements InvocationResult {
       
    40         final private JDWP.InterfaceType.InvokeMethod rslt;
       
    41 
       
    42         public IResult(JDWP.InterfaceType.InvokeMethod rslt) {
       
    43             this.rslt = rslt;
       
    44         }
       
    45 
       
    46         @Override
       
    47         public ObjectReferenceImpl getException() {
       
    48             return rslt.exception;
       
    49         }
       
    50 
       
    51         @Override
       
    52         public ValueImpl getResult() {
       
    53             return rslt.returnValue;
       
    54         }
       
    55 
       
    56     }
       
    57 
       
    58     private SoftReference<List<InterfaceType>> superinterfacesRef = null;
       
    59 
       
    60     protected InterfaceTypeImpl(VirtualMachine aVm,long aRef) {
       
    61         super(aVm, aRef);
       
    62     }
       
    63 
       
    64     public List<InterfaceType> superinterfaces() {
       
    65         List<InterfaceType> superinterfaces = (superinterfacesRef == null) ? null :
       
    66                                      superinterfacesRef.get();
       
    67         if (superinterfaces == null) {
       
    68             superinterfaces = getInterfaces();
       
    69             superinterfaces = Collections.unmodifiableList(superinterfaces);
       
    70             superinterfacesRef = new SoftReference<List<InterfaceType>>(superinterfaces);
       
    71         }
       
    72         return superinterfaces;
       
    73     }
       
    74 
       
    75     public List<InterfaceType> subinterfaces() {
       
    76         List<InterfaceType> subs = new ArrayList<InterfaceType>();
       
    77         for (ReferenceType refType : vm.allClasses()) {
       
    78             if (refType instanceof InterfaceType) {
       
    79                 InterfaceType interfaze = (InterfaceType)refType;
       
    80                 if (interfaze.isPrepared() && interfaze.superinterfaces().contains(this)) {
       
    81                     subs.add(interfaze);
       
    82                 }
       
    83             }
       
    84         }
       
    85         return subs;
       
    86     }
       
    87 
       
    88     public List<ClassType> implementors() {
       
    89         List<ClassType> implementors = new ArrayList<ClassType>();
       
    90         for (ReferenceType refType : vm.allClasses()) {
       
    91             if (refType instanceof ClassType) {
       
    92                 ClassType clazz = (ClassType)refType;
       
    93                 if (clazz.isPrepared() && clazz.interfaces().contains(this)) {
       
    94                     implementors.add(clazz);
       
    95                 }
       
    96             }
       
    97         }
       
    98         return implementors;
       
    99     }
       
   100 
       
   101     public boolean isInitialized() {
       
   102         return isPrepared();
       
   103     }
       
   104 
       
   105     public String toString() {
       
   106        return "interface " + name() + " (" + loaderString() + ")";
       
   107     }
       
   108 
       
   109     @Override
       
   110     InvocationResult waitForReply(PacketStream stream) throws JDWPException {
       
   111         return new IResult(JDWP.InterfaceType.InvokeMethod.waitForReply(vm, stream));
       
   112     }
       
   113 
       
   114     @Override
       
   115     CommandSender getInvokeMethodSender(final ThreadReferenceImpl thread,
       
   116                                         final MethodImpl method,
       
   117                                         final ValueImpl[] args,
       
   118                                         final int options) {
       
   119         return () ->
       
   120             JDWP.InterfaceType.InvokeMethod.enqueueCommand(vm,
       
   121                                                            InterfaceTypeImpl.this,
       
   122                                                            thread,
       
   123                                                            method.ref(),
       
   124                                                            args,
       
   125                                                            options);
       
   126     }
       
   127 
       
   128     @Override
       
   129     ClassType superclass() {
       
   130         return null;
       
   131     }
       
   132 
       
   133     @Override
       
   134     List<InterfaceType> interfaces() {
       
   135         return superinterfaces();
       
   136     }
       
   137 
       
   138     @Override
       
   139     boolean canInvoke(Method method) {
       
   140         // method must be directly in this interface
       
   141         return this.equals(method.declaringType());
       
   142     }
       
   143 }