Package gov.nasa.jpf.jvm

Source Code of gov.nasa.jpf.jvm.FieldInfoGenerator

package gov.nasa.jpf.jvm;

import gov.nasa.jpf.JPFException;

import java.util.Arrays;

/**
* This class generates and adds new FieldInfo objects to a class.
* It is placed in the gov.nasa.jpf.jvm package (the same of the
* {@link ClassInfo} class) in order to gain access to protected
* members of ClassInfo.
*
* @author Matteo Ceccarello <matteo.ceccarello AT gmail.com>
*
*/
public abstract class FieldInfoGenerator {

  /**
   * Adds the given field to the give class. The field object that
   * is passed is not actually the one that is added. It is copyed
   * and then added to the class.
   */
  public static void addField(ClassInfo cls, FieldInfo field) {
    if(field.isStatic()) {
      addStaticField(cls, field);
    } else {
      addInstanceField(cls, field);
    }
  }

  /**
   * Adds a copy of the given field to the class as a static field.
   */
  public static void addStaticField(ClassInfo cls, FieldInfo field) {
    int idx = cls.sFields.length;
    FieldInfo prev = cls.sFields[idx-1];
    int off = prev.getStorageOffset() + prev.getStorageSize();
    cls.sFields = Arrays.copyOf(cls.sFields, idx+1);
    cls.sFields[idx] = FieldInfo.create(cls, field.getName(),
        field.getSignature(), field.getModifiers(), idx, off);
    setValue(cls.sFields[idx]);
  }

  /**
   * Adds a copy of the given field to the class, as an instance field.
   * @param cls
   * @param field
   */
  public static void addInstanceField(ClassInfo cls, FieldInfo field) {
    int idx = cls.iFields.length;
    FieldInfo prev = cls.iFields[idx-1];
    int off = prev.getStorageOffset() + prev.getStorageSize();
    cls.iFields = Arrays.copyOf(cls.iFields, idx+1);
    cls.iFields[idx] = FieldInfo.create(cls, field.getName(),
        field.getSignature(), field.getModifiers(), idx, off);
    setValue(cls.iFields[idx]);
  }
 
  static void setValue(FieldInfo f) {
    if(f.isReference())
      f.setConstantValue(""); // JPF currently support only String
                                      // constants
    else if(f.isBooleanField())
      f.setConstantValue(false);
    else if(f.isCharField())
      f.setConstantValue('\0');
    else if(f.isByteField())
      f.setConstantValue(new Byte((byte) 0));
    else if(f.isShortField())
      f.setConstantValue(new Short((short) 0));
    else if(f.isIntField())
      f.setConstantValue(new Integer(0));
    else if(f.isLongField())
      f.setConstantValue(new Long(0));
    else if(f.isFloatField())
      f.setConstantValue(0);
    else if(f.isDoubleField())
      f.setConstantValue(new Double(0));
    else
      throw new JPFException("Unsupported field type " + f.getType());
  }
 
}
TOP

Related Classes of gov.nasa.jpf.jvm.FieldInfoGenerator

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.