FileDocCategorySizeDatePackage
ClassFile.javaAPI DocGlassfish v2 API18276Fri May 04 22:34:28 BST 2007com.sun.jdo.api.persistence.enhancer.classfile

ClassFile

public final class ClassFile extends Object implements Serializable, VMConstants
ClassFile models the structure of a class as represented within a class file.

Fields Summary
public static final int
magic
public static final short[]
jdkMajorMinorVersions
public static final List
jdkVersions
public static final String
supportedVersions
private int
majorVersion
private int
minorVersion
private ConstantPool
constantPool
private int
accessFlags
private ConstClass
thisClassName
private ConstClass
superClassName
private Vector
classInterfaces
private Vector
classFields
private Vector
classMethods
private AttributeVector
classAttributes
Constructors Summary
public ClassFile(DataInputStream data)
Construct a ClassFile from an input stream

    try {
      int thisMagic = data.readInt();
      if (thisMagic != magic)
        throw new ClassFormatError("Bad magic value for input");//NOI18N

      short thisMinorVersion = data.readShort();
      short thisMajorVersion = data.readShort();
      //@olsen: changed checking only target 1.1 and 1.2 to more
      //general check for a list of versions.

      if (isSupportedVersion(thisMajorVersion, thisMinorVersion)) {
         minorVersion = thisMinorVersion;
         majorVersion = thisMajorVersion;
      } else {
        throw new ClassFormatError("Bad version number: {" + //NOI18N
                  thisMajorVersion + "," + //NOI18N
                  thisMinorVersion +
                  "} expected one of: " + //NOI18N
                  supportedVersions);
      }
      readConstants(data);
      accessFlags = data.readUnsignedShort();
      thisClassName = (ConstClass)
      constantPool.constantAt(data.readUnsignedShort());
      superClassName = (ConstClass)
      constantPool.constantAt(data.readUnsignedShort());
      readInterfaces(data);
      readFields(data);
      readMethods(data);
      classAttributes = AttributeVector.readAttributes(data, constantPool);
    } catch (IOException e) {
      ClassFormatError cfe = new ClassFormatError("IOException during reading");//NOI18N
      cfe.initCause(e);
      throw cfe;
    }
    //@olsen: added println() for debugging
    //System.out.println("ClassFile(): new class = " + thisClassName.asString());
  
public ClassFile(String cname, String supername)
Construct a bare bones class, ready for additions

    thisClassName = constantPool.addClass(cname);
    superClassName = constantPool.addClass(supername);
    //@olsen: added println() for debugging
    //System.out.println("ClassFile(): new bare class file = " + thisClassName);
  
Methods Summary
public intaccess()
Return the access flags for the class - see VMConstants

    return accessFlags;
  
public voidaddField(ClassField field)
Add a field to the list of the fields which the class contains

    classFields.addElement(field);
  
public voidaddField(ClassField field, int index)
Add a field to the list of the fields which the class contains, at the index'th position.

    classFields.insertElementAt(field, index);
  
public voidaddInterface(ConstClass iface)
Add an interface to the list of the interfaces which the class implements

    classInterfaces.addElement(iface);
  
public voidaddMethod(ClassMethod method)
Add a method to the list of the methods which the class defines

    classMethods.addElement(method);
  
public AttributeVectorattributes()
Return the list of the attributes associated with the class

    return classAttributes;
  
public ConstClassclassName()
Return the name of the class

    return thisClassName;
  
private static java.util.ListconvertMajorMinorVersions(short[][] majorMinor)

      int length = majorMinor.length;
      List result = new ArrayList(length);
      for (int i = 0; i < length; i++) {
        result.add(getVersionInt(majorMinor[i][0], majorMinor[i][1]));
      }
      return result;
    
public java.util.Vectorfields()
Return the list of the fields which the class contains The contents are ClassField objects

    return classFields;
  
public ClassFieldfindField(java.lang.String fieldName)
Look for a field with the specified name

    for (Enumeration e = fields().elements(); e.hasMoreElements();) {
      ClassField field = (ClassField) e.nextElement();
      if (field.name().asString().equals(fieldName))
	return field;
    }
    return null;
  
public ClassMethodfindMethod(java.lang.String methodName, java.lang.String methodSig)
Look for a method with the specified name and type signature

    for (Enumeration e = methods().elements(); e.hasMoreElements();) {
      ClassMethod method = (ClassMethod) e.nextElement();
      if (method.name().asString().equals(methodName) &&
	  method.signature().asString().equals(methodSig))
	return method;
    }
    return null;
  
public byte[]getBytes()
Returns a byte array representation of this class.

    /* Write the class bytes to a file, for debugging. */

    String writeClassToDirectory =
      System.getProperty("filter.writeClassToDirectory");
    if (writeClassToDirectory != null) {
      String filename = writeClassToDirectory + java.io.File.separator +
          thisClassName.asString() + ".class";//NOI18N
      System.err.println("Writing class to file " + filename);
      DataOutputStream stream = new DataOutputStream(
	  new java.io.FileOutputStream(filename));
      write(stream);
      stream.close();
    }

    /* Get the class bytes and return them. */

    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    write(new DataOutputStream(byteStream));

    return byteStream.toByteArray();
  
private static java.lang.IntegergetVersionInt(short major, short minor)

        return  new Integer(major * 65536 + minor);
    
public java.util.Vectorinterfaces()
Return the list of the interfaces which the class implements The contents are ConstClass objects

    return classInterfaces;
  
public final booleanisAbstract()
Is the class abstract?

    return (accessFlags & ACCAbstract) != 0;
  
public final booleanisFinal()
Is the class final?

    return (accessFlags & ACCFinal) != 0;
  
public final booleanisInterface()
Is the class an interface?

    return (accessFlags & ACCInterface) != 0;
  
public final booleanisPublic()
Is the class public?

    return (accessFlags & ACCPublic) != 0;
  
private static booleanisSupportedVersion(short major, short minor)

      Integer version = getVersionInt(major, minor);
      return jdkVersions.contains(version);
    
public java.util.Vectormethods()
Return the list of the methods which the class defines The contents are ClassMethod objects

    return classMethods;
  
public ConstantPoolpool()
Return the constant pool for the class file




  /* public accessors */



             
     
    return constantPool;
  
public voidprint(java.io.PrintStream out)

    constantPool.print(out);
    out.println();

    out.println("majorVersion = " + Integer.toString(majorVersion));//NOI18N
    out.println("minorVersion = " + Integer.toString(minorVersion));//NOI18N
    out.println("accessFlags = " + Integer.toString(accessFlags));//NOI18N
    out.println("className = " + thisClassName.asString());//NOI18N
    out.println("superClassName = " + superClassName.asString());//NOI18N
    out.print("Interfaces =");//NOI18N
    for (int i=0; i<classInterfaces.size(); i++) {
        out.print(" " + ((ConstClass) classInterfaces.elementAt(i)).asString());//NOI18N
    }
    out.println();

    out.println("fields =");//NOI18N
    for (int i=0; i<classFields.size(); i++) {
      ((ClassField) classFields.elementAt(i)).print(out, 3);
    }

    out.println("methods =");//NOI18N
    for (int i=0; i<classMethods.size(); i++) {
      ((ClassMethod) classMethods.elementAt(i)).print(out, 3);
    }

    out.println("attributes =");//NOI18N
    classAttributes.print(out, 3);

  
public static final java.lang.StringprintSupportedVersions()

      StringBuffer buf = new StringBuffer("{"); //NOI18N
      int length = jdkMajorMinorVersions.length;
      for (int i = 0; i < length; i++) {
        int major = jdkMajorMinorVersions[i][0];
        int minor = jdkMajorMinorVersions[i][1];
        buf.append("{"); //NOI18N
        buf.append(major);
        buf.append(","); //NOI18N
        buf.append(minor);
        buf.append("}"); //NOI18N
        }
        buf.append("}"); //NOI18N
        return buf.toString();
    
private voidreadConstants(java.io.DataInputStream data)

    constantPool = new ConstantPool(data);
  
private voidreadFields(java.io.DataInputStream data)

    int nFields = data.readUnsignedShort();
    while (nFields-- > 0) {
      classFields.addElement (ClassField.read(data, constantPool));
    }
  
private voidreadInterfaces(java.io.DataInputStream data)

    int nInterfaces = data.readUnsignedShort();
    while (nInterfaces-- > 0) {
      int interfaceIndex = data.readUnsignedShort();
      ConstClass ci = null;
      if (interfaceIndex != 0)
	ci = (ConstClass) constantPool.constantAt(interfaceIndex);
      classInterfaces.addElement(ci);
    }
  
private voidreadMethods(java.io.DataInputStream data)

    int nMethods = data.readUnsignedShort();
    while (nMethods-- > 0) {
      classMethods.addElement (ClassMethod.read(data, constantPool));
    }
  
public voidsetAccessFlags(int flags)
Set the access flags for the class - see VMConstants

    accessFlags = flags;
  
public voidsetSuperName(ConstClass superCl)
Set the name of the super class

    superClassName = superCl;
  
public voidsummarize()

    PrintStream os = System.out;

    constantPool.summarize();
    int codeSize = 0;
    for (int i=0; i<classMethods.size(); i++) {
      codeSize += ((ClassMethod) classMethods.elementAt(i)).codeSize();
    }
    System.out.println(classMethods.size() + " methods in " + codeSize + " bytes");//NOI18N
  
public ConstClasssuperName()
Return the name of the super class

    return superClassName;
  
public java.lang.StringsuperNameString()
Return the name of the super class as a string

    return (superClassName == null) ? null : superClassName.asString();
  
public voidwrite(java.io.DataOutputStream buff)
Write the Class file to the data output stream

    buff.writeInt(magic);
    buff.writeShort(minorVersion);
    buff.writeShort(majorVersion);
    constantPool.write(buff);
    buff.writeShort(accessFlags);
    buff.writeShort(thisClassName.getIndex());
    //@lars: superclass may be null (java.lang.Object); VMSpec 2nd ed., section 4.1
    buff.writeShort(superClassName == null ? 0 : superClassName.getIndex());
//    buff.writeShort(superClassName.getIndex());
    writeInterfaces(buff);
    writeFields(buff);
    writeMethods(buff);
    classAttributes.write(buff);
  
private voidwriteFields(java.io.DataOutputStream data)

    data.writeShort(classFields.size());
    for (int i=0; i<classFields.size(); i++)
      ((ClassField)classFields.elementAt(i)).write(data);
  
private voidwriteInterfaces(java.io.DataOutputStream data)

    data.writeShort(classInterfaces.size());
    for (int i=0; i<classInterfaces.size(); i++) {
      ConstClass ci = (ConstClass) classInterfaces.elementAt(i);
      int interfaceIndex = 0;
      if (ci != null)
	interfaceIndex = ci.getIndex();
      data.writeShort(interfaceIndex);
    }
  
private voidwriteMethods(java.io.DataOutputStream data)

    data.writeShort(classMethods.size());
    for (int i=0; i<classMethods.size(); i++)
      ((ClassMethod)classMethods.elementAt(i)).write(data);