Esempio n. 1
0
 /// <summary>
 /// Constructor.
 /// </summary>
 public JavaClass(
     Type type,
     JavaClass baseClass,
     IList<JavaClassProperty> properties)
 {
     Type = type;
     BaseClass = baseClass;
     Properties = properties;
 }
Esempio n. 2
0
        /// <summary>
        /// Returns the full java model for the object graph
        /// rooted at RootObject.
        /// </summary>
        public static IList<JavaClass> GetFullJavaModel()
        {
            var baseClass = new JavaClass
            (
                typeof(BaseChildObject),
                null /*baseClass*/,
                Collections.CreateList
                (
                    new JavaClassProperty("StringProp", "String", isCollection: false, javaClass: null)
                )
            );

            var derivedClass1 = new JavaClass
            (
                typeof(DerivedChildObject1),
                baseClass,
                Collections.CreateList
                (
                    new JavaClassProperty("IntProp", "int", isCollection: false, javaClass: null)
                )
            );

            var derivedClass2 = new JavaClass
            (
                typeof(DerivedChildObject2),
                baseClass,
                Collections.CreateList
                (
                    new JavaClassProperty("BoolProp", "boolean", isCollection: false, javaClass: null)
                )
            );

            var rootClass1 = new JavaClass
            (
                typeof(RootObject),
                null /*baseClass*/,
                Collections.CreateList
                (
                    new JavaClassProperty("StringProp", "String", isCollection: false, javaClass: null),
                    new JavaClassProperty("IntProp", "int", isCollection: false, javaClass: null),
                    new JavaClassProperty("BoolProp", "boolean", isCollection: false, javaClass: null),
                    new JavaClassProperty("SingleBaseChildObject", "BaseChildObject", isCollection: false, javaClass: baseClass),
                    new JavaClassProperty("BaseChildObjectList", "List<BaseChildObject>", isCollection: true, javaClass: baseClass),
                    new JavaClassProperty("SingleDerivedChildObject", "DerivedChildObject1", isCollection: false, javaClass: derivedClass1),
                    new JavaClassProperty("DerivedChildObjectList", "List<DerivedChildObject2>", isCollection: true, javaClass: derivedClass2)
                )
            );

            return Collections.CreateList
            (
                rootClass1,
                baseClass,
                derivedClass1,
                derivedClass2
            );
        }
        /// <summary>
        /// Verifies that the resulting java class matches our expectations.
        /// </summary>
        private void VerifyJavaClass(
            JavaClass expectedJavaClass,
            JavaClass actualJavaClass)
        {
            Assert.Equal(expectedJavaClass.Type, actualJavaClass.Type);
            Assert.Equal(expectedJavaClass.ClassName, actualJavaClass.ClassName);
            Assert.Equal(expectedJavaClass.AbstractClass, actualJavaClass.AbstractClass);
            Assert.Equal(expectedJavaClass.BaseClass?.ClassName, actualJavaClass.BaseClass?.ClassName);

            var expectedJavaProps = expectedJavaClass.Properties;
            var actualJavaProps = actualJavaClass.Properties;

            Assert.Equal(expectedJavaProps.Count, actualJavaProps.Count);

            foreach (var actualJavaProp in actualJavaClass.Properties)
            {
                var expectedJavaProp = expectedJavaProps
                    .Single(c => c.Name == actualJavaProp.Name);

                VerifyJavaProperty(expectedJavaProp, actualJavaProp);
            }
        }
        /// <summary>
        /// Adds a parameter value.
        /// </summary>
        private void AddParameterValue(JavaClass javaClass, object obj, JavaClassProperty prop, bool last)
        {
            var separator = last ? "" : ",";
            var rawValue = javaClass.Type.GetProperty(prop.Name).GetValue(obj);

            if (prop.IsCollection)
            {
                AddCollectionParameterValue(prop, rawValue, separator);
            }
            else if (prop.JavaClass != null)
            {
                GenerateConstructorInvocation(rawValue, prefix: $"/* {prop.Name}: */ ", suffix: separator);
            }
            else if (prop.JavaType == "String")
            {
                AddStringParameterValue(prop, rawValue, separator);
            }
            else if (prop.JavaType == "int")
            {
                AddPrimitiveParameterValue(prop, rawValue.ToString(), separator);
            }
            else if (prop.JavaType == "boolean")
            {
                AddPrimitiveParameterValue(prop, rawValue.Equals(true) ? "true" : "false", separator);
            }
            else
            {
                throw new InvalidOperationException("Unsupported property type.");
            }
        }
        /// <summary>
        /// Returns a list of all properties in the inheritance hierarchy.
        /// </summary>
        private IEnumerable<JavaClassProperty> GetAllProperties(JavaClass javaClass)
        {
            if (javaClass.BaseClass != null)
            {
                foreach (var prop in GetAllProperties(javaClass.BaseClass))
                {
                    yield return prop;
                }
            }

            foreach (var prop in javaClass.Properties)
            {
                yield return prop;
            }
        }
        /// <summary>
        /// Adds all parameter values for the constructor invocation.
        /// </summary>
        private void AddParameterValues(JavaClass javaClass, object obj)
        {
            var allProps = GetAllProperties(javaClass).ToList();
            var lastProp = allProps[allProps.Count - 1];

            foreach (var prop in allProps)
            {
                AddParameterValue(javaClass, obj, prop, last: prop == lastProp);
            }
        }
		/// <summary>
		/// Returns a mock java class definition generator.
		/// </summary>
		private static IJavaClassDefinitionGenerator GetMockJavaClassDefinitionGenerator(
			JavaFileBuilder builder,
			JavaClass javaClass)
		{
			var generator = new Mock<IJavaClassDefinitionGenerator>();

			generator
				.Setup
				(
					g => g.GenerateClassDefinition()
				)
				.Callback
				(
					() => builder.AddLine(javaClass.ClassName)
				);

			return generator.Object;
		}
 /// <summary>
 /// Creates a java class definition generator.
 /// </summary>
 public IJavaClassDefinitionGenerator CreateJavaClassDefinitionGenerator(
     JavaFileBuilder fileBuilder,
     JavaClass javaClass)
 {
     return new JavaClassDefinitionGenerator(fileBuilder, javaClass);
 }
        /// <summary>
        /// Returns a string containing the parameters of the constructor signature.
        /// </summary>
        private string GetConstructorParams(JavaClass javaClass, bool includeTypes)
        {
            var curClassParams = string.Join(
                ", ",
                javaClass.Properties.Select(prop => $"{(includeTypes ? $"{prop.JavaType} " : "")}{prop.Name.ToCamelCase()}"));

            var baseClassParams = javaClass.BaseClass != null
                ? GetConstructorParams(javaClass.BaseClass, includeTypes)
                : null;

            if (!string.IsNullOrEmpty(baseClassParams))
                return $"{baseClassParams}, {curClassParams}";
            else
                return curClassParams;
        }
 /// <summary>
 /// Constructor.
 /// </summary>
 public JavaClassDefinitionGenerator(JavaFileBuilder builder, JavaClass javaClass)
 {
     _builder = builder;
     _javaClass = javaClass;
 }
 /// <summary>
 /// Returns an accessor for the string.
 /// </summary>
 private IEnumerable<string> GetTypeAccessor(JavaClass javaClass)
 {
     if (!javaClass.AbstractClass && javaClass.BaseClass != null)
         yield return $"public String getType() {{ return \"{javaClass.ClassName}\"; }}";
 }