home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1997 October / PCO1097.ISO / FilesBBS / WIN95 / IAVAZIP.EXE / DATA.Z / ConstantPoolInfo.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-08-18  |  4.3 KB  |  285 lines

  1. package com.sfs.hardcore;
  2.  
  3. import java.io.DataInputStream;
  4. import java.io.DataOutputStream;
  5. import java.io.IOException;
  6.  
  7. public class ConstantPoolInfo {
  8.    int type;
  9.    String name;
  10.    ConstantPoolInfo arg1 = null;
  11.    ConstantPoolInfo arg2 = null;
  12.    short index1 = -1;
  13.    short index2 = -1;
  14.    String strValue;
  15.    int intValue;
  16.    long longValue;
  17.    float floatValue;
  18.    double doubleValue;
  19.    public static final int CLASS = 7;
  20.    public static final int FIELDREF = 9;
  21.    public static final int METHODREF = 10;
  22.    public static final int STRING = 8;
  23.    public static final int INTEGER = 3;
  24.    public static final int FLOAT = 4;
  25.    public static final int LONG = 5;
  26.    public static final int DOUBLE = 6;
  27.    public static final int INTERFACE = 11;
  28.    public static final int NAMEANDTYPE = 12;
  29.    public static final int ASCIZ = 1;
  30.    public static final int UNICODE = 2;
  31.  
  32.    public boolean isEqual(ConstantPoolInfo var1) {
  33.       if (var1 == null) {
  34.          return false;
  35.       } else if (var1.type != this.type) {
  36.          return false;
  37.       } else {
  38.          switch (var1.type) {
  39.             case 1:
  40.             case 2:
  41.                if (var1.strValue.compareTo(this.strValue) != 0) {
  42.                   return false;
  43.                }
  44.  
  45.                return true;
  46.             case 3:
  47.                if (var1.intValue != this.intValue) {
  48.                   return false;
  49.                }
  50.  
  51.                return true;
  52.             case 4:
  53.                if (var1.floatValue != this.floatValue) {
  54.                   return false;
  55.                }
  56.  
  57.                return true;
  58.             case 5:
  59.                if (var1.longValue != this.longValue) {
  60.                   return false;
  61.                }
  62.  
  63.                return true;
  64.             case 6:
  65.                if (var1.doubleValue != this.doubleValue) {
  66.                   return false;
  67.                }
  68.  
  69.                return true;
  70.             case 7:
  71.             case 8:
  72.                if (this.arg1 != var1.arg1) {
  73.                   return false;
  74.                }
  75.  
  76.                return true;
  77.             case 9:
  78.             case 10:
  79.             case 11:
  80.             case 12:
  81.                if (this.arg1 == var1.arg1 && this.arg2 == var1.arg2) {
  82.                   return true;
  83.                }
  84.  
  85.                return false;
  86.             default:
  87.                return false;
  88.          }
  89.       }
  90.    }
  91.  
  92.    public String toString() {
  93.       if (this.type == 1) {
  94.          return this.strValue;
  95.       } else if (this.type == 3) {
  96.          return "= " + this.intValue;
  97.       } else if (this.type == 5) {
  98.          return "= " + this.longValue;
  99.       } else if (this.type == 4) {
  100.          return "= " + this.floatValue;
  101.       } else if (this.type == 6) {
  102.          return "= " + this.doubleValue;
  103.       } else {
  104.          StringBuffer var1 = new StringBuffer();
  105.          var1.append(this.name);
  106.          var1.append(":");
  107.          if (this.arg1 != null) {
  108.             var1.append(this.arg1.toString());
  109.          } else if (this.index1 != -1) {
  110.             var1.append("I1[" + this.index1 + "], ");
  111.          }
  112.  
  113.          if (this.arg2 != null) {
  114.             var1.append(this.arg2.toString());
  115.          } else if (this.index2 != -1) {
  116.             var1.append("I2[" + this.index2 + "], ");
  117.          }
  118.  
  119.          return var1.toString();
  120.       }
  121.    }
  122.  
  123.    public boolean read(DataInputStream var1) throws IOException {
  124.       this.type = var1.readByte();
  125.       switch (this.type) {
  126.          case 1:
  127.          case 2:
  128.             if (this.type == 1) {
  129.                this.name = "ASCIZ";
  130.             } else {
  131.                this.name = "UNICODE";
  132.             }
  133.  
  134.             StringBuffer var4 = new StringBuffer();
  135.  
  136.             for(int var2 = var1.readShort(); var2 > 0; --var2) {
  137.                char var3 = (char)var1.readByte();
  138.                var4.append(var3);
  139.             }
  140.  
  141.             this.strValue = var4.toString();
  142.             break;
  143.          case 3:
  144.             this.name = "Integer";
  145.             this.intValue = var1.readInt();
  146.             break;
  147.          case 4:
  148.             this.name = "Float";
  149.             this.floatValue = var1.readFloat();
  150.             break;
  151.          case 5:
  152.             this.name = "Long";
  153.             this.longValue = var1.readLong();
  154.             break;
  155.          case 6:
  156.             this.name = "Double";
  157.             this.doubleValue = var1.readDouble();
  158.             break;
  159.          case 7:
  160.             this.name = "Class";
  161.             this.index1 = var1.readShort();
  162.             this.index2 = -1;
  163.             break;
  164.          case 8:
  165.             this.name = "String";
  166.             this.index1 = var1.readShort();
  167.             this.index2 = -1;
  168.             break;
  169.          case 9:
  170.             this.name = "Field Reference";
  171.             this.index1 = var1.readShort();
  172.             this.index2 = var1.readShort();
  173.             break;
  174.          case 10:
  175.             this.name = "Method Reference";
  176.             this.index1 = var1.readShort();
  177.             this.index2 = var1.readShort();
  178.             break;
  179.          case 11:
  180.             this.name = "Interface Method Reference";
  181.             this.index1 = var1.readShort();
  182.             this.index2 = var1.readShort();
  183.             break;
  184.          case 12:
  185.             this.name = "Name and Type";
  186.             this.index1 = var1.readShort();
  187.             this.index2 = var1.readShort();
  188.             break;
  189.          default:
  190.             System.out.println("Warning bad type.");
  191.       }
  192.  
  193.       return true;
  194.    }
  195.  
  196.    public static short indexOf(ConstantPoolInfo var0, ConstantPoolInfo[] var1) throws Exception {
  197.       for(int var2 = 0; var2 < var1.length; ++var2) {
  198.          if (var0 == var1[var2]) {
  199.             return (short)var2;
  200.          }
  201.       }
  202.  
  203.       throw new Exception("ConstantPoolInfo:: indexOf() - item not in pool.");
  204.    }
  205.  
  206.    public void write(DataOutputStream var1, ConstantPoolInfo[] var2) throws IOException, Exception {
  207.       var1.write(this.type);
  208.       switch (this.type) {
  209.          case 1:
  210.          case 2:
  211.             var1.writeShort(this.strValue.length());
  212.             var1.writeBytes(this.strValue);
  213.             return;
  214.          case 3:
  215.             var1.writeInt(this.intValue);
  216.             return;
  217.          case 4:
  218.             var1.writeFloat(this.floatValue);
  219.             return;
  220.          case 5:
  221.             var1.writeLong(this.longValue);
  222.             return;
  223.          case 6:
  224.             var1.writeDouble(this.doubleValue);
  225.             return;
  226.          case 7:
  227.          case 8:
  228.             var1.writeShort(indexOf(this.arg1, var2));
  229.             return;
  230.          case 9:
  231.          case 10:
  232.          case 11:
  233.          case 12:
  234.             var1.writeShort(indexOf(this.arg1, var2));
  235.             var1.writeShort(indexOf(this.arg2, var2));
  236.             return;
  237.          default:
  238.             throw new Exception("ConstantPoolInfo::write() - bad type.");
  239.       }
  240.    }
  241.  
  242.    public ConstantPoolInfo(String var1) {
  243.       this.type = 1;
  244.       this.strValue = var1;
  245.    }
  246.  
  247.    public ConstantPoolInfo(int var1) {
  248.       this.type = 3;
  249.       this.intValue = var1;
  250.    }
  251.  
  252.    public ConstantPoolInfo(float var1) {
  253.       this.type = 4;
  254.       this.floatValue = var1;
  255.    }
  256.  
  257.    public ConstantPoolInfo(long var1) {
  258.       this.type = 5;
  259.       this.longValue = var1;
  260.    }
  261.  
  262.    public ConstantPoolInfo(double var1) {
  263.       this.type = 6;
  264.       this.doubleValue = var1;
  265.    }
  266.  
  267.    public ConstantPoolInfo() {
  268.       this.type = -1;
  269.    }
  270.  
  271.    public ConstantPoolInfo inPool(ConstantPoolInfo[] var1) {
  272.       for(int var2 = 1; var2 < var1.length; ++var2) {
  273.          if (this.isEqual(var1[var2])) {
  274.             return var1[var2];
  275.          }
  276.       }
  277.  
  278.       return null;
  279.    }
  280.  
  281.    public int isType() {
  282.       return this.type;
  283.    }
  284. }
  285.