예제 #1
0
            /// <summary>
            /// Default ctor
            /// </summary>
            internal AssemblyClasses(AssemblyDefinition assembly, Action <ClassSource> jarLoaded = null)
            {
                this.assembly = assembly;
                foreach (var attr in assembly.GetJavaCodeAttributes())
                {
                    var resourceName = (string)attr.ConstructorArguments[0].Value;
                    var fileName     = attr.ConstructorArguments.Count > 1 ? (string)attr.ConstructorArguments[1].Value : null;

                    JavaCode javaCode;
                    if (!javaCodes.TryGetValue(resourceName, out javaCode))
                    {
                        var resource = assembly.MainModule.Resources.FirstOrDefault(x => x.Name == resourceName) as EmbeddedResource;
                        if (resource == null)
                        {
                            throw new LoaderException("Cannot find resource " + resourceName);
                        }
                        javaCode = new JavaCode(resource, fileName);
                        javaCodes[resourceName] = javaCode;

                        if (jarLoaded != null)
                        {
                            jarLoaded(javaCode.ClassSource);
                        }

                        foreach (var classFileName in javaCode.Resolve(null).ClassFileNames)
                        {
                            var className = classFileName;
                            if (className.EndsWith(".class", StringComparison.OrdinalIgnoreCase))
                            {
                                className = className.Substring(0, className.Length - ".class".Length);
                            }
                            var jClass = new JavaClass(className, javaCode);
                            javaClasses[className] = jClass;
                        }
                    }
                }
                var scope = AssemblyResolver.IsFrameworkAssembly(assembly) ? AttributeConstants.Dot42Scope : assembly.FullName;
                var ignoreFromJavaTypes = new List <TypeDefinition>();

                foreach (var type in assembly.MainModule.Types)
                {
                    CollectDexImportClasses(type, className2DexImportMap, type2DexImportMap, null, scope, ignoreFromJavaTypes);
                }
                foreach (var type in ignoreFromJavaTypes)
                {
                    var       attr      = type.GetDexOrJavaImportAttribute();
                    var       className = (string)attr.ConstructorArguments[0].Value;
                    DexImport dexImport;
                    if (className2DexImportMap.TryGetValue(className, out dexImport))
                    {
                        dexImport.AddType(type);
                        type2DexImportMap[type] = dexImport;
                    }
                }
            }
            /// <summary>
            /// Default ctor
            /// </summary>
            internal AssemblyClasses(AssemblyDefinition assembly, Action<ClassSource> jarLoaded=null)
            {
                this.assembly = assembly;
                foreach (var attr in assembly.GetJavaCodeAttributes())
                {
                    var resourceName = (string)attr.ConstructorArguments[0].Value;
                    var fileName = attr.ConstructorArguments.Count > 1 ? (string)attr.ConstructorArguments[1].Value : null;

                    JavaCode javaCode;
                    if (!javaCodes.TryGetValue(resourceName, out javaCode))
                    {
                        var resource = assembly.MainModule.Resources.FirstOrDefault(x => x.Name == resourceName) as EmbeddedResource;
                        if (resource == null)
                            throw new LoaderException("Cannot find resource " + resourceName);
                        javaCode = new JavaCode(resource, fileName);
                        javaCodes[resourceName] = javaCode;

                        if (jarLoaded != null)
                            jarLoaded(javaCode.ClassSource);

                        foreach (var classFileName in javaCode.Resolve(null).ClassFileNames)
                        {
                            var className = classFileName;
                            if (className.EndsWith(".class", StringComparison.OrdinalIgnoreCase))
                            {
                                className = className.Substring(0, className.Length - ".class".Length);
                            }
                            var jClass = new JavaClass(className, javaCode);
                            javaClasses[className] = jClass;
                        }
                    }
                }
                var scope = AssemblyResolver.IsFrameworkAssembly(assembly) ? AttributeConstants.Dot42Scope : assembly.FullName;
                var ignoreFromJavaTypes = new List<TypeDefinition>();
                foreach (var type in assembly.MainModule.Types)
                {
                    CollectDexImportClasses(type, className2DexImportMap, type2DexImportMap, null, scope, ignoreFromJavaTypes);
                }
                foreach (var type in ignoreFromJavaTypes)
                {
                    var attr = type.GetDexOrJavaImportAttribute();
                    var className = (string)attr.ConstructorArguments[0].Value;
                    DexImport dexImport;
                    if (className2DexImportMap.TryGetValue(className, out dexImport))
                    {
                        dexImport.AddType(type);
                        type2DexImportMap[type] = dexImport;
                    }
                }
            }
예제 #3
0
 /// <summary>
 /// Resolve the payload into a class file.
 /// </summary>
 public ClassFile Resolve(IClassLoader loader, Action <ClassFile> classLoaded)
 {
     if (resolved == null)
     {
         // Load the class
         var raw = javaCode.Resolve(loader).GetResource(className + ".class");
         resolved = new ClassFile(new MemoryStream(raw), loader);
         if (classLoaded != null)
         {
             classLoaded(resolved);
         }
     }
     return(resolved);
 }