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

Commit c13ba267 authored by Makoto Onuki's avatar Makoto Onuki
Browse files

HostStubGen clean up.

Addressing the comments on the previous CL.

Test: ./scripts/run-all-tests.sh
Bug: 292141694
Change-Id: I0a1daa6d336cf80a00afeb8f067422a4baf95475
parent 84471ce4
Loading
Loading
Loading
Loading
+23 −17
Original line number Diff line number Diff line
@@ -69,7 +69,8 @@ public class HostTestUtils {
            String methodDescriptor,
            String callbackMethod
    ) {
        callStaticMethodByName(callbackMethod, methodClass, methodName, methodDescriptor);
        callStaticMethodByName(callbackMethod, "method call hook", methodClass,
                methodName, methodDescriptor);
    }

    /**
@@ -167,31 +168,38 @@ public class HostTestUtils {
        logPrintStream.println("! Class loaded: " + loadedClass.getCanonicalName()
                + " calling hook " + callbackMethod);

        callStaticMethodByName(callbackMethod, loadedClass);
        callStaticMethodByName(callbackMethod, "class load hook", loadedClass);
    }

    private static void callStaticMethodByName(String classAndMethodName, Object... args) {
    private static void callStaticMethodByName(String classAndMethodName,
            String description, Object... args) {
        // Forward the call to callbackMethod.
        final int lastPeriod = classAndMethodName.lastIndexOf(".");
        final String className = classAndMethodName.substring(0, lastPeriod);
        final String methodName = classAndMethodName.substring(lastPeriod + 1);

        if (lastPeriod < 0 || className.isEmpty() || methodName.isEmpty()) {
        if ((lastPeriod) < 0 || (lastPeriod == classAndMethodName.length() - 1)) {
            throw new HostTestException(String.format(
                    "Unable to find class load hook: malformed method name \"%s\"",
                    "Unable to find %s: malformed method name \"%s\"",
                    description,
                    classAndMethodName));
        }

        final String className = classAndMethodName.substring(0, lastPeriod);
        final String methodName = classAndMethodName.substring(lastPeriod + 1);

        Class<?> clazz = null;
        try {
            clazz = Class.forName(className);
        } catch (Exception e) {
            throw new HostTestException(String.format(
                    "Unable to find class load hook: Class %s not found", className), e);
                    "Unable to find %s: Class %s not found",
                    description,
                    className), e);
        }
        if (!Modifier.isPublic(clazz.getModifiers())) {
            throw new HostTestException(String.format(
                    "Unable to find class load hook: Class %s must be public", className));
                    "Unable to find %s: Class %s must be public",
                    description,
                    className));
        }

        Class<?>[] argTypes = new Class[args.length];
@@ -204,25 +212,23 @@ public class HostTestUtils {
            method = clazz.getMethod(methodName, argTypes);
        } catch (Exception e) {
            throw new HostTestException(String.format(
                    "Unable to find class load hook: class %s doesn't have method %s"
                    "Unable to find %s: class %s doesn't have method %s"
                            + " (method must take exactly one parameter of type Class,"
                            + " and public static)",
                    className,
                    methodName), e);
                    description, className, methodName), e);
        }
        if (!(Modifier.isPublic(method.getModifiers())
                && Modifier.isStatic(method.getModifiers()))) {
            throw new HostTestException(String.format(
                    "Unable to find class load hook: Method %s in class %s must be public static",
                    methodName, className));
                    "Unable to find %s: Method %s in class %s must be public static",
                    description, methodName, className));
        }
        try {
            method.invoke(null, args);
        } catch (Exception e) {
            throw new HostTestException(String.format(
                    "Unable to invoke class load hook %s.%s",
                    className,
                    methodName), e);
                    "Unable to invoke %s %s.%s",
                    description, className, methodName), e);
        }
    }

+8 −0
Original line number Diff line number Diff line
@@ -32,6 +32,10 @@ fun normalizeTextLine(s: String): String {
    return uncommented.trim()
}

/**
 * Concatenate list [a] and [b] and return it. As an optimization, it returns an input
 * [List] as-is if the other [List] is empty, so do not modify input [List]'s.
 */
fun <T> addLists(a: List<T>, b: List<T>): List<T> {
    if (a.isEmpty()) {
        return b
@@ -42,6 +46,10 @@ fun <T> addLists(a: List<T>, b: List<T>): List<T> {
    return a + b
}

/**
 * Add element [b] to list [a] if [b] is not null. Otherwise, just return [a].
 * (because the method may return [a] as-is, do not modify it after passing it.)
 */
fun <T> addNonNullElement(a: List<T>, b: T?): List<T> {
    if (b == null) {
        return a