use of com.android.dx.rop.annotation.NameValuePair in project J2ME-Loader by nikita36078.
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.android.dx.rop.annotation.NameValuePair in project dexmaker by linkedin.
the class AnnotationId method set.
/**
* Set an annotation element of this instance.
* If there is a preexisting element with the same name, it will be
* replaced by this method.
*
* @param element {@code non-null;} the annotation element to be set.
*/
public void set(Element element) {
if (element == null) {
throw new NullPointerException("element == null");
}
CstString pairName = new CstString(element.getName());
Constant pairValue = Element.toConstant(element.getValue());
NameValuePair nameValuePair = new NameValuePair(pairName, pairValue);
elements.put(element.getName(), nameValuePair);
}
use of com.android.dx.rop.annotation.NameValuePair in project dexmaker by linkedin.
the class AnnotationId method addToMethod.
/**
* Add this annotation to a method.
*
* @param dexMaker DexMaker instance.
* @param method Method to be added to.
*/
public void addToMethod(DexMaker dexMaker, MethodId<?, ?> method) {
if (annotatedElement != ElementType.METHOD) {
throw new IllegalStateException("This annotation is not for method");
}
if (!method.declaringType.equals(declaringType)) {
throw new IllegalArgumentException("Method" + method + "'s declaring type is inconsistent with" + this);
}
ClassDefItem classDefItem = dexMaker.getTypeDeclaration(declaringType).toClassDefItem();
if (classDefItem == null) {
throw new NullPointerException("No class defined item is found");
} else {
CstMethodRef cstMethodRef = method.constant;
if (cstMethodRef == null) {
throw new NullPointerException("Method reference is NULL");
} else {
// Generate CstType
CstType cstType = CstType.intern(type.ropType);
// Generate Annotation
Annotation annotation = new Annotation(cstType, AnnotationVisibility.RUNTIME);
// Add generated annotation
Annotations annotations = new Annotations();
for (NameValuePair nvp : elements.values()) {
annotation.add(nvp);
}
annotations.add(annotation);
classDefItem.addMethodAnnotations(cstMethodRef, annotations, dexMaker.getDexFile());
}
}
}
use of com.android.dx.rop.annotation.NameValuePair in project buck by facebook.
the class AnnotationParser method parseElement.
/**
* Parses a {@link NameValuePair}.
*
* @return {@code non-null;} the parsed element
*/
private NameValuePair parseElement() throws IOException {
requireLength(5);
int elementNameIndex = input.readUnsignedShort();
CstString elementName = (CstString) pool.get(elementNameIndex);
if (observer != null) {
parsed(2, "element_name: " + elementName.toHuman());
parsed(0, "value: ");
changeIndent(1);
}
Constant value = parseValue();
if (observer != null) {
changeIndent(-1);
}
return new NameValuePair(elementName, value);
}
use of com.android.dx.rop.annotation.NameValuePair in project buck by facebook.
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());
}
}
Aggregations