Search in sources :

Example 1 with ClsAliasPair

use of jadx.core.deobf.ClsAliasPair 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;
}
Also used : IAnnotation(jadx.api.plugins.input.data.annotations.IAnnotation) EncodedValue(jadx.api.plugins.input.data.annotations.EncodedValue) ClsAliasPair(jadx.core.deobf.ClsAliasPair) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with ClsAliasPair

use of jadx.core.deobf.ClsAliasPair in project jadx by skylot.

the class KotlinMetadataUtils method splitAndCheckClsName.

// Don't use ClassInfo facility to not pollute class into cache
private static ClsAliasPair splitAndCheckClsName(ClassNode originCls, String fullClsName) {
    if (!NameMapper.isValidFullIdentifier(fullClsName)) {
        return null;
    }
    String pkg;
    String name;
    int dot = fullClsName.lastIndexOf('.');
    if (dot == -1) {
        pkg = "";
        name = fullClsName;
    } else {
        pkg = fullClsName.substring(0, dot);
        name = fullClsName.substring(dot + 1);
    }
    ClassInfo originClsInfo = originCls.getClassInfo();
    String originName = originClsInfo.getShortName();
    if (originName.equals(name) || name.contains("$") || !NameMapper.isValidIdentifier(name) || countPkgParts(originClsInfo.getPackage()) != countPkgParts(pkg) || pkg.startsWith("java.")) {
        return null;
    }
    ClassNode newClsNode = originCls.root().resolveClass(fullClsName);
    if (newClsNode != null) {
        // class with alias name already exist
        return null;
    }
    return new ClsAliasPair(pkg, name);
}
Also used : ClassNode(jadx.core.dex.nodes.ClassNode) ClsAliasPair(jadx.core.deobf.ClsAliasPair) ClassInfo(jadx.core.dex.info.ClassInfo)

Aggregations

ClsAliasPair (jadx.core.deobf.ClsAliasPair)2 EncodedValue (jadx.api.plugins.input.data.annotations.EncodedValue)1 IAnnotation (jadx.api.plugins.input.data.annotations.IAnnotation)1 ClassInfo (jadx.core.dex.info.ClassInfo)1 ClassNode (jadx.core.dex.nodes.ClassNode)1 Nullable (org.jetbrains.annotations.Nullable)1