use of jadx.api.plugins.input.data.annotations.EncodedValue in project jadx by skylot.
the class DexAnnotationsConvert method extractSignature.
@SuppressWarnings({ "unchecked", "ConstantConditions" })
private static String extractSignature(IAnnotation annotation) {
List<EncodedValue> values = (List<EncodedValue>) annotation.getDefaultValue().getValue();
if (values.size() == 1) {
return (String) values.get(0).getValue();
}
StringBuilder sb = new StringBuilder();
for (EncodedValue part : values) {
sb.append((String) part.getValue());
}
return sb.toString();
}
use of jadx.api.plugins.input.data.annotations.EncodedValue 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.EncodedValue in project jadx by skylot.
the class InvokeCustomBuilder method build.
public static InsnNode build(MethodNode mth, InsnData insn, boolean isRange) {
try {
ICallSite callSite = InsnDataUtils.getCallSite(insn);
if (callSite == null) {
throw new JadxRuntimeException("Failed to get call site for insn: " + insn);
}
callSite.load();
List<EncodedValue> values = callSite.getValues();
if (CustomLambdaCall.isLambdaInvoke(values)) {
return CustomLambdaCall.buildLambdaMethodCall(mth, insn, isRange, values);
}
if (CustomStringConcat.isStringConcat(values)) {
return CustomStringConcat.buildStringConcat(insn, isRange, values);
}
// TODO: output raw dynamic call
throw new JadxRuntimeException("Failed to process invoke-custom instruction: " + callSite);
} catch (Exception e) {
throw new JadxRuntimeException("'invoke-custom' instruction processing error: " + e.getMessage(), e);
}
}
use of jadx.api.plugins.input.data.annotations.EncodedValue 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.EncodedValue in project jadx by skylot.
the class TestRFieldRestore method test.
@Test
public void test() {
// unknown R class
disableCompilation();
Map<Integer, String> map = new HashMap<>();
int buttonConstValue = 2131230730;
map.put(buttonConstValue, "id.Button");
setResMap(map);
ClassNode cls = getClassNode(TestCls.class);
String code = cls.getCode().toString();
assertThat(code, containsOne("return R.id.Button;"));
assertThat(code, not(containsString("import R;")));
// check 'R' class
ClassNode rCls = cls.root().searchClassByFullAlias("R");
assertThat(rCls, notNullValue());
// check inner 'id' class
List<ClassNode> innerClasses = rCls.getInnerClasses();
assertThat(innerClasses, hasSize(1));
ClassNode idCls = innerClasses.get(0);
assertThat(idCls.getShortName(), is("id"));
// check 'Button' field
FieldNode buttonField = idCls.searchFieldByName("Button");
assertThat(buttonField, notNullValue());
EncodedValue constVal = buttonField.get(JadxAttrType.CONSTANT_VALUE);
Integer buttonValue = (Integer) constVal.getValue();
assertThat(buttonValue, is(buttonConstValue));
}
Aggregations