Kotlin and reflection on properties using kotlin-reflect - reflection

I am using kotlin-reflect for reflection on Kotlin data class
Definition is like this
#JsonIgnoreProperties(ignoreUnknown = true)
data class TopicConfiguration(
#JsonProperty("max.message.bytes") var maxMessageBytes: Long? = null,
#JsonProperty("compression.type") var compressionType: String? = null,
#JsonProperty("retention.ms") var retentionMs: Long? = null
)
I want to get #JsonProperty using reflection, but when I try
obj
.javaClass
.kotlin
.declaredMemberProperties
.first()
.findAnnotation<JsonProperty>()
Then I get null no matter what I try.
How do I access property annotations using reflection on Kotlin data class (namely #JsonProperty that's defined in jackson data-bind)

I just found an answer:
Using java-decompiler, it's apparent that annotations are not on fields or getters but on constructor parameters
public TopicConfiguration(#Nullable #JsonProperty("max.message.bytes") Long maxMessageBytes, #Nullable #JsonProperty("compression.type") String compressionType, #Nullable #JsonProperty("retention.ms") Long retentionMs)
{
this.maxMessageBytes = maxMessageBytes;this.compressionType = compressionType;this.retentionMs = retentionMs;
}
I was able to retrieve annotations when I used Kotlin's refection for constructor parameters
obj
.javaClass
.kotlin
.constructors
.first()
.parameters
.first()
.findAnnotation<JsonProperty>()

Related

Mix [JsonExtensionData] with MissingMemberHandling.Error with JSON.NET

In order to avoid losing JSON properties when deserializating to a POCO that is missing members, I use the [JsonExtensionData] attribute. Ex:
public class Foo
{
public int Y { get; set; }
[JsonExtensionData]
private IDictionary<string, JToken> _extraStuff;
}
That way, if I try to deserialize the following, I won't lose the z property:
{
"y": 1,
"z": "added in foo"
}
So far so good.
But in reality, I have a really deep object graph. So every POCO in the graph must use the [JsonExtensionData] attribute. This is a little dangerous. As soon as I forget to add this in one of the class, doing a deserialization followed by a serialization will lose data. (the real use case is doing a HTTP GET followed by a HTTP POST and I want to be sure that I don't lose anything)
So, to be sure that I haven't forgotten any [JsonExtensionData] in my whole POCO object tree, I thought about using the following deserializer setting:
var serializerSettings = new JsonSerializerSettings
{
MissingMemberHandling = MissingMemberHandling.Error
};
But then, if I try to deserialize the previous JSON, I get the following exception:
Could not find member 'z' on object of type 'Foo'. Path 'z', line 3, position 6.
This is a quite annoying, it complains about a field that has no member in the POCO but that is covered by the [JsonExtensionData] attribute.
Is there a way to only raise errors when data is actually data being lost during the deserialization?
You may mark your object with [JsonObject(MissingMemberHandling = MissingMemberHandling.Ignore)]. This will override the serializer setting:
[JsonObject(MissingMemberHandling = MissingMemberHandling.Ignore)]
public class Foo
{
public int Y { get; set; }
[JsonExtensionData]
private IDictionary<string, JToken> _extraStuff;
}
Demo fiddle #1 here.
Alternatively, you could create a custom contract resolver that does this automatically:
public class MissingMemberContractResolver : DefaultContractResolver
{
protected override JsonObjectContract CreateObjectContract(Type objectType)
{
var contract = base.CreateObjectContract(objectType);
if (contract.ExtensionDataSetter != null && contract.MissingMemberHandling == null)
{
contract.MissingMemberHandling = MissingMemberHandling.Ignore;
}
return contract;
}
}
Then use it as follow. First cache a copy somewhere for performance:
static IContractResolver contractResolver = new MissingMemberContractResolver
{
// Modify settings such as the naming strategy if required.
NamingStrategy = new CamelCaseNamingStrategy(),
};
And then set in settings as follows:
var serializerSettings = new JsonSerializerSettings
{
ContractResolver = contractResolver,
MissingMemberHandling = MissingMemberHandling.Error
};
var foo = JsonConvert.DeserializeObject<Foo>(json, serializerSettings);
var json2 = JsonConvert.SerializeObject(foo, Formatting.Indented, serializerSettings);
Demo fiddle #2 here.
Note that MissingMemberHandling was added to to JsonObjectAttribute and JsonObjectContract in Json.NET release 12.0.2. On earlier versions neither of the above solutions are available.
Honestly I'm a bit surprised this is necessary.
As an aside, if you're creating a custom contract resolver anyway, you could make DefaultContractResolver.CreateObjectContract() throw for any object in your assembly or namespace that lacks an ExtensionDataGetter and ExtensionDataSetter. If you do that you'll be able to discover any types that lack a [JsonExtensionData] during unit testing.

Return type for stored procedure returning ints

I have a stored procedure with a simple SELECT statement:
SELECT UserId FROM SomeTable
I find that I need to create a new object in order to wrap it in LINQ.
[FunctionAttribute(Name = "dbo.MyProc")]
[ResultType(typeof(ISingleResult<TestClass>))]
public ISingleResult<TestClass> GetMyProc([Parameter(Name = "SomeId", DbType = "Int")]int someId, [Parameter(Name = "AnotherId", DbType = "Int")]int anotherId)
{
var result = ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), someId, anotherId);
return (ISingleResult<TestClass>)(result.ReturnValue);
}
Had to add this class:
public class TestClass
{
[Column(Name = "UserId", DbType = "Int")]
public string UserId { get; set; }
}
This seems kind of ridiculous. Can I avoid creating TestClass, in this case?
Can I avoid creating TestClass, in this case?
Not with the framework you're currently using.
There are many other options out there. You could use ADO.NET directly; but then you're working with a DataTable instead of TestClass. You could use Dapper; where you'd still need TestClass but you wouldn't need GetMyProc. You could use a framework that generates the code, like an EF data model, or some other third-party framework.
But in general, the data needs marshaled into something.

Dynamic Instantiate in Dart

I need an object that makes instances of other objects. I want the ability to pass in the class of the objects being created, but they all need to have the same type, and it would be great if they could all start out with the same values:
class Cloner{
BaseType prototype;
BaseType getAnother(){
BaseType newthing = prototype.clone(); //but there's no clone() in Dart
newthing.callsomeBaseTypeMethod();
return newthing;
}
}
So, prototype could be set to any object that is of type BaseClass, even if it's something whose class is a subclass of BaseClass. I'm sure there's a way to do this with the mirrors library, but I just wanted to make sure I'm not missing some obvious built-in factory way to do it.
I could see how this could be set up with a generic: Cloner<T>, but then there's no way that we can make sure T is a subtype of BaseType at compile-time, right?
To get you started, you can create a small "constructor" function that returns new instances. Try this:
typedef BaseType Builder();
class Cloner {
Builder builder;
Cloner(Builder builder);
BaseType getAnother() {
BaseType newthing = builder();
newthing.callsomeBaseTypeMethod();
return newthing;
}
}
main() {
var cloner = new Cloner(() => new BaseType());
var thing = cloner.getAnother();
}
In the above code, we create a typedef to define a function that returns a BaseType.

How to call ValidationAttributes manually? (DataAnnotations and ModelState)

We have a need within some of our logic to iterate through the properties of a model to auto-bind properties and want to extend the functionality to include the new dataannotations in C# 4.0.
At the moment, I basically iterate over each property loading in all ValidationAttribute instances and attempting to validate using the Validate/IsValid function, but this doesn't seem to be working for me.
As an example I have a model such as:
public class HobbyModel
{
[Required(AllowEmptyStrings = false, ErrorMessage = "Do not allow empty strings")]
[DisplayName("Hobby")]
[DataType(DataType.Text)]
public string Hobby
{
get;
set;
}
}
And the code to check the attributes is:
object[] attributes = propertyInfo.GetCustomAttributes(true);
TypeConverter typeConverter =
TypeDescriptor.GetConverter(typeof(ValidationAttribute));
bool isValid = false;
foreach (object attr in attributes)
{
ValidationAttribute attrib = attr as ValidationAttribute;
if (attrib != null)
{
attrib.Validate(obj, propertyInfo.Name);
}
}
I've debugged the code and the model does have 3 attributes, 2 of which are derived from ValidationAttribute, but when the code passes through the Validate function (with a empty or null value) it does thrown an exception as expected.
I'm expecting I'm doing something silly, so am wondering whether anyone has used this functionality and could help.
Thanks in advance,
Jamie
This is because you are passing the source object to the Validate method, instead of the property value. The following is more likely to work as expected (though obviously not for indexed properties):
attrib.Validate(propertyInfo.GetValue(obj, null), propertyInfo.Name);
You would certainly have an easier time using the Validator class as Steven suggested, though.
You do use the System.ComponentModel.DataAnnotations.Validator class to validate objects.

Scala: set a field value reflectively from field name

I'm learning scala and can't find out how to do this:
I'm doing a mapper between scala objects and google appengine entities, so if i have a class like this:
class Student {
var id:Long
var name:String
}
I need to create an instance of that class, in java i would get the Field by it's name and then do field.set(object, value) but I can't find how to do so in scala.
I can't use java reflection since the fields of Student are seen as private and field.set throws an error because of that.
Thanks
Scala turns "var" into a private field, one getter and one setter. So in order to get/set a var by identifying it using a String, you need to use Java reflection to find the getter/setter methods. Below is a code snippet that does that. Please note this code runs under Scala 2.8.0 and handlings of duplicated method names and errors are nonexistent.
class Student {
var id: Long = _
var name: String = _
}
implicit def reflector(ref: AnyRef) = new {
def getV(name: String): Any = ref.getClass.getMethods.find(_.getName == name).get.invoke(ref)
def setV(name: String, value: Any): Unit = ref.getClass.getMethods.find(_.getName == name + "_$eq").get.invoke(ref, value.asInstanceOf[AnyRef])
}
val s = new Student
s.setV("name", "Walter")
println(s.getV("name")) // prints "Walter"
s.setV("id", 1234)
println(s.getV("id")) // prints "1234"

Resources