Java Code Examples for com.android.dx.rop.cst.CstType

The following examples show how to use com.android.dx.rop.cst.CstType. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: Box   Source File: MemberListParser.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Constructs an instance.
 *
 * @param cf {@code non-null;} the class file to parse from
 * @param definer {@code non-null;} class being defined
 * @param offset offset in {@code bytes} to the start of the list
 * @param attributeFactory {@code non-null;} attribute factory to use
 */
public MemberListParser(DirectClassFile cf, CstType definer,
        int offset, AttributeFactory attributeFactory) {
    if (cf == null) {
        throw new NullPointerException("cf == null");
    }

    if (offset < 0) {
        throw new IllegalArgumentException("offset < 0");
    }

    if (attributeFactory == null) {
        throw new NullPointerException("attributeFactory == null");
    }

    this.cf = cf;
    this.definer = definer;
    this.offset = offset;
    this.attributeFactory = attributeFactory;
    this.endOffset = -1;
}
 
Example 2
Source Project: J2ME-Loader   Source File: ByteCatchList.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Constructs an instance.
 *
 * @param startPc {@code >= 0;} the start pc (inclusive) of the
 * handler's range
 * @param endPc {@code >= startPc;} the end pc (exclusive) of the
 * handler's range
 * @param handlerPc {@code >= 0;} the pc of the exception handler
 * @param exceptionClass {@code null-ok;} the exception class or
 * {@code null} to catch all exceptions with this handler
 */
public Item(int startPc, int endPc, int handlerPc,
        CstType exceptionClass) {
    if (startPc < 0) {
        throw new IllegalArgumentException("startPc < 0");
    }

    if (endPc < startPc) {
        throw new IllegalArgumentException("endPc < startPc");
    }

    if (handlerPc < 0) {
        throw new IllegalArgumentException("handlerPc < 0");
    }

    this.startPc = startPc;
    this.endPc = endPc;
    this.handlerPc = handlerPc;
    this.exceptionClass = exceptionClass;
}
 
Example 3
Source Project: Box   Source File: AttributeTranslator.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Gets the {@code EnclosingMethod} attribute out of a given
 * {@link AttributeList}, if any, translating it to an annotation.
 * If the class really has an enclosing method, this returns an
 * {@code EnclosingMethod} annotation; if not, this returns
 * an {@code EnclosingClass} annotation.
 *
 * @param attribs {@code non-null;} the attributes list to search in
 * @return {@code null-ok;} the converted {@code EnclosingMethod} or
 * {@code EnclosingClass} annotation, if there was an
 * attribute to translate
 */
private static Annotation translateEnclosingMethod(AttributeList attribs) {
    AttEnclosingMethod enclosingMethod = (AttEnclosingMethod)
        attribs.findFirst(AttEnclosingMethod.ATTRIBUTE_NAME);

    if (enclosingMethod == null) {
        return null;
    }

    CstType enclosingClass = enclosingMethod.getEnclosingClass();
    CstNat nat = enclosingMethod.getMethod();

    if (nat == null) {
        /*
         * Dalvik doesn't use EnclosingMethod annotations unless
         * there really is an enclosing method. Anonymous classes
         * are unambiguously identified by having an InnerClass
         * annotation with an empty name along with an appropriate
         * EnclosingClass.
         */
        return AnnotationUtils.makeEnclosingClass(enclosingClass);
    }

    return AnnotationUtils.makeEnclosingMethod(
            new CstMethodRef(enclosingClass, nat));
}
 
Example 4
Source Project: Box   Source File: Form35c.java    License: Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public boolean isCompatible(DalvInsn insn) {
    if (!(insn instanceof CstInsn)) {
        return false;
    }

    CstInsn ci = (CstInsn) insn;
    int cpi = ci.getIndex();

    if (! unsignedFitsInShort(cpi)) {
        return false;
    }

    Constant cst = ci.getConstant();
    if (!((cst instanceof CstMethodRef) ||
          (cst instanceof CstType) ||
          (cst instanceof CstCallSiteRef))) {
        return false;
    }

    RegisterSpecList regs = ci.getRegisters();
    return (wordCount(regs) >= 0);
}
 
Example 5
Source Project: Box   Source File: DexFile.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Interns the given constant in the appropriate section of this
 * instance, or do nothing if the given constant isn't the sort
 * that should be interned.
 *
 * @param cst {@code non-null;} constant to possibly intern
 */
/*package*/ void internIfAppropriate(Constant cst) {
    if (cst == null) {
        throw new NullPointerException("cst == null");
    }

    if (cst instanceof CstString) {
        stringIds.intern((CstString) cst);
    } else if (cst instanceof CstType) {
        typeIds.intern((CstType) cst);
    } else if (cst instanceof CstBaseMethodRef) {
        methodIds.intern((CstBaseMethodRef) cst);
    } else if (cst instanceof CstFieldRef) {
        fieldIds.intern((CstFieldRef) cst);
    } else if (cst instanceof CstEnumRef) {
        fieldIds.intern(((CstEnumRef) cst).getFieldRef());
    } else if (cst instanceof CstProtoRef) {
        protoIds.intern(((CstProtoRef) cst).getPrototype());
    } else if (cst instanceof CstMethodHandle) {
        methodHandles.intern((CstMethodHandle) cst);
    }
}
 
Example 6
Source Project: buck   Source File: ClassDefsSection.java    License: Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public IndexedItem get(Constant cst) {
    if (cst == null) {
        throw new NullPointerException("cst == null");
    }

    throwIfNotPrepared();

    Type type = ((CstType) cst).getClassType();
    IndexedItem result = classDefs.get(type);

    if (result == null) {
        throw new IllegalArgumentException("not found");
    }

    return result;
}
 
Example 7
Source Project: RocooFix   Source File: ClassReferenceListBuilder.java    License: MIT License 6 votes vote down vote up
private void addClassWithHierachy(String classBinaryName) {
    if (classNames.contains(classBinaryName)) {
        return;
    }
    try {
        DirectClassFile classFile = path.getClass(classBinaryName + CLASS_EXTENSION);
        classNames.add(classBinaryName);
        CstType superClass = classFile.getSuperclass();
        if (superClass != null) {
            addClassWithHierachy(superClass.getClassType().getClassName());
        }
        TypeList interfaceList = classFile.getInterfaces();
        int interfaceNumber = interfaceList.size();
        for (int i = 0; i < interfaceNumber; i++) {
            addClassWithHierachy(interfaceList.getType(i).getClassName());
        }
    } catch (FileNotFoundException e) {
        // Ignore: The referenced type is not in the path it must be part of the libraries.
    }
}
 
Example 8
Source Project: Box   Source File: TypeIdsSection.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Interns an element into this instance.
 *
 * @param type {@code non-null;} the type to intern
 * @return {@code non-null;} the interned reference
 */
public synchronized TypeIdItem intern(CstType type) {
    if (type == null) {
        throw new NullPointerException("type == null");
    }

    throwIfPrepared();

    Type typePerSe = type.getClassType();
    TypeIdItem result = typeIds.get(typePerSe);

    if (result == null) {
        result = new TypeIdItem(type);
        typeIds.put(typePerSe, result);
    }

    return result;
}
 
Example 9
Source Project: J2ME-Loader   Source File: Form35c.java    License: Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public boolean isCompatible(DalvInsn insn) {
    if (!(insn instanceof CstInsn)) {
        return false;
    }

    CstInsn ci = (CstInsn) insn;
    int cpi = ci.getIndex();

    if (! unsignedFitsInShort(cpi)) {
        return false;
    }

    Constant cst = ci.getConstant();
    if (!((cst instanceof CstMethodRef) ||
          (cst instanceof CstType))) {
        return false;
    }

    RegisterSpecList regs = ci.getRegisters();
    return (wordCount(regs) >= 0);
}
 
Example 10
Source Project: J2ME-Loader   Source File: TypeIdsSection.java    License: Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public IndexedItem get(Constant cst) {
    if (cst == null) {
        throw new NullPointerException("cst == null");
    }

    throwIfNotPrepared();

    Type type = ((CstType) cst).getClassType();
    IndexedItem result = typeIds.get(type);

    if (result == null) {
        throw new IllegalArgumentException("not found: " + cst);
    }

    return result;
}
 
Example 11
Source Project: J2ME-Loader   Source File: ClassDataItem.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Constructs an instance. Its sets of members are initially
 * empty.
 *
 * @param thisClass {@code non-null;} what class this data is for, just
 * for listing generation
 */
public ClassDataItem(CstType thisClass) {
    super(1, -1);

    if (thisClass == null) {
        throw new NullPointerException("thisClass == null");
    }

    this.thisClass = thisClass;
    this.staticFields = new ArrayList<EncodedField>(20);
    this.staticValues = new HashMap<EncodedField, Constant>(40);
    this.instanceFields = new ArrayList<EncodedField>(20);
    this.directMethods = new ArrayList<EncodedMethod>(20);
    this.virtualMethods = new ArrayList<EncodedMethod>(20);
    this.staticValuesConstant = null;
}
 
Example 12
Source Project: Box   Source File: StdMember.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Constructs an instance.
 *
 * @param definingClass {@code non-null;} the defining class
 * @param accessFlags access flags
 * @param nat {@code non-null;} member name and type (descriptor)
 * @param attributes {@code non-null;} list of associated attributes
 */
public StdMember(CstType definingClass, int accessFlags, CstNat nat,
                 AttributeList attributes) {
    if (definingClass == null) {
        throw new NullPointerException("definingClass == null");
    }

    if (nat == null) {
        throw new NullPointerException("nat == null");
    }

    if (attributes == null) {
        throw new NullPointerException("attributes == null");
    }

    this.definingClass = definingClass;
    this.accessFlags = accessFlags;
    this.nat = nat;
    this.attributes = attributes;
}
 
Example 13
Source Project: RocooFix   Source File: ClassReferenceListBuilder.java    License: MIT License 6 votes vote down vote up
private void addDependencies(ConstantPool pool) {

        for (Constant constant : pool.getEntries()) {
            if (constant instanceof CstType) {
                checkDescriptor(((CstType) constant).getClassType());
            } else if (constant instanceof CstFieldRef) {
                checkDescriptor(((CstFieldRef) constant).getType());
            } else if (constant instanceof CstMethodRef) {
                Prototype proto = ((CstMethodRef) constant).getPrototype();
                checkDescriptor(proto.getReturnType());
                StdTypeList args = proto.getParameterTypes();
                for (int i = 0; i < args.size(); i++) {
                    checkDescriptor(args.get(i));
                }
            }
        }
    }
 
Example 14
Source Project: J2ME-Loader   Source File: MemberListParser.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Constructs an instance.
 *
 * @param cf {@code non-null;} the class file to parse from
 * @param definer {@code non-null;} class being defined
 * @param offset offset in {@code bytes} to the start of the list
 * @param attributeFactory {@code non-null;} attribute factory to use
 */
public MemberListParser(DirectClassFile cf, CstType definer,
        int offset, AttributeFactory attributeFactory) {
    if (cf == null) {
        throw new NullPointerException("cf == null");
    }

    if (offset < 0) {
        throw new IllegalArgumentException("offset < 0");
    }

    if (attributeFactory == null) {
        throw new NullPointerException("attributeFactory == null");
    }

    this.cf = cf;
    this.definer = definer;
    this.offset = offset;
    this.attributeFactory = attributeFactory;
    this.endOffset = -1;
}
 
Example 15
Source Project: Box   Source File: ClassReferenceListBuilder.java    License: Apache License 2.0 6 votes vote down vote up
private void addClassWithHierachy(String classBinaryName) {
    if (classNames.contains(classBinaryName)) {
        return;
    }

    try {
        DirectClassFile classFile = path.getClass(classBinaryName + CLASS_EXTENSION);
        classNames.add(classBinaryName);
        CstType superClass = classFile.getSuperclass();
        if (superClass != null) {
            addClassWithHierachy(superClass.getClassType().getClassName());
        }

        TypeList interfaceList = classFile.getInterfaces();
        int interfaceNumber = interfaceList.size();
        for (int i = 0; i < interfaceNumber; i++) {
            addClassWithHierachy(interfaceList.getType(i).getClassName());
        }
    } catch (FileNotFoundException e) {
        // Ignore: The referenced type is not in the path it must be part of the libraries.
    }
}
 
Example 16
Source Project: buck   Source File: DexFile.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Gets the {@link IndexedItem} corresponding to the given constant,
 * if it is a constant that has such a correspondence, or return
 * {@code null} if it isn't such a constant. This will throw
 * an exception if the given constant <i>should</i> have been found
 * but wasn't.
 *
 * @param cst {@code non-null;} the constant to look up
 * @return {@code null-ok;} its corresponding item, if it has a corresponding
 * item, or {@code null} if it's not that sort of constant
 */
/*package*/ IndexedItem findItemOrNull(Constant cst) {
    IndexedItem item;

    if (cst instanceof CstString) {
        return stringIds.get(cst);
    } else if (cst instanceof CstType) {
        return typeIds.get(cst);
    } else if (cst instanceof CstBaseMethodRef) {
        return methodIds.get(cst);
    } else if (cst instanceof CstFieldRef) {
        return fieldIds.get(cst);
    } else {
        return null;
    }
}
 
Example 17
Source Project: J2ME-Loader   Source File: DexFile.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Interns the given constant in the appropriate section of this
 * instance, or do nothing if the given constant isn't the sort
 * that should be interned.
 *
 * @param cst {@code non-null;} constant to possibly intern
 */
/*package*/ void internIfAppropriate(Constant cst) {
    if (cst instanceof CstString) {
        stringIds.intern((CstString) cst);
    } else if (cst instanceof CstType) {
        typeIds.intern((CstType) cst);
    } else if (cst instanceof CstBaseMethodRef) {
        methodIds.intern((CstBaseMethodRef) cst);
    } else if (cst instanceof CstFieldRef) {
        fieldIds.intern((CstFieldRef) cst);
    } else if (cst instanceof CstEnumRef) {
        fieldIds.intern(((CstEnumRef) cst).getFieldRef());
    } else if (cst == null) {
        throw new NullPointerException("cst == null");
    }
}
 
Example 18
Source Project: buck   Source File: TypeIdsSection.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Interns an element into this instance.
 *
 * @param type {@code non-null;} the type to intern
 * @return {@code non-null;} the interned reference
 */
public synchronized TypeIdItem intern(Type type) {
    if (type == null) {
        throw new NullPointerException("type == null");
    }

    throwIfPrepared();

    TypeIdItem result = typeIds.get(type);

    if (result == null) {
        result = new TypeIdItem(new CstType(type));
        typeIds.put(type, result);
    }

    return result;
}
 
Example 19
Source Project: Box   Source File: ClassReferenceListBuilder.java    License: Apache License 2.0 6 votes vote down vote up
private void addClassWithHierachy(String classBinaryName) {
    if (classNames.contains(classBinaryName)) {
        return;
    }

    try {
        DirectClassFile classFile = path.getClass(classBinaryName + CLASS_EXTENSION);
        classNames.add(classBinaryName);
        CstType superClass = classFile.getSuperclass();
        if (superClass != null) {
            addClassWithHierachy(superClass.getClassType().getClassName());
        }

        TypeList interfaceList = classFile.getInterfaces();
        int interfaceNumber = interfaceList.size();
        for (int i = 0; i < interfaceNumber; i++) {
            addClassWithHierachy(interfaceList.getType(i).getClassName());
        }
    } catch (FileNotFoundException e) {
        // Ignore: The referenced type is not in the path it must be part of the libraries.
    }
}
 
Example 20
Source Project: buck   Source File: TypeIdsSection.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Interns an element into this instance.
 *
 * @param type {@code non-null;} the type to intern
 * @return {@code non-null;} the interned reference
 */
public synchronized TypeIdItem intern(CstType type) {
    if (type == null) {
        throw new NullPointerException("type == null");
    }

    throwIfPrepared();

    Type typePerSe = type.getClassType();
    TypeIdItem result = typeIds.get(typePerSe);

    if (result == null) {
        result = new TypeIdItem(type);
        typeIds.put(typePerSe, result);
    }

    return result;
}
 
Example 21
Source Project: Box   Source File: CatchHandlerList.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Constructs an instance.
 *
 * @param exceptionType {@code non-null;} type of exception handled
 * @param handler {@code >= 0;} exception handler address
 */
public Entry(CstType exceptionType, int handler) {
    if (handler < 0) {
        throw new IllegalArgumentException("handler < 0");
    }

    if (exceptionType == null) {
        throw new NullPointerException("exceptionType == null");
    }

    this.handler = handler;
    this.exceptionType = exceptionType;
}
 
Example 22
Source Project: buck   Source File: CodeObserver.java    License: Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
public void visitNewarray(int offset, int length, CstType cst,
        ArrayList<Constant> intVals) {
    String commentOrSpace = (length == 1) ? " // " : " ";
    String typeName = cst.getClassType().getComponentType().toHuman();

    observer.parsed(bytes, offset, length,
                    header(offset) + commentOrSpace + typeName);
}
 
Example 23
Source Project: Box   Source File: AttributeTranslator.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Gets the {@code AnnotationDefault} attributes out of a
 * given class, if any, reforming them as an
 * {@code AnnotationDefault} annotation.
 *
 * @param cf {@code non-null;} the class in question
 * @return {@code null-ok;} an appropriately-constructed
 * {@code AnnotationDefault} annotation, if there were any
 * annotation defaults in the class, or {@code null} if not
 */
private static Annotation translateAnnotationDefaults(DirectClassFile cf) {
    CstType thisClass = cf.getThisClass();
    MethodList methods = cf.getMethods();
    int sz = methods.size();
    Annotation result =
        new Annotation(thisClass, AnnotationVisibility.EMBEDDED);
    boolean any = false;

    for (int i = 0; i < sz; i++) {
        Method one = methods.get(i);
        AttributeList attribs = one.getAttributes();
        AttAnnotationDefault oneDefault = (AttAnnotationDefault)
            attribs.findFirst(AttAnnotationDefault.ATTRIBUTE_NAME);

        if (oneDefault != null) {
            NameValuePair pair = new NameValuePair(
                    one.getNat().getName(),
                    oneDefault.getValue());
            result.add(pair);
            any = true;
        }
    }

    if (! any) {
        return null;
    }

    result.setImmutable();
    return AnnotationUtils.makeAnnotationDefault(result);
}
 
Example 24
Source Project: dexmaker   Source File: TypeId.java    License: Apache License 2.0 5 votes vote down vote up
TypeId(String name, com.android.dx.rop.type.Type ropType) {
    if (name == null || ropType == null) {
        throw new NullPointerException();
    }
    this.name = name;
    this.ropType = ropType;
    this.constant = CstType.intern(ropType);
}
 
Example 25
Source Project: Box   Source File: CodeObserver.java    License: Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public void visitNewarray(int offset, int length, CstType cst,
        ArrayList<Constant> intVals) {
    String commentOrSpace = (length == 1) ? " // " : " ";
    String typeName = cst.getClassType().getComponentType().toHuman();

    observer.parsed(bytes, offset, length,
                    header(offset) + commentOrSpace + typeName);
}
 
Example 26
Source Project: buck   Source File: AttributeTranslator.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Gets the {@code AnnotationDefault} attributes out of a
 * given class, if any, reforming them as an
 * {@code AnnotationDefault} annotation.
 *
 * @param cf {@code non-null;} the class in question
 * @return {@code null-ok;} an appropriately-constructed
 * {@code AnnotationDefault} annotation, if there were any
 * annotation defaults in the class, or {@code null} if not
 */
private static Annotation translateAnnotationDefaults(DirectClassFile cf) {
    CstType thisClass = cf.getThisClass();
    MethodList methods = cf.getMethods();
    int sz = methods.size();
    Annotation result =
        new Annotation(thisClass, AnnotationVisibility.EMBEDDED);
    boolean any = false;

    for (int i = 0; i < sz; i++) {
        Method one = methods.get(i);
        AttributeList attribs = one.getAttributes();
        AttAnnotationDefault oneDefault = (AttAnnotationDefault)
            attribs.findFirst(AttAnnotationDefault.ATTRIBUTE_NAME);

        if (oneDefault != null) {
            NameValuePair pair = new NameValuePair(
                    one.getNat().getName(),
                    oneDefault.getValue());
            result.add(pair);
            any = true;
        }
    }

    if (! any) {
        return null;
    }

    result.setImmutable();
    return AnnotationUtils.makeAnnotationDefault(result);
}
 
Example 27
Source Project: buck   Source File: IdItem.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Constructs an instance.
 *
 * @param type {@code non-null;} the type constant for the defining
 * class of the reference
 */
public IdItem(CstType type) {
    if (type == null) {
        throw new NullPointerException("type == null");
    }

    this.type = type;
}
 
Example 28
Source Project: J2ME-Loader   Source File: Frame.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Makes a new frame for an exception handler block invoked from this
 * frame.
 *
 * @param exceptionClass exception that the handler block will handle
 * @return new frame
 */
public Frame makeExceptionHandlerStartFrame(CstType exceptionClass) {
    ExecutionStack newStack = getStack().copy();

    newStack.clear();
    newStack.push(exceptionClass);

    return new Frame(getLocals(), newStack, subroutines);
}
 
Example 29
Source Project: J2ME-Loader   Source File: AnnotationUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a standard {@code EnclosingClass} annotation.
 *
 * @param clazz {@code non-null;} the enclosing class
 * @return {@code non-null;} the annotation
 */
public static Annotation makeEnclosingClass(CstType clazz) {
    Annotation result = new Annotation(ENCLOSING_CLASS_TYPE, SYSTEM);

    result.put(new NameValuePair(VALUE_STRING, clazz));
    result.setImmutable();
    return result;
}
 
Example 30
Source Project: buck   Source File: Main.java    License: Apache License 2.0 5 votes vote down vote up
private void computeReferencedResources() {
    for (Item genericItem : outputDex.getFieldIds().items()) {
        FieldIdItem item = (FieldIdItem) genericItem;
        CstType fieldClass = item.getDefiningClass();
        CstString fieldName = item.getRef().getNat().getName();
        if (fieldClass.getClassType().getDescriptor().contains("/R$")) {
            // Add the packageName of the class for better accuracy.
            resourceNames.add(fieldClass.getPackageName() + "." + fieldName.getString());
        }
    }
}