summaryrefslogtreecommitdiff
path: root/deps/v8/build/android/bytecode/java/org/chromium/bytecode/CustomClassLoaderClassWriter.java
blob: 3a52c85d56629c0ec962c6484d08c29ef4eb30b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.bytecode;

import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;

/**
 * A ClassWriter that uses a custom class loader.
 */
class CustomClassLoaderClassWriter extends ClassWriter {
    private ClassLoader mClassLoader;

    public CustomClassLoaderClassWriter(ClassLoader classLoader, ClassReader reader, int flags) {
        super(reader, flags);
        this.mClassLoader = classLoader;
    }

    /**
     * The only modifications from the org.objectweb.asm.ClassWriter implementations is that this
     * method is final and it uses a custom ClassLoader.
     *
     * See https://github.com/llbit/ow2-asm/blob/master/src/org/objectweb/asm/ClassWriter.java.
     */
    @Override
    protected final String getCommonSuperClass(final String type1, final String type2) {
        Class<?> c, d;
        try {
            c = Class.forName(type1.replace('/', '.'), false, mClassLoader);
            d = Class.forName(type2.replace('/', '.'), false, mClassLoader);
        } catch (Exception e) {
            throw new RuntimeException(e.toString());
        }
        if (c.isAssignableFrom(d)) {
            return type1;
        }
        if (d.isAssignableFrom(c)) {
            return type2;
        }
        if (c.isInterface() || d.isInterface()) {
            return "java/lang/Object";
        } else {
            do {
                c = c.getSuperclass();
            } while (!c.isAssignableFrom(d));
            return c.getName().replace('.', '/');
        }
    }
}