All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sun.jdo.api.persistence.enhancer.classfile.ConstantPool Maven / Gradle / Ivy

The newest version!
/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
 *
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common Development
 * and Distribution License("CDDL") (collectively, the "License").  You
 * may not use this file except in compliance with the License.  You can
 * obtain a copy of the License at
 * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
 * or packager/legal/LICENSE.txt.  See the License for the specific
 * language governing permissions and limitations under the License.
 *
 * When distributing the software, include this License Header Notice in each
 * file and include the License file at packager/legal/LICENSE.txt.
 *
 * GPL Classpath Exception:
 * Oracle designates this particular file as subject to the "Classpath"
 * exception as provided by Oracle in the GPL Version 2 section of the License
 * file that accompanied this code.
 *
 * Modifications:
 * If applicable, add the following below the License Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyright [year] [name of copyright owner]"
 *
 * Contributor(s):
 * If you wish your version of this file to be governed by only the CDDL or
 * only the GPL Version 2, indicate your decision by adding "[Contributor]
 * elects to include this software in this distribution under the [CDDL or GPL
 * Version 2] license."  If you don't indicate a single choice of license, a
 * recipient has the option to distribute your version of this file under
 * either the CDDL, the GPL Version 2 or to extend the choice of license to
 * its licensees as provided above.  However, if you add GPL Version 2 code
 * and therefore, elected the GPL Version 2 license, then the option applies
 * only if the new code is made subject to such option by the copyright
 * holder.
 */

package com.sun.jdo.api.persistence.enhancer.classfile;

import java.util.Vector;
import java.util.Hashtable;
import java.io.*;

/**
 *  Constant Pool implementation - this represents the constant pool
 *  of a class in a class file.
 */

public class ConstantPool implements VMConstants {

  /* The actual pool */
  private Vector pool = new Vector();

  /* uniqifier tables */
  private boolean hashed = false;
  private Hashtable utfTable = new Hashtable(11);
  private Hashtable unicodeTable = new Hashtable(3);
  private Hashtable stringTable = new Hashtable(11);
  private Hashtable classTable = new Hashtable(11);
  private Hashtable intTable = new Hashtable(3);
  private Hashtable floatTable = new Hashtable(3);
  private Hashtable longTable = new Hashtable(3);
  private Hashtable doubleTable = new Hashtable(3);

  private Vector methodRefTable = new Vector();
  private Vector fieldRefTable = new Vector();
  private Vector ifaceMethodRefTable = new Vector();
  private Vector nameAndTypeTable = new Vector();

  /* public accessors */

  /**
   * Return the number of pool entries.
   */
  public int nEntries() {
    return pool.size();
  }

  /**
   * Return the constant in the pool at the specified entry index
   */
  public ConstBasic constantAt (int index) {
    return (ConstBasic) pool.elementAt(index);
  }

  /**
   * Find or create a class constant in the pool
   */
  public ConstClass addClass (String className) {
    hashConstants();
    ConstClass c = (ConstClass) classTable.get(className);
    if (c == null) {
      c = new ConstClass(addUtf8(className));
      internConstant(c);
    }
    return c;
  }
  
  /**
   * Find or create a field constant in the pool
   */
  public ConstFieldRef addFieldRef (String className, String fieldName,
                                     String type) {
    hashConstants();
    ConstFieldRef f = (ConstFieldRef)
      searchTable(fieldRefTable, className, fieldName, type);

    if (f == null) {
      f = new ConstFieldRef (addClass(className),
			     addNameAndType(fieldName, type));
      internConstant(f);
    }
    return f;
  }

  /**
   * Find or create a method constant in the pool
   */
  public ConstMethodRef addMethodRef (String className, String methodName,
				      String type) {
    hashConstants();
    ConstMethodRef m = (ConstMethodRef)
      searchTable(methodRefTable, className, methodName, type);
    if (m == null) {
      m = new ConstMethodRef (addClass(className),
			      addNameAndType(methodName, type));
      internConstant(m);
    }
    return m;
  }

  /**
   * Find or create an interface method constant in the pool
   */
  public ConstInterfaceMethodRef addInterfaceMethodRef (String className,
                              String methodName, String type) {
    hashConstants();
    ConstInterfaceMethodRef m = (ConstInterfaceMethodRef)
      searchTable(ifaceMethodRefTable, className, methodName, type);
    if (m == null) {
      m = new ConstInterfaceMethodRef (addClass(className),
				       addNameAndType(methodName, type));
      internConstant(m);
    }
    return m;
  }

  /**
   * Find or create a string constant in the pool
   */
  public ConstString addString (String s) {
    hashConstants();
    ConstString cs = (ConstString) stringTable.get(s);
    if (cs == null) {
      cs = new ConstString(addUtf8(s));
      internConstant(cs);
    }
    return cs;
  }
  
  /**
   * Find or create an integer constant in the pool
   */
  public ConstInteger addInteger (int i) {
    hashConstants();
    Integer io = Integer.valueOf(i);
    ConstInteger ci = (ConstInteger) intTable.get(io);
    if (ci == null) {
      ci = new ConstInteger(i);
      internConstant(ci);
    }
    return ci;
  }
  
  /**
   * Find or create a float constant in the pool
   */
  public ConstFloat addFloat (float f) {
    hashConstants();
    Float fo = Float.valueOf(f);
    ConstFloat cf = (ConstFloat) floatTable.get(fo);
    if (cf == null) {
      cf = new ConstFloat(f);
      internConstant(cf);
    }
    return cf;
  }
  
  /**
   * Find or create a long constant in the pool
   */
  public ConstLong addLong (long l) {
    hashConstants();
    Long lo = Long.valueOf(l);
    ConstLong cl = (ConstLong) longTable.get(lo);
    if (cl == null) {
      cl = new ConstLong(l);
      internConstant(cl);
      internConstant(null);
    }
    return cl;
  }
  
  /**
   * Find or create a double constant in the pool
   */
  public ConstDouble addDouble (double d) {
    hashConstants();
    Double dobj = Double.valueOf(d);
    ConstDouble cd = (ConstDouble) doubleTable.get(dobj);
    if (cd == null) {
      cd = new ConstDouble(d);
      internConstant(cd);
      internConstant(null);
    }
    return cd;
  }
  
  /**
   * Find or create a name/type constant in the pool
   */
  public ConstNameAndType addNameAndType (String name, String type) {
    hashConstants();
    for (int i=0; i 0)
      nconstants -= readConstant(input);

    resolvePool();
  }

  void print (PrintStream out) {
    for (int i=0; i 1)
      pool.addElement(null);   
    return slots;
  }

  private void internConstant (ConstBasic c) {
    if (c != null) {
      c.setIndex(pool.size());
      recordConstant(c);
    }
    pool.addElement(c);
  }

  private void recordConstant (ConstBasic c) {
    if (c != null) {
      switch (c.tag()) {
      case CONSTANTUtf8:
	utfTable.put(((ConstUtf8)c).asString(), c);
	break;
      case CONSTANTUnicode:
	unicodeTable.put(((ConstUnicode)c).asString(), c);
	break;
      case CONSTANTInteger:
	intTable.put(new Integer(((ConstInteger)c).value()), c);
	break;
      case CONSTANTFloat:
	floatTable.put(new Float(((ConstFloat)c).value()), c);
	break;
      case CONSTANTLong:
	longTable.put(new Long(((ConstLong)c).value()), c);
	break;
      case CONSTANTDouble:
	doubleTable.put(new Double(((ConstDouble)c).value()), c);
	break;
      case CONSTANTClass:
	classTable.put(((ConstClass)c).asString(), c);
	break;
      case CONSTANTString:
	stringTable.put(((ConstString)c).value().asString(), c);
	break;
      case CONSTANTFieldRef:
	fieldRefTable.addElement(c);
	break;
      case CONSTANTMethodRef:
	methodRefTable.addElement(c);
	break;
      case CONSTANTInterfaceMethodRef:
	ifaceMethodRefTable.addElement(c);
	break;
      case CONSTANTNameAndType:
	nameAndTypeTable.addElement(c);
	break;
      }
    }
  }

  private ConstBasicMemberRef searchTable(Vector table, String cname,
					  String mname, String sig) {
    for (int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy