private void testFields(Map<String, ExpectedSignature> expectedSignatures, ClassFile classFile) throws ConstantPoolException { String className = classFile.getName(); Set<String> foundFields = new HashSet<>(); for (Field field : classFile.fields) { String fieldName = field.getName(classFile.constant_pool); printf("Testing field %s\n", fieldName); testAttribute( fieldName, classFile, () -> (Signature_attribute) field.attributes.get(Attribute.Signature), expectedSignatures.get(fieldName)); foundFields.add(fieldName); } checkAllMembersFound(foundFields, expectedSignatures, "Checking that all fields of class " + className + " with Signature attribute found"); }
String getFieldName(Field f) { try { return f.getName(constant_pool); } catch (ConstantPoolException e) { return report(e); } }
private AccessFlags.Kind getKind(Element e) { switch(e.getName()) { case "Class": return AccessFlags.Kind.Class; case "InnerClass": return AccessFlags.Kind.InnerClass; case "Field": return AccessFlags.Kind.Field ; case "Method": return AccessFlags.Kind.Method; default: throw new RuntimeException("should not reach here"); } }
private void testFields(ClassFile cf) throws ConstantPoolException { for (Field f : cf.fields) { String fieldName = cf.constant_pool.getUTF8Value(f.name_index); echo("Testing field : " + fieldName); Deprecated_attribute attr = (Deprecated_attribute) f.attributes.get(Attribute.Deprecated); testAttribute(fieldName, attr, cf); } }
void analyzeClassFile(ClassFile classFileToCheck) throws IOException, ConstantPoolException, Descriptor.InvalidDescriptor { boolean enumClass = (classFileToCheck.access_flags.flags & ACC_ENUM) != 0; boolean nonFinalStaticEnumField; boolean nonFinalStaticField; currentFieldsToIgnore = classFieldsToIgnoreMap.get(classFileToCheck.getName()); for (Field field : classFileToCheck.fields) { if (ignoreField(field.getName(classFileToCheck.constant_pool))) { continue; } nonFinalStaticEnumField = (field.access_flags.flags & (ACC_ENUM | ACC_FINAL)) == 0; nonFinalStaticField = (field.access_flags.flags & ACC_STATIC) != 0 && (field.access_flags.flags & ACC_FINAL) == 0; if (enumClass ? nonFinalStaticEnumField : nonFinalStaticField) { errors.add("There is a mutable field named " + field.getName(classFileToCheck.constant_pool) + ", at class " + classFileToCheck.getName()); } } }
private static void findAnnotations(ClassFile cf, List<TypeAnnotation> annos) { findAnnotations(cf, Attribute.RuntimeVisibleTypeAnnotations, annos); findAnnotations(cf, Attribute.RuntimeInvisibleTypeAnnotations, annos); for (Field f : cf.fields) { findAnnotations(cf, f, annos); } for (Method m: cf.methods) { findAnnotations(cf, m, annos); } }
private static void findAnnotations(ClassFile cf, Field m, String name, List<TypeAnnotation> annos) { int index = m.attributes.getIndex(cf.constant_pool, name); if (index != -1) { Attribute attr = m.attributes.get(index); assert attr instanceof RuntimeTypeAnnotations_attribute; RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr; annos.addAll(Arrays.asList(tAttr.annotations)); } }
private void scanFields(Set<Integer> utf8Descriptors) throws Exception { for (Field field : cf.fields) { int descriptorIndex = field.descriptor.index; utf8Descriptors.add(descriptorIndex); scanAttributes(field.attributes, utf8Descriptors); } }