I need to do important refactoring in a framework. I have a method called about 300 times from various locations in the code (i.e. Find Usages give me about 300 results).
I would like to filter those results so that it only return usages that are not in the body of a constructor.
I tried to use "view call hierarchy", it gives more readable results (i.e. it's more easy to identify call from outside constructor). But I was wondering if there is a way to exclude automatically calls that are done from within a constructor body?
I'm not used to work with the "structural search", but it's maybe something that can help ?
I'm using IDEA EAP 12
(Answering my own question)
I tried to explore features of SSR and finally found an helpful pattern.
What I want : find all calls to method myMethod that are done, but excluding those that are done inside constructor body (i.e. only those that are done in a regular instance method).
The search pattern:
class $Class$ {
$ReturnType$ $MethodName$($ParameterType$ $Parameter$) {$MethodCode$;}
}
This pattern will match all non empty methods. So I still have to restrict $MethodCode$ with a regexp:
.*\.myMethod\(.*
I think it should be possible to improve $MethodCode$ regexp, but I didn't get any false match... so I'm happy with that.
Related
I'd love to create a new library for my purposes and am currently struggling with the technical approach needed to fulfill the requirements.
I want to have something like this:
class MyFoo {
#Populate("myText")
fun giveMeAValue(bar: Bar) {
LOG.info(bar.message) // "myText"
}
}
I'd like to hook on the function call of giveMeAValue or better said I want to hook logic on every function that is annotated with #Populate. If a call is being registered I'd like to execute some logic and pass a value for the bar parameter. Please ignore the fact that I might overwrite a value that has been passed already.
I was reading up for Reflection using Kotling (i.e. https://medium.com/tompee/kotlin-annotation-processor-and-code-generation-58bd7d0d333b) but could not find the thing I'd like to have here.
The hooking/proxy mechanism before the "real" function is being called. That's what I want to achieve.
Can anybody tell me what I am actually looking for? Is there a word for that concept or an article/guide describing this? I was not able to find anything. A code snippet/example would be awesome, too!
I saw some examples of extension functions being defined inside a class/interface but I didn't understand the reason it would be done. Could someone show when it would be the proper way to implement some use case?
One particular example that I didn't understand very well:
interface Monoid<A> {
fun z(): A
fun A.add(other:A):A
}
When you only ever want to use this function inside your class/interface and its subtypes (or nearly so; you already know how to get out with with as per your previous question, but that shouldn't be a common case).
The specific example just seems like a bad idea once you need to work with more than one Monoid at once.
I want to create a Perl 6 module that would "export" a LEAVE phaser to the scope in which the use statement is placed. I have not found a way to do that.
I assume this would need to be done inside an EXPORT sub, but how? The default functionality of an EXPORT sub is to just return a Map with name => object mapping of things to export. As far as I know, there's no way to introspect what the outer scope is. Or am I missing something?
Thanks to Zoffix++ for pointing out a very hacky way of doing this.
sub EXPORT() {
$*W.add_phaser: $*LANG, 'LEAVE', { code you want to run }
{} # need to show that we're not exporting anything
}
This hack depends on various Rakudo internals, and is therefore not recommended to be used "in the wild". And it's quite likely that a better, more supportable way will be implemented for this functionality in the near future.
This hack was needed for a module that supports a sort of timely destruction other than from the direct scope in which an object is created (aka LEAVE phaser). This is typically handled in Perl 5 by using reference counting and calling DESTROY if the reference count of an object goes to 0.
This module can now be found in the Perl 6 ecosystem: FINALIZER. This module allows module developers to mark created objects for finalization: by default on program exit. Or from a scope indicated by the client program.
Not sure this is possible, but other people might know more. But what are you after anyway? I had a similar desire a while ago, I wanted to do something like a RAII lock. I solved it by wrapping the block rather than putting the LEAVE into it directly:
sub mtest($block) { LEAVE { say "hoo" }; $block() }
mtest { say "woo"; }
Perhaps that works for you as well...
I'm tying to implement a novel way of overriding functions based on which DLLs I have loaded. In this model, I have a list of class instances from First = Highest Priority to Last = Lowest priority.
Any of those classes may implement a Hook function or callback. I'm currently at the stage where I can pass a string to a function, and then call it - my library convention looks like this:
Dim hookclasses as HooksList
Dim callable as Object
hookclasses.Add(new ClassA)
hookclasses.Add(new ClassB)
'... etc.
if hookclasses.Has("MyHookFunction", callable) then
callable.MyHookFunction()
end if
This all works, but I'd like to reduce typos by leveraging Intellisense. I've already thought of popping the strings into a class containing constant strings, so I'm after something better than that.
Ideally I'd like to have a fallback class that implements all of the hook functions (even if it simply returns), and if the language supported it, I'd like to do the following:
if hookclasses.Has(NameOf(FallbackClass.MyHookFunction), callable) then ...
Clearly there is no 'NameOf' operator, and I don't know how to write a NameOf function.
Is this possible?
Thanks.
Check this article nameOf (C# and Visual Basic reference)
https://msdn.microsoft.com/en-us/library/dn986596.aspx
It does exactly what you want. And before that String Litterals were almost the only option.
Edit :
Question was : "Clearly there is no 'NameOf' operator, and I don't know how to write a NameOf function."
If I understand your problem right, you have a list of classes that you fetched from dynamically loaded DLL, point is you don't know if a class implements all of the hooks or only a few.
If you use an interface, like IHookable and put all the hook functions in there, it means all the DLL have to implement all the hook functions, which is not what you want.
And (if I understand it properly) if the first class in list does not implement the hook, you check the second one and so on. So with an interface you wouldn't know if the hook is implemented or not.
I have a method in my Service class which executes an hibernate update for any domain object:
update(Object obj)
It's called from lot's of classes in my project for different kind of objects. I would like to find all usages of this method when it's called for a specific domain object. I.e. call methods call wich executes an update of my Title object:
serviceClass.update(Title title)
I'm using IntelliJ as my IDE and I'm wondering if there is a way to find all those usages.
Does anyone have an IDEA how to do this?
Thanks a lot in advance,
Ronny
I've tried it with a small sample project and was able to achieve the desired behavior using Structural Search and Replace feature with the modified method calls template:
$MethodCall$ Text constraints, Text/regexp should be set to update so that methods with other names are ignored. $Parameter$ Occurrences count, Minimum count should be set to 1 to ignore method calls with no or more parameters.
Results:
If you're interested in the call chains that are providing a specific input into a given method, try the Analyze->Data Flow to Here command.
This allows you to see which values are passed in, through which call chains. And, for example, where null values might be coming from.
Quite a powerful feature, really.