@Test public void testMethodInjection() throws IOException, LogAbortException, ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException { ICreateInfo ci = new ICreateInfo() { @Override public Class<?>[] getInjectedClasses() { return new Class<?>[0]; } @Override public String[] getDelegateMethods() { return new String[0]; } @Override public String[] getDelegateClassNatives() { return new String[0]; } @Override public String[] getOverriddenMethods() { return new String[0]; } @Override public String[] getRenamedClasses() { return new String[0]; } @Override public String[] getJavaPkgClasses() { return new String[0]; } @Override public Set<String> getExcludedClasses() { return new HashSet<String>(0); } @Override public String[] getDeleteReturns() { return new String[0]; } @Override public String[] getPromotedFields() { return new String[0]; } @Override public Map<String, InjectMethodRunnable> getInjectedMethodsMap() { HashMap<String, InjectMethodRunnable> map = new HashMap<String, InjectMethodRunnable>(1); map.put("mock_android.util.EmptyArray", InjectMethodRunnables.CONTEXT_GET_FRAMEWORK_CLASS_LOADER); return map; } }; AsmGenerator agen = new AsmGenerator(mLog, mOsDestJar, ci); AsmAnalyzer aa = new AsmAnalyzer(mLog, mOsJarPath, agen, null, new String[] { "**" }, ci.getExcludedClasses(), new String[] { "mock_android/data/data*" }); aa.analyze(); agen.generate(); Map<String, ClassReader> output = new TreeMap<String, ClassReader>(); Map<String, InputStream> filesFound = new TreeMap<String, InputStream>(); parseZip(mOsDestJar, output, filesFound); final String modifiedClass = "mock_android.util.EmptyArray"; final String modifiedClassPath = modifiedClass.replace('.', '/').concat(".class"); ZipFile zipFile = new ZipFile(mOsDestJar); ZipEntry entry = zipFile.getEntry(modifiedClassPath); assertNotNull(entry); final byte[] bytes; final InputStream inputStream = zipFile.getInputStream(entry); try { bytes = getByteArray(inputStream); } finally { inputStream.close(); } ClassLoader classLoader = new ClassLoader(getClass().getClassLoader()) { @Override protected Class<?> findClass(String name) throws ClassNotFoundException { if (name.equals(modifiedClass)) { return defineClass(null, bytes, 0, bytes.length); } throw new ClassNotFoundException(name + " not found."); } }; Class<?> emptyArrayClass = classLoader.loadClass(modifiedClass); Object emptyArrayInstance = emptyArrayClass.newInstance(); Method method = emptyArrayClass.getMethod("getFrameworkClassLoader"); Object cl = method.invoke(emptyArrayInstance); assertEquals(classLoader, cl); }
public void generate() throws IOException { TreeMap<String, byte[]> all = new TreeMap<String, byte[]>(); for (Class<?> clazz : mInjectClasses) { String name = classToEntryPath(clazz); InputStream is = ClassLoader.getSystemResourceAsStream(name); ClassReader cr = new ClassReader(is); byte[] b = transform(cr, true); name = classNameToEntryPath(transformName(cr.getClassName())); all.put(name, b); } for (Entry<String, ClassReader> entry : mDeps.entrySet()) { ClassReader cr = entry.getValue(); byte[] b = transform(cr, true); String name = classNameToEntryPath(transformName(cr.getClassName())); all.put(name, b); } for (Entry<String, ClassReader> entry : mKeep.entrySet()) { ClassReader cr = entry.getValue(); byte[] b = transform(cr, true); String name = classNameToEntryPath(transformName(cr.getClassName())); all.put(name, b); } for (Entry<String, InputStream> entry : mCopyFiles.entrySet()) { try { byte[] b = inputStreamToByteArray(entry.getValue()); all.put(entry.getKey(), b); } catch (IOException e) { } } mLog.info("# deps classes: %d", mDeps.size()); mLog.info("# keep classes: %d", mKeep.size()); mLog.info("# renamed : %d", mRenameCount); createJar(new FileOutputStream(mOsDestJar), all); mLog.info("Created JAR file %s", mOsDestJar); }
AsmGenerator { public void generate() throws IOException { TreeMap<String, byte[]> all = new TreeMap<String, byte[]>(); for (Class<?> clazz : mInjectClasses) { String name = classToEntryPath(clazz); InputStream is = ClassLoader.getSystemResourceAsStream(name); ClassReader cr = new ClassReader(is); byte[] b = transform(cr, true); name = classNameToEntryPath(transformName(cr.getClassName())); all.put(name, b); } for (Entry<String, ClassReader> entry : mDeps.entrySet()) { ClassReader cr = entry.getValue(); byte[] b = transform(cr, true); String name = classNameToEntryPath(transformName(cr.getClassName())); all.put(name, b); } for (Entry<String, ClassReader> entry : mKeep.entrySet()) { ClassReader cr = entry.getValue(); byte[] b = transform(cr, true); String name = classNameToEntryPath(transformName(cr.getClassName())); all.put(name, b); } for (Entry<String, InputStream> entry : mCopyFiles.entrySet()) { try { byte[] b = inputStreamToByteArray(entry.getValue()); all.put(entry.getKey(), b); } catch (IOException e) { } } mLog.info("# deps classes: %d", mDeps.size()); mLog.info("# keep classes: %d", mKeep.size()); mLog.info("# renamed : %d", mRenameCount); createJar(new FileOutputStream(mOsDestJar), all); mLog.info("Created JAR file %s", mOsDestJar); } }
AsmGenerator { public void generate() throws IOException { TreeMap<String, byte[]> all = new TreeMap<String, byte[]>(); for (Class<?> clazz : mInjectClasses) { String name = classToEntryPath(clazz); InputStream is = ClassLoader.getSystemResourceAsStream(name); ClassReader cr = new ClassReader(is); byte[] b = transform(cr, true); name = classNameToEntryPath(transformName(cr.getClassName())); all.put(name, b); } for (Entry<String, ClassReader> entry : mDeps.entrySet()) { ClassReader cr = entry.getValue(); byte[] b = transform(cr, true); String name = classNameToEntryPath(transformName(cr.getClassName())); all.put(name, b); } for (Entry<String, ClassReader> entry : mKeep.entrySet()) { ClassReader cr = entry.getValue(); byte[] b = transform(cr, true); String name = classNameToEntryPath(transformName(cr.getClassName())); all.put(name, b); } for (Entry<String, InputStream> entry : mCopyFiles.entrySet()) { try { byte[] b = inputStreamToByteArray(entry.getValue()); all.put(entry.getKey(), b); } catch (IOException e) { } } mLog.info("# deps classes: %d", mDeps.size()); mLog.info("# keep classes: %d", mKeep.size()); mLog.info("# renamed : %d", mRenameCount); createJar(new FileOutputStream(mOsDestJar), all); mLog.info("Created JAR file %s", mOsDestJar); } AsmGenerator(Log log, String osDestJar, ICreateInfo createInfo); }
AsmGenerator { public void generate() throws IOException { TreeMap<String, byte[]> all = new TreeMap<String, byte[]>(); for (Class<?> clazz : mInjectClasses) { String name = classToEntryPath(clazz); InputStream is = ClassLoader.getSystemResourceAsStream(name); ClassReader cr = new ClassReader(is); byte[] b = transform(cr, true); name = classNameToEntryPath(transformName(cr.getClassName())); all.put(name, b); } for (Entry<String, ClassReader> entry : mDeps.entrySet()) { ClassReader cr = entry.getValue(); byte[] b = transform(cr, true); String name = classNameToEntryPath(transformName(cr.getClassName())); all.put(name, b); } for (Entry<String, ClassReader> entry : mKeep.entrySet()) { ClassReader cr = entry.getValue(); byte[] b = transform(cr, true); String name = classNameToEntryPath(transformName(cr.getClassName())); all.put(name, b); } for (Entry<String, InputStream> entry : mCopyFiles.entrySet()) { try { byte[] b = inputStreamToByteArray(entry.getValue()); all.put(entry.getKey(), b); } catch (IOException e) { } } mLog.info("# deps classes: %d", mDeps.size()); mLog.info("# keep classes: %d", mKeep.size()); mLog.info("# renamed : %d", mRenameCount); createJar(new FileOutputStream(mOsDestJar), all); mLog.info("Created JAR file %s", mOsDestJar); } AsmGenerator(Log log, String osDestJar, ICreateInfo createInfo); Set<String> getClassesNotRenamed(); void setKeep(Map<String, ClassReader> keep); void setDeps(Map<String, ClassReader> deps); void setCopyFiles(Map<String, InputStream> copyFiles); void setRewriteMethodCallClasses(Set<String> rewriteMethodCallClasses); void generate(); }
AsmGenerator { public void generate() throws IOException { TreeMap<String, byte[]> all = new TreeMap<String, byte[]>(); for (Class<?> clazz : mInjectClasses) { String name = classToEntryPath(clazz); InputStream is = ClassLoader.getSystemResourceAsStream(name); ClassReader cr = new ClassReader(is); byte[] b = transform(cr, true); name = classNameToEntryPath(transformName(cr.getClassName())); all.put(name, b); } for (Entry<String, ClassReader> entry : mDeps.entrySet()) { ClassReader cr = entry.getValue(); byte[] b = transform(cr, true); String name = classNameToEntryPath(transformName(cr.getClassName())); all.put(name, b); } for (Entry<String, ClassReader> entry : mKeep.entrySet()) { ClassReader cr = entry.getValue(); byte[] b = transform(cr, true); String name = classNameToEntryPath(transformName(cr.getClassName())); all.put(name, b); } for (Entry<String, InputStream> entry : mCopyFiles.entrySet()) { try { byte[] b = inputStreamToByteArray(entry.getValue()); all.put(entry.getKey(), b); } catch (IOException e) { } } mLog.info("# deps classes: %d", mDeps.size()); mLog.info("# keep classes: %d", mKeep.size()); mLog.info("# renamed : %d", mRenameCount); createJar(new FileOutputStream(mOsDestJar), all); mLog.info("Created JAR file %s", mOsDestJar); } AsmGenerator(Log log, String osDestJar, ICreateInfo createInfo); Set<String> getClassesNotRenamed(); void setKeep(Map<String, ClassReader> keep); void setDeps(Map<String, ClassReader> deps); void setCopyFiles(Map<String, InputStream> copyFiles); void setRewriteMethodCallClasses(Set<String> rewriteMethodCallClasses); void generate();  }