Search in sources :

Example 1 with EncodedValue

use of jadx.api.plugins.input.data.annotations.EncodedValue in project jadx by skylot.

the class ConstPoolReader method resolveMethodCallSite.

private CallSite resolveMethodCallSite(int bootstrapMthIdx, int nameIdx, int descIdx) {
    JavaBootstrapMethodsAttr bootstrapMethodsAttr = clsData.loadAttribute(data, JavaAttrType.BOOTSTRAP_METHODS);
    if (bootstrapMethodsAttr == null) {
        throw new JavaClassParseException("Unexpected missing BootstrapMethods attribute");
    }
    RawBootstrapMethod rawBootstrapMethod = bootstrapMethodsAttr.getList().get(bootstrapMthIdx);
    List<EncodedValue> values = new ArrayList<>(6);
    values.add(new EncodedValue(EncodedType.ENCODED_METHOD_HANDLE, getMethodHandle(rawBootstrapMethod.getMethodHandleIdx())));
    values.add(new EncodedValue(EncodedType.ENCODED_STRING, getUtf8(nameIdx)));
    values.add(new EncodedValue(EncodedType.ENCODED_METHOD_TYPE, DescriptorParser.parseToMethodProto(getUtf8(descIdx))));
    for (int argConstIdx : rawBootstrapMethod.getArgs()) {
        values.add(readAsEncodedValue(argConstIdx));
    }
    return new CallSite(values);
}
Also used : EncodedValue(jadx.api.plugins.input.data.annotations.EncodedValue) RawBootstrapMethod(jadx.plugins.input.java.data.attributes.types.data.RawBootstrapMethod) JavaClassParseException(jadx.plugins.input.java.utils.JavaClassParseException) ArrayList(java.util.ArrayList) ICallSite(jadx.api.plugins.input.data.ICallSite) CallSite(jadx.api.plugins.input.data.impl.CallSite) JavaBootstrapMethodsAttr(jadx.plugins.input.java.data.attributes.types.JavaBootstrapMethodsAttr)

Example 2 with EncodedValue

use of jadx.api.plugins.input.data.annotations.EncodedValue in project jadx by skylot.

the class JavaAnnotationsAttr method readAnnotation.

public static JadxAnnotation readAnnotation(AnnotationVisibility visibility, JavaClassData clsData, DataReader reader) {
    ConstPoolReader constPool = clsData.getConstPoolReader();
    String type = constPool.getUtf8(reader.readU2());
    int pairsCount = reader.readU2();
    Map<String, EncodedValue> pairs = new LinkedHashMap<>(pairsCount);
    for (int j = 0; j < pairsCount; j++) {
        String name = constPool.getUtf8(reader.readU2());
        EncodedValue value = EncodedValueReader.read(clsData, reader);
        pairs.put(name, value);
    }
    return new JadxAnnotation(visibility, type, pairs);
}
Also used : EncodedValue(jadx.api.plugins.input.data.annotations.EncodedValue) JadxAnnotation(jadx.api.plugins.input.data.annotations.JadxAnnotation) ConstPoolReader(jadx.plugins.input.java.data.ConstPoolReader) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with EncodedValue

use of jadx.api.plugins.input.data.annotations.EncodedValue in project jadx by skylot.

the class AndroidResourcesUtils method addResourceFields.

private static void addResourceFields(ClassNode resCls, ResourceStorage resStorage, boolean rClsExists) {
    Map<Integer, FieldNode> resFieldsMap = fillResFieldsMap(resCls);
    Map<String, ResClsInfo> innerClsMap = new TreeMap<>();
    if (rClsExists) {
        for (ClassNode innerClass : resCls.getInnerClasses()) {
            ResClsInfo innerResCls = new ResClsInfo(innerClass);
            innerClass.getFields().forEach(field -> innerResCls.getFieldsMap().put(field.getName(), field));
            innerClsMap.put(innerClass.getShortName(), innerResCls);
        }
    }
    for (ResourceEntry resource : resStorage.getResources()) {
        String resTypeName = resource.getTypeName();
        String resName = resTypeName.equals("style") ? resource.getKeyName().replace('.', '_') : resource.getKeyName();
        ResClsInfo typeClsInfo = innerClsMap.computeIfAbsent(resTypeName, name -> getClassForResType(resCls, rClsExists, name));
        typeClsInfo.getFieldsMap().computeIfAbsent(resName, name -> {
            ClassNode typeCls = typeClsInfo.getTypeCls();
            FieldInfo rFieldInfo = FieldInfo.from(typeCls.root(), typeCls.getClassInfo(), resName, ArgType.INT);
            FieldNode newResField = new FieldNode(typeCls, rFieldInfo, AccessFlags.PUBLIC | AccessFlags.STATIC | AccessFlags.FINAL);
            newResField.addAttr(new EncodedValue(EncodedType.ENCODED_INT, resource.getId()));
            typeCls.getFields().add(newResField);
            if (rClsExists) {
                newResField.addInfoComment("Added by JADX");
            }
            return newResField;
        });
        FieldNode fieldNode = resFieldsMap.get(resource.getId());
        if (fieldNode != null && !fieldNode.getName().equals(resName) && NameMapper.isValidAndPrintable(resName) && resCls.root().getArgs().isRenameValid()) {
            fieldNode.add(AFlag.DONT_RENAME);
            fieldNode.getFieldInfo().setAlias(resName);
        }
    }
}
Also used : ClassNode(jadx.core.dex.nodes.ClassNode) EncodedValue(jadx.api.plugins.input.data.annotations.EncodedValue) FieldNode(jadx.core.dex.nodes.FieldNode) ResourceEntry(jadx.core.xmlgen.entry.ResourceEntry) TreeMap(java.util.TreeMap) FieldInfo(jadx.core.dex.info.FieldInfo)

Example 4 with EncodedValue

use of jadx.api.plugins.input.data.annotations.EncodedValue in project jadx by skylot.

the class InsnUtils method getConstValueByInsn.

/**
 * Return constant value from insn or null if not constant.
 *
 * @return LiteralArg, String, ArgType or null
 */
@Nullable
public static Object getConstValueByInsn(RootNode root, InsnNode insn) {
    switch(insn.getType()) {
        case CONST:
            return insn.getArg(0);
        case CONST_STR:
            return ((ConstStringNode) insn).getString();
        case CONST_CLASS:
            return ((ConstClassNode) insn).getClsType();
        case SGET:
            FieldInfo f = (FieldInfo) ((IndexInsnNode) insn).getIndex();
            FieldNode fieldNode = root.resolveField(f);
            if (fieldNode == null) {
                LOG.warn("Field {} not found", f);
                return null;
            }
            EncodedValue constVal = fieldNode.get(JadxAttrType.CONSTANT_VALUE);
            if (constVal != null) {
                return EncodedValueUtils.convertToConstValue(constVal);
            }
            return null;
        default:
            return null;
    }
}
Also used : EncodedValue(jadx.api.plugins.input.data.annotations.EncodedValue) FieldNode(jadx.core.dex.nodes.FieldNode) ConstStringNode(jadx.core.dex.instructions.ConstStringNode) ConstClassNode(jadx.core.dex.instructions.ConstClassNode) FieldInfo(jadx.core.dex.info.FieldInfo) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with EncodedValue

use of jadx.api.plugins.input.data.annotations.EncodedValue in project jadx by skylot.

the class MethodGen method addDefinition.

public boolean addDefinition(ICodeWriter code) {
    if (mth.getMethodInfo().isClassInit()) {
        code.attachDefinition(mth);
        code.startLine("static");
        return true;
    }
    if (mth.contains(AFlag.ANONYMOUS_CONSTRUCTOR)) {
        // don't add method name and arguments
        code.startLine();
        code.attachDefinition(mth);
        return false;
    }
    if (Consts.DEBUG_USAGE) {
        ClassGen.addMthUsageInfo(code, mth);
    }
    addOverrideAnnotation(code, mth);
    annotationGen.addForMethod(code, mth);
    AccessInfo clsAccFlags = mth.getParentClass().getAccessFlags();
    AccessInfo ai = mth.getAccessFlags();
    // don't add 'abstract' and 'public' to methods in interface
    if (clsAccFlags.isInterface()) {
        ai = ai.remove(AccessFlags.ABSTRACT);
        ai = ai.remove(AccessFlags.PUBLIC);
    }
    // don't add 'public' for annotations
    if (clsAccFlags.isAnnotation()) {
        ai = ai.remove(AccessFlags.PUBLIC);
    }
    if (mth.getMethodInfo().isConstructor() && mth.getParentClass().isEnum()) {
        ai = ai.remove(AccessInfo.VISIBILITY_FLAGS);
    }
    if (mth.getMethodInfo().hasAlias() && !ai.isConstructor()) {
        CodeGenUtils.addRenamedComment(code, mth, mth.getName());
    }
    if (mth.contains(AFlag.INCONSISTENT_CODE) && mth.checkCommentsLevel(CommentsLevel.ERROR)) {
        code.startLine("/*");
        code.incIndent();
        code.startLine("Code decompiled incorrectly, please refer to instructions dump.");
        if (!mth.root().getArgs().isShowInconsistentCode()) {
            if (code.isMetadataSupported()) {
                code.startLine("To view partially-correct code enable 'Show inconsistent code' option in preferences");
            } else {
                code.startLine("To view partially-correct add '--show-bad-code' argument");
            }
        }
        code.decIndent();
        code.startLine("*/");
    }
    code.startLineWithNum(mth.getSourceLine());
    code.add(ai.makeString(mth.checkCommentsLevel(CommentsLevel.INFO)));
    if (clsAccFlags.isInterface() && !mth.isNoCode() && !mth.getAccessFlags().isStatic()) {
        // add 'default' for method with code in interface
        code.add("default ");
    }
    if (classGen.addGenericTypeParameters(code, mth.getTypeParameters(), false)) {
        code.add(' ');
    }
    if (ai.isConstructor()) {
        code.attachDefinition(mth);
        // constructor
        code.add(classGen.getClassNode().getShortName());
    } else {
        classGen.useType(code, mth.getReturnType());
        code.add(' ');
        code.attachDefinition(mth);
        code.add(mth.getAlias());
    }
    code.add('(');
    List<RegisterArg> args = mth.getArgRegs();
    if (mth.getMethodInfo().isConstructor() && mth.getParentClass().contains(AType.ENUM_CLASS)) {
        if (args.size() == 2) {
            args = Collections.emptyList();
        } else if (args.size() > 2) {
            args = args.subList(2, args.size());
        } else {
            mth.addWarnComment("Incorrect number of args for enum constructor: " + args.size() + " (expected >= 2)");
        }
    } else if (mth.contains(AFlag.SKIP_FIRST_ARG)) {
        args = args.subList(1, args.size());
    }
    addMethodArguments(code, args);
    code.add(')');
    annotationGen.addThrows(mth, code);
    // add default value for annotation class
    if (mth.getParentClass().getAccessFlags().isAnnotation()) {
        EncodedValue def = annotationGen.getAnnotationDefaultValue(mth);
        if (def != null) {
            code.add(" default ");
            annotationGen.encodeValue(mth.root(), code, def);
        }
    }
    return true;
}
Also used : EncodedValue(jadx.api.plugins.input.data.annotations.EncodedValue) RegisterArg(jadx.core.dex.instructions.args.RegisterArg) AccessInfo(jadx.core.dex.info.AccessInfo)

Aggregations

EncodedValue (jadx.api.plugins.input.data.annotations.EncodedValue)17 FieldInfo (jadx.core.dex.info.FieldInfo)4 FieldNode (jadx.core.dex.nodes.FieldNode)4 ArrayList (java.util.ArrayList)4 IAnnotation (jadx.api.plugins.input.data.annotations.IAnnotation)3 ClassNode (jadx.core.dex.nodes.ClassNode)3 List (java.util.List)3 Nullable (org.jetbrains.annotations.Nullable)3 ICallSite (jadx.api.plugins.input.data.ICallSite)2 IFieldRef (jadx.api.plugins.input.data.IFieldRef)2 AnnotationVisibility (jadx.api.plugins.input.data.annotations.AnnotationVisibility)2 JadxAnnotation (jadx.api.plugins.input.data.annotations.JadxAnnotation)2 AnnotationsAttr (jadx.api.plugins.input.data.attributes.types.AnnotationsAttr)2 JadxRuntimeException (jadx.core.utils.exceptions.JadxRuntimeException)2 ConstPoolReader (jadx.plugins.input.java.data.ConstPoolReader)2 JavaClassParseException (jadx.plugins.input.java.utils.JavaClassParseException)2 LinkedHashMap (java.util.LinkedHashMap)2 EncodedType (jadx.api.plugins.input.data.annotations.EncodedType)1 IJadxAttribute (jadx.api.plugins.input.data.attributes.IJadxAttribute)1 AnnotationDefaultClassAttr (jadx.api.plugins.input.data.attributes.types.AnnotationDefaultClassAttr)1