Demo entry 6625207

dd

   

Submitted by anonymous on Jun 20, 2017 at 05:03
Language: Java. Code size: 4.8 kB.

package com.kugou.common.dynamic;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

import com.kugou.common.utils.KGLog;

import dalvik.system.DexClassLoader;
import dalvik.system.PathClassLoader;

public class ClassLoaderHigherCombiner extends AbsClassLoaderCombiner {
    class CTest{
        private void test(){
            
        }
    }

    @Override
    void combineDexIntoPath(List<DexClassLoader> dexClassLoaderArray,
            PathClassLoader pathClassLoader) throws KGDynamicLoadException, ClassNotFoundException {
        combine(dexClassLoaderArray, pathClassLoader);
    }

    private void combine(List<DexClassLoader> dexClassLoaderArray, PathClassLoader pathClassLoader)
            throws KGDynamicLoadException, ClassNotFoundException {
        try {
            // check if it works
            Class.forName("dalvik.system.BaseDexClassLoader");
            try {
                List<Object> dexElementList = getDexElements(getField(pathClassLoader, "pathList"));
                for (DexClassLoader dexClassLoader : dexClassLoaderArray) {
                    dexElementList.addAll(getDexElements(getField(dexClassLoader, "pathList")));
                }
                Object dexElements = combineArray(dexElementList);
                Object pathList = getField(pathClassLoader, "pathList");
                setFieldValue(pathList, "dexElements", dexElements);
                KGLog.e("", "ClassLoaderHigherCombiner success");
            } catch (Throwable e) {
                e.printStackTrace();
                throw new KGDynamicLoadException(e);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            // we cannot deal with it
            throw e;
        }
    }

    /**
     * 单个合并
     * 
     * @param dexClassLoaderArray
     * @param pathClassLoader
     * @throws KGDynamicLoadException
     * @throws ClassNotFoundException
     */
    public void singleCombine(DexClassLoader dexClassLoader, PathClassLoader pathClassLoader)
            throws KGDynamicLoadException, ClassNotFoundException {
        try {
            // check if it works
            Class.forName("dalvik.system.BaseDexClassLoader");
            try {
                List<DexClassLoader> dexClassLoaderArray = new ArrayList<DexClassLoader>();
                dexClassLoaderArray.add(dexClassLoader);
                List<Object> dexElementList = getDexElements(getField(pathClassLoader, "pathList"));
                for (DexClassLoader dexClassLoadera : dexClassLoaderArray) {
                    dexElementList.addAll(getDexElements(getField(dexClassLoadera, "pathList")));
                }
                Object dexElements = combineArray(dexElementList);
                Object pathList = getField(pathClassLoader, "pathList");
                setFieldValue(pathList, "dexElements", dexElements);
                KGLog.e("", "ClassLoaderHigherCombiner success");
            } catch (Throwable e) {
                e.printStackTrace();
                throw new KGDynamicLoadException(e);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            // we cannot deal with it
            throw e;
        }
    }

    private List<Object> getDexElements(Object pathList) throws Exception {
        Object dexElements = getFieldValue(pathList, "dexElements");

        ArrayList<Object> elements = new ArrayList<Object>();
        int index = 0;
        do {
            Object element = null;
            try {
                element = Array.get(dexElements, index);
            } catch (Exception e) {
                element = null;
            }
            if (element == null) {
                break;
            } else {
                if (!"dalvik.system.DexPathList$Element".equals(element.getClass().getName())) {
                    break;
                }
            }
            elements.add(element);
            index++;
        } while (true);
        return elements;
    }
}
/**
 * 单个合并
 * 
 * @param dexClassLoaderArray
 * @param pathClassLoader
 * @throws KGDynamicLoadException
 * @throws ClassNotFoundException
 */
 
public class Singleton {
    int x = 0;
    private static  Singleton sInstance; // 需要volatile关键字防止指令乱序

    private Singleton() {}

    public static Singleton getInstance() {
        // 双重校验锁提高性能
        if (sInstance == null) {
            synchronized (Singleton.class) {
                if (sInstance == null) {
                    sInstance = new Singleton();
                }
            }
        }
        return sInstance;
    }
}

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).