use of jadx.api.plugins.input.data.annotations.IAnnotation in project jadx by skylot.
the class AnnotationGen method add.
private void add(IAttributeNode node, ICodeWriter code) {
AnnotationsAttr aList = node.get(JadxAttrType.ANNOTATION_LIST);
if (aList == null || aList.isEmpty()) {
return;
}
for (IAnnotation a : aList.getAll()) {
String aCls = a.getAnnotationClass();
if (!aCls.equals(Consts.OVERRIDE_ANNOTATION)) {
code.startLine();
formatAnnotation(code, a);
}
}
}
use of jadx.api.plugins.input.data.annotations.IAnnotation in project jadx by skylot.
the class AnnotationGen method addForParameter.
public void addForParameter(ICodeWriter code, AnnotationMethodParamsAttr paramsAnnotations, int n) {
List<AnnotationsAttr> paramList = paramsAnnotations.getParamList();
if (n >= paramList.size()) {
return;
}
AnnotationsAttr aList = paramList.get(n);
if (aList == null || aList.isEmpty()) {
return;
}
for (IAnnotation a : aList.getAll()) {
formatAnnotation(code, a);
code.add(' ');
}
}
use of jadx.api.plugins.input.data.annotations.IAnnotation in project jadx by skylot.
the class DexAnnotationsConvert method convertSystemAnnotations.
@SuppressWarnings("unchecked")
private static void convertSystemAnnotations(@Nullable String cls, List<IJadxAttribute> attributes, IAnnotation annotation) {
switch(annotation.getAnnotationClass()) {
case "Ldalvik/annotation/Signature;":
attributes.add(new SignatureAttr(extractSignature(annotation)));
break;
case "Ldalvik/annotation/InnerClass;":
try {
String name = AnnotationsUtils.getValue(annotation, "name", EncodedType.ENCODED_STRING, null);
int accFlags = AnnotationsUtils.getValue(annotation, "accessFlags", EncodedType.ENCODED_INT, 0);
if (name != null || accFlags != 0) {
InnerClsInfo innerClsInfo = new InnerClsInfo(cls, null, name, accFlags);
attributes.add(new InnerClassesAttr(Collections.singletonMap(cls, innerClsInfo)));
}
} catch (Exception e) {
LOG.warn("Failed to parse annotation: " + annotation, e);
}
break;
case "Ldalvik/annotation/AnnotationDefault;":
EncodedValue annValue = annotation.getDefaultValue();
if (annValue != null && annValue.getType() == EncodedType.ENCODED_ANNOTATION) {
IAnnotation defAnnotation = (IAnnotation) annValue.getValue();
attributes.add(new AnnotationDefaultClassAttr(defAnnotation.getValues()));
}
break;
case "Ldalvik/annotation/Throws;":
try {
EncodedValue defaultValue = annotation.getDefaultValue();
if (defaultValue != null) {
List<String> excs = ((List<EncodedValue>) defaultValue.getValue()).stream().map(ev -> ((String) ev.getValue())).collect(Collectors.toList());
attributes.add(new ExceptionsAttr(excs));
}
} catch (Exception e) {
LOG.warn("Failed to convert dalvik throws annotation", e);
}
break;
case "Ldalvik/annotation/MethodParameters;":
try {
List<EncodedValue> names = AnnotationsUtils.getArray(annotation, "names");
List<EncodedValue> accFlags = AnnotationsUtils.getArray(annotation, "accessFlags");
if (!names.isEmpty() && names.size() == accFlags.size()) {
int size = names.size();
List<MethodParametersAttr.Info> list = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
String name = (String) names.get(i).getValue();
int accFlag = (int) accFlags.get(i).getValue();
list.add(new MethodParametersAttr.Info(accFlag, name));
}
attributes.add(new MethodParametersAttr(list));
}
} catch (Exception e) {
LOG.warn("Failed to parse annotation: " + annotation, e);
}
break;
}
}
use of jadx.api.plugins.input.data.annotations.IAnnotation in project jadx by skylot.
the class KotlinMetadataUtils method getClassAlias.
/**
* Try to get class info from Kotlin Metadata annotation
*/
@Nullable
public static ClsAliasPair getClassAlias(ClassNode cls) {
IAnnotation metadataAnnotation = cls.getAnnotation(KOTLIN_METADATA_ANNOTATION);
List<EncodedValue> d2Param = getParamAsList(metadataAnnotation, KOTLIN_METADATA_D2_PARAMETER);
if (d2Param == null || d2Param.isEmpty()) {
return null;
}
EncodedValue firstValue = d2Param.get(0);
if (firstValue == null || firstValue.getType() != EncodedType.ENCODED_STRING) {
return null;
}
try {
String rawClassName = ((String) firstValue.getValue()).trim();
if (rawClassName.isEmpty()) {
return null;
}
String clsName = Utils.cleanObjectName(rawClassName);
ClsAliasPair alias = splitAndCheckClsName(cls, clsName);
if (alias != null) {
RenameReasonAttr.forNode(cls).append("from Kotlin metadata");
return alias;
}
} catch (Exception e) {
LOG.error("Failed to parse kotlin metadata", e);
}
return null;
}
use of jadx.api.plugins.input.data.annotations.IAnnotation in project jadx by skylot.
the class Smali method writeClass.
private void writeClass(SmaliWriter smali, ClassNode cls) {
IClassData clsData = cls.getClsData();
if (clsData == null) {
smali.startLine(String.format("###### Class %s is created by jadx", cls.getFullName()));
return;
}
AttributeStorage attributes = new AttributeStorage();
attributes.add(clsData.getAttributes());
smali.startLine("Class: " + clsData.getType()).startLine("AccessFlags: " + AccessFlags.format(clsData.getAccessFlags(), AccessFlagsScope.CLASS)).startLine("SuperType: " + clsData.getSuperType()).startLine("Interfaces: " + clsData.getInterfacesTypes()).startLine("SourceFile: " + attributes.get(JadxAttrType.SOURCE_FILE));
AnnotationsAttr annotationsAttr = attributes.get(JadxAttrType.ANNOTATION_LIST);
if (annotationsAttr != null) {
Collection<IAnnotation> annos = annotationsAttr.getList();
if (!annos.isEmpty()) {
smali.startLine(String.format("# %d annotations", annos.size()));
writeAnnotations(smali, new ArrayList<>(annos));
smali.startLine();
}
}
List<RawField> fields = new ArrayList<>();
// first is access flag, second is name
int[] colWidths = new int[] { 0, 0 };
int[] mthIndex = new int[] { 0 };
LineInfo line = new LineInfo();
clsData.visitFieldsAndMethods(f -> {
RawField fld = RawField.make(f);
fields.add(fld);
if (fld.accessFlag.length() > colWidths[0]) {
colWidths[0] = fld.accessFlag.length();
}
if (fld.name.length() > colWidths[1]) {
colWidths[1] = fld.name.length();
}
}, m -> {
if (!fields.isEmpty()) {
writeFields(smali, clsData, fields, colWidths);
fields.clear();
}
writeMethod(smali, cls.getMethods().get(mthIndex[0]++), m, line);
line.reset();
});
if (!fields.isEmpty()) {
// in case there's no methods.
writeFields(smali, clsData, fields, colWidths);
}
for (ClassNode innerClass : cls.getInnerClasses()) {
writeClass(smali, innerClass);
}
}
Aggregations