Android Open Source - AndroidPlugin Plugin Class Loader






From Project

Back to project page AndroidPlugin.

License

The source code is released under:

MIT License

If you think the Android project AndroidPlugin listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2015 HouKx <hkx.aidream@gmail.com>
 *//w w w  . j  ava  2  s  . com
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package androidx.pluginmgr;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import android.util.Log;
import dalvik.system.DexClassLoader;

/**
 * ????????
 * 
 * @author HouKangxi
 * 
 */
class PluginClassLoader extends DexClassLoader {
    private final String tag;
    private final PlugInfo thisPlugin;
    private final String optimizedDirectory;
    private final String libraryPath;
    /**
     * Activity ?????
     */
    private final Map<String, ClassLoader> proxyActivityLoaderMap;

    public PluginClassLoader(String dexPath, String optimizedDir,
            ClassLoader parent, PlugInfo plugin) {
        super(dexPath, optimizedDir,
                plugin.getPackageInfo().applicationInfo.nativeLibraryDir,
                parent);
        thisPlugin = plugin;
        proxyActivityLoaderMap = new HashMap<String, ClassLoader>(plugin
                .getActivities().size());
        this.libraryPath = plugin.getPackageInfo().applicationInfo.nativeLibraryDir;
        this.optimizedDirectory = optimizedDir;
        tag = "PluginClassLoader( " + plugin.getPackageInfo().packageName
                + " )";
        Log.i(tag, "libraryPath = " + libraryPath);
    }

    Class<?> loadActivityClass(final String actClassName)
            throws ClassNotFoundException {
        Log.d(tag, "loadActivityClass: " + actClassName);

        // ????????????????Activity dex??
        File dexSavePath = ActivityOverider.createProxyDex(thisPlugin,
                actClassName, true);
        ClassLoader actLoader = proxyActivityLoaderMap.get(actClassName);
        if (actLoader == null) {
            actLoader = new DexClassLoader(dexSavePath.getAbsolutePath(),
                    optimizedDirectory, libraryPath, this) {
                @Override
                protected Class<?> loadClass(String name, boolean resolve)
                        throws ClassNotFoundException {
                    Log.d("PlugActClassLoader(" + actClassName + ")",
                            "loadClass: " + name);
                    if (ActivityOverider.targetClassName.equals(name)) {
                        Class<?> c = findLoadedClass(name);
                        if (c == null) {
                            Log.d("PlugActClassLoader(" + actClassName + ")",
                                    "findClass");
                            c = findClass(name);
                        }
                        if (resolve) {
                            resolveClass(c);
                        }
                        return c;
                    }
                    return super.loadClass(name, resolve);
                }
            };
            proxyActivityLoaderMap.put(actClassName, actLoader);
        }
        return actLoader.loadClass(ActivityOverider.targetClassName);
    }

    protected Object getClassLoadingLock(String name) {
        return name.hashCode();
    }

    private Class<?> findByParent(String name, boolean throwEx)
            throws ClassNotFoundException {
        Class<?> c = null;
        try {
            ClassLoader parent = getParent();
            if (parent != null) {
                if (parent.getClass() == FrameworkClassLoader.class) {
                    parent = parent.getParent();
                }
                if (parent != null) {
                    c = parent.loadClass(name);
                }
            }
        } catch (ClassNotFoundException e) {
            if (throwEx) {
                throw e;
            }
        }
        return c;
    }

    protected Class<?> loadClass(String name, boolean resolve)
            throws ClassNotFoundException {
        synchronized (getClassLoadingLock(name)) {
            // First, check if the class has already been loaded
            Class<?> c = findLoadedClass(name);
            if (c == null) {
                if (name.startsWith("android.support.")) {
                    try {
                        c = findClass(name);
                    } catch (ClassNotFoundException e) {
                    }
                    if (c == null) {
                        c = findByParent(name, true);
                    }
                } else {
                    c = findByParent(name, false);
                    if (c == null) {
                        c = findClass(name);
                    }
                }
            }
            if (resolve) {
                resolveClass(c);
            }
            return c;
        }
    }
}




Java Source Code List

android.widget.ViewStub.java
androidx.plmgrdemo.MainActivity.java
androidx.plmgrdemo.PlugListViewAdapter.java
androidx.pluginmgr.ActivityClassGenerator.java
androidx.pluginmgr.ActivityOverider.java
androidx.pluginmgr.FileUtil.java
androidx.pluginmgr.FrameworkClassLoader.java
androidx.pluginmgr.LayoutInflaterWrapper.java
androidx.pluginmgr.PlugInfo.java
androidx.pluginmgr.PluginActivityLifeCycleCallback.java
androidx.pluginmgr.PluginClassLoader.java
androidx.pluginmgr.PluginContextWrapper.java
androidx.pluginmgr.PluginManager.java
androidx.pluginmgr.PluginManifestUtil.java
androidx.pluginmgr.ReflectionUtils.java
androidx.pluginmgr.XmlManifestReader.java
com.limemobile.app.demo.pluginclienta.ClientABindService.java
com.limemobile.app.demo.pluginclienta.ClientAStartedService.java
com.limemobile.app.demo.pluginclienta.MainActivity.java
com.limemobile.app.demo.pluginclienta.TestButton.java
com.limemobile.app.demo.pluginclienta.TestFragmentActivity.java
com.limemobile.app.demo.pluginclienta.TestFragment.java
com.limemobile.app.demo.pluginclientb.ClientBStartedService.java
com.limemobile.app.demo.pluginclientb.MainActivity.java
com.limemobile.app.demo.pluginhost.HostBindService.java
com.limemobile.app.demo.pluginhost.HostStartedService.java
com.limemobile.app.demo.pluginhost.MainActivity.java
com.limemobile.app.demo.pluginhost.MyApplication.java
com.limemobile.app.demo.pluginhost.TestHostClass.java
com.limemobile.app.plugin.IPluginActivity.java
com.limemobile.app.plugin.IPluginContentProvider.java
com.limemobile.app.plugin.IPluginService.java
com.limemobile.app.plugin.PluginClientActivity.java
com.limemobile.app.plugin.PluginClientFragmentActivity.java
com.limemobile.app.plugin.PluginClientService.java
com.limemobile.app.plugin.PluginHostApplication.java
com.limemobile.app.plugin.PluginHostContentProvider.java
com.limemobile.app.plugin.PluginHostDelegateActivity.java
com.limemobile.app.plugin.PluginHostDelegateContentProvider.java
com.limemobile.app.plugin.PluginHostDelegateFragmentActivity.java
com.limemobile.app.plugin.PluginHostDelegateService.java
com.limemobile.app.plugin.internal.IPluginActivityDelegate.java
com.limemobile.app.plugin.internal.IPluginContentProviderDelegate.java
com.limemobile.app.plugin.internal.IPluginServiceDelegate.java
com.limemobile.app.plugin.internal.PluginClientDexClassLoader.java
com.limemobile.app.plugin.internal.PluginClientInfo.java
com.limemobile.app.plugin.internal.PluginClientManager.java
com.limemobile.app.plugin.internal.PluginDelegateActivityImpl.java
com.limemobile.app.plugin.internal.PluginDelegateContentProviderImpl.java
com.limemobile.app.plugin.internal.PluginDelegateServiceImpl.java
com.limemobile.app.plugin.internal.ReflectFieldAccessor.java