public ManagedGenBaseSupport(TypeDefinition t) { this.t = t; var regatt = t.CustomAttributes.FirstOrDefault(a => a.AttributeType.FullNameCorrected() == "Android.Runtime.RegisterAttribute"); is_acw = regatt != null; string jn = regatt != null ? ((string)regatt.ConstructorArguments [0].Value).Replace('/', '.') : t.FullNameCorrected(); int idx = jn.LastIndexOf('.'); pkg_name = idx < 0 ? String.Empty : jn.Substring(0, idx); java_name = SymbolTable.FilterPrimitiveFullName(t.FullNameCorrected()); if (java_name == null) { java_name = idx < 0 ? jn : jn.Substring(idx + 1); full_name = t.FullNameCorrected(); } else { var sym = SymbolTable.Lookup(java_name); full_name = sym != null ? sym.FullName : t.FullNameCorrected(); } java_name = java_name.Replace('$', '.'); type_parameters = GenericParameterDefinitionList.FromMetadata(t.GenericParameters); var obsolete = t.CustomAttributes.FirstOrDefault(ca => ca.AttributeType.FullName == "System.ObsoleteAttribute"); if (obsolete != null) { deprecated = true; deprecatedComment = obsolete.HasConstructorArguments ? obsolete.ConstructorArguments [0].Value.ToString() : "This class is obsoleted in this android platform"; } }
public bool Validate(CodeGenerationOptions opt, GenericParameterDefinitionList type_params) { if (ConstraintExpressions == null || ConstraintExpressions.Length == 0) { return(true); } if (validated) { return(is_valid); } var syms = new List <ISymbol> (); foreach (var c in ConstraintExpressions) { var sym = SymbolTable.Lookup(c, type_params); if (sym == null) { Report.Warning(0, Report.WarningGenericParameterDefinition + 0, "Unknown generic argument constraint type {0} {1}.", c, opt.ContextString); validated = true; return(false); } syms.Add(sym); } Constraints = syms.ToArray(); validated = is_valid = true; return(true); }
protected void GenerateAnnotationAttribute(CodeGenerationOptions opt, GenerationInfo gen_info) { if (ShouldGenerateAnnotationAttribute) { var baseName = Namespace.Length > 0 ? FullName.Substring(Namespace.Length + 1) : FullName; var attrClassNameBase = baseName.Substring(TypeNamePrefix.Length) + "Attribute"; var localFullName = Namespace + (Namespace.Length > 0 ? "." : string.Empty) + attrClassNameBase; gen_info.CurrentType = localFullName; StreamWriter sw = gen_info.Writer = gen_info.OpenStream(opt.GetFileName(localFullName)); sw.WriteLine("using System;"); sw.WriteLine(); sw.WriteLine("namespace {0} {{", Namespace); sw.WriteLine(); sw.WriteLine("\t[global::Android.Runtime.Annotation (\"{0}\")]", JavaName); sw.WriteLine("\t{0} partial class {1} : Attribute", this.Visibility, attrClassNameBase); sw.WriteLine("\t{"); // An Annotation attribute property is generated for each applicable annotation method, // where *applicable* means java annotation compatible types. See IsTypeCommensurate(). foreach (var method in Methods.Where(m => m.Parameters.Count == 0 && IsTypeCommensurate(SymbolTable.Lookup(m.RetVal.JavaName)))) { sw.WriteLine("\t\t[global::Android.Runtime.Register (\"{0}\"{1})]", method.JavaName, method.AdditionalAttributeString()); sw.WriteLine("\t\tpublic {0} {1} {{ get; set; }}", opt.GetOutputName(method.RetVal.FullName), method.Name); sw.WriteLine(); } sw.WriteLine("\t}"); sw.WriteLine("}"); sw.Close(); gen_info.Writer = null; } }
public bool Validate(CodeGenerationOptions opt, GenericParameterDefinitionList in_params) { if (validated) { return(is_valid); } validated = true; is_concrete = true; type_params = new ISymbol [java_params.Length]; for (int i = 0; i < java_params.Length; i++) { string tp = java_params [i].TrimStart(); var gpd = in_params != null?in_params.FirstOrDefault(t => t.Name == tp) : null; if (in_params != null && gpd != null) { type_params [i] = new GenericTypeParameter(gpd); is_concrete = false; continue; } else if (tp == "?") { if (in_params != null && in_params.Count == 1) { type_params [i] = new GenericTypeParameter(in_params [0]); is_concrete = false; } else { type_params [i] = new SimpleSymbol("null", "java.lang.Object", "object", "Ljava/lang/Object;"); } continue; } ISymbol psym = SymbolTable.Lookup(tp, in_params); if (psym == null || !psym.Validate(opt, in_params)) { return(false); } if (psym is GenericSymbol && !(psym as GenericSymbol).IsConcrete) { is_concrete = false; } type_params [i] = /*psym is IGeneric ? (psym as IGeneric).GetGenericType (null) :*/ psym; } managed = "<" + String.Join(", ", (from tp in type_params select tp.FullName).ToArray()) + ">"; is_valid = true; return(true); }
protected override bool OnValidate(CodeGenerationOptions opt, GenericParameterDefinitionList type_params) { if (validated) { return(is_valid); } validated = true; // We're validating this in prior to BaseType. if (TypeParameters != null && !TypeParameters.Validate(opt, type_params)) { return(false); } if (Char.IsNumber(Name [0])) { // it is an anonymous class which does not need output. is_valid = false; return(false); } base_symbol = IsAnnotation ? SymbolTable.Lookup("java.lang.Object") : BaseType != null?SymbolTable.Lookup(BaseType) : null; if (base_symbol == null && FullName != "Java.Lang.Object" && FullName != "System.Object") { Report.Warning(0, Report.WarningClassGen + 2, "Class {0} has unknown base type {1}.", FullName, BaseType); is_valid = false; return(false); } if ((base_symbol != null && !base_symbol.Validate(opt, TypeParameters)) || !base.OnValidate(opt, type_params)) { Report.Warning(0, Report.WarningClassGen + 3, "Class {0} has invalid base type {1}.", FullName, BaseType); is_valid = false; return(false); } List <Ctor> valid_ctors = new List <Ctor> (); foreach (Ctor c in ctors) { if (c.Validate(opt, TypeParameters)) { valid_ctors.Add(c); } } ctors = valid_ctors; return(true); }
public bool Validate(CodeGenerationOptions opt, GenericParameterDefinitionList type_params) { sym = SymbolTable.Lookup(type, type_params); if (sym == null) { Report.Warning(0, Report.WarningParameter + 0, "Unknown parameter type {0} {1}.", type, opt.ContextString); return(false); } if (!sym.Validate(opt, type_params)) { Report.Warning(0, Report.WarningParameter + 1, "Invalid parameter type {0} {1}.", type, opt.ContextString); return(false); } return(true); }
public bool Validate(CodeGenerationOptions opt, GenericParameterDefinitionList type_params) { sym = (IsEnumified ? SymbolTable.Lookup(managed_type, type_params) : null) ?? SymbolTable.Lookup(java_type, type_params); if (sym == null) { Report.Warning(0, Report.WarningReturnValue + 0, "Unknown return type {0} {1}.", java_type, opt.ContextString); return(false); } if (!sym.Validate(opt, type_params)) { Report.Warning(0, Report.WarningReturnValue + 1, "Invalid return type {0} {1}.", java_type, opt.ContextString); return(false); } return(true); }
public bool Validate (CodeGenerationOptions opt, GenericParameterDefinitionList type_params) { symbol = SymbolTable.Lookup (TypeName, type_params); if (symbol == null || !symbol.Validate (opt, type_params)) { Report.Warning (0, Report.WarningField + 0, "unexpected field type {0} {1}.", TypeName, opt.ContextString); return false; } setParameters = new ParameterList () { SetterParameter, }; if (!setParameters.Validate (opt, type_params)) throw new NotSupportedException ( string.Format ("Unable to generate setter parameter list {0}", opt.ContextString)); return true; }
public string GetGenericReturn(CodeGenerationOptions opt, string name, Dictionary <string, string> mappings) { string targetType = sym.GetGenericType(mappings); if (string.IsNullOrEmpty(targetType)) { return(name); } if (targetType == "string") { return(string.Format("{0}.ToString ()", name)); } var rgm = SymbolTable.Lookup(targetType) as IRequireGenericMarshal; return(string.Format("global::Java.Interop.JavaObjectExtensions.JavaCast<{0}>({1})", rgm != null ? (rgm.GetGenericJavaObjectTypeOverride() ?? sym.FullName) : sym.FullName, opt.GetSafeIdentifier(rgm != null ? rgm.ToInteroperableJavaObject(name) : name))); }
// This is not a perfect match with Java language specification http://docs.oracle.com/javase/specs/jls/se5.0/html/interfaces.html#9.7 // as it does not cover java.lang.Enum. Though C# attributes cannot handle JLE. // Class literal (FooBar.class) cannot be supported either. // We might be able to support System.Type for JLC and custom generated .NET Enums for JLE in the future. static bool IsTypeCommensurate(ISymbol sym) { if (sym == null) { return(false); } if (sym is StringSymbol) { return(true); } if (sym is SimpleSymbol) { switch (sym.JavaName) { case "boolean": case "char": case "byte": case "short": case "int": case "long": case "float": case "double": return(true); } } var arr = sym as ArraySymbol; if (arr != null) { return(IsTypeCommensurate(SymbolTable.Lookup(arr.ElementType))); } if (sym is GenericSymbol) { return(sym.JavaName == "java.lang.Class"); } // outside Mono.Android.dll they are ManagedClassGen. if (sym is ClassGen) { return(sym.JavaName == "java.lang.Class" || sym.JavaName == "java.lang.String"); } return(false); }
GenBase GetBaseGen() { if (this is InterfaceGen) { return(null); } if (this.BaseGen != null) { return(this.BaseGen); } if (this.BaseSymbol == null) { return(null); } var bg = SymbolTable.Lookup(this.BaseSymbol.FullName) as GenBase; if (bg != null && bg != this) { return(bg); } return(null); }
public string GetGenericCall(CodeGenerationOptions opt, Dictionary <string, string> mappings) { string targetType = sym.GetGenericType(mappings); if (string.IsNullOrEmpty(targetType)) { return(name); } if (targetType == "string") { return(string.Format("{0}.ToString ()", name)); } if (targetType.EndsWith("[]")) { return(string.Format("{0}.ToArray<{1}> ()", name, targetType.Replace("[]", ""))); } var rgm = SymbolTable.Lookup(targetType) as IRequireGenericMarshal; return(string.Format("global::Java.Interop.JavaObjectExtensions.JavaCast<{0}>({1})", opt.GetOutputName(rgm != null ? (rgm.GetGenericJavaObjectTypeOverride() ?? targetType) : targetType), name)); }
protected virtual bool OnValidate(CodeGenerationOptions opt, GenericParameterDefinitionList type_params) { if (Name.Length > TypeNamePrefix.Length && (Name [TypeNamePrefix.Length] == '.' || Char.IsDigit(Name [TypeNamePrefix.Length]))) // see bug #5111 { return(false); } if (!support.ValidateNamespace()) { return(false); } List <GenBase> valid_nests = new List <GenBase> (); foreach (GenBase gen in nested_types) { if (gen.Validate(opt, TypeParameters)) { valid_nests.Add(gen); } } nested_types = valid_nests; AdjustNestedTypeFullName(this); foreach (string iface_name in iface_names) { ISymbol isym = SymbolTable.Lookup(iface_name); if (isym != null && isym.Validate(opt, TypeParameters)) { ifaces.Add(isym); } else { if (isym == null) { Report.Warning(0, Report.WarningGenBase + 0, "For type {0}, base interface {1} does not exist.", FullName, iface_name); } else { Report.Warning(0, Report.WarningGenBase + 0, "For type {0}, base interface {1} is invalid.", FullName, iface_name); } iface_validation_failed = true; } } List <Field> valid_fields = new List <Field> (); foreach (Field f in fields) { if (!f.Validate(opt, TypeParameters)) { continue; } valid_fields.Add(f); } fields = valid_fields; int method_cnt = methods.Count; methods = methods.Where(m => ValidateMethod(opt, m)).ToList(); method_validation_failed = method_cnt != methods.Count; foreach (Method m in methods) { if (m.IsVirtual) { has_virtual_methods = true; } if (m.Name == "HashCode" && m.Parameters.Count == 0) { m.IsOverride = true; m.Name = "GetHashCode"; } jni_sig_hash [m.JavaName + m.JniSignature] = m; if ((m.Name == "ToString" && m.Parameters.Count == 0) || (BaseSymbol != null && BaseSymbol.ContainsMethod(m, true))) { m.IsOverride = true; } } return(true); }