1 /*
   2  * Copyright (c) 2000, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 package sun.jvm.hotspot.runtime;
  26 
  27 import java.util.*;
  28 import sun.jvm.hotspot.debugger.*;
  29 import sun.jvm.hotspot.debugger.cdbg.CDebugger;
  30 import sun.jvm.hotspot.debugger.cdbg.ClosestSymbol;
  31 import sun.jvm.hotspot.debugger.cdbg.LoadObject;
  32 import sun.jvm.hotspot.types.*;
  33 import sun.jvm.hotspot.HotSpotTypeDataBase;
  34 
  35 /** This provides a factory to create instances where the base virtual
  36  * type is know and the expected subclasses are within a particular
  37  * package. */
  38 
  39 public class VirtualBaseConstructor<T> extends InstanceConstructor {
  40   private TypeDataBase db;
  41   private HashMap      map; // Map<String, Class>
  42   private Type         baseType;
  43   private Class        unknownTypeHandler;
  44 
  45   public VirtualBaseConstructor(TypeDataBase db, Type baseType, String packageName, Class unknownTypeHandler) {
  46     this.db = (HotSpotTypeDataBase)db;
  47     map     = new HashMap();
  48     this.baseType = baseType;
  49     this.unknownTypeHandler = unknownTypeHandler;
  50     // Try to find mirror types for each of the types.  If there isn't
  51     // a direct mirror then try to find an instantiable superclass and
  52     // treat it as that.
  53     for (Iterator iter = db.getTypes(); iter.hasNext(); ) {
  54       Type t = (Type) iter.next();
  55       Type superType = t;
  56       while (superType != null && superType != baseType) {
  57         superType = superType.getSuperclass();
  58       }
  59       if (superType == baseType) {
  60         superType = t;
  61         Class c = null;
  62         while (c == null && superType != null) {
  63           try {
  64             c = Class.forName(packageName + "." + superType.getName());
  65           } catch (Exception e) {
  66           }
  67           if (c == null) superType = superType.getSuperclass();
  68         }
  69         if (c == null) {
  70           c = unknownTypeHandler;
  71         }
  72         map.put(t.getName(), c);
  73       }
  74     }
  75   }
  76 
  77   /** Instantiate the most-precisely typed wrapper object available
  78       for the type of the given Address. If no type in the mapping
  79       matched the type of the Address, throws a WrongTypeException.
  80       Returns null for a null address (similar behavior to
  81       VMObjectFactory). */
  82   public VMObject instantiateWrapperFor(Address addr) throws WrongTypeException {
  83     if (addr == null) {
  84       return null;
  85     }
  86 
  87     Type type = db.findDynamicTypeForAddress(addr, baseType);
  88     if (type != null) {
  89         return (VMObject) VMObjectFactory.newObject((Class) map.get(type.getName()), addr);
  90     } else if (unknownTypeHandler != null) {
  91         return (VMObject) VMObjectFactory.newObject(unknownTypeHandler, addr);
  92     }
  93 
  94     throw newWrongTypeException(addr);
  95   }
  96 }