package com.tns; import android.os.Build; import android.util.Log; import com.tns.bindings.AnnotationDescriptor; import com.tns.bindings.DefaultMethodBodies; import com.tns.bindings.DefaultMethods; import com.tns.bindings.Dump; import com.tns.bindings.ProxyGenerator; import com.tns.bindings.desc.ClassDescriptor; import com.tns.bindings.desc.reflection.ClassInfo; import com.tns.system.classes.loading.ClassStorageService; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.InvalidClassException; import java.io.OutputStreamWriter; import java.lang.reflect.Array; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import dalvik.system.BaseDexClassLoader; import dalvik.system.DexClassLoader; public class DexFactory { static final String COM_TNS_GEN_PREFIX = "com.tns.gen."; private final Logger logger; private final File dexDir; private final File odexDir; private final String dexThumb; private final ClassLoader classLoader; private final ClassStorageService classStorageService; private final boolean injectIntoParentClassLoader; private ProxyGenerator proxyGenerator; private HashMap> injectedDexClasses = new HashMap>(); // Process-wide: injection puts a generated class in the app's class loader, where every // runtime's lookup of a prebuilt class by that name then finds it. private static final Set generatedClassNames = Collections.newSetFromMap(new ConcurrentHashMap()); DexFactory(Logger logger, ClassLoader classLoader, File dexBaseDir, String dexThumb, ClassStorageService classStorageService) { this(logger, classLoader, dexBaseDir, dexThumb, classStorageService, false); } DexFactory(Logger logger, ClassLoader classLoader, File dexBaseDir, String dexThumb, ClassStorageService classStorageService, boolean injectIntoParentClassLoader) { this.logger = logger; this.classLoader = classLoader; this.dexDir = dexBaseDir; this.dexThumb = dexThumb; this.injectIntoParentClassLoader = injectIntoParentClassLoader; this.odexDir = new File(this.dexDir, "odex"); this.proxyGenerator = new ProxyGenerator(this.dexDir.getAbsolutePath()); ProxyGenerator.IsLogEnabled = logger.isEnabled(); if (!dexDir.exists()) { dexDir.mkdirs(); } if (!odexDir.exists()) { odexDir.mkdir(); } this.updateDexThumbAndPurgeCache(); this.proxyGenerator.setProxyThumb(this.dexThumb); this.classStorageService = classStorageService; } static long totalGenTime = 0; static long totalMultiDexTime = 0; static long totalLoadDexTime = 0; public Class resolveClass(String baseClassName, String name, String className, String[] methodOverrides, String[] implementedInterfaces, boolean isInterface) throws ClassNotFoundException, IOException { // The interface's class name plus this suffix asks for the class that also stubs its // default methods. A prebuilt class of the interface is used instead when there is one. // Only native asks for it, without a base class name; an extend() name the script // chose always comes with the base class's. boolean overridesDefaults = isInterface && baseClassName.isEmpty() && className.startsWith(COM_TNS_GEN_PREFIX) && className.endsWith(Dump.DEFAULT_OVERRIDING_SUFFIX); String requestedClassName = className; if (overridesDefaults) { className = className.substring(0, className.length() - Dump.DEFAULT_OVERRIDING_SUFFIX.length()); } String fullClassName = className.replace("$", "_"); String originalFullClassName = fullClassName; // The static binding generator keeps a `$` from the script's file name in an // anonymous extend's class name; the runtime already replaced the base class's `$`. Class pregeneratedClass = null; if (!fullClassName.equals(className)) { pregeneratedClass = loadPregeneratedClass(className.replace("-", "_")); } if (pregeneratedClass == null) { pregeneratedClass = loadPregeneratedClass(fullClassName.replace("-", "_")); } // Once injected, the class generated for the interface loads by the same name. if (overridesDefaults && pregeneratedClass != null && generatedClassNames.contains(pregeneratedClass.getName())) { pregeneratedClass = null; } if (pregeneratedClass != null) { return pregeneratedClass; } if (overridesDefaults) { // Cached under the name native asks for, whichever class answers it. originalFullClassName = fullClassName + Dump.DEFAULT_OVERRIDING_SUFFIX; } // new: com.tns.gen.android.widget.DatePicker_MyActivity_59_56_ // old: com.tns.tests.Button1_fMyActivity_l56_c44__MyButton // ne1: com.tns.tests.Button1_MyActivity_58_44_MyButton_0 Class existingClass = this.injectedDexClasses.get(originalFullClassName); if (existingClass != null) { return existingClass; } DefaultMethodBodies bodies = null; if (overridesDefaults) { bodies = DefaultSuperCalls.bodies(Class.forName(this.getClassToProxyName(className))); fullClassName += bodies.classSuffix; } String classToProxy; if (!baseClassName.isEmpty()) { classToProxy = this.getClassToProxyName(baseClassName); } else { classToProxy = this.getClassToProxyName(className); } // strip the `com.tns.gen` off the base extended class name String desiredDexClassName = this.getClassToProxyName(fullClassName); if (overridesDefaults) { // The same file name ProxyGenerator writes the class under. desiredDexClassName = this.getClassToProxyName(className.replace("$", "_")) + ProxyGenerator.defaultOverridingFileSuffix(DefaultMethods.collect(Class.forName(classToProxy)), bodies); } // A named proxy (`Base.extend('a.b.C', {...})` / @JavaProxy) asks for // exactly that Java class name; the substitutions below are for the // anonymous form only, where the name is derived from the base. boolean isNamedProxy = !fullClassName.startsWith(COM_TNS_GEN_PREFIX) && fullClassName.contains("."); // when interfaces are extended as classes, we still want to preserve // just the interface name without the extra file, line, column information if (!baseClassName.isEmpty() && isInterface && !isNamedProxy) { fullClassName = COM_TNS_GEN_PREFIX + classToProxy; } // The thumb only changes on reinstall, so a cache key of name + thumb // cannot see an edit to the proxy's contents: under HMR a named // proxy's new method overrides would silently load the previous dex. // The digest carries the contents into the file name. String contentDigest = computeContentDigest(classToProxy, methodOverrides, implementedInterfaces, isInterface); File dexFile = this.getDexFile(desiredDexClassName, contentDigest); // generate dex file if (dexFile == null) { long startGenTime = System.nanoTime(); if (logger.isEnabled()) { logger.write("generating proxy in place"); } String dexFilePath; if (isInterface && !isNamedProxy) { dexFilePath = this.generateDex(name, contentDigest, classToProxy, methodOverrides, implementedInterfaces, isInterface, bodies); } else { dexFilePath = this.generateDex(desiredDexClassName, contentDigest, classToProxy, methodOverrides, implementedInterfaces, isInterface, null); } dexFile = new File(dexFilePath); long stopGenTime = System.nanoTime(); totalGenTime += stopGenTime - startGenTime; if (logger.isEnabled()) { logger.write("Finished inplace gen took: " + (stopGenTime - startGenTime) / 1000000.0 + "ms"); logger.write("TotalGenTime: " + totalGenTime / 1000000.0 + "ms"); } } // creates jar file from already generated dex file String jarFilePath = dexFile.getPath().replaceFirst("\\.dex$", ".jar"); File jarFile = new File(jarFilePath); if (!jarFile.exists()) { FileOutputStream jarFileStream = new FileOutputStream(jarFile); ZipOutputStream out = new ZipOutputStream(jarFileStream); out.putNextEntry(new ZipEntry("classes.dex")); byte[] dexData = new byte[(int) dexFile.length()]; FileInputStream fi = new FileInputStream(dexFile); fi.read(dexData, 0, dexData.length); fi.close(); out.write(dexData); out.closeEntry(); out.close(); } jarFile.setReadOnly(); Class result = null; String classNameToLoad = isInterface ? fullClassName : desiredDexClassName; boolean injectIntoParent = injectIntoParentClassLoader && classLoader instanceof BaseDexClassLoader; if (injectIntoParent && ProxyRegistrationGc.isRequired()) { // The dex is registered by the first loadClass below, so the blocking // collection has to stay immediately before it. java.lang.Runtime.getRuntime().gc(); } if (injectIntoParent && injectDexIntoClassLoader((BaseDexClassLoader) classLoader, jarFilePath)) { try { result = classLoader.loadClass(classNameToLoad); } catch (ClassNotFoundException e) { // fall through to the isolated DexClassLoader below } } if (result == null) { DexClassLoader dexClassLoader = new DexClassLoader(jarFilePath, this.odexDir.getAbsolutePath(), null, classLoader); result = dexClassLoader.loadClass(classNameToLoad); } if (bodies == DefaultMethodBodies.INVOKE_SUPER && !DefaultSuperCalls.verify(result)) { return resolveClass(baseClassName, name, requestedClassName, methodOverrides, implementedInterfaces, isInterface); } generatedClassNames.add(result.getName()); classStorageService.storeClass(result.getName(), result); this.injectedDexClasses.put(originalFullClassName, result); return result; } public Class findClass(String className) throws ClassNotFoundException { String canonicalName = className.replace('/', '.'); if (logger.isEnabled()) { logger.write(canonicalName); } Class existingClass = this.injectedDexClasses.get(canonicalName); if (existingClass != null) { return existingClass; } String underscored = canonicalName.replace('$', '_'); if (!underscored.equals(canonicalName)) { existingClass = this.injectedDexClasses.get(underscored); if (existingClass != null) { return existingClass; } } try { return classLoader.loadClass(canonicalName); } catch (ClassNotFoundException e) { if (!underscored.equals(canonicalName)) { return classLoader.loadClass(underscored); } throw e; } } public static String strJoin(String[] array, String separator) { if (array == null) { return ""; } StringBuilder sbStr = new StringBuilder(); for (int i = 0, il = array.length; i < il; i++) { if (i > 0) { sbStr.append(separator); } sbStr.append(array[i]); } return sbStr.toString(); } private Class loadPregeneratedClass(String name) { try { if (logger.isEnabled()) { logger.write("getting pre-generated proxy class with name: " + name); } Class pregeneratedClass = classLoader.loadClass(name); if (logger.isEnabled()) { logger.write("Pre-generated class found: " + name); } return pregeneratedClass; } catch (Exception e) { if (logger.isEnabled()) { logger.write("Pre-generated class not found: " + name); } return null; } } private String getClassToProxyName(String className) throws InvalidClassException { String classToProxy = className; if (className.startsWith(COM_TNS_GEN_PREFIX)) { classToProxy = className.substring(12); } if (classToProxy.startsWith(COM_TNS_GEN_PREFIX)) { throw new InvalidClassException("Can't generate proxy of proxy"); } return classToProxy; } /** * Digest of everything that shapes the generated proxy besides its name, * so the dex cache key changes when the proxy's contents do. Sorted, so * JS property-enumeration order cannot produce a spurious miss. */ private static String computeContentDigest(String classToProxy, String[] methodOverrides, String[] implementedInterfaces, boolean isInterface) { StringBuilder canonical = new StringBuilder(classToProxy).append('\n').append(isInterface); if (methodOverrides != null) { String[] sortedOverrides = methodOverrides.clone(); java.util.Arrays.sort(sortedOverrides); for (String override : sortedOverrides) { canonical.append('\n').append(override); } } if (implementedInterfaces != null) { String[] sortedInterfaces = implementedInterfaces.clone(); java.util.Arrays.sort(sortedInterfaces); for (String iface : sortedInterfaces) { canonical.append('').append(iface); } } try { byte[] hash = java.security.MessageDigest.getInstance("SHA-256") .digest(canonical.toString().getBytes(java.nio.charset.StandardCharsets.UTF_8)); StringBuilder hex = new StringBuilder(10); for (int i = 0; i < 5; i++) { hex.append(String.format("%02x", hash[i])); } return hex.toString(); } catch (java.security.NoSuchAlgorithmException e) { // SHA-256 is mandatory on Android; a digestless key only loses // cache freshness, never correctness of a fresh generation. return null; } } private File getDexFile(String className, String contentDigest) throws InvalidClassException { String classToProxyFile = className.replace("$", "_"); if (this.dexThumb != null) { classToProxyFile += "-" + this.dexThumb; } if (contentDigest != null) { classToProxyFile += "-" + contentDigest; } String dexFilePath = dexDir + "/" + classToProxyFile + ".dex"; File dexFile = new File(dexFilePath); if (dexFile.exists()) { if (logger.isEnabled()) { logger.write("Looking for proxy file: " + dexFilePath + " Result: proxy file Found. ClassName: " + className); } return dexFile; } if (logger.isEnabled()) { logger.write("Looking for proxy file: " + dexFilePath + " Result: NOT Found. Proxy Gen needed. ClassName: " + className); } return null; } // defaultBodies: those of the default-overriding class of the interface className; null for // any other class. private String generateDex(String proxyName, String contentDigest, String className, String[] methodOverrides, String[] implementedInterfaces, boolean isInterface, DefaultMethodBodies defaultBodies) throws ClassNotFoundException, IOException { boolean overridesDefaults = defaultBodies != null; Class classToProxy = Class.forName(className); HashSet methodOverridesSet = null; HashSet implementedInterfacesSet = new HashSet(); List> implementedInterfaceClasses = new ArrayList>(); if (methodOverrides != null) { methodOverridesSet = new HashSet(); for (int i = 0; i < methodOverrides.length; i++) { String methodOverride = methodOverrides[i]; methodOverridesSet.add(methodOverride); } } if (implementedInterfaces.length > 0) { for (int j = 0; j < implementedInterfaces.length; j++) { if (!implementedInterfaces[j].isEmpty()) { Class implementedInterface = Class.forName(implementedInterfaces[j]); implementedInterfacesSet.add(new ClassInfo(implementedInterface)); implementedInterfaceClasses.add(implementedInterface); } } } AnnotationDescriptor[] annotations = null; List defaultMethods = null; if (overridesDefaults) { defaultMethods = DefaultMethods.collect(classToProxy); } else if ((!classToProxy.isInterface() || proxyName.contains(".")) && Build.VERSION.SDK_INT >= 24) { // A named interface implementation is one extend() call's class, so like a class // it stubs only the default methods its implementation object names. List inheritedDefaults = classToProxy.isInterface() ? DefaultMethods.collect(classToProxy) : DefaultMethods.collect(classToProxy, implementedInterfaceClasses); defaultMethods = new ArrayList(); for (Method inherited : inheritedDefaults) { if (methodOverridesSet == null || methodOverridesSet.contains(inherited.getName())) { defaultMethods.add(inherited); } } } return proxyGenerator.generateProxy(proxyName, contentDigest, new ClassInfo(classToProxy), methodOverridesSet, implementedInterfacesSet, isInterface, annotations, defaultMethods, overridesDefaults ? defaultBodies : DefaultMethodBodies.INVOKE_SUPER); } private void updateDexThumbAndPurgeCache() { if (this.dexThumb == null) { throw new RuntimeException("Error generating proxy thumb 1"); } String oldDexThumb = this.getCachedProxyThumb(this.dexDir); if (this.dexThumb.equals(oldDexThumb)) { return; } if (oldDexThumb != null) { this.purgeDexesByThumb(oldDexThumb, this.dexDir); this.purgeDexesByThumb(oldDexThumb, this.odexDir); } else { // purge all dex files if no thumb file is found. This is crucial for CLI livesync purgeAllProxies(); } this.saveNewDexThumb(this.dexThumb, this.dexDir); } public void purgeAllProxies() { this.purgeDexesByThumb(null, this.dexDir); this.purgeDexesByThumb(null, this.odexDir); } private void saveNewDexThumb(String newDexThumb, File dexDir) { File cachedThumbFile = new File(dexDir, "proxyThumb"); try { FileOutputStream out = new FileOutputStream(cachedThumbFile, false); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out)); try { writer.write(newDexThumb); writer.newLine(); writer.flush(); } finally { writer.close(); out.close(); } } catch (FileNotFoundException e) { Log.w("JS", String.format("Error while writing current proxy thumb: %s", e.getMessage())); if (com.tns.Runtime.isDebuggable()) { e.printStackTrace(); } } catch (IOException e) { Log.w("JS", String.format("Error while writing current proxy thumb: %s", e.getMessage())); if (com.tns.Runtime.isDebuggable()) { e.printStackTrace(); } } } private void purgeDexesByThumb(String cachedDexThumb, File pathToPurge) { if (!pathToPurge.exists()) { return; } if (!pathToPurge.isDirectory()) { logger.write("Purge proxies path not a directory. Path: " + pathToPurge); throw new RuntimeException("Purge path not a directory"); } String[] children = pathToPurge.list(); for (int i = 0; i < children.length; i++) { String filename = children[i]; File purgeCandidate = new File(pathToPurge, filename); if (purgeCandidate.isDirectory()) { this.purgeDexesByThumb(cachedDexThumb, purgeCandidate); } else { if (cachedDexThumb != null && !filename.contains(cachedDexThumb)) { continue; } if (!purgeCandidate.delete()) { logger.write("Error purging cached proxy file: " + purgeCandidate.getAbsolutePath()); } } } } private String getCachedProxyThumb(File proxyDir) { try { File cachedThumbFile = new File(proxyDir, "proxyThumb"); if (cachedThumbFile.exists()) { FileInputStream in = new FileInputStream(cachedThumbFile); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); String cachedThumb = reader.readLine(); reader.close(); in.close(); return cachedThumb; } } catch (FileNotFoundException e) { Log.w("JS", String.format("Error while writing current proxy thumb: %s", e.getMessage())); if (com.tns.Runtime.isDebuggable()) { e.printStackTrace(); } } catch (IOException e) { Log.w("JS", String.format("Error while writing current proxy thumb: %s", e.getMessage())); if (com.tns.Runtime.isDebuggable()) { e.printStackTrace(); } } return null; } /** * Injects a DEX jar into the app's PathClassLoader so that classes in it are * findable by Class.forName(). This is needed because Android framework components * (e.g. FragmentFactory) use Class.forName() to instantiate classes by name, but * NativeScript's dynamically-generated classes normally live in isolated DexClassLoaders * that Class.forName() doesn't search. * * The jar must be added through the target class loader's own DexPathList so that * the resulting DexFile has the PathClassLoader as its only owner. Opening the jar * through a separate DexClassLoader first and splicing its dex element would leave * the same DexFile claimed by two loaders, which ART rejects on non-debuggable * builds with "Attempt to register dex file ... with multiple class loaders". * * @return true if the jar was injected and the class can be loaded through the * target class loader, false if the caller should fall back to an * isolated DexClassLoader. */ private boolean injectDexIntoClassLoader(BaseDexClassLoader targetClassLoader, String jarFilePath) { try { if (Build.VERSION.SDK_INT >= 24) { // BaseDexClassLoader.addDexPath exists since API 24 Method addDexPath = BaseDexClassLoader.class.getDeclaredMethod("addDexPath", String.class); addDexPath.setAccessible(true); addDexPath.invoke(targetClassLoader, jarFilePath); } else { appendDexElements(targetClassLoader, jarFilePath); } return true; } catch (Exception e) { Log.w("JS", "Failed to inject dex into parent classloader: " + e); return false; } } /** * Pre API 24 equivalent of BaseDexClassLoader.addDexPath: builds the dex elements * through DexPathList's static factory methods (so no temporary class loader is * involved) and splices them into the target loader's dexElements array. This is * the same technique MultiDex used on these OS versions. */ private void appendDexElements(BaseDexClassLoader targetClassLoader, String jarFilePath) throws Exception { Field pathListField = BaseDexClassLoader.class.getDeclaredField("pathList"); pathListField.setAccessible(true); Object pathList = pathListField.get(targetClassLoader); ArrayList files = new ArrayList(); files.add(new File(jarFilePath)); ArrayList suppressedExceptions = new ArrayList(); Object newElements; if (Build.VERSION.SDK_INT >= 23) { Method makePathElements = pathList.getClass().getDeclaredMethod("makePathElements", List.class, File.class, List.class); makePathElements.setAccessible(true); newElements = makePathElements.invoke(null, files, this.odexDir, suppressedExceptions); } else { Method makeDexElements = pathList.getClass().getDeclaredMethod("makeDexElements", ArrayList.class, File.class, ArrayList.class); makeDexElements.setAccessible(true); newElements = makeDexElements.invoke(null, files, this.odexDir, suppressedExceptions); } if (!suppressedExceptions.isEmpty()) { throw suppressedExceptions.get(0); } Field dexElementsField = pathList.getClass().getDeclaredField("dexElements"); dexElementsField.setAccessible(true); Object oldElements = dexElementsField.get(pathList); int oldLen = Array.getLength(oldElements); int newLen = Array.getLength(newElements); Object merged = Array.newInstance(oldElements.getClass().getComponentType(), oldLen + newLen); System.arraycopy(oldElements, 0, merged, 0, oldLen); System.arraycopy(newElements, 0, merged, oldLen, newLen); dexElementsField.set(pathList, merged); } }