com.android.dx.rop.code.AccessFlags Java Examples

The following examples show how to use com.android.dx.rop.code.AccessFlags. 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: FixAccessModifiers.java    From Box with Apache License 2.0 6 votes vote down vote up
private static int fixVisibility(MethodNode mth) {
	if (mth.isVirtual()) {
		// make virtual methods public
		return AccessFlags.ACC_PUBLIC;
	} else {
		AccessInfo accessFlags = mth.getAccessFlags();
		if (accessFlags.isAbstract()) {
			// make abstract methods public
			return AccessFlags.ACC_PUBLIC;
		}
		// enum constructor can't be public
		if (accessFlags.isConstructor()
				&& accessFlags.isPublic()
				&& mth.getParentClass().isEnum()) {
			return 0;
		}
		if (accessFlags.isConstructor() || accessFlags.isStatic()) {
			// TODO: make public if used outside
			return -1;
		}
		// make other direct methods private
		return AccessFlags.ACC_PRIVATE;
	}
}
 
Example #2
Source File: BlockDumper.java    From buck with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void endParsingMember(ByteArray bytes, int offset, String name,
        String descriptor, Member member) {
    if (!(member instanceof Method)) {
        return;
    }

    if (!shouldDumpMethod(name)) {
        return;
    }

    if ((member.getAccessFlags() & (AccessFlags.ACC_ABSTRACT |
            AccessFlags.ACC_NATIVE)) != 0) {
        return;
    }

    ConcreteMethod meth =
        new ConcreteMethod((Method) member, classFile, true, true);

    if (rop) {
        ropDump(meth);
    } else {
        regularDump(meth);
    }
}
 
Example #3
Source File: EncodedMethod.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs an instance.
 *
 * @param method {@code non-null;} constant for the method
 * @param accessFlags access flags
 * @param code {@code null-ok;} code for the method, if it is neither
 * {@code abstract} nor {@code native}
 * @param throwsList {@code non-null;} list of possibly-thrown exceptions,
 * just used in generating debugging output (listings)
 */
public EncodedMethod(CstMethodRef method, int accessFlags,
        DalvCode code, TypeList throwsList) {
    super(accessFlags);

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

    this.method = method;

    if (code == null) {
        this.code = null;
    } else {
        boolean isStatic = (accessFlags & AccessFlags.ACC_STATIC) != 0;
        this.code = new CodeItem(method, code, isStatic, throwsList);
    }
}
 
Example #4
Source File: EncodedMethod.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs an instance.
 *
 * @param method {@code non-null;} constant for the method
 * @param accessFlags access flags
 * @param code {@code null-ok;} code for the method, if it is neither
 * {@code abstract} nor {@code native}
 * @param throwsList {@code non-null;} list of possibly-thrown exceptions,
 * just used in generating debugging output (listings)
 */
public EncodedMethod(CstMethodRef method, int accessFlags,
        DalvCode code, TypeList throwsList) {
    super(accessFlags);

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

    this.method = method;

    if (code == null) {
        this.code = null;
    } else {
        boolean isStatic = (accessFlags & AccessFlags.ACC_STATIC) != 0;
        this.code = new CodeItem(method, code, isStatic, throwsList);
    }
}
 
Example #5
Source File: BlockDumper.java    From Box with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void endParsingMember(ByteArray bytes, int offset, String name,
        String descriptor, Member member) {
    if (!(member instanceof Method)) {
        return;
    }

    if (!shouldDumpMethod(name)) {
        return;
    }

    if ((member.getAccessFlags() & (AccessFlags.ACC_ABSTRACT |
            AccessFlags.ACC_NATIVE)) != 0) {
        return;
    }

    ConcreteMethod meth =
        new ConcreteMethod((Method) member, classFile, true, true);

    if (rop) {
        ropDump(meth);
    } else {
        regularDump(meth);
    }
}
 
Example #6
Source File: EncodedField.java    From buck with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public int encode(DexFile file, AnnotatedOutput out,
        int lastIndex, int dumpSeq) {
    int fieldIdx = file.getFieldIds().indexOf(field);
    int diff = fieldIdx - lastIndex;
    int accessFlags = getAccessFlags();

    if (out.annotates()) {
        out.annotate(0, String.format("  [%x] %s", dumpSeq,
                        field.toHuman()));
        out.annotate(Leb128.unsignedLeb128Size(diff),
                "    field_idx:    " + Hex.u4(fieldIdx));
        out.annotate(Leb128.unsignedLeb128Size(accessFlags),
                "    access_flags: " +
                AccessFlags.fieldString(accessFlags));
    }

    out.writeUleb128(diff);
    out.writeUleb128(accessFlags);

    return fieldIdx;
}
 
Example #7
Source File: FixAccessModifiers.java    From Box with Apache License 2.0 6 votes vote down vote up
private static int fixVisibility(MethodNode mth) {
	if (mth.isVirtual()) {
		// make virtual methods public
		return AccessFlags.ACC_PUBLIC;
	} else {
		AccessInfo accessFlags = mth.getAccessFlags();
		if (accessFlags.isAbstract()) {
			// make abstract methods public
			return AccessFlags.ACC_PUBLIC;
		}
		// enum constructor can't be public
		if (accessFlags.isConstructor()
				&& accessFlags.isPublic()
				&& mth.getParentClass().isEnum()) {
			return 0;
		}
		if (accessFlags.isConstructor() || accessFlags.isStatic()) {
			// TODO: make public if used outside
			return -1;
		}
		// make other direct methods private
		return AccessFlags.ACC_PRIVATE;
	}
}
 
Example #8
Source File: BlockDumper.java    From Box with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void endParsingMember(ByteArray bytes, int offset, String name,
        String descriptor, Member member) {
    if (!(member instanceof Method)) {
        return;
    }

    if (!shouldDumpMethod(name)) {
        return;
    }

    if ((member.getAccessFlags() & (AccessFlags.ACC_ABSTRACT |
            AccessFlags.ACC_NATIVE)) != 0) {
        return;
    }

    ConcreteMethod meth =
        new ConcreteMethod((Method) member, classFile, true, true);

    if (rop) {
        ropDump(meth);
    } else {
        regularDump(meth);
    }
}
 
Example #9
Source File: EncodedMethod.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs an instance.
 *
 * @param method {@code non-null;} constant for the method
 * @param accessFlags access flags
 * @param code {@code null-ok;} code for the method, if it is neither
 * {@code abstract} nor {@code native}
 * @param throwsList {@code non-null;} list of possibly-thrown exceptions,
 * just used in generating debugging output (listings)
 */
public EncodedMethod(CstMethodRef method, int accessFlags,
        DalvCode code, TypeList throwsList) {
    super(accessFlags);

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

    this.method = method;

    if (code == null) {
        this.code = null;
    } else {
        boolean isStatic = (accessFlags & AccessFlags.ACC_STATIC) != 0;
        this.code = new CodeItem(method, code, isStatic, throwsList);
    }
}
 
Example #10
Source File: EncodedField.java    From Box with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public int encode(DexFile file, AnnotatedOutput out,
        int lastIndex, int dumpSeq) {
    int fieldIdx = file.getFieldIds().indexOf(field);
    int diff = fieldIdx - lastIndex;
    int accessFlags = getAccessFlags();

    if (out.annotates()) {
        out.annotate(0, String.format("  [%x] %s", dumpSeq,
                        field.toHuman()));
        out.annotate(Leb128.unsignedLeb128Size(diff),
                "    field_idx:    " + Hex.u4(fieldIdx));
        out.annotate(Leb128.unsignedLeb128Size(accessFlags),
                "    access_flags: " +
                AccessFlags.fieldString(accessFlags));
    }

    out.writeUleb128(diff);
    out.writeUleb128(accessFlags);

    return fieldIdx;
}
 
Example #11
Source File: StdMethod.java    From Box with Apache License 2.0 5 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 StdMethod(CstType definingClass, int accessFlags, CstNat nat,
        AttributeList attributes) {
    super(definingClass, accessFlags, nat, attributes);

    String descStr = getDescriptor().getString();
    effectiveDescriptor =
        Prototype.intern(descStr, definingClass.getClassType(),
                                AccessFlags.isStatic(accessFlags),
                                nat.isInstanceInit());
}
 
Example #12
Source File: StdMethod.java    From buck with Apache License 2.0 5 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 StdMethod(CstType definingClass, int accessFlags, CstNat nat,
        AttributeList attributes) {
    super(definingClass, accessFlags, nat, attributes);

    String descStr = getDescriptor().getString();
    effectiveDescriptor =
        Prototype.intern(descStr, definingClass.getClassType(),
                                AccessFlags.isStatic(accessFlags),
                                nat.isInstanceInit());
}
 
Example #13
Source File: AccessInfo.java    From Box with Apache License 2.0 5 votes vote down vote up
public String rawString() {
	switch (type) {
		case CLASS:
			return AccessFlags.classString(accFlags);
		case FIELD:
			return AccessFlags.fieldString(accFlags);
		case METHOD:
			return AccessFlags.methodString(accFlags);
		default:
			return "?";
	}
}
 
Example #14
Source File: MethodInlineVisitor.java    From Box with Apache License 2.0 5 votes vote down vote up
private static boolean fixVisibilityOfInlineCode(MethodNode mth, InsnNode insn) {
	int newVisFlag = AccessFlags.ACC_PUBLIC; // TODO: calculate more precisely
	InsnType insnType = insn.getType();
	if (insnType == InsnType.INVOKE) {
		InvokeNode invoke = (InvokeNode) insn;
		MethodNode callMthNode = mth.root().deepResolveMethod(invoke.getCallMth());
		if (callMthNode != null) {
			FixAccessModifiers.changeVisibility(callMthNode, newVisFlag);
		}
		return true;
	}
	if (insnType == InsnType.ONE_ARG) {
		InsnArg arg = insn.getArg(0);
		if (!arg.isInsnWrap()) {
			return false;
		}
		return fixVisibilityOfInlineCode(mth, ((InsnWrapArg) arg).getWrapInsn());
	}
	if (insn instanceof IndexInsnNode) {
		Object indexObj = ((IndexInsnNode) insn).getIndex();
		if (indexObj instanceof FieldInfo) {
			FieldNode fieldNode = mth.root().deepResolveField(((FieldInfo) indexObj));
			if (fieldNode != null) {
				FixAccessModifiers.changeVisibility(fieldNode, newVisFlag);
			}
			return true;
		}
	}
	if (Consts.DEBUG) {
		mth.addAttr(AType.COMMENTS, "JADX DEBUG: can't inline method, not implemented redirect type: " + insn);
	}
	return false;
}
 
Example #15
Source File: EnumVisitor.java    From Box with Apache License 2.0 5 votes vote down vote up
@Override
public boolean visit(ClassNode cls) throws JadxException {
	if (!convertToEnum(cls)) {
		AccessInfo accessFlags = cls.getAccessFlags();
		if (accessFlags.isEnum()) {
			cls.setAccessFlags(accessFlags.remove(AccessFlags.ACC_ENUM));
			cls.addAttr(AType.COMMENTS, "'enum' modifier removed");
		}
	}
	return true;
}
 
Example #16
Source File: AndroidResourcesUtils.java    From Box with Apache License 2.0 5 votes vote down vote up
@Nullable
private static ClassNode makeClass(RootNode root, String clsName, ResourceStorage resStorage) {
	List<DexNode> dexNodes = root.getDexNodes();
	if (dexNodes.isEmpty()) {
		return null;
	}
	ClassNode rCls = new ClassNode(dexNodes.get(0), clsName, AccessFlags.ACC_PUBLIC | AccessFlags.ACC_FINAL);
	rCls.addAttr(AType.COMMENTS, "This class is generated by JADX");
	rCls.setState(ProcessState.PROCESS_COMPLETE);
	return rCls;
}
 
Example #17
Source File: AndroidResourcesUtils.java    From Box with Apache License 2.0 5 votes vote down vote up
@NotNull
private static ClassNode addClassForResType(ClassNode resCls, boolean rClsExists, String typeName) {
	ClassNode newTypeCls = new ClassNode(resCls.dex(), resCls.getFullName() + '$' + typeName,
			AccessFlags.ACC_PUBLIC | AccessFlags.ACC_STATIC | AccessFlags.ACC_FINAL);
	resCls.addInnerClass(newTypeCls);
	if (rClsExists) {
		newTypeCls.addAttr(AType.COMMENTS, "added by JADX");
	}
	return newTypeCls;
}
 
Example #18
Source File: AttributeTranslator.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the annotations out of a given class, similar to {@link
 * #getAnnotations}, also including annotations for translations
 * of class-level attributes {@code EnclosingMethod} and
 * {@code InnerClasses}, if present. Additionally, if the
 * class is an annotation class, then this also includes a
 * representation of all the {@code AnnotationDefault}
 * values.
 *
 * @param cf {@code non-null;} the class in question
 * @param args {@code non-null;} the high-level options
 * @return {@code non-null;} the set of annotations, which may be empty
 */
public static Annotations getClassAnnotations(DirectClassFile cf,
        CfOptions args) {
    CstType thisClass = cf.getThisClass();
    AttributeList attribs = cf.getAttributes();
    Annotations result = getAnnotations(attribs);
    Annotation enclosingMethod = translateEnclosingMethod(attribs);

    try {
        Annotations innerClassAnnotations =
            translateInnerClasses(thisClass, attribs,
                    enclosingMethod == null);
        if (innerClassAnnotations != null) {
            result = Annotations.combine(result, innerClassAnnotations);
        }
    } catch (Warning warn) {
        args.warn.println("warning: " + warn.getMessage());
    }

    if (enclosingMethod != null) {
        result = Annotations.combine(result, enclosingMethod);
    }

    if (AccessFlags.isAnnotation(cf.getAccessFlags())) {
        Annotation annotationDefault =
            translateAnnotationDefaults(cf);
        if (annotationDefault != null) {
            result = Annotations.combine(result, annotationDefault);
        }
    }

    return result;
}
 
Example #19
Source File: EncodedMethod.java    From Box with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public int encode(DexFile file, AnnotatedOutput out,
        int lastIndex, int dumpSeq) {
    int methodIdx = file.getMethodIds().indexOf(method);
    int diff = methodIdx - lastIndex;
    int accessFlags = getAccessFlags();
    int codeOff = OffsettedItem.getAbsoluteOffsetOr0(code);
    boolean hasCode = (codeOff != 0);
    boolean shouldHaveCode = (accessFlags &
            (AccessFlags.ACC_ABSTRACT | AccessFlags.ACC_NATIVE)) == 0;

    /*
     * Verify that code appears if and only if a method is
     * declared to have it.
     */
    if (hasCode != shouldHaveCode) {
        throw new UnsupportedOperationException(
                "code vs. access_flags mismatch");
    }

    if (out.annotates()) {
        out.annotate(0, String.format("  [%x] %s", dumpSeq,
                        method.toHuman()));
        out.annotate(Leb128.unsignedLeb128Size(diff),
                "    method_idx:   " + Hex.u4(methodIdx));
        out.annotate(Leb128.unsignedLeb128Size(accessFlags),
                "    access_flags: " +
                AccessFlags.methodString(accessFlags));
        out.annotate(Leb128.unsignedLeb128Size(codeOff),
                "    code_off:     " + Hex.u4(codeOff));
    }

    out.writeUleb128(diff);
    out.writeUleb128(accessFlags);
    out.writeUleb128(codeOff);

    return methodIdx;
}
 
Example #20
Source File: AccessInfoTest.java    From Box with Apache License 2.0 5 votes vote down vote up
@Test
public void changeVisibility() {
	AccessInfo accessInfo = new AccessInfo(AccessFlags.ACC_PROTECTED | AccessFlags.ACC_STATIC, AFType.METHOD);
	AccessInfo result = accessInfo.changeVisibility(AccessFlags.ACC_PUBLIC);

	assertThat(result.isPublic(), is(true));
	assertThat(result.isPrivate(), is(false));
	assertThat(result.isProtected(), is(false));

	assertThat(result.isStatic(), is(true));
}
 
Example #21
Source File: DexMaker.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Declares a method or constructor.
 *
 * @param flags a bitwise combination of {@link Modifier#PUBLIC}, {@link
 *              Modifier#PRIVATE}, {@link Modifier#PROTECTED}, {@link Modifier#STATIC},
 *              {@link Modifier#FINAL} and {@link Modifier#SYNCHRONIZED}.
 *              <p><strong>Warning:</strong> the {@link Modifier#SYNCHRONIZED} flag
 *              is insufficient to generate a synchronized method. You must also use
 *              {@link Code#monitorEnter} and {@link Code#monitorExit} to acquire
 *              a monitor.
 */
public Code declare(MethodId<?, ?> method, int flags) {
    TypeDeclaration typeDeclaration = getTypeDeclaration(method.declaringType);
    if (typeDeclaration.methods.containsKey(method)) {
        throw new IllegalStateException("already declared: " + method);
    }

    int supportedFlags = Modifier.PUBLIC | Modifier.PRIVATE | Modifier.PROTECTED
            | Modifier.STATIC | Modifier.FINAL | Modifier.SYNCHRONIZED |
            Modifier.ABSTRACT | Modifier.NATIVE;
    if ((flags & ~supportedFlags) != 0) {
        throw new IllegalArgumentException("Unexpected flag: "
                + Integer.toHexString(flags));
    }
    if ((flags & Modifier.ABSTRACT) != 0 && (flags & Modifier.NATIVE) != 0) {
        throw new IllegalArgumentException("Native method can't be abstract");
    }

    // replace the SYNCHRONIZED flag with the DECLARED_SYNCHRONIZED flag
    if ((flags & Modifier.SYNCHRONIZED) != 0) {
        flags = (flags & ~Modifier.SYNCHRONIZED) | AccessFlags.ACC_DECLARED_SYNCHRONIZED;
    }

    if (method.isConstructor() || method.isStaticInitializer()) {
        flags |= ACC_CONSTRUCTOR;
    }

    MethodDeclaration methodDeclaration = new MethodDeclaration(method, flags);
    typeDeclaration.methods.put(method, methodDeclaration);
    return methodDeclaration.code;
}
 
Example #22
Source File: AccessInfo.java    From Box with Apache License 2.0 5 votes vote down vote up
public String rawString() {
	switch (type) {
		case CLASS:
			return AccessFlags.classString(accFlags);
		case FIELD:
			return AccessFlags.fieldString(accFlags);
		case METHOD:
			return AccessFlags.methodString(accFlags);
		default:
			return "?";
	}
}
 
Example #23
Source File: MethodInlineVisitor.java    From Box with Apache License 2.0 5 votes vote down vote up
private static boolean fixVisibilityOfInlineCode(MethodNode mth, InsnNode insn) {
	int newVisFlag = AccessFlags.ACC_PUBLIC; // TODO: calculate more precisely
	InsnType insnType = insn.getType();
	if (insnType == InsnType.INVOKE) {
		InvokeNode invoke = (InvokeNode) insn;
		MethodNode callMthNode = mth.root().deepResolveMethod(invoke.getCallMth());
		if (callMthNode != null) {
			FixAccessModifiers.changeVisibility(callMthNode, newVisFlag);
		}
		return true;
	}
	if (insnType == InsnType.ONE_ARG) {
		InsnArg arg = insn.getArg(0);
		if (!arg.isInsnWrap()) {
			return false;
		}
		return fixVisibilityOfInlineCode(mth, ((InsnWrapArg) arg).getWrapInsn());
	}
	if (insn instanceof IndexInsnNode) {
		Object indexObj = ((IndexInsnNode) insn).getIndex();
		if (indexObj instanceof FieldInfo) {
			FieldNode fieldNode = mth.root().deepResolveField(((FieldInfo) indexObj));
			if (fieldNode != null) {
				FixAccessModifiers.changeVisibility(fieldNode, newVisFlag);
			}
			return true;
		}
	}
	if (Consts.DEBUG) {
		mth.addAttr(AType.COMMENTS, "JADX DEBUG: can't inline method, not implemented redirect type: " + insn);
	}
	return false;
}
 
Example #24
Source File: AndroidResourcesUtils.java    From Box with Apache License 2.0 5 votes vote down vote up
@Nullable
private static ClassNode makeClass(RootNode root, String clsName, ResourceStorage resStorage) {
	List<DexNode> dexNodes = root.getDexNodes();
	if (dexNodes.isEmpty()) {
		return null;
	}
	ClassNode rCls = new ClassNode(dexNodes.get(0), clsName, AccessFlags.ACC_PUBLIC | AccessFlags.ACC_FINAL);
	rCls.addAttr(AType.COMMENTS, "This class is generated by JADX");
	rCls.setState(ProcessState.PROCESS_COMPLETE);
	return rCls;
}
 
Example #25
Source File: EncodedMethod.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public int encode(DexFile file, AnnotatedOutput out,
        int lastIndex, int dumpSeq) {
    int methodIdx = file.getMethodIds().indexOf(method);
    int diff = methodIdx - lastIndex;
    int accessFlags = getAccessFlags();
    int codeOff = OffsettedItem.getAbsoluteOffsetOr0(code);
    boolean hasCode = (codeOff != 0);
    boolean shouldHaveCode = (accessFlags &
            (AccessFlags.ACC_ABSTRACT | AccessFlags.ACC_NATIVE)) == 0;

    /*
     * Verify that code appears if and only if a method is
     * declared to have it.
     */
    if (hasCode != shouldHaveCode) {
        throw new UnsupportedOperationException(
                "code vs. access_flags mismatch");
    }

    if (out.annotates()) {
        out.annotate(0, String.format("  [%x] %s", dumpSeq,
                        method.toHuman()));
        out.annotate(Leb128.unsignedLeb128Size(diff),
                "    method_idx:   " + Hex.u4(methodIdx));
        out.annotate(Leb128.unsignedLeb128Size(accessFlags),
                "    access_flags: " +
                AccessFlags.methodString(accessFlags));
        out.annotate(Leb128.unsignedLeb128Size(codeOff),
                "    code_off:     " + Hex.u4(codeOff));
    }

    out.writeUleb128(diff);
    out.writeUleb128(accessFlags);
    out.writeUleb128(codeOff);

    return methodIdx;
}
 
Example #26
Source File: AttributeTranslator.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the annotations out of a given class, similar to {@link
 * #getAnnotations}, also including annotations for translations
 * of class-level attributes {@code EnclosingMethod} and
 * {@code InnerClasses}, if present. Additionally, if the
 * class is an annotation class, then this also includes a
 * representation of all the {@code AnnotationDefault}
 * values.
 *
 * @param cf {@code non-null;} the class in question
 * @param args {@code non-null;} the high-level options
 * @return {@code non-null;} the set of annotations, which may be empty
 */
public static Annotations getClassAnnotations(DirectClassFile cf,
        CfOptions args) {
    CstType thisClass = cf.getThisClass();
    AttributeList attribs = cf.getAttributes();
    Annotations result = getAnnotations(attribs);
    Annotation enclosingMethod = translateEnclosingMethod(attribs);

    try {
        Annotations innerClassAnnotations =
            translateInnerClasses(thisClass, attribs,
                    enclosingMethod == null);
        if (innerClassAnnotations != null) {
            result = Annotations.combine(result, innerClassAnnotations);
        }
    } catch (Warning warn) {
        args.warn.println("warning: " + warn.getMessage());
    }

    if (enclosingMethod != null) {
        result = Annotations.combine(result, enclosingMethod);
    }

    if (AccessFlags.isAnnotation(cf.getAccessFlags())) {
        Annotation annotationDefault =
            translateAnnotationDefaults(cf);
        if (annotationDefault != null) {
            result = Annotations.combine(result, annotationDefault);
        }
    }

    return result;
}
 
Example #27
Source File: EncodedMethod.java    From Box with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public int encode(DexFile file, AnnotatedOutput out,
        int lastIndex, int dumpSeq) {
    int methodIdx = file.getMethodIds().indexOf(method);
    int diff = methodIdx - lastIndex;
    int accessFlags = getAccessFlags();
    int codeOff = OffsettedItem.getAbsoluteOffsetOr0(code);
    boolean hasCode = (codeOff != 0);
    boolean shouldHaveCode = (accessFlags &
            (AccessFlags.ACC_ABSTRACT | AccessFlags.ACC_NATIVE)) == 0;

    /*
     * Verify that code appears if and only if a method is
     * declared to have it.
     */
    if (hasCode != shouldHaveCode) {
        throw new UnsupportedOperationException(
                "code vs. access_flags mismatch");
    }

    if (out.annotates()) {
        out.annotate(0, String.format("  [%x] %s", dumpSeq,
                        method.toHuman()));
        out.annotate(Leb128.unsignedLeb128Size(diff),
                "    method_idx:   " + Hex.u4(methodIdx));
        out.annotate(Leb128.unsignedLeb128Size(accessFlags),
                "    access_flags: " +
                AccessFlags.methodString(accessFlags));
        out.annotate(Leb128.unsignedLeb128Size(codeOff),
                "    code_off:     " + Hex.u4(codeOff));
    }

    out.writeUleb128(diff);
    out.writeUleb128(accessFlags);
    out.writeUleb128(codeOff);

    return methodIdx;
}
 
Example #28
Source File: AccessInfo.java    From Box with Apache License 2.0 4 votes vote down vote up
public boolean isPublic() {
	return (accFlags & AccessFlags.ACC_PUBLIC) != 0;
}
 
Example #29
Source File: AccessInfo.java    From Box with Apache License 2.0 4 votes vote down vote up
public boolean isProtected() {
	return (accFlags & AccessFlags.ACC_PROTECTED) != 0;
}
 
Example #30
Source File: StdAttributeFactory.java    From Box with Apache License 2.0 4 votes vote down vote up
/**
 * Parses an {@code InnerClasses} attribute.
 */
private Attribute innerClasses(DirectClassFile cf, int offset, int length,
        ParseObserver observer) {
    if (length < 2) {
        return throwSeverelyTruncated();
    }

    ByteArray bytes = cf.getBytes();
    ConstantPool pool = cf.getConstantPool();
    int count = bytes.getUnsignedShort(offset); // number_of_classes

    if (observer != null) {
        observer.parsed(bytes, offset, 2,
                        "number_of_classes: " + Hex.u2(count));
    }

    offset += 2;
    length -= 2;

    if (length != (count * 8)) {
        throwBadLength((count * 8) + 2);
    }

    InnerClassList list = new InnerClassList(count);

    for (int i = 0; i < count; i++) {
        int innerClassIdx = bytes.getUnsignedShort(offset);
        int outerClassIdx = bytes.getUnsignedShort(offset + 2);
        int nameIdx = bytes.getUnsignedShort(offset + 4);
        int accessFlags = bytes.getUnsignedShort(offset + 6);
        CstType innerClass = (CstType) pool.get(innerClassIdx);
        CstType outerClass = (CstType) pool.get0Ok(outerClassIdx);
        CstString name = (CstString) pool.get0Ok(nameIdx);
        list.set(i, innerClass, outerClass, name, accessFlags);
        if (observer != null) {
            observer.parsed(bytes, offset, 2,
                            "inner_class: " +
                            DirectClassFile.stringOrNone(innerClass));
            observer.parsed(bytes, offset + 2, 2,
                            "  outer_class: " +
                            DirectClassFile.stringOrNone(outerClass));
            observer.parsed(bytes, offset + 4, 2,
                            "  name: " +
                            DirectClassFile.stringOrNone(name));
            observer.parsed(bytes, offset + 6, 2,
                            "  access_flags: " +
                            AccessFlags.innerClassString(accessFlags));
        }
        offset += 8;
    }

    list.setImmutable();
    return new AttInnerClasses(list);
}