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: 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 #2
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 #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: 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 #6
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 #7
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 #8
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 #9
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 #10
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 #11
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 #12
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 #13
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 #14
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 #15
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 #16
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 #17
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 #18
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 #19
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 #20
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 #21
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 #22
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 #23
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 #24
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 #25
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 #26
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 #27
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 #28
Source File: AccessInfo.java    From Box with Apache License 2.0 4 votes vote down vote up
public boolean isNative() {
	return (accFlags & AccessFlags.ACC_NATIVE) != 0;
}
 
Example #29
Source File: MethodListParser.java    From J2ME-Loader with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
protected String humanAccessFlags(int accessFlags) {
    return AccessFlags.methodString(accessFlags);
}
 
Example #30
Source File: ClassDefItem.java    From J2ME-Loader with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public void writeTo(DexFile file, AnnotatedOutput out) {
    boolean annotates = out.annotates();
    TypeIdsSection typeIds = file.getTypeIds();
    int classIdx = typeIds.indexOf(thisClass);
    int superIdx = (superclass == null) ? -1 :
        typeIds.indexOf(superclass);
    int interOff = OffsettedItem.getAbsoluteOffsetOr0(interfaces);
    int annoOff = annotationsDirectory.isEmpty() ? 0 :
        annotationsDirectory.getAbsoluteOffset();
    int sourceFileIdx = (sourceFile == null) ? -1 :
        file.getStringIds().indexOf(sourceFile);
    int dataOff = classData.isEmpty()? 0 : classData.getAbsoluteOffset();
    int staticValuesOff =
        OffsettedItem.getAbsoluteOffsetOr0(staticValuesItem);

    if (annotates) {
        out.annotate(0, indexString() + ' ' + thisClass.toHuman());
        out.annotate(4, "  class_idx:           " + Hex.u4(classIdx));
        out.annotate(4, "  access_flags:        " +
                     AccessFlags.classString(accessFlags));
        out.annotate(4, "  superclass_idx:      " + Hex.u4(superIdx) +
                     " // " + ((superclass == null) ? "<none>" :
                      superclass.toHuman()));
        out.annotate(4, "  interfaces_off:      " + Hex.u4(interOff));
        if (interOff != 0) {
            TypeList list = interfaces.getList();
            int sz = list.size();
            for (int i = 0; i < sz; i++) {
                out.annotate(0, "    " + list.getType(i).toHuman());
            }
        }
        out.annotate(4, "  source_file_idx:     " + Hex.u4(sourceFileIdx) +
                     " // " + ((sourceFile == null) ? "<none>" :
                      sourceFile.toHuman()));
        out.annotate(4, "  annotations_off:     " + Hex.u4(annoOff));
        out.annotate(4, "  class_data_off:      " + Hex.u4(dataOff));
        out.annotate(4, "  static_values_off:   " +
                Hex.u4(staticValuesOff));
    }

    out.writeInt(classIdx);
    out.writeInt(accessFlags);
    out.writeInt(superIdx);
    out.writeInt(interOff);
    out.writeInt(sourceFileIdx);
    out.writeInt(annoOff);
    out.writeInt(dataOff);
    out.writeInt(staticValuesOff);
}