use of com.oracle.truffle.espresso.classfile.attributes.PermittedSubclassesAttribute in project graal by oracle.
the class ClassfileParser method parseClassAttributes.
private Attribute[] parseClassAttributes() {
int attributeCount = stream.readU2();
if (attributeCount == 0) {
if (maxBootstrapMethodAttrIndex >= 0) {
throw ConstantPool.classFormatError("BootstrapMethods attribute is missing");
}
return Attribute.EMPTY_ARRAY;
}
SourceFileAttribute sourceFileName = null;
SourceDebugExtensionAttribute sourceDebugExtensionAttribute = null;
NestHostAttribute nestHost = null;
NestMembersAttribute nestMembers = null;
EnclosingMethodAttribute enclosingMethod = null;
BootstrapMethodsAttribute bootstrapMethods = null;
InnerClassesAttribute innerClasses = null;
PermittedSubclassesAttribute permittedSubclasses = null;
RecordAttribute record = null;
CommonAttributeParser commonAttributeParser = new CommonAttributeParser(InfoType.Class);
final Attribute[] classAttributes = spawnAttributesArray(attributeCount);
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 (attributeName.equals(Name.SourceFile)) {
if (sourceFileName != null) {
throw ConstantPool.classFormatError("Duplicate SourceFile attribute");
}
classAttributes[i] = sourceFileName = parseSourceFileAttribute(attributeName);
} else if (attributeName.equals(Name.SourceDebugExtension)) {
if (sourceDebugExtensionAttribute != null) {
throw ConstantPool.classFormatError("Duplicate SourceDebugExtension attribute");
}
classAttributes[i] = sourceDebugExtensionAttribute = parseSourceDebugExtensionAttribute(attributeName, attributeSize);
} else if (attributeName.equals(Name.Synthetic)) {
classFlags |= ACC_SYNTHETIC;
classAttributes[i] = new Attribute(attributeName, null);
} else if (attributeName.equals(Name.InnerClasses)) {
if (innerClasses != null) {
throw ConstantPool.classFormatError("Duplicate InnerClasses attribute");
}
classAttributes[i] = innerClasses = parseInnerClasses(attributeName);
} else if (majorVersion >= JAVA_1_5_VERSION) {
if (majorVersion >= JAVA_7_VERSION && attributeName.equals(Name.BootstrapMethods)) {
if (bootstrapMethods != null) {
throw ConstantPool.classFormatError("Duplicate BootstrapMethods attribute");
}
classAttributes[i] = bootstrapMethods = parseBootstrapMethods(attributeName);
} else if (attributeName.equals(Name.EnclosingMethod)) {
if (enclosingMethod != null) {
throw ConstantPool.classFormatError("Duplicate EnclosingMethod attribute");
}
classAttributes[i] = enclosingMethod = parseEnclosingMethodAttribute(attributeName);
} else if (majorVersion >= JAVA_11_VERSION && attributeName.equals(Name.NestHost)) {
if (nestHost != null) {
throw ConstantPool.classFormatError("Duplicate NestHost attribute");
}
if (nestMembers != null) {
throw ConstantPool.classFormatError("Classfile cannot have both a nest members and a nest host attribute.");
}
if (attributeSize != 2) {
throw ConstantPool.classFormatError("Attribute length of NestHost must be 2");
}
classAttributes[i] = nestHost = parseNestHostAttribute(attributeName);
} else if (majorVersion >= JAVA_11_VERSION && attributeName.equals(Name.NestMembers)) {
if (nestMembers != null) {
throw ConstantPool.classFormatError("Duplicate NestMembers attribute");
}
if (nestHost != null) {
throw ConstantPool.classFormatError("Classfile cannot have both a nest members and a nest host attribute.");
}
classAttributes[i] = nestMembers = parseNestMembers(attributeName);
} else if (majorVersion >= JAVA_14_VERSION && attributeName.equals(Name.Record)) {
if (record != null) {
throw ConstantPool.classFormatError("Duplicate Record attribute");
}
classAttributes[i] = record = parseRecord(attributeName);
} else if (majorVersion >= JAVA_17_VERSION && attributeName.equals(Name.PermittedSubclasses)) {
if (permittedSubclasses != null) {
throw ConstantPool.classFormatError("Duplicate PermittedSubclasses attribute");
}
classAttributes[i] = permittedSubclasses = parsePermittedSubclasses(attributeName);
} else {
Attribute attr = commonAttributeParser.parseCommonAttribute(attributeName, attributeSize);
// stream.skip(attributeSize);
classAttributes[i] = attr == null ? new Attribute(attributeName, stream.readByteArray(attributeSize)) : attr;
}
} else {
// stream.skip(attributeSize);
classAttributes[i] = new Attribute(attributeName, stream.readByteArray(attributeSize));
}
if (attributeSize != stream.getPosition() - startPosition) {
throw ConstantPool.classFormatError("Invalid attribute length for " + attributeName + " attribute");
}
}
if (maxBootstrapMethodAttrIndex >= 0 && bootstrapMethods == null) {
throw ConstantPool.classFormatError("BootstrapMethods attribute is missing");
}
return classAttributes;
}
use of com.oracle.truffle.espresso.classfile.attributes.PermittedSubclassesAttribute in project graal by oracle.
the class VM method JVM_GetPermittedSubclasses.
@VmImpl(isJni = true)
@TruffleBoundary
@JavaType(Class[].class)
public StaticObject JVM_GetPermittedSubclasses(@JavaType(Class.class) StaticObject self) {
Klass k = self.getMirrorKlass();
if (!(k instanceof ObjectKlass)) {
return StaticObject.NULL;
}
ObjectKlass klass = (ObjectKlass) k;
if (!klass.isSealed()) {
return StaticObject.NULL;
}
char[] classes = ((PermittedSubclassesAttribute) klass.getAttribute(PermittedSubclassesAttribute.NAME)).getClasses();
StaticObject[] permittedSubclasses = new StaticObject[classes.length];
RuntimeConstantPool pool = klass.getConstantPool();
int nClasses = 0;
for (int index : classes) {
Klass permitted;
try {
permitted = pool.resolvedKlassAt(klass, index);
} catch (EspressoException e) {
/* Suppress and continue */
continue;
}
if (permitted instanceof ObjectKlass) {
permittedSubclasses[nClasses++] = permitted.mirror();
}
}
if (nClasses == permittedSubclasses.length) {
return StaticObject.createArray(getMeta().java_lang_Class_array, permittedSubclasses);
}
return getMeta().java_lang_Class.allocateReferenceArray(nClasses, (i) -> permittedSubclasses[i]);
}
use of com.oracle.truffle.espresso.classfile.attributes.PermittedSubclassesAttribute in project graal by oracle.
the class ObjectKlass method permittedSubclassCheck.
public boolean permittedSubclassCheck(ObjectKlass k) {
CompilerAsserts.neverPartOfCompilation();
if (!getContext().getJavaVersion().java17OrLater()) {
return true;
}
PermittedSubclassesAttribute permittedSubclasses = (PermittedSubclassesAttribute) getAttribute(PermittedSubclassesAttribute.NAME);
if (permittedSubclasses == null) {
return true;
}
if (module() != k.module()) {
return false;
}
if (!isPublic() && !sameRuntimePackage(k)) {
return false;
}
RuntimeConstantPool pool = getConstantPool();
for (int index : permittedSubclasses.getClasses()) {
if (k.getName().equals(pool.classAt(index).getName(pool))) {
// resolve to k, but resolving here would cause circularity errors.
return true;
}
}
return false;
}
Aggregations