private void parseAnnotation(Annotation anno, Element p) { Element ea = new Element("Annotation"); ea.setAttr("name", "" + x.getCpString(anno.type_index)); for (Annotation.element_value_pair evp : anno.element_value_pairs) { Element evpe = new Element("Element"); evpe.setAttr("tag", "" + evp.value.tag); evpe.setAttr("value", x.getCpString(evp.element_name_index)); Element child = aev.visit(evp.value, evpe); if (child != null) { evpe.add(child); } ea.add(evpe); } ea.trimToSize(); p.add(ea); }
public void testAnnotation(TestResult testResult, ClassFile classFile, Annotation annotation) throws ConstantPoolException { testResult.checkEquals(classFile.constant_pool.getUTF8Value(annotation.type_index), String.format("L%s;", annotationName), "Testing annotation name : " + annotationName); testResult.checkEquals(annotation.num_element_value_pairs, elementValues.size(), "Number of element values"); if (!testResult.checkEquals(annotation.num_element_value_pairs, elementValues.size(), "Number of element value pairs")) { return; } for (int i = 0; i < annotation.num_element_value_pairs; ++i) { Annotation.element_value_pair pair = annotation.element_value_pairs[i]; testResult.checkEquals(classFile.constant_pool.getUTF8Value(pair.element_name_index), elementValues.get(i).elementName, "element_name_index : " + elementValues.get(i).elementName); elementValues.get(i).elementValue.testElementValue(testResult, classFile, pair.value); } }
@Override public void testElementValue( TestResult testResult, ClassFile classFile, Annotation.element_value element_value) throws ConstantPoolException { testTag(testResult, element_value.tag); Annotation ev = ((Annotation.Annotation_element_value) element_value).annotation_value; testResult.checkEquals( classFile.constant_pool.getUTF8Info(ev.type_index).value, String.format("L%s;", annotationName), "type_index"); for (int i = 0; i < ev.num_element_value_pairs; ++i) { Annotation.element_value_pair pair = ev.element_value_pairs[i]; Pair expectedPair = annotation.elementValues.get(i); expectedPair.elementValue.testElementValue(testResult, classFile, pair.value); testResult.checkEquals( classFile.constant_pool.getUTF8Info(pair.element_name_index).value, expectedPair.elementName, "element_name_index"); } }
public void write(Annotation annot, boolean resolveIndices) { writeDescriptor(annot.type_index, resolveIndices); boolean showParens = annot.num_element_value_pairs > 0 || !resolveIndices; if (showParens) print("("); for (int i = 0; i < annot.num_element_value_pairs; i++) { if (i > 0) print(","); write(annot.element_value_pairs[i], resolveIndices); } if (showParens) print(")"); }
@Override public Element visitRuntimeVisibleParameterAnnotations(RuntimeVisibleParameterAnnotations_attribute rvpa, Element p) { Element e = new Element(x.getCpString(rvpa.attribute_name_index)); for (Annotation[] pa : rvpa.parameter_annotations) { parseAnnotations(pa, e); } p.add(e); return null; }
@Override public Element visitRuntimeInvisibleParameterAnnotations(RuntimeInvisibleParameterAnnotations_attribute ripa, Element p) { Element e = new Element(x.getCpString(ripa.attribute_name_index)); for (Annotation[] pa : ripa.parameter_annotations) { parseAnnotations(pa, e); } p.add(e); return null; }
@Override public Element visitAnnotation(Annotation_element_value a, Element p) { Element el = new Element("Annotation"); Annotation anno = a.annotation_value; for (Annotation.element_value_pair evp : anno.element_value_pairs) { Element child = visit(evp.value, el); if (child != null) { el.add(child); } } el.trimToSize(); return el; }
@Override public Element visitArray(Array_element_value a, Element p) { Element el = new Element("Array"); for (Annotation.element_value v : a.values) { Element child = visit(v, el); if (child != null) { el.add(child); } } el.trimToSize(); return el; }
private Annotation[] createAnnotations(List<CPInfo> constantPool, List<AnnotationDescription> desc) { Annotation[] result = new Annotation[desc.size()]; int i = 0; for (AnnotationDescription ad : desc) { result[i++] = createAnnotation(constantPool, ad); } return result; }
private Annotation[][] createParameterAnnotations(List<CPInfo> constantPool, List<List<AnnotationDescription>> desc) { Annotation[][] result = new Annotation[desc.size()][]; int i = 0; for (List<AnnotationDescription> paramAnnos : desc) { result[i++] = createAnnotations(constantPool, paramAnnos); } return result; }
private element_value createAttributeValue(List<CPInfo> constantPool, Object value) { Pair<Integer, Character> constantPoolEntry = addConstant(constantPool, value, true); if (constantPoolEntry != null) { return new Primitive_element_value(constantPoolEntry.fst, constantPoolEntry.snd); } else if (value instanceof EnumConstant) { EnumConstant ec = (EnumConstant) value; return new Enum_element_value(addString(constantPool, ec.type), addString(constantPool, ec.constant), 'e'); } else if (value instanceof ClassConstant) { ClassConstant cc = (ClassConstant) value; return new Class_element_value(addString(constantPool, cc.type), 'c'); } else if (value instanceof AnnotationDescription) { Annotation annotation = createAnnotation(constantPool, ((AnnotationDescription) value)); return new Annotation_element_value(annotation, '@'); } else if (value instanceof Collection) { @SuppressWarnings("unchecked") Collection<Object> array = (Collection<Object>) value; element_value[] values = new element_value[array.size()]; int i = 0; for (Object elem : array) { values[i++] = createAttributeValue(constantPool, elem); } return new Array_element_value(values, '['); } throw new IllegalStateException(value.getClass().getName()); }
private List<AnnotationDescription> annotations2Description(ConstantPool cp, Attribute attr) throws ConstantPoolException { RuntimeAnnotations_attribute annotationsAttr = (RuntimeAnnotations_attribute) attr; List<AnnotationDescription> descs = new ArrayList<>(); for (Annotation a : annotationsAttr.annotations) { descs.add(annotation2Description(cp, a)); } return descs; }
private List<List<AnnotationDescription>> parameterAnnotations2Description(ConstantPool cp, Attribute attr) throws ConstantPoolException { RuntimeParameterAnnotations_attribute annotationsAttr = (RuntimeParameterAnnotations_attribute) attr; List<List<AnnotationDescription>> descs = new ArrayList<>(); for (Annotation[] attrAnnos : annotationsAttr.parameter_annotations) { List<AnnotationDescription> paramDescs = new ArrayList<>(); for (Annotation ann : attrAnnos) { paramDescs.add(annotation2Description(cp, ann)); } descs.add(paramDescs); } return descs; }
private AnnotationDescription annotation2Description(ConstantPool cp, Annotation a) throws ConstantPoolException { String annotationType = cp.getUTF8Value(a.type_index); Map<String, Object> values = new HashMap<>(); for (element_value_pair e : a.element_value_pairs) { values.put(cp.getUTF8Value(e.element_name_index), convertElementValue(cp, e.value)); } return new AnnotationDescription(annotationType, values); }
@Override public void testElementValue( TestResult testCase, ClassFile classFile, Annotation.element_value element_value, String[] values) throws ConstantPool.InvalidIndex { Annotation.Primitive_element_value ev = (Annotation.Primitive_element_value) element_value; ConstantPool.CONSTANT_Integer_info info = (ConstantPool.CONSTANT_Integer_info) classFile.constant_pool.get(ev.const_value_index); testCase.checkEquals(info.value, Integer.parseInt(values[0]), "const_value_index"); }
@Override public void testElementValue( TestResult testCase, ClassFile classFile, Annotation.element_value element_value, String[] values) throws ConstantPool.InvalidIndex { Annotation.Primitive_element_value ev = (Annotation.Primitive_element_value) element_value; ConstantPool.CONSTANT_Long_info info = (ConstantPool.CONSTANT_Long_info) classFile.constant_pool.get(ev.const_value_index); testCase.checkEquals(info.value, Long.parseLong(values[0]), "const_value_index"); }
@Override public void testElementValue( TestResult testCase, ClassFile classFile, Annotation.element_value element_value, String[] values) throws ConstantPool.InvalidIndex { Annotation.Primitive_element_value ev = (Annotation.Primitive_element_value) element_value; ConstantPool.CONSTANT_Float_info info = (ConstantPool.CONSTANT_Float_info) classFile.constant_pool.get(ev.const_value_index); testCase.checkEquals(info.value, Float.parseFloat(values[0]), "const_value_index"); }
@Override public void testElementValue( TestResult testCase, ClassFile classFile, Annotation.element_value element_value, String[] values) throws ConstantPool.InvalidIndex { Annotation.Primitive_element_value ev = (Annotation.Primitive_element_value) element_value; ConstantPool.CONSTANT_Double_info info = (ConstantPool.CONSTANT_Double_info) classFile.constant_pool.get(ev.const_value_index); testCase.checkEquals(info.value, Double.parseDouble(values[0]), "const_value_index"); }
@Override public void testElementValue( TestResult testCase, ClassFile classFile, Annotation.element_value element_value, String[] values) throws ConstantPool.InvalidIndex { Annotation.Primitive_element_value ev = (Annotation.Primitive_element_value) element_value; ConstantPool.CONSTANT_Utf8_info info = (ConstantPool.CONSTANT_Utf8_info) classFile.constant_pool.get(ev.const_value_index); testCase.checkEquals(info.value, values[0], "const_value_index"); }
@Override public void testElementValue( TestResult testCase, ClassFile classFile, Annotation.element_value element_value, String[] values) throws ConstantPool.InvalidIndex, ConstantPool.UnexpectedEntry { Annotation.Enum_element_value ev = (Annotation.Enum_element_value) element_value; testCase.checkEquals(classFile.constant_pool.getUTF8Info(ev.type_name_index).value, values[0], "type_name_index"); testCase.checkEquals(classFile.constant_pool.getUTF8Info(ev.const_name_index).value, values[1], "const_name_index"); }
@Override public void testElementValue( TestResult testCase, ClassFile classFile, Annotation.element_value element_value, String[] values) throws ConstantPool.InvalidIndex, ConstantPool.UnexpectedEntry { Annotation.Class_element_value ev = (Annotation.Class_element_value) element_value; testCase.checkEquals( classFile.constant_pool.getUTF8Info(ev.class_info_index).value, values[0], "class_info_index"); }
@Override public void testLength(TestResult testCase, AnnotationDefault_attribute attr) { Annotation.Array_element_value ev = (Annotation.Array_element_value) attr.default_value; int sizeOfTag = ev.values[0].tag == 'e' ? 0 : 1; // tag (1 byte) + array header (2 byte) + length of entries testCase.checkEquals(attr.attribute_length, 1 + 2 + (sizeOfTag + ev.length() / ev.num_values) * ev.num_values, "attribute_length"); }
@Override public void testElementValue(TestResult testResult, ClassFile classFile, Annotation.element_value element_value) throws ConstantPoolException { testTag(testResult, element_value.tag); Annotation.Primitive_element_value ev = (Annotation.Primitive_element_value) element_value; ConstantPool.CONSTANT_Integer_info info = (ConstantPool.CONSTANT_Integer_info) classFile.constant_pool.get(ev.const_value_index); testResult.checkEquals(info.value, value, "const_value_index : " + value); }
@Override public void testElementValue(TestResult testResult, ClassFile classFile, Annotation.element_value element_value) throws ConstantPoolException { testTag(testResult, element_value.tag); Annotation.Primitive_element_value ev = (Annotation.Primitive_element_value) element_value; ConstantPool.CONSTANT_Integer_info info = (ConstantPool.CONSTANT_Integer_info) classFile.constant_pool.get(ev.const_value_index); testResult.checkEquals(info.value, value ? 1 : 0, "const_value_index : " + value); }
@Override public void testElementValue(TestResult testResult, ClassFile classFile, Annotation.element_value element_value) throws ConstantPoolException { testTag(testResult, element_value.tag); Annotation.Primitive_element_value ev = (Annotation.Primitive_element_value) element_value; ConstantPool.CONSTANT_Integer_info info = (ConstantPool.CONSTANT_Integer_info) classFile.constant_pool.get(ev.const_value_index); testResult.checkEquals(info.value, (int) value, "const_value_index : " + value); }
@Override public void testElementValue(TestResult testResult, ClassFile classFile, Annotation.element_value element_value) throws ConstantPool.InvalidIndex { testTag(testResult, element_value.tag); Annotation.Primitive_element_value ev = (Annotation.Primitive_element_value) element_value; ConstantPool.CONSTANT_Long_info info = (ConstantPool.CONSTANT_Long_info) classFile.constant_pool.get(ev.const_value_index); testResult.checkEquals(info.value, value, "const_value_index"); }
@Override public void testElementValue(TestResult testResult, ClassFile classFile, Annotation.element_value element_value) throws ConstantPool.InvalidIndex { testTag(testResult, element_value.tag); Annotation.Primitive_element_value ev = (Annotation.Primitive_element_value) element_value; ConstantPool.CONSTANT_Float_info info = (ConstantPool.CONSTANT_Float_info) classFile.constant_pool.get(ev.const_value_index); testResult.checkEquals(info.value, value, "const_value_index"); }
@Override public void testElementValue(TestResult testResult, ClassFile classFile, Annotation.element_value element_value) throws ConstantPoolException { testTag(testResult, element_value.tag); Annotation.Primitive_element_value ev = (Annotation.Primitive_element_value) element_value; ConstantPool.CONSTANT_Double_info info = (ConstantPool.CONSTANT_Double_info) classFile.constant_pool.get(ev.const_value_index); testResult.checkEquals(info.value, value, "const_value_index"); }
@Override public void testElementValue(TestResult testResult, ClassFile classFile, Annotation.element_value element_value) throws ConstantPoolException { testTag(testResult, element_value.tag); Annotation.Primitive_element_value ev = (Annotation.Primitive_element_value) element_value; ConstantPool.CONSTANT_Utf8_info info = (ConstantPool.CONSTANT_Utf8_info) classFile.constant_pool.get(ev.const_value_index); testResult.checkEquals(info.value, value, "const_value_index"); }
@Override public void testElementValue( TestResult testResult, ClassFile classFile, Annotation.element_value element_value) throws ConstantPoolException { testTag(testResult, element_value.tag); Annotation.Enum_element_value ev = (Annotation.Enum_element_value) element_value; testResult.checkEquals(classFile.constant_pool.getUTF8Info(ev.type_name_index).value, String.format("L%s;", typeName), "type_name_index"); testResult.checkEquals(classFile.constant_pool.getUTF8Info(ev.const_name_index).value, constName, "const_name_index"); }