use of com.oracle.truffle.espresso.descriptors.Symbol.ModifiedUTF8 in project graal by oracle.
the class ClassfileParser method parseField.
private ParserField parseField(boolean isInterface) {
int fieldFlags = stream.readU2();
int nameIndex = stream.readU2();
int typeIndex = stream.readU2();
pool.utf8At(nameIndex).validateFieldName();
final Symbol<Name> name = pool.symbolAt(nameIndex, "field name");
verifyFieldFlags(name, fieldFlags, isInterface);
final boolean isStatic = Modifier.isStatic(fieldFlags);
pool.utf8At(typeIndex).validateType(false);
Symbol<ModifiedUTF8> rawDescriptor = pool.symbolAt(typeIndex, "field descriptor");
final Symbol<Type> descriptor = Types.fromSymbol(rawDescriptor);
if (descriptor == null) {
throw ConstantPool.classFormatError("Invalid descriptor: " + rawDescriptor);
}
final int attributeCount = stream.readU2();
final Attribute[] fieldAttributes = spawnAttributesArray(attributeCount);
ConstantValueAttribute constantValue = null;
CommonAttributeParser commonAttributeParser = new CommonAttributeParser(InfoType.Field);
for (int i = 0; i < attributeCount; ++i) {
final int attributeNameIndex = stream.readU2();
final Symbol<Name> attributeName = pool.symbolAt(attributeNameIndex, "attribute name");
final int attributeSize = stream.readS4();
final int startPosition = stream.getPosition();
if (isStatic && attributeName.equals(Name.ConstantValue)) {
if (constantValue != null) {
throw ConstantPool.classFormatError("Duplicate ConstantValue attribute");
}
fieldAttributes[i] = constantValue = new ConstantValueAttribute(stream.readU2());
if (constantValue.getConstantValueIndex() == 0) {
throw ConstantPool.classFormatError("Invalid ConstantValue index");
}
} else if (attributeName.equals(Name.Synthetic)) {
fieldFlags |= ACC_SYNTHETIC;
fieldAttributes[i] = new Attribute(attributeName, null);
} else if (majorVersion >= JAVA_1_5_VERSION) {
Attribute attr = commonAttributeParser.parseCommonAttribute(attributeName, attributeSize);
// stream.skip(attributeSize);
fieldAttributes[i] = attr == null ? new Attribute(attributeName, stream.readByteArray(attributeSize)) : attr;
} else {
// stream.skip(attributeSize);
fieldAttributes[i] = new Attribute(attributeName, stream.readByteArray(attributeSize));
}
if (attributeSize != stream.getPosition() - startPosition) {
throw ConstantPool.classFormatError("Invalid attribute_length for " + attributeName + " attribute");
}
}
final JavaKind kind = Types.getJavaKind(descriptor);
if (kind == JavaKind.Void) {
throw ConstantPool.classFormatError("Fields cannot be of type void");
}
if (constantValue != null) {
Tag tag = pool.tagAt(constantValue.getConstantValueIndex());
boolean valid = false;
switch(kind) {
// fall through
case Boolean:
// fall through
case Byte:
// fall through
case Short:
// fall through
case Char:
case // fall through
Int:
valid = (tag == Tag.INTEGER);
break;
case Float:
valid = (tag == Tag.FLOAT);
break;
case Long:
valid = (tag == Tag.LONG);
break;
case Double:
valid = (tag == Tag.DOUBLE);
break;
case Object:
valid = (tag == Tag.STRING) && descriptor.equals(Type.java_lang_String);
break;
default:
{
throw ConstantPool.classFormatError("Cannot have ConstantValue for fields of type " + kind);
}
}
if (!valid) {
throw ConstantPool.classFormatError("ConstantValue attribute does not match field type");
}
}
return new ParserField(fieldFlags, name, descriptor, fieldAttributes);
}
Aggregations