use of org.jf.dexlib2.iface.Annotation in project atlas by alibaba.
the class FieldDefinition method writeTo.
public static void writeTo(baksmaliOptions options, IndentingWriter writer, Field field, boolean setInStaticConstructor) throws IOException {
EncodedValue initialValue = field.getInitialValue();
int accessFlags = field.getAccessFlags();
if (setInStaticConstructor && AccessFlags.STATIC.isSet(accessFlags) && AccessFlags.FINAL.isSet(accessFlags) && initialValue != null) {
if (!EncodedValueUtils.isDefaultValue(initialValue)) {
writer.write("# The value of this static final field might be set in the static constructor\n");
} else {
// don't write out the default initial value for static final fields that get set in the static
// constructor
initialValue = null;
}
}
writer.write(".field ");
writeAccessFlags(writer, field.getAccessFlags());
writer.write(field.getName());
writer.write(':');
String clazz = TypeGenUtil.newType(field.getType());
writer.write(clazz);
if (initialValue != null) {
writer.write(" = ");
String containingClass = null;
if (options.useImplicitReferences) {
containingClass = field.getDefiningClass();
}
EncodedValueAdaptor.writeTo(writer, initialValue, containingClass);
}
writer.write('\n');
Collection<? extends Annotation> annotations = field.getAnnotations();
if (annotations.size() > 0) {
writer.indent(4);
String containingClass = null;
if (options.useImplicitReferences) {
containingClass = field.getDefiningClass();
}
AnnotationFormatter.writeTo(writer, annotations, containingClass);
writer.deindent(4);
writer.write(".end field\n");
}
}
use of org.jf.dexlib2.iface.Annotation in project atlas by alibaba.
the class ClassReIClassDef method reAnnotation.
@Override
protected Annotation reAnnotation(Annotation annotation) {
String type = annotation.getType();
boolean isArray = false;
if (type.startsWith("[")) {
isArray = true;
}
String newType = DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(type)).className, isArray);
Set<? extends AnnotationElement> sets = annotation.getElements();
Set<ImmutableAnnotationElement> newAnnotationElement = new HashSet<ImmutableAnnotationElement>();
for (AnnotationElement annotationElement : sets) {
String name = annotationElement.getName();
EncodedValue encodedValue = annotationElement.getValue();
if (encodedValue instanceof ArrayEncodedValue) {
List<EncodedValue> lists = new ArrayList<EncodedValue>();
for (EncodedValue encodedValueSub : ((ArrayEncodedValue) encodedValue).getValue()) {
if (encodedValueSub instanceof StringEncodedValue) {
String newValue = null;
boolean isArray1 = false;
String value = ((StringEncodedValue) encodedValueSub).getValue();
if (value.startsWith("[")) {
isArray1 = true;
}
if (basicValue.contains(value)) {
newValue = value;
} else if (value.startsWith("Ljava/util/") || value.startsWith("Ljava/lang/") || !value.endsWith(";")) {
newValue = value;
} else {
newValue = DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(value)).className, isArray1);
}
ImmutableStringEncodedValue immutableStringEncodedValue = new ImmutableStringEncodedValue(newValue);
lists.add(immutableStringEncodedValue);
} else if (encodedValueSub instanceof TypeEncodedValue) {
String newValueSub = null;
String value = ((TypeEncodedValue) encodedValueSub).getValue();
boolean isArray2 = false;
if (value.startsWith("[")) {
isArray2 = true;
}
if (basicValue.contains(value)) {
newValueSub = value;
} else if (value.startsWith("Ljava/util/") || !value.endsWith(";")) {
newValueSub = value;
} else {
newValueSub = DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(value)).className, isArray2);
}
ImmutableTypeEncodedValue immutableTypeEncodedValue = new ImmutableTypeEncodedValue(newValueSub);
lists.add(immutableTypeEncodedValue);
} else {
lists.add(encodedValue);
}
}
ImmutableArrayEncodedValue immutableArrayEncodedValue = new ImmutableArrayEncodedValue(lists);
ImmutableAnnotationElement immutableAnnotationElement = new ImmutableAnnotationElement(name, immutableArrayEncodedValue);
newAnnotationElement.add(immutableAnnotationElement);
} else if (encodedValue instanceof StringEncodedValue) {
String value = ((StringEncodedValue) encodedValue).getValue();
String newValue = null;
isArray = false;
if (value.startsWith("[")) {
isArray = true;
}
if (basicValue.contains(value)) {
newValue = value;
} else if (value.startsWith("Ljava/util/") || !value.endsWith(";")) {
newValue = value;
} else {
newValue = DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(value)).className, isArray);
}
ImmutableStringEncodedValue immutableStringEncodedValue = new ImmutableStringEncodedValue(newValue);
ImmutableAnnotationElement immutableAnnotationElement = new ImmutableAnnotationElement(name, immutableStringEncodedValue);
newAnnotationElement.add(immutableAnnotationElement);
} else if (encodedValue instanceof TypeEncodedValue) {
String newValue = null;
String value = ((TypeEncodedValue) encodedValue).getValue();
boolean isArray2 = false;
if (value.startsWith("[")) {
isArray2 = true;
}
if (basicValue.contains(value)) {
newValue = value;
} else if (value.startsWith("Ljava/util/") || !value.endsWith(";")) {
newValue = value;
} else {
newValue = DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(value)).className, isArray2);
}
ImmutableTypeEncodedValue immutableTypeEncodedValue = new ImmutableTypeEncodedValue(newValue);
ImmutableAnnotationElement immutableAnnotationElement = new ImmutableAnnotationElement(name, immutableTypeEncodedValue);
newAnnotationElement.add(immutableAnnotationElement);
} else if (encodedValue instanceof MethodEncodedValue) {
MethodReference methodReference = ((MethodEncodedValue) encodedValue).getValue();
String returnType = methodReference.getReturnType();
boolean isBasic = false;
List<? extends CharSequence> paramTypes = methodReference.getParameterTypes();
List<CharSequence> dalvikParamTypes = new ArrayList<CharSequence>();
List<CharSequence> newParamTypes = new ArrayList<CharSequence>();
for (CharSequence charSequence : paramTypes) {
if (basicType.containsKey(charSequence.toString())) {
newParamTypes.add(charSequence);
dalvikParamTypes.add(basicType.get(charSequence.toString()));
continue;
}
dalvikParamTypes.add(DefineUtils.getDalvikClassName(charSequence.toString()) + (isArray ? "[]" : ""));
newParamTypes.add(DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(charSequence.toString())).className, isArray));
}
final ImmutableMethodReference immutableReference = new ImmutableMethodReference(DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(methodReference.getDefiningClass())).className, false), classProcessor.methodProcess(DefineUtils.getDalvikClassName(methodReference.getDefiningClass()), methodReference.getName(), isBasic ? basicType.get(methodReference.getReturnType()) : DefineUtils.getDalvikClassName(methodReference.getReturnType()) + (isArray ? "[]" : ""), StringUtils.join(dalvikParamTypes.toArray(), ",")).methodName, newParamTypes, isBasic ? returnType : DefineUtils.getDefineClassName(classProcessor.classProcess(DefineUtils.getDalvikClassName(methodReference.getReturnType())).className, methodReference.getReturnType().startsWith("[")));
ImmutableMethodEncodedValue immutableMethodEncodedValue = new ImmutableMethodEncodedValue(immutableReference);
ImmutableAnnotationElement immutableAnnotationElement = new ImmutableAnnotationElement(name, immutableMethodEncodedValue);
newAnnotationElement.add(immutableAnnotationElement);
} else {
newAnnotationElement.add(ImmutableAnnotationElement.of(annotationElement));
}
}
return new ImmutableAnnotation(annotation.getVisibility(), newType, newAnnotationElement);
}
use of org.jf.dexlib2.iface.Annotation in project atlas by alibaba.
the class AbIClassDef method reClassDef.
@Override
public ClassDef reClassDef(ClassDef classDef) {
Iterable<? extends Method> methods = classDef.getMethods();
LinkedHashSet<Method> newMethods = new LinkedHashSet<Method>();
Iterable<? extends Field> fields = classDef.getFields();
LinkedHashSet<Field> newFields = new LinkedHashSet<Field>();
Set<? extends Annotation> annotations = classDef.getAnnotations();
Set<String> interfaces = classDef.getInterfaces();
Set<String> newInterfaces = new HashSet<String>();
Set<Annotation> immutableAnnotations = new HashSet<Annotation>();
String type = classDef.getType();
reType = reType(type);
String superClass = classDef.getSuperclass();
for (String inter : interfaces) {
newInterfaces.add(reInterface(inter));
}
String reSuperClass = reSuperClass(superClass);
for (Annotation annotation : annotations) {
if (type.startsWith("Landroid/taobao/atlas/bundleInfo/AtlasBundleInfoManager;")) {
System.out.println("xxxx");
}
immutableAnnotations.add(reAnnotation(annotation));
}
for (Field field : fields) {
newFields.add(reField(field));
}
for (Method method : methods) {
if (method.getName().equals("<cinit>") || method.getName().equals("<init>")) {
newMethods.add(reMethod(method));
continue;
}
// if (method.getName().equals("getArchiveFile")) {
newMethods.add(reMethod(method));
// }
}
return new ImmutableClassDef(reType, classDef.getAccessFlags(), reSuperClass, newInterfaces, classDef.getSourceFile(), immutableAnnotations, newFields, newMethods);
}
use of org.jf.dexlib2.iface.Annotation in project android-classyshark by google.
the class MetaObjectDex method convertAnnotations.
private AnnotationInfo[] convertAnnotations(Set<? extends Annotation> annotations) {
List<AnnotationInfo> result = new ArrayList<>();
for (Annotation anot : annotations) {
AnnotationInfo ai = new AnnotationInfo();
ai.annotationStr = DexlibAdapter.getTypeName(anot.getType());
result.add(ai);
}
AnnotationInfo[] array = new AnnotationInfo[result.size()];
return result.toArray(array);
}
use of org.jf.dexlib2.iface.Annotation in project smali by JesusFreke.
the class DexWriterTest method testEncodedAnnotationElementOrder.
@Test
public void testEncodedAnnotationElementOrder() {
// Elements are out of order wrt to the element name
ImmutableSet<ImmutableAnnotationElement> encodedElements = ImmutableSet.of(new ImmutableAnnotationElement("zabaglione", ImmutableNullEncodedValue.INSTANCE), new ImmutableAnnotationElement("blah", ImmutableNullEncodedValue.INSTANCE));
ImmutableAnnotationEncodedValue encodedAnnotations = new ImmutableAnnotationEncodedValue("Lan/encoded/annotation", encodedElements);
ImmutableSet<ImmutableAnnotationElement> elements = ImmutableSet.of(new ImmutableAnnotationElement("encoded_annotation", encodedAnnotations));
ImmutableAnnotation annotation = new ImmutableAnnotation(AnnotationVisibility.RUNTIME, "Lorg/test/anno;", elements);
ImmutableClassDef classDef = new ImmutableClassDef("Lorg/test/blah;", 0, "Ljava/lang/Object;", null, null, ImmutableSet.of(annotation), null, null);
MemoryDataStore dataStore = new MemoryDataStore();
try {
DexPool.writeTo(dataStore, new ImmutableDexFile(Opcodes.getDefault(), ImmutableSet.of(classDef)));
} catch (IOException ex) {
throw new RuntimeException(ex);
}
DexBackedDexFile dexFile = new DexBackedDexFile(Opcodes.getDefault(), dataStore.getData());
ClassDef dbClassDef = Iterables.getFirst(dexFile.getClasses(), null);
Assert.assertNotNull(dbClassDef);
Annotation dbAnnotation = Iterables.getFirst(dbClassDef.getAnnotations(), null);
Assert.assertNotNull(dbAnnotation);
AnnotationElement element = Iterables.getFirst(dbAnnotation.getElements(), null);
AnnotationEncodedValue dbAnnotationEncodedValue = (AnnotationEncodedValue) element.getValue();
List<AnnotationElement> dbElements = Lists.newArrayList(dbAnnotationEncodedValue.getElements());
// Ensure that the elements were written out in sorted order
Assert.assertEquals(2, dbElements.size());
Assert.assertEquals("blah", dbElements.get(0).getName());
Assert.assertEquals("zabaglione", dbElements.get(1).getName());
}
Aggregations