use of com.oracle.truffle.espresso.runtime.Attribute in project graal by oracle.
the class VM method JVM_GetClassDeclaredFields.
@VmImpl(isJni = true)
@JavaType(java.lang.reflect.Field[].class)
public StaticObject JVM_GetClassDeclaredFields(@JavaType(Class.class) StaticObject self, boolean publicOnly) {
// TODO(peterssen): From Hostpot: 4496456 We need to filter out
// java.lang.Throwable.backtrace.
Meta meta = getMeta();
ArrayList<Field> collectedMethods = new ArrayList<>();
Klass klass = self.getMirrorKlass();
klass.ensureLinked();
for (Field f : klass.getDeclaredFields()) {
if (!publicOnly || f.isPublic()) {
collectedMethods.add(f);
}
}
final Field[] fields = collectedMethods.toArray(Field.EMPTY_ARRAY);
EspressoContext context = meta.getContext();
// TODO(peterssen): Cache guest j.l.reflect.Field constructor.
// Calling the constructor is just for validation, manually setting the fields would be
// faster.
Method fieldInit;
if (meta.getJavaVersion().java15OrLater()) {
fieldInit = meta.java_lang_reflect_Field.lookupDeclaredMethod(Name._init_, context.getSignatures().makeRaw(Type._void, /* declaringClass */
Type.java_lang_Class, /* name */
Type.java_lang_String, /* type */
Type.java_lang_Class, /* modifiers */
Type._int, /* trustedFinal */
Type._boolean, /* slot */
Type._int, /* signature */
Type.java_lang_String, /* annotations */
Type._byte_array));
} else {
fieldInit = meta.java_lang_reflect_Field.lookupDeclaredMethod(Name._init_, context.getSignatures().makeRaw(Type._void, /* declaringClass */
Type.java_lang_Class, /* name */
Type.java_lang_String, /* type */
Type.java_lang_Class, /* modifiers */
Type._int, /* slot */
Type._int, /* signature */
Type.java_lang_String, /* annotations */
Type._byte_array));
}
StaticObject fieldsArray = meta.java_lang_reflect_Field.allocateReferenceArray(fields.length, new IntFunction<StaticObject>() {
@Override
public StaticObject apply(int i) {
final Field f = fields[i];
StaticObject instance = meta.java_lang_reflect_Field.allocateInstance();
Attribute rawRuntimeVisibleAnnotations = f.getAttribute(Name.RuntimeVisibleAnnotations);
StaticObject runtimeVisibleAnnotations = rawRuntimeVisibleAnnotations != null ? StaticObject.wrap(rawRuntimeVisibleAnnotations.getData(), meta) : StaticObject.NULL;
Attribute rawRuntimeVisibleTypeAnnotations = f.getAttribute(Name.RuntimeVisibleTypeAnnotations);
StaticObject runtimeVisibleTypeAnnotations = rawRuntimeVisibleTypeAnnotations != null ? StaticObject.wrap(rawRuntimeVisibleTypeAnnotations.getData(), meta) : StaticObject.NULL;
if (meta.getJavaVersion().java15OrLater()) {
fieldInit.invokeDirect(/* this */
instance, /* declaringKlass */
f.getDeclaringKlass().mirror(), /* name */
context.getStrings().intern(f.getName()), /* type */
f.resolveTypeKlass().mirror(), /* modifiers */
f.getModifiers(), /* trustedFinal */
f.isTrustedFinal(), /* slot */
f.getSlot(), /* signature */
meta.toGuestString(f.getGenericSignature()), /* annotations */
runtimeVisibleAnnotations);
} else {
fieldInit.invokeDirect(/* this */
instance, /* declaringKlass */
f.getDeclaringKlass().mirror(), /* name */
context.getStrings().intern(f.getName()), /* type */
f.resolveTypeKlass().mirror(), /* modifiers */
f.getModifiers(), /* slot */
f.getSlot(), /* signature */
meta.toGuestString(f.getGenericSignature()), /* annotations */
runtimeVisibleAnnotations);
}
meta.HIDDEN_FIELD_KEY.setHiddenObject(instance, f);
meta.HIDDEN_FIELD_RUNTIME_VISIBLE_TYPE_ANNOTATIONS.setHiddenObject(instance, runtimeVisibleTypeAnnotations);
return instance;
}
});
return fieldsArray;
}
use of com.oracle.truffle.espresso.runtime.Attribute in project graal by oracle.
the class VM method JVM_GetClassDeclaredConstructors.
// TODO(tg): inject constructor calltarget.
@VmImpl(isJni = true)
@JavaType(Constructor[].class)
public StaticObject JVM_GetClassDeclaredConstructors(@JavaType(Class.class) StaticObject self, boolean publicOnly) {
Meta meta = getMeta();
ArrayList<Method> collectedMethods = new ArrayList<>();
Klass klass = self.getMirrorKlass();
klass.ensureLinked();
for (Method m : klass.getDeclaredConstructors()) {
if (Name._init_.equals(m.getName()) && (!publicOnly || m.isPublic())) {
collectedMethods.add(m);
}
}
final Method[] constructors = collectedMethods.toArray(Method.EMPTY_ARRAY);
EspressoContext context = meta.getContext();
// TODO(peterssen): Cache guest j.l.reflect.Constructor constructor.
// Calling the constructor is just for validation, manually setting the fields would be
// faster.
Method constructorInit = meta.java_lang_reflect_Constructor.lookupDeclaredMethod(Name._init_, context.getSignatures().makeRaw(Type._void, /* declaringClass */
Type.java_lang_Class, /* parameterTypes */
Type.java_lang_Class_array, /* checkedExceptions */
Type.java_lang_Class_array, /* modifiers */
Type._int, /* slot */
Type._int, /* signature */
Type.java_lang_String, /* annotations */
Type._byte_array, /* parameterAnnotations */
Type._byte_array));
StaticObject arr = meta.java_lang_reflect_Constructor.allocateReferenceArray(constructors.length, new IntFunction<StaticObject>() {
@Override
public StaticObject apply(int i) {
final Method m = constructors[i];
Attribute rawRuntimeVisibleAnnotations = m.getAttribute(Name.RuntimeVisibleAnnotations);
StaticObject runtimeVisibleAnnotations = rawRuntimeVisibleAnnotations != null ? StaticObject.wrap(rawRuntimeVisibleAnnotations.getData(), meta) : StaticObject.NULL;
Attribute rawRuntimeVisibleParameterAnnotations = m.getAttribute(Name.RuntimeVisibleParameterAnnotations);
StaticObject runtimeVisibleParameterAnnotations = rawRuntimeVisibleParameterAnnotations != null ? StaticObject.wrap(rawRuntimeVisibleParameterAnnotations.getData(), meta) : StaticObject.NULL;
Attribute rawRuntimeVisibleTypeAnnotations = m.getAttribute(Name.RuntimeVisibleTypeAnnotations);
StaticObject runtimeVisibleTypeAnnotations = rawRuntimeVisibleTypeAnnotations != null ? StaticObject.wrap(rawRuntimeVisibleTypeAnnotations.getData(), meta) : StaticObject.NULL;
final Klass[] rawParameterKlasses = m.resolveParameterKlasses();
StaticObject parameterTypes = meta.java_lang_Class.allocateReferenceArray(m.getParameterCount(), new IntFunction<StaticObject>() {
@Override
public StaticObject apply(int j) {
return rawParameterKlasses[j].mirror();
}
});
final Klass[] rawCheckedExceptions = m.getCheckedExceptions();
StaticObject checkedExceptions = meta.java_lang_Class.allocateReferenceArray(rawCheckedExceptions.length, new IntFunction<StaticObject>() {
@Override
public StaticObject apply(int j) {
return rawCheckedExceptions[j].mirror();
}
});
SignatureAttribute signatureAttribute = (SignatureAttribute) m.getAttribute(Name.Signature);
StaticObject genericSignature = StaticObject.NULL;
if (signatureAttribute != null) {
String sig = m.getConstantPool().symbolAt(signatureAttribute.getSignatureIndex(), "signature").toString();
genericSignature = meta.toGuestString(sig);
}
StaticObject instance = meta.java_lang_reflect_Constructor.allocateInstance();
constructorInit.invokeDirect(/* this */
instance, /* declaringKlass */
m.getDeclaringKlass().mirror(), /* parameterTypes */
parameterTypes, /* checkedExceptions */
checkedExceptions, /* modifiers */
m.getMethodModifiers(), // TODO(peterssen): Fill method slot.
i, /* signature */
genericSignature, /* annotations */
runtimeVisibleAnnotations, /* parameterAnnotations */
runtimeVisibleParameterAnnotations);
meta.HIDDEN_CONSTRUCTOR_KEY.setHiddenObject(instance, m);
meta.HIDDEN_CONSTRUCTOR_RUNTIME_VISIBLE_TYPE_ANNOTATIONS.setHiddenObject(instance, runtimeVisibleTypeAnnotations);
return instance;
}
});
return arr;
}
use of com.oracle.truffle.espresso.runtime.Attribute in project graal by oracle.
the class ClassRedefinition method isUnchangedField.
private static boolean isUnchangedField(Field oldField, ParserField newField, Map<ParserField, Field> compatibleFields) {
boolean sameName = oldField.getName() == newField.getName();
boolean sameType = oldField.getType() == newField.getType();
boolean sameFlags = oldField.getModifiers() == (newField.getFlags() & Constants.JVM_RECOGNIZED_FIELD_MODIFIERS);
if (sameName && sameType) {
if (sameFlags) {
// same name + type + flags
// check field attributes
Attribute[] oldAttributes = oldField.getAttributes();
Attribute[] newAttributes = newField.getAttributes();
if (oldAttributes.length != newAttributes.length) {
return false;
}
for (Attribute oldAttribute : oldAttributes) {
boolean found = false;
for (Attribute newAttribute : newAttributes) {
if (oldAttribute.getName() == newAttribute.getName() && oldAttribute.sameAs(newAttribute)) {
found = true;
break;
}
}
if (!found) {
return false;
}
}
// identical field found
return true;
} else {
// same name + type
if (Modifier.isStatic(oldField.getModifiers()) != Modifier.isStatic(newField.getFlags())) {
// a change from static -> non-static or vice versa is not compatible
} else {
compatibleFields.put(newField, oldField);
}
return false;
}
}
return false;
}
use of com.oracle.truffle.espresso.runtime.Attribute 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);
}
use of com.oracle.truffle.espresso.runtime.Attribute in project graal by oracle.
the class ClassfileParser method parseMethod.
private ParserMethod parseMethod(boolean isInterface) {
int methodFlags = stream.readU2();
int nameIndex = stream.readU2();
int signatureIndex = stream.readU2();
final Symbol<Name> name;
final Symbol<Signature> signature;
int attributeCount;
Attribute[] methodAttributes;
int extraFlags = methodFlags;
boolean isClinit = false;
boolean isInit = false;
try (DebugCloseable closeable = METHOD_INIT.scope(context.getTimers())) {
try (DebugCloseable nameCheck = NAME_CHECK.scope(context.getTimers())) {
pool.utf8At(nameIndex).validateMethodName(true);
name = pool.symbolAt(nameIndex, "method name");
if (name.equals(Name._clinit_)) {
// ACC_STRICT flag.
if (majorVersion < JAVA_7_VERSION) {
// Backwards compatibility.
methodFlags = ACC_STATIC;
} else if ((methodFlags & ACC_STATIC) == ACC_STATIC) {
methodFlags &= (ACC_STRICT | ACC_STATIC);
} else if (context.getJavaVersion().java9OrLater()) {
throw ConstantPool.classFormatError("Method <clinit> is not static.");
}
// extraFlags = INITIALIZER | methodFlags;
isClinit = true;
} else if (name.equals(Name._init_)) {
if (isInterface) {
throw ConstantPool.classFormatError("Method <init> is not valid in an interface.");
}
isInit = true;
}
}
final boolean isStatic = Modifier.isStatic(extraFlags);
verifyMethodFlags(methodFlags, isInterface, isInit, isClinit, majorVersion);
/*
* A method is a class or interface initialization method if all of the following are
* true:
*
* It has the special name <clinit>.
*
* It is void (4.3.3). (checked earlier)
*
* In a class file whose version number is 51.0 or above, the method has its ACC_STATIC
* flag set and takes no arguments (4.6).
*/
try (DebugCloseable signatureCheck = SIGNATURE_CHECK.scope(context.getTimers())) {
// Checks for void method if init or clinit.
/*
* Obtain slot number for the signature. Forces a validation, but better in startup
* than going twice through the sequence, once for validation, once for slots.
*/
int slots = pool.utf8At(signatureIndex).validateSignatureGetSlots(isInit || isClinit);
signature = Signatures.check(pool.symbolAt(signatureIndex, "method descriptor"));
if (isClinit && majorVersion >= JAVA_7_VERSION) {
// Checks clinit takes no arguments.
if (!signature.equals(Signature._void)) {
throw ConstantPool.classFormatError("Method <clinit> has invalid signature: " + signature);
}
}
if (slots + (isStatic ? 0 : 1) > 255) {
throw ConstantPool.classFormatError("Too many arguments in method signature: " + signature);
}
if (name.equals(Name.finalize) && signature.equals(Signature._void) && !Modifier.isStatic(methodFlags) && !Type.java_lang_Object.equals(classType)) {
// This class has a finalizer method implementation (ignore for
// java.lang.Object).
classFlags |= ACC_FINALIZER;
}
}
attributeCount = stream.readU2();
methodAttributes = spawnAttributesArray(attributeCount);
}
CodeAttribute codeAttribute = null;
Attribute checkedExceptions = null;
Attribute runtimeVisibleAnnotations = null;
CommonAttributeParser commonAttributeParser = new CommonAttributeParser(InfoType.Method);
MethodParametersAttribute methodParameters = null;
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.Code)) {
if (codeAttribute != null) {
throw ConstantPool.classFormatError("Duplicate Code attribute");
}
try (DebugCloseable code = CODE_PARSE.scope(context.getTimers())) {
methodAttributes[i] = codeAttribute = parseCodeAttribute(attributeName);
}
} else if (attributeName.equals(Name.Exceptions)) {
if (checkedExceptions != null) {
throw ConstantPool.classFormatError("Duplicate Exceptions attribute");
}
methodAttributes[i] = checkedExceptions = parseExceptions(attributeName);
} else if (attributeName.equals(Name.Synthetic)) {
methodFlags |= ACC_SYNTHETIC;
methodAttributes[i] = checkedExceptions = new Attribute(attributeName, null);
} else if (majorVersion >= JAVA_1_5_VERSION) {
if (attributeName.equals(Name.RuntimeVisibleAnnotations)) {
if (runtimeVisibleAnnotations != null) {
throw ConstantPool.classFormatError("Duplicate RuntimeVisibleAnnotations attribute");
}
// Check if java.lang.invoke.LambdaForm.Compiled is present here.
byte[] data = stream.readByteArray(attributeSize);
ClassfileStream subStream = new ClassfileStream(data, this.classfile);
int count = subStream.readU2();
for (int j = 0; j < count; j++) {
int typeIndex = parseAnnotation(subStream);
Utf8Constant constant = pool.utf8At(typeIndex, "annotation type");
// Validation of the type is done at runtime by guest java code.
Symbol<Type> annotType = constant.value();
if (Type.java_lang_invoke_LambdaForm$Compiled.equals(annotType)) {
methodFlags |= ACC_LAMBDA_FORM_COMPILED;
} else if (Type.java_lang_invoke_LambdaForm$Hidden.equals(annotType) || Type.jdk_internal_vm_annotation_Hidden.equals(annotType)) {
methodFlags |= ACC_HIDDEN;
} else if (Type.sun_reflect_CallerSensitive.equals(annotType) || Type.jdk_internal_reflect_CallerSensitive.equals(annotType)) {
methodFlags |= ACC_CALLER_SENSITIVE;
}
}
methodAttributes[i] = runtimeVisibleAnnotations = new Attribute(attributeName, data);
} else if (attributeName.equals(Name.MethodParameters)) {
if (methodParameters != null) {
throw ConstantPool.classFormatError("Duplicate MethodParameters attribute");
}
methodAttributes[i] = methodParameters = parseMethodParameters(attributeName);
} else {
Attribute attr = commonAttributeParser.parseCommonAttribute(attributeName, attributeSize);
// stream.skip(attributeSize);
methodAttributes[i] = attr == null ? new Attribute(attributeName, stream.readByteArray(attributeSize)) : attr;
}
} else {
// stream.skip(attributeSize);
methodAttributes[i] = new Attribute(attributeName, stream.readByteArray(attributeSize));
}
final int distance = stream.getPosition() - startPosition;
if (attributeSize != distance) {
final String message = "Invalid attribute_length for " + attributeName + " attribute (reported " + attributeSize + " != parsed " + distance + ")";
throw ConstantPool.classFormatError(message);
}
}
if (Modifier.isAbstract(methodFlags) || Modifier.isNative(methodFlags)) {
if (codeAttribute != null) {
throw ConstantPool.classFormatError("Code attribute supplied for native or abstract method");
}
} else {
if (codeAttribute == null) {
throw ConstantPool.classFormatError("Missing Code attribute");
}
}
if (classDefinitionInfo.isHidden()) {
methodFlags |= ACC_HIDDEN;
}
return ParserMethod.create(methodFlags, name, signature, methodAttributes);
}
Aggregations