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

The following examples show how to use com.android.dx.rop.cst.CstType. 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 File: TypeIdsSection.java    From buck with 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 #2
Source File: ByteCatchList.java    From J2ME-Loader with 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 File: MemberListParser.java    From Box with 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 #4
Source File: AttributeTranslator.java    From Box with 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 #5
Source File: Form35c.java    From Box with 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 #6
Source File: DexFile.java    From Box with 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 #7
Source File: ClassDefsSection.java    From buck with 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 #8
Source File: ClassReferenceListBuilder.java    From RocooFix with 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 #9
Source File: TypeIdsSection.java    From Box with 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 #10
Source File: TypeIdsSection.java    From J2ME-Loader with 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 File: StdMember.java    From Box with 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 #12
Source File: ClassReferenceListBuilder.java    From RocooFix with 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 #13
Source File: MemberListParser.java    From J2ME-Loader with 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 #14
Source File: ClassReferenceListBuilder.java    From Box with 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 #15
Source File: DexFile.java    From buck with 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 #16
Source File: DexFile.java    From J2ME-Loader with 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 #17
Source File: ClassDataItem.java    From J2ME-Loader with 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 #18
Source File: TypeIdsSection.java    From buck with 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 File: ClassReferenceListBuilder.java    From Box with 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 File: Form35c.java    From J2ME-Loader with 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 #21
Source File: TypeIdItem.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public void writeTo(DexFile file, AnnotatedOutput out) {
    CstType type = getDefiningClass();
    CstString descriptor = type.getDescriptor();
    int idx = file.getStringIds().indexOf(descriptor);

    if (out.annotates()) {
        out.annotate(0, indexString() + ' ' + descriptor.toHuman());
        out.annotate(4, "  descriptor_idx: " + Hex.u4(idx));
    }

    out.writeInt(idx);
}
 
Example #22
Source File: BootstrapMethodsList.java    From Box with Apache License 2.0 5 votes vote down vote up
public Item(CstType declaringClass, CstMethodHandle bootstrapMethodHandle,
            BootstrapMethodArgumentsList bootstrapMethodArguments) {
    if (declaringClass == null) {
        throw new NullPointerException("declaringClass == null");
    }
    if (bootstrapMethodHandle == null) {
        throw new NullPointerException("bootstrapMethodHandle == null");
    }
    if (bootstrapMethodArguments == null) {
        throw new NullPointerException("bootstrapMethodArguments == null");
    }
    this.bootstrapMethodHandle = bootstrapMethodHandle;
    this.bootstrapMethodArgumentsList = bootstrapMethodArguments;
    this.declaringClass = declaringClass;
}
 
Example #23
Source File: CatchHandlerList.java    From Box with 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 #24
Source File: CodeObserver.java    From buck with 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 #25
Source File: AttEnclosingMethod.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs an instance.
 *
 * @param type {@code non-null;} the innermost enclosing class
 * @param method {@code null-ok;} the name-and-type of the innermost enclosing
 * method, if any
 */
public AttEnclosingMethod(CstType type, CstNat method) {
    super(ATTRIBUTE_NAME);

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

    this.type = type;
    this.method = method;
}
 
Example #26
Source File: AttributeTranslator.java    From Box with 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 File: AnnotationUtils.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Converts a {@link TypeList} to a {@link CstArray}.
 *
 * @param types {@code non-null;} the type list
 * @return {@code non-null;} the corresponding array constant
 */
private static CstArray makeCstArray(TypeList types) {
    int size = types.size();
    CstArray.List list = new CstArray.List(size);

    for (int i = 0; i < size; i++) {
        list.set(i, CstType.intern(types.getType(i)));
    }

    list.setImmutable();
    return new CstArray(list);
}
 
Example #28
Source File: Annotation.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Construct an instance. It initially contains no elements.
 *
 * @param type {@code non-null;} type of the annotation
 * @param visibility {@code non-null;} the visibility of the annotation
 */
public Annotation(CstType type, AnnotationVisibility visibility) {
    if (type == null) {
        throw new NullPointerException("type == null");
    }

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

    this.type = type;
    this.visibility = visibility;
    this.elements = new TreeMap<CstString, NameValuePair>();
}
 
Example #29
Source File: IdItem.java    From buck with 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 #30
Source File: AnnotationParser.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Parses a single annotation.
 *
 * @param visibility {@code non-null;} visibility of the parsed annotation
 * @return {@code non-null;} the parsed annotation
 */
private Annotation parseAnnotation(AnnotationVisibility visibility)
        throws IOException {
    requireLength(4);

    int typeIndex = input.readUnsignedShort();
    int numElements = input.readUnsignedShort();
    CstString typeString = (CstString) pool.get(typeIndex);
    CstType type = new CstType(Type.intern(typeString.getString()));

    if (observer != null) {
        parsed(2, "type: " + type.toHuman());
        parsed(2, "num_elements: " + numElements);
    }

    Annotation annotation = new Annotation(type, visibility);

    for (int i = 0; i < numElements; i++) {
        if (observer != null) {
            parsed(0, "elements[" + i + "]:");
            changeIndent(1);
        }

        NameValuePair element = parseElement();
        annotation.add(element);

        if (observer != null) {
            changeIndent(-1);
        }
    }

    annotation.setImmutable();
    return annotation;
}