コード例 #1
0
  /** Returns the constant string at the specified offset */
  public static String get_constant_str(ConstantPool pool, int index) {

    Constant c = pool.getConstant(index);
    if (c instanceof ConstantUtf8) return ((ConstantUtf8) c).getBytes();
    else if (c instanceof ConstantClass) {
      ConstantClass cc = (ConstantClass) c;
      return cc.getBytes(pool) + " [" + cc.getNameIndex() + "]";
    } else assert false : "unexpected constant " + c + " class " + c.getClass();
    return (null);
  }
コード例 #2
0
  /**
   * Returns all related classes
   *
   * @param start
   * @return
   */
  private static Collection<Class<?>> getAllRelatedClasses(Class<?> start) {

    List<Class<?>> rval = new ArrayList<Class<?>>();
    JavaClass lookupClass;

    // In case the class fails, return empty.
    try {
      lookupClass = Repository.lookupClass(start);
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
      return rval;
    }

    ConstantPool constantPool = lookupClass.getConstantPool();

    int length = constantPool.getLength();
    for (int i = 0; i < length; i++) {
      Constant constant = constantPool.getConstant(i);
      if (constant instanceof ConstantClass) {
        ConstantClass cc = (ConstantClass) constant;
        ConstantUtf8 constant2 = (ConstantUtf8) constantPool.getConstant(cc.getNameIndex());

        // In case a subclass fails, skip, but print warning.
        try {
          String toLoad = constant2.getBytes().replace('/', '.');
          if (toLoad.contains("[")) continue;
          Class<?> forName = Class.forName(toLoad);
          rval.add(forName);
        } catch (ClassNotFoundException e) {
          e.printStackTrace();
        }
      }
    }

    return rval;
  }