In order to create a simple annotation that logs function calls, I'm trying to grab the following attributes from a function that has said annotation:
Function name
Parameter names
Parameter values
What I have so far uses KCallable as a value, which makes grabbing the name and names from the list of KParameter fairly simple. However, I cannot figure out how to get the values of said parameters to make the log statement more contextual.
Does anyone have ideas on grabbing these parameters values within the annotation? It doesn't need to use KCallable, that just seemed like the most intuitive receiver.
You will need a different approach. Annotations and parameter type are a compile time features while values are a runtime feature.
What you will have to do is use a bytecode processing framework like ASM or google "aspect oriented programming". That allows you to examine the generated bytecode and modify if before the JVM tries to execute it.
The other approach is to write a Kotlin compiler plugin which generates the necessary code (google "Writing Your First Kotlin Compiler Plugin")
This blog post contains an example for Java and Spring using the AOP approach: https://solocoding.dev/blog/eng_spring_centrlize_logging_with_aop
I recommend the compiler plugin because the other approach is much more complicated, brittle and badly documented. Use AOP only if you find a framework which already contains all the features you need.
Related
Poco supports HMACEngine with different hash functions. For example to create a HMAC-SHA1 function I can use HMACEngine<SHA1Engine> hmca_sha1("secret");
This works well with DigestEngine like SHA1Engine and MD5Engine that have a constructor without argument.
Is it possible to use HMACEngine with hash functions from OpenSSL as they are provided by Poco::Crypto::DigestEngine?
The problem is that these functions take a string parameter in their constructor that specifies which hashing algorithm to use. This means HMACEngine<Poco::Crypto:DigestEngine> hmca_xxx("secret"); wouldn't work.
Any idea how to do this?
I think it will require some coding on Poco side. HMAC is accepting any class as template argument, and hoping it is a Digest class ans using methods like digest() that may not even exist. The HMACEngine that instantiates the class as template, so you can´t passa anything to its constructor as it is. In fact I don´t think that is a good use of templates, while I´m not very familiar with using this C++ resource yet, I think one should not expect anything to be available from templated class.
Probably a new HMACEngine for the Crypto module (instead of Foundation module) would be good. But looks like an approach more like the Poco::Crypto::RSADigestEngine would be better than the current template based one. I´ve recently modified Poco::Crypto::RSADigestEngine to be based on Poco::Crypto::DigestEngine instead off old Foundation Poco::DigestEngine, and therefore for next release it will also support all hashes that OpenSSL support. Basically RSADigestEngine creates an instance of a Poco::Crypto::DigestEngine as base and uses it.
I could help on a patch proposal if you create one at github. Poco::Crypto does need some updates :)
I have haxe code like this:
var fn:String->Int = function(s:String):Int{
return 1;
}
getParameterType(fn,1);//Should return String as it is first parameter of fn
what should function getParameterType code look like?
The Haxe Reflection APIs can be found here: Reflect and Type.
Looking through them, there doesn't seem to be any way to check the types of arguments on functions, probably because this information isn't available at runtime on many of the platforms. Javascript for instance is loosely typed, and the information you are looking for is not included by default.
So you have three options:
Use RTTI (Run Time Type Info). If a class is marked with #:rtti metadata (in Haxe 3, or if it implements haxe.rtti.Infos in Haxe 2), then information about that class, including the types of function parameters, is available in Xml format at run time. You will have to look at the Xml to figure out what the argument is. This will only work for functions which are attached to classes though, it won't work for anonymous functions.
Use macros. This is outside the scope of my answer, but maybe ask on the Haxe mailing list if you need help :) If the argument type is known at compile time, it can be made known to macros, and you can probably save that information back somewhere so it is available at run time.
Figure out another usage that doesn't require you to know the type :)
Of course, if you only need the information at compile-time, not at run-time, you can do: $type(fn) anywhere in your code and when you compile it will let you know the exact type signature of "fn".
I try to set up a web based application using spring and xslt. Since i always use xslt in a pipelining style, i would like to use calabash. Is there a possibility to call calabash from Java? I read thru the documentation on http://xmlcalabash.com but there is only a description how to use it from command line. I also tired to find some javadoc on githup but wasn't successful. Obviously, there is the Main class with the main() method and i could supply the command line parameters as a string array...
I wonder if there is a better way to do it.
I looked into this recently too. I took a pragmatic approach where I call Main.run(), and pass in a string array that I generate from a (File)Properties object. It doesn't allow passing in file inputs as streams or sources however, they must reside on the file-system.
Likely there are nicer ways. You could for instance look into http://expath.org/ . There should be sources of that project. The webapp modules (formerly known as servlex?) seems to provide XMLCalabash integration.
HTH!
At my company we use MVC.NET and Entity Framework to perform a SQL connection. I was wondering if there is a way to create a custom attribute on a class that will create warnings if the return type of a method is not IEnumberable? The idea is to avoid the developers defaulting to a collection making the function less generic.
.Net attributes are evaluated at runtime, and would not be useful for giving warning as the developers are churning out code. You can probably look at static analysis tools like FxCop / StyleCop so that these warnings are shown during compile time. In your particular case you might have to write a Custom Rule which will make the check.
Write a unit test checking the return type, rather than an attribute or anything else. That way you wont uglify your code.
Question:
Is there a static way to reliably determine the type contained by a type derived from CollectionBase, using Reflection or Microsoft.Cci?
Background:
I am working on a code generator that copies types, makes customized versions of those types, and converters between. It walks the types in the source assembly via Microsoft.Cci. It prints out source code using textual templates. It does a lot of conversion and customization, and tosses out code that I don't care about.
In my resulting code, I intend to replace List<T> everywhere that a CollectionBase, IEnumerable<T>, or T[] was previously used. I want to use List<T> because I am pretty sure I can serialize it without extra work, which is important for my application. T is concrete in every case. I am trying not to copy CollectionBase classes because I'd have to copy over the custom implementation, and I'd like to avoid having to do that in my code generator.
The only part I'm having a problem with is determining T for List<T> when replacing a custom CollectionBase.
What I've done so far:
I have briefly looked at the MSDN docs and samples for CollectionBase, and they mention creating a custom Add method on your derived type. I don't think this is in any way enforced, so I'm not sure I can rely on that. An implementor could name it something else, or worse, have a collection that supports multiple types, with Object as their only common ancestor.
Alternatives I have considered:
Maybe the default serialization does some tricks that I can take advantage of. Is there a default serialization for CollectionBase collections, or do you generally have to implement it yourself? If you have to do it yourself, is there some reliable metadata I could look at in order to determine the types? If it supports default serialization, does it rely on the runtime types of the items in the collection?
I could make a mapping in my code generator of known CollectionBase types, mapped to their corresponding T for List<T>. If a given CollectionBase type that I encounter isn't in the list, throw an exception. This is probably what I'll go with if I there isn't a reliable alternative.
I'm still not sure enough about what you want to do to give advice. Still, do your CollectionBase-derived classes all implement Add(T)? If so, you could look for an Add method with single parameter of type other than object, and use that type for T.