FileDocCategorySizeDatePackage
Grep.javaAPI DocAndroid 5.1 API4172Thu Mar 12 22:18:30 GMT 2015com.android.dx.command.grep

Grep

public final class Grep extends Object

Fields Summary
private final com.android.dex.Dex
dex
private final com.android.dx.io.CodeReader
codeReader
private final Set
stringIds
private final PrintWriter
out
private int
count
private com.android.dex.ClassDef
currentClass
private ClassData.Method
currentMethod
Constructors Summary
public Grep(com.android.dex.Dex dex, Pattern pattern, PrintWriter out)


             
        this.dex = dex;
        this.out = out;

        stringIds = getStringIds(dex, pattern);

        codeReader.setStringVisitor(new CodeReader.Visitor() {
            public void visit(DecodedInstruction[] all, DecodedInstruction one) {
                encounterString(one.getIndex());
            }
        });
    
Methods Summary
private voidencounterString(int index)

        if (stringIds.contains(index)) {
            out.println(location() + " " + dex.strings().get(index));
            count++;
        }
    
private java.util.SetgetStringIds(com.android.dex.Dex dex, java.util.regex.Pattern pattern)

        Set<Integer> stringIds = new HashSet<Integer>();
        int stringIndex = 0;
        for (String s : dex.strings()) {
            if (pattern.matcher(s).find()) {
                stringIds.add(stringIndex);
            }
            stringIndex++;
        }
        return stringIds;
    
public intgrep()
Prints usages to out. Returns the number of matches found.

        for (ClassDef classDef : dex.classDefs()) {
            currentClass = classDef;
            currentMethod = null;

            if (classDef.getClassDataOffset() == 0) {
                continue;
            }

            ClassData classData = dex.readClassData(classDef);

            // find the strings in encoded constants
            int staticValuesOffset = classDef.getStaticValuesOffset();
            if (staticValuesOffset != 0) {
                readArray(new EncodedValueReader(dex.open(staticValuesOffset)));
            }

            // find the strings in method bodies
            for (ClassData.Method method : classData.allMethods()) {
                currentMethod = method;
                if (method.getCodeOffset() != 0) {
                    codeReader.visitAll(dex.readCode(method).getInstructions());
                }
            }
        }

        currentClass = null;
        currentMethod = null;
        return count;
    
private java.lang.Stringlocation()

        String className = dex.typeNames().get(currentClass.getTypeIndex());
        if (currentMethod != null) {
            MethodId methodId = dex.methodIds().get(currentMethod.getMethodIndex());
            return className + "." + dex.strings().get(methodId.getNameIndex());
        } else {
            return className;
        }
    
private voidreadArray(com.android.dex.EncodedValueReader reader)

        for (int i = 0, size = reader.readArray(); i < size; i++) {
            switch (reader.peek()) {
            case EncodedValueReader.ENCODED_STRING:
                encounterString(reader.readString());
                break;
            case EncodedValueReader.ENCODED_ARRAY:
                readArray(reader);
                break;
            }
        }