⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 comsharedstubbuilder.java

📁 Java调用Windows API,支持Office
💻 JAVA
字号:
package org.jawin.tools;

import java.io.*;

/**
 * FIXME - see COMSharedStubDriver
 *
 * @version     $Revision: 1.3 $
 * @author      Stuart Halloway, http://www.relevancellc.com/halloway/weblog/
 */
public class COMSharedStubBuilder {

  public String decl;
  public String baseName;
  public String baseSig;
  public String methodIntro;
  public String methodInvoke;
  public String invokeClose;
  public String methodExit;
  public String methodClose;
  private int typeLength;
  /**
   * if the types array has n elements and you are building only permutations of n
   * signatures, where n < m, you should use the last m elements.  This index
   * offsets past the part of the beginning of the types array that is ignored
   */
  private int typeIndex;
  /**
   * array of legal types for each arg slot, e.g. COMType[3] is the array of COMTypes
   * that can be used as arg3.  Typically only the last subarray is different, to allow
   * for out params
   */
  private COMType[][] types;
  private boolean done;
  /** 
   * current position in the types array.  Use to loop through every permutation of 
   * types 
   */
  private int[] pos;

  public COMType[] getTypeArray(int n) {
    return types[n+typeIndex];
  }

  public COMType getTypeAtPos(int n) {
    return getTypeArray(n)[pos[n]];
  }
  
  public void buildStubs(COMType[][] types, int depth, PrintStream ps, boolean impl) {
    this.types = types;
    for (int n=depth; n>0; n--) {
      buildDepth(n, ps, impl);
      typeIndex++;
    }
  }

  public String getName(COMType t, boolean isNative) {
      return isNative ? t.nativeName : t.name;
  }

  public String retval(boolean isNative) {
    COMType t = getTypeAtPos(pos.length-1);
    if (t.isOut) {
      return getName(t, isNative);
    }
    else
      return "void";
  }

  public String returnStatement() {
    COMType t = getTypeAtPos(pos.length-1);
    if (t.isOut)
      return t.returnStatement;
    else
      return "";
  }

  public void buildDepth(int depth, PrintStream ps, boolean impl) {
    if (depth > 4) {
      throw new Error("too deep -- this would build a lot of stubs!");
    }
    System.out.println("//methods with " + depth  + " args");
    typeLength = types.length;
    pos = new int[depth];
    done = false;
    while (done == false) {
      ps.print(decl);
      ps.print(retval(impl));
      ps.print(baseName);
      ps.print(varName());
      ps.print(baseSig);
      ps.print(varSig(true, impl));
      if (impl) {
	ps.print(replace(methodIntro, '%', new Integer(depth+1).toString()));
	ps.print(preInvokeArgs());
	ps.print(methodInvoke);
	ps.print(listArgs(depth));
	ps.print(invokeClose);
	ps.print(postInvokeArgs());
	ps.print(methodExit);
	ps.print(returnStatement());
      }
      ps.print(methodClose);
      incPos();
    }
  }

  public void incPos() {
    int last = pos.length;
    while (last > 0) {
      last--;
      pos[last]++;
      if (pos[last] < getTypeArray(last).length)
	break;
      else {
	pos[last] = 0;
	if (last == 0) {
	  done = true;
	}
      }
    }
  }

  public String listArgs(int depth) {
    StringBuffer sb = new StringBuffer();
    for (int n=0; n<depth; n++) {
      sb.append(", ").append("inv").append(n);
    }
    return sb.toString();
  }

  public String varName() {
    StringBuffer sb = new StringBuffer();
    for (int n=0; n<pos.length; n++) {
      sb.append(getTypeAtPos(n).code);
    }
    return sb.toString();
  }

  public String varSig(boolean initialComma, boolean isNative) {
    StringBuffer sb = new StringBuffer();
    for (int n=0; n<pos.length; n++) {
      if (!getTypeAtPos(n).isOut) {
	if ((n != 0) || (initialComma)) {
	  sb.append(", ");
	}
	sb.append(getName(getTypeAtPos(n), isNative)).append(" arg").append(n);
      }
    }
    return sb.toString();
  }

  public String preInvokeArgs() {
    StringBuffer sb = new StringBuffer();
    for (int n=0; n<pos.length; n++) {
      sb.append(replace(getTypeAtPos(n).preInvoke, '%', new Integer(n).toString()));
    }
    return sb.toString();
  }

  public String postInvokeArgs() {
    StringBuffer sb = new StringBuffer();
    for (int n=0; n<pos.length; n++) {
      sb.append(replace(getTypeAtPos(n).postInvoke, '%', new Integer(n).toString()));
    }
    return sb.toString();
  }
  
  public String replace(String src, char match, String rep) {
    if (-1 != rep.lastIndexOf(match)) {
      throw new Error("cannot include match char in replacement string");
    }
    StringBuffer work = new StringBuffer(src);
    int n = 0;
    while (n<work.length()) {
      if (work.charAt(n) == match) {
        work.replace(n, n+1, rep);
      }
      n++;
    }
    return work.toString();
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -