Usually to find the callers of a method in Eclipse I use ctrl-alt-h on the method. If nothing comes up, it is a callback from some other library. I'm pretty certain this method is not a callback and I'm pretty certain it's being called from somewhere. How can I find out if nothing shows up with a ctrl-alt-h?
You can put the cursor on the method name and press Ctrl-Shift-G (default key combination).
This will open a search in the project to look for references to your method.
Edit
On frameworks using declarative markup (Spring, Android, etc.) you might actually need to use a more manual approach: the file search.
Copy or highlight your method name
Press Ctrl-H (default binding) and select File search (you can assign a custom key binding for that but no custom is provided)
Fill the containing text with your method name if not automatically filled
Optionally, select "case sensitive", or your file pattern if any
Then select your scope (the project should be a good place to start)
Other tactics include:
If the method is implementing a interface, ask Eclipse for the callers of the interface method. Sometimes Eclipse will identify more callers through the interface method, particularly when callers don't know they're using a particular implementation.
Similarly, if the method is overriding a base class method, ask Eclipse for the callers of that.
Put a breakpoint in the method, and look at the call stack in the debugger.
If it's possible that the Java method might be called by native code through JNI, use text search for the Java method name in the native code.
If available, ask the authors of the method.
Related
I'm trying to find all fields with the Disposable interface, so I can know if whether I forgot to call the dispose() method on any of them in the cleanup phase.
Is there a way to do that in intelliJ?
I've only managed to find those, whose dispose() methods I've already remembered to call. I've done that by ticking the "Usages of methods" checkbox in the find usages options dialog (when doing a search on the Disposable interface)... but this wasn't what I need.
You can try Edit | Find | Search Structurally. You can copy a predefined template named "fields of class" that can be modified to your needs by removing Init part and specifying a hierarchy constraint for $FieldType$ variable.
You can read more about that functionality at https://www.jetbrains.com/idea/help/structural-search-and-replace.html
**Alt+F7**
Instead of the dispose method, find usages of the interface
Usages - if this check box is selected, the search is performed for all references of the class by its name.
This way you will get all the places where Disposable interface is being referenced in your workspace
You can find usage of the your interface.
The keycode for that in my system is alt+f7, but it can be different on your system.
So you can right click and find usage of your interface.
and then filter it down to
Usage in extends/implements clause
I have been using premain() with addTransformer(). Since, it gives javassist.ClassNotFound exceptions for certain classes when i run the agent with a server, i thought to try the agentMain() with redefineClasses(). I went through many links, but so far i am unable to find a piece of code that gives me clear idea on how to set up a simple java agent using these two methods. Some help would be really appreciated.
Can we use redefineClasses() with premain()? (When we use redefineClasses() do we still need the transform method?)
I am trying to instrument set of methods of set of classes, where i know the fully qualified name of those classes as com.test.Foo. I wanted to instrument them without going through the entire set of classes loaded onto JVM. I have been reading those documents back and forth, but still i am unable to get a clear idea on how to use that redefineClasses method?
You can call redefineClasses from anywhere, also from a premain method which is nothing but an extension to a normal Java program run by the same JVM process previous to a main method.
A trivial example for running a redefinition is:
instrumentation.redefineClasses(new ClassDefinition(Foo.class, new byte[] {...}));
This way, Foo is set to be represented by the byte array that must contain a valid class file for Foo where all signatures of fields and methods are the same as by the loaded Foo.class. You can use a tool like ASM for instrumenting the class.
If you really only want to instrument Foo, then this might just be the way to go instead of using a ClassFileTransformer.
Can't find anything on this and sort of would like a report on it and not going through all of this code. Here's the question.
I am analyzing a method that calls about 45 other methods that calls other methods. I need to find out all calls that ends up with a specific package from this specific method. How could I do this?
An option I have is to to use "call hierarchy" and drill down one method at a time (but there is no filter option as I can see to just see methods from one given package).
Actually you can do filtering when using the call hierarchy (CTRL - ALT - H). On the newly opened pane, there is a drop-down list with the name "Scope:". Here you can add a new scope where you can include/exclude filter not only on packages but also on classes or to search given a certain regex.
I checked this on the latest Intellij version (14.1.5).
I have an Eclipse plugin which has the purpose of indexing and searching XML files for custom frameworks used by the application my team develops.
There is a toolbar with several buttons on it. Each button has its own command and each command is linked to a separate handler which brings up a search dialog.
The handlers differ only by which file type they search. Currently there are ten concrete handlers and one abstract. All of the functionality is handled in the abstract class, and the concrete classes only implement an abstract "get file type" method.
Ideally I would only have one handler. This means there would need to be a way to inject the enum into the base class directly.
I looked at command parameters, but this appears to be user-facing. I need something hard-coded where the command tells the handler "use this value." I don't care if this is a constructor argument or some hard-coded parameter in plugin.xml.
Thus far I have not been able to find a way to do this. Perhaps my Google-fu is weak, perhaps I am just not seeing it.
Is there a way to specify a hard-coded parameter to a handler constructor or to call a method to set a parameter after it is constructed but before it is invoked?
Command parameters are the right way to achieve that. What are the problems you face with that? Here is an example to use the parameters
Can I enumerate all native methods in java, those that have to be
implemented in c/c++ using JNI?
Can I enumerate native methods by
name (there could be multiple overloads with the same name)?
How can I retrieve method signature to be able to generate the method
signature used by JNI?
Is there a way to check if all native jni methods have been bound properly, instead of trying to call them and get java.lang.UnsatisfiedLinkError exceptions. Sometimes method signature changes on either side without properly updating java or c++ side and I'd like to add some debugging code to detect these issues and handle them (perhaps by generating proper method signature and printing it to the log so I can easily fix the code).
I prefer JNI solution, but if something can be done with help on java side then it's ok also.
If I use registerNatives and register methods that weren't declared in java then it fails and prints it to logcat:
E/dalvikvm( 1445): ERROR: couldn't find native method
E/dalvikvm( 1445): Requested: Lcom/bla/bla/bla/Test;.nativeTestXX:()Z
but I'd like to catch this error and handle it myself. Is it possible to do it?
EDIT:
In my JNI code I have a static nativeInit (as suggested in Android JNI tips) that registers all native methods. In that same function I'd like to verify that all native methods are properly bound. That is, I don't need to wait till some uninitialized method is called and the app exists. The problem that I have: there is a lot of jni code written at different times by different ppl and some methods simply became incorrect, but they are used only in some obscure conditions. The best way for me, I think, is to check that all native methods are bound to some c++ function. The other problem, is that part of JNI code uses binding by exporting all these Long_java_names where method signature changes on either side cannot be detected.
There is no call to check for "unbound" native methods. Using RegisterNatives to perform explicit registration ensures that all methods you register have a matching declaration in the Java sources, but there is no way to check for native-declared methods for which there is no implementation (other than calling it and catching the exception).
At the point where a method with a native implementation is called, if nothing has yet been registered then Dalvik will search through the various shared libraries to find a match. What it sounds like you want is a way to force this search and check the result without actually calling the method. There is no such thing.
There are various ways to generate lists of native-declared methods, either statically or at runtime, but you also need a way to determine if an implementation is available. You're better off in the long run having unit tests that exercise the code.