Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f65e2c43 authored by Makoto Onuki's avatar Makoto Onuki Committed by Android (Google) Code Review
Browse files

Merge "Make text filter policy resolution more intuitive" into main

parents 14b4511c c0d10972
Loading
Loading
Loading
Loading
+1 −4
Original line number Diff line number Diff line
@@ -15,9 +15,6 @@
 */
package android.hosttest.annotation;

import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;

import java.lang.annotation.Retention;
@@ -27,7 +24,7 @@ import java.lang.annotation.Target;
/**
 * Only used for HostStubGen tests. It's not used by Ravenwood.
 */
@Target({TYPE, FIELD, METHOD, CONSTRUCTOR})
@Target({TYPE})
@Retention(RetentionPolicy.CLASS)
public @interface HostSideTestStaticInitializerKeep {
}
+2 −1
Original line number Diff line number Diff line
@@ -52,7 +52,8 @@ public class HostTestUtils {
                case HostStubGenProcessedAsIgnore a -> a.reason();
                case HostStubGenProcessedAsThrow a -> a.reason();
                case HostStubGenProcessedAsThrowButSupported a -> a.reason();
                // case HostStubGenProcessedAsSubstitute a -> a.reason();
                case HostStubGenProcessedAsExperimental a -> a.reason();
                case HostStubGenProcessedAsSubstitute a -> a.reason();
                default -> null;
            };
            if (reason != null && !reason.isEmpty()) {
+11 −6
Original line number Diff line number Diff line
@@ -35,13 +35,13 @@ import com.android.hoststubgen.utils.ZipEntryData
import com.android.hoststubgen.visitors.ImplGeneratingAdapter
import com.android.hoststubgen.visitors.JdkPatchVisitor
import com.android.hoststubgen.visitors.PackageRedirectRemapper
import java.io.PrintWriter
import org.objectweb.asm.ClassReader
import org.objectweb.asm.ClassVisitor
import org.objectweb.asm.ClassWriter
import org.objectweb.asm.commons.ClassRemapper
import org.objectweb.asm.util.CheckClassAdapter
import org.objectweb.asm.util.TraceClassVisitor
import java.io.PrintWriter

/**
 * This class implements bytecode transformation of HostStubGen.
@@ -61,14 +61,15 @@ class HostStubGenClassProcessor(
        // Connect to the base visitor
        var outVisitor: ClassVisitor = base

        if (!options.disableJdkPatch.get) {
            outVisitor = JdkPatchVisitor(outVisitor)
        }

        // This should be the innermost visitor. This one checks the final bytecode.
        if (options.enableClassChecker.get) {
            outVisitor = CheckClassAdapter(outVisitor)
        }

        if (!options.disableJdkPatch.get) {
            outVisitor = JdkPatchVisitor(outVisitor)
        }

        // Remapping should happen at the end.
        outVisitor = ClassRemapper(outVisitor, remapper)

@@ -182,7 +183,11 @@ class HostStubGenClassProcessor(
            var filter: OutputFilter

            // The first filter is for the default policy from the command line options.
            filter = ConstantFilter(options.defaultPolicy.get, "default-by-options")
            filter = ConstantFilter(
                options.defaultPolicy.get.resolveDefaultForClass(),
                options.defaultPolicy.get.resolveDefaultForFields(),
                options.defaultPolicy.get.resolveDefaultForMethods(),
            )

            // Next, we build a filter that preserves all native methods by default
            filter = KeepNativeFilter(allClasses, filter)
+1 −1
Original line number Diff line number Diff line
@@ -65,7 +65,7 @@ class AnnotationBasedFilter(
     * policy.
     */
    var annotationAllowedMembers: OutputFilter =
        ConstantFilter(FilterPolicy.Remove, "default disallowed")
        ConstantFilter(FilterPolicy.Remove.withReason("default disallowed"))

    private val keepAnnotations = convertToInternalNames(keepAnnotations_)
    private val keepClassAnnotations = convertToInternalNames(keepClassAnnotations_)
+13 −21
Original line number Diff line number Diff line
@@ -22,38 +22,30 @@ import com.android.hoststubgen.HostStubGenInternalException
 * [OutputFilter] with a given policy. Used to represent the default policy.
 *
 * This is used as the last fallback filter.
 *
 * @param policy the policy. Cannot be a "substitute" policy.
 */
class ConstantFilter(
    policy: FilterPolicy,
    private val reason: String
    private val classPolicy: FilterPolicyWithReason,
    private val fieldPolicy: FilterPolicyWithReason = classPolicy,
    private val methodPolicy: FilterPolicyWithReason = classPolicy,
) : OutputFilter() {

    private val classPolicy: FilterPolicy
    private val fieldPolicy: FilterPolicy
    private val methodPolicy: FilterPolicy

    init {
        if (!policy.isUsableWithDefault) {
            throw HostStubGenInternalException("ConstantFilter doesn't support $policy.")
        if (!classPolicy.policy.isUsableWithClasses) {
            throw HostStubGenInternalException("${classPolicy.policy} can't be used for classes")
        }
        methodPolicy = policy

        // If the default policy is "throw", we convert it to "keep" for classes and fields.
        classPolicy = when (policy) {
            FilterPolicy.Throw -> FilterPolicy.Keep
            else -> policy
        if (!fieldPolicy.policy.isUsableWithFields) {
            throw HostStubGenInternalException("${fieldPolicy.policy} can't be used for fields")
        }
        if (!methodPolicy.policy.isUsableWithMethods) {
            throw HostStubGenInternalException("${methodPolicy.policy} can't be used for methods")
        }
        fieldPolicy = classPolicy
    }

    override fun getPolicyForClass(className: String): FilterPolicyWithReason {
        return classPolicy.withReason(reason)
        return classPolicy
    }

    override fun getPolicyForField(className: String, fieldName: String): FilterPolicyWithReason {
        return fieldPolicy.withReason(reason)
        return fieldPolicy
    }

    override fun getPolicyForMethod(
@@ -61,6 +53,6 @@ class ConstantFilter(
        methodName: String,
        descriptor: String,
    ): FilterPolicyWithReason {
        return methodPolicy.withReason(reason)
        return methodPolicy
    }
}
Loading