Ejemplo n.º 1
0
    public DotNetImporter(ModuleDefinition _module)
    {
        module       = _module;
        typeMap      = new Dictionary <string, TypeReference>();
        typeUse      = new HashSet <TypeReference>();
        funcIfcNames = new HashSet <string>();
        funcIfcTypes = new HashSet <TypeDefinition>();

        var typeSystem = module.TypeSystem;

        typeMap["./" + TypeCode.Empty]   = typeSystem.Void;
        typeMap["./" + TypeCode.Boolean] = typeSystem.Boolean;
        typeMap["./" + TypeCode.Byte]    = typeSystem.Byte;
        typeMap["./" + TypeCode.SByte]   = typeSystem.SByte;
        typeMap["./" + TypeCode.Char]    = typeSystem.Char;
        typeMap["./" + TypeCode.Int16]   = typeSystem.Int16;
        typeMap["./" + TypeCode.Int32]   = typeSystem.Int32;
        typeMap["./" + TypeCode.Int64]   = typeSystem.Int64;
        typeMap["./" + TypeCode.UInt16]  = typeSystem.UInt16;
        typeMap["./" + TypeCode.UInt32]  = typeSystem.UInt32;
        typeMap["./" + TypeCode.UInt64]  = typeSystem.UInt64;
        typeMap["./" + TypeCode.Single]  = typeSystem.Single;
        typeMap["./" + TypeCode.Double]  = typeSystem.Double;
        typeMap["java.lang.Object"]      = typeSystem.Object;
        typeMap["java.lang.String"]      = typeSystem.String;

        systemException = new TypeReference(
            "System", "Exception", module, typeSystem.CoreLibrary);

        methodRefNotImplementedException = new MethodReference(
            ".ctor", typeSystem.Void,
            new TypeReference(
                "System", "NotImplementedException",
                module, typeSystem.CoreLibrary));
        methodRefNotImplementedException.HasThis = true;

        Where = new JavaException.Where();
        Where.Push($"assembly '{module.Assembly.Name.Name}'");

        ImportCilTypes(module.Types);
    }
Ejemplo n.º 2
0
    public void Merge(List <JavaClass> classes)
    {
        var classes2 = new HashSet <JavaClass>(classes.Count);

        foreach (var jclass in classes)
        {
            if (CreateCilTypeForClass(jclass))
            {
                classes2.Add(jclass);
            }

            if (jclass.Flags == (JavaAccessFlags.ACC_INTERFACE
                                 | JavaAccessFlags.ACC_ABSTRACT
                                 | JavaAccessFlags.ACC_PUBLIC) &&
                jclass.Methods != null && jclass.Methods.Count == 1)
            {
                // add interface to set of potentially functional interfaces
                funcIfcNames.Add(jclass.Name);
            }
        }

        // arrange Java inner classes as CIL nested classes,
        // and add Java outer classes to the CIL module itself

        foreach (var jclass in classes2)
        {
            Where.Push($"class '{jclass.Name}'");

            var cilType = typeMap[jclass.Name] as TypeDefinition;
            AddClassToDeclaringType(cilType, jclass);

            Where.Pop();
        }

        // establish links between classes and their
        // super classes and interfaces

        foreach (var jclass in classes2)
        {
            Where.Push($"class '{jclass.Name}'");

            var cilType = typeMap[jclass.Name] as TypeDefinition;
            LinkCilTypesByClass(cilType, jclass);
            BuildCilTypeFromClass(cilType, jclass);

            Where.Pop();
        }

        // discard private types that are not referenced

        foreach (var cilType in typeMap.Values)
        {
            var cilTypeDef = cilType.Resolve();
            var visibility = cilTypeDef.Attributes & TypeAttributes.VisibilityMask;
            if (visibility == TypeAttributes.NotPublic ||
                visibility == TypeAttributes.NestedPrivate)
            {
                if (!typeUse.Contains(cilType))
                {
                    if (cilTypeDef.DeclaringType == null)
                    {
                        module.Types.Remove(cilTypeDef);
                    }
                    else
                    {
                        cilTypeDef.DeclaringType.NestedTypes.Remove(cilTypeDef);
                    }
                }
            }
        }

        // create artificial delegate types for functional interfaces

        foreach (var functionalInterface in funcIfcTypes)
        {
            Where.Push($"functional interface '{functionalInterface}'");

            BuildDelegate(functionalInterface);

            Where.Pop();
        }
    }