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

org.fife.rsta.ac.java.classreader.FieldInfo Maven / Gradle / Ivy

Go to download

A library adding code completion and other advanced features for Java, JavaScript, Perl, and other languages to RSyntaxTextArea.

There is a newer version: 3.3.0
Show newest version
/*
 * 03/21/2010
 *
 * Copyright (C) 2010 Robert Futrell
 * robert_futrell at users.sourceforge.net
 * http://fifesoft.com/rsyntaxtextarea
 *
 * This library is distributed under a modified BSD license.  See the included
 * RSTALanguageSupport.License.txt file for details.
 */
package org.fife.rsta.ac.java.classreader;

import java.io.*;
import java.util.*;

import org.fife.rsta.ac.java.classreader.attributes.*;
import org.fife.rsta.ac.java.classreader.constantpool.ConstantUtf8Info;


/**
 * Represents a "field_info" structure as defined by the Java VM spec.
 *
 * @author Robert Futrell
 * @version 1.0
 */
public class FieldInfo extends MemberInfo {

	/**
	 * Index into the constant pool of a {@link ConstantUtf8Info} structure
	 * representing the field name, as a simple name.
	 */
	private int nameIndex; // u2

	/**
	 * Index into the constant pool of a {@link ConstantUtf8Info} structure
	 * representing a valid field descriptor.
	 */
	private int descriptorIndex; // u2

	/**
	 * An array of attributes of this field.
	 */
	private List attributes;

	public static final String CONSTANT_VALUE		= "ConstantValue";


	/**
	 * Constructor.
	 *
	 * @see AccessFlags
	 */
	public FieldInfo(ClassFile cf, int accessFlags, int nameIndex,
					int descriptorIndex) {
		super(cf, accessFlags);
		this.nameIndex = nameIndex;
		this.descriptorIndex = descriptorIndex;
		attributes = new ArrayList(1); // Usually 0 or 1?
	}


	/**
	 * Adds the specified attribute to this field.
	 *
	 * @param info Information about the attribute.
	 */
	public void addAttribute(AttributeInfo info) {
		attributes.add(info);
	}


	/**
	 * Returns the specified attribute.
	 *
	 * @param index The index of the attribute.
	 * @return The attribute.
	 */
	public AttributeInfo getAttribute(int index) {
		return (AttributeInfo)attributes.get(index);
	}


	/**
	 * Returns the number of attributes of this field.
	 *
	 * @return The number of attributes.
	 */
	public int getAttributeCount() {
		return attributes.size();
	}


	public String getConstantValueAsString() {
		ConstantValue cv = getConstantValueAttributeInfo();
		return cv==null ? null : cv.getConstantValueAsString();
	}


	/**
	 * Returns the {@link ConstantValue} attribute info for this field, if any.
	 *
	 * @return The ConstantValue attribute, or null
	 *         if there isn't one.
	 */
	private ConstantValue getConstantValueAttributeInfo() {
		for (int i=0; iFieldInfo structure from the specified input
	 * stream.
	 *
	 * @param cf The class file containing this field.
	 * @param in The input stream to read from.
	 * @return The field information read.
	 * @throws IOException If an IO error occurs.
	 */
	public static FieldInfo read(ClassFile cf, DataInputStream in)
									throws IOException {
		FieldInfo info = new FieldInfo(cf, in.readUnsignedShort(),
										in.readUnsignedShort(),
										in.readUnsignedShort());
		int attrCount = in.readUnsignedShort();
		for (int i=0; inull if it was known
	 *         to be unimportant for our purposes.
	 * @throws IOException If an IO error occurs.
	 */
	private AttributeInfo readAttribute(DataInputStream in) throws IOException {

		AttributeInfo ai = null;

		int attributeNameIndex = in.readUnsignedShort();
		int attributeLength = in.readInt();

		String attrName = cf.getUtf8ValueFromConstantPool(attributeNameIndex);

		if (CONSTANT_VALUE.equals(attrName)) { // 4.7.2
			int constantValueIndex = in.readUnsignedShort();
			ConstantValue cv = new ConstantValue(cf, constantValueIndex);
			ai = cv;
		}

		// Attributes common to all members, or unhandled attributes.
		else {
			ai = super.readAttribute(in, attrName, attributeLength);
		}

		return ai;

	}


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy