ClassFile.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one
  3.  * or more contributor license agreements.  See the NOTICE file
  4.  * distributed with this work for additional information
  5.  * regarding copyright ownership.  The ASF licenses this file
  6.  * to you under the Apache License, Version 2.0 (the
  7.  * "License"); you may not use this file except in compliance
  8.  * with the License.  You may obtain a copy of the License at
  9.  *
  10.  *   https://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing,
  13.  * software distributed under the License is distributed on an
  14.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15.  * KIND, either express or implied.  See the License for the
  16.  * specific language governing permissions and limitations
  17.  * under the License.
  18.  */
  19. package org.apache.commons.compress.harmony.unpack200.bytecode;

  20. import java.io.DataOutputStream;
  21. import java.io.IOException;

  22. /**
  23.  * ClassFile is used to represent and write out Java class files.
  24.  */
  25. public class ClassFile {

  26.     private static final int MAGIC = 0xCAFEBABE;

  27.     public int major;
  28.     public int minor;
  29.     public ClassConstantPool pool = new ClassConstantPool();
  30.     public int accessFlags;
  31.     public int thisClass;
  32.     public int superClass;
  33.     public int[] interfaces;
  34.     public ClassFileEntry[] fields;
  35.     public ClassFileEntry[] methods;
  36.     public Attribute[] attributes;

  37.     public void write(final DataOutputStream dos) throws IOException {
  38.         dos.writeInt(MAGIC);
  39.         dos.writeShort(minor);
  40.         dos.writeShort(major);
  41.         dos.writeShort(pool.size() + 1);
  42.         for (int i = 1; i <= pool.size(); i++) {
  43.             final ConstantPoolEntry entry;
  44.             (entry = (ConstantPoolEntry) pool.get(i)).doWrite(dos);
  45.             // Doubles and longs take up two spaces in the pool, but only one
  46.             // gets written
  47.             if (entry.getTag() == ConstantPoolEntry.CP_Double || entry.getTag() == ConstantPoolEntry.CP_Long) {
  48.                 i++;
  49.             }
  50.         }
  51.         dos.writeShort(accessFlags);
  52.         dos.writeShort(thisClass);
  53.         dos.writeShort(superClass);
  54.         dos.writeShort(interfaces.length);
  55.         for (final int element : interfaces) {
  56.             dos.writeShort(element);
  57.         }
  58.         dos.writeShort(fields.length);
  59.         for (final ClassFileEntry field : fields) {
  60.             field.write(dos);
  61.         }
  62.         dos.writeShort(methods.length);
  63.         for (final ClassFileEntry method : methods) {
  64.             method.write(dos);
  65.         }
  66.         dos.writeShort(attributes.length);
  67.         for (final Attribute attribute : attributes) {
  68.             attribute.write(dos);
  69.         }
  70.     }
  71. }