use of com.taobao.android.dx.rop.annotation.NameValuePair in project atlas by alibaba.
the class ValueEncoder method writeAnnotation.
/**
* Writes out the encoded form of the given annotation, that is,
* as an {@code encoded_annotation} and not including a
* {@code value_type} prefix. If the output stream keeps
* (debugging) annotations and {@code topLevel} is
* {@code true}, then this method will write (debugging)
* annotations.
*
* @param annotation {@code non-null;} annotation instance to write
* @param topLevel {@code true} iff the given annotation is the
* top-level annotation or {@code false} if it is a sub-annotation
* of some other annotation
*/
public void writeAnnotation(Annotation annotation, boolean topLevel) {
boolean annotates = topLevel && out.annotates();
StringIdsSection stringIds = file.getStringIds();
TypeIdsSection typeIds = file.getTypeIds();
CstType type = annotation.getType();
int typeIdx = typeIds.indexOf(type);
if (annotates) {
out.annotate(" type_idx: " + Hex.u4(typeIdx) + " // " + type.toHuman());
}
out.writeUleb128(typeIds.indexOf(annotation.getType()));
Collection<NameValuePair> pairs = annotation.getNameValuePairs();
int size = pairs.size();
if (annotates) {
out.annotate(" size: " + Hex.u4(size));
}
out.writeUleb128(size);
int at = 0;
for (NameValuePair pair : pairs) {
CstString name = pair.getName();
int nameIdx = stringIds.indexOf(name);
Constant value = pair.getValue();
if (annotates) {
out.annotate(0, " elements[" + at + "]:");
at++;
out.annotate(" name_idx: " + Hex.u4(nameIdx) + " // " + name.toHuman());
}
out.writeUleb128(nameIdx);
if (annotates) {
out.annotate(" value: " + constantToHuman(value));
}
writeConstant(value);
}
if (annotates) {
out.endAnnotation();
}
}
use of com.taobao.android.dx.rop.annotation.NameValuePair in project atlas by alibaba.
the class AnnotationParser method parseAnnotation.
/**
* Parses a single annotation.
*
* @param visibility {@code non-null;} visibility of the parsed annotation
* @return {@code non-null;} the parsed annotation
*/
private Annotation parseAnnotation(AnnotationVisibility visibility) throws IOException {
requireLength(4);
int typeIndex = input.readUnsignedShort();
int numElements = input.readUnsignedShort();
CstString typeString = (CstString) pool.get(typeIndex);
CstType type = new CstType(Type.intern(typeString.getString()));
if (observer != null) {
parsed(2, "type: " + type.toHuman());
parsed(2, "num_elements: " + numElements);
}
Annotation annotation = new Annotation(type, visibility);
for (int i = 0; i < numElements; i++) {
if (observer != null) {
parsed(0, "elements[" + i + "]:");
changeIndent(1);
}
NameValuePair element = parseElement();
annotation.add(element);
if (observer != null) {
changeIndent(-1);
}
}
annotation.setImmutable();
return annotation;
}
use of com.taobao.android.dx.rop.annotation.NameValuePair in project atlas by alibaba.
the class AttributeTranslator method translateAnnotationDefaults.
/**
* Gets the {@code AnnotationDefault} attributes out of a
* given class, if any, reforming them as an
* {@code AnnotationDefault} annotation.
*
* @param cf {@code non-null;} the class in question
* @return {@code null-ok;} an appropriately-constructed
* {@code AnnotationDefault} annotation, if there were any
* annotation defaults in the class, or {@code null} if not
*/
private static Annotation translateAnnotationDefaults(DirectClassFile cf) {
CstType thisClass = cf.getThisClass();
MethodList methods = cf.getMethods();
int sz = methods.size();
Annotation result = new Annotation(thisClass, AnnotationVisibility.EMBEDDED);
boolean any = false;
for (int i = 0; i < sz; i++) {
Method one = methods.get(i);
AttributeList attribs = one.getAttributes();
AttAnnotationDefault oneDefault = (AttAnnotationDefault) attribs.findFirst(AttAnnotationDefault.ATTRIBUTE_NAME);
if (oneDefault != null) {
NameValuePair pair = new NameValuePair(one.getNat().getName(), oneDefault.getValue());
result.add(pair);
any = true;
}
}
if (!any) {
return null;
}
result.setImmutable();
return AnnotationUtils.makeAnnotationDefault(result);
}
use of com.taobao.android.dx.rop.annotation.NameValuePair in project atlas by alibaba.
the class ValueEncoder method addContents.
/**
* Helper for {@code addContents()} methods, which adds
* contents for a particular {@link Annotation}, calling itself
* recursively should it encounter a nested annotation.
*
* @param file {@code non-null;} the file to add to
* @param annotation {@code non-null;} the annotation to add contents for
*/
public static void addContents(DexFile file, Annotation annotation) {
TypeIdsSection typeIds = file.getTypeIds();
StringIdsSection stringIds = file.getStringIds();
typeIds.intern(annotation.getType());
for (NameValuePair pair : annotation.getNameValuePairs()) {
stringIds.intern(pair.getName());
addContents(file, pair.getValue());
}
}
use of com.taobao.android.dx.rop.annotation.NameValuePair in project atlas by alibaba.
the class AnnotationItem method annotateTo.
/**
* Write a (listing file) annotation for this instance to the given
* output, that consumes no bytes of output. This is for annotating
* a reference to this instance at the point of the reference.
*
* @param out {@code non-null;} where to output to
* @param prefix {@code non-null;} prefix for each line of output
*/
public void annotateTo(AnnotatedOutput out, String prefix) {
out.annotate(0, prefix + "visibility: " + annotation.getVisibility().toHuman());
out.annotate(0, prefix + "type: " + annotation.getType().toHuman());
for (NameValuePair pair : annotation.getNameValuePairs()) {
CstString name = pair.getName();
Constant value = pair.getValue();
out.annotate(0, prefix + name.toHuman() + ": " + ValueEncoder.constantToHuman(value));
}
}
Aggregations