简体   繁体   English

Java调试器找到实例化对象的位置

[英]Java debugger find where an object was instantiated

I'm debugging with Eclipse and got a parameter which is an anonymous-instantiated-interface. 我正在使用Eclipse进行调试,并获得了一个匿名实例化接口的参数。 Is there a way to find out where it was created? 有没有办法找出它的创建位置?

Unless you do a step by step debugging from the start of your application, there's no direct way to find out where it was instanciated using Eclipse. 除非您从应用程序的开始进行逐步调试,否则无法直接找到使用Eclipse实例化的位置。

The best way I've found to acheive that is to do a File Search (ctrl+h, File search tab) and search for new "yourInterface"() { , in all *.java files. 我发现最好的方法是执行文件搜索(ctrl + h,文件搜索选项卡),然后在所有* .java文件中搜索new "yourInterface"() { This will find all places where you instanciate that interface in your code. 这将在代码中找到实例化该接口的所有位置。

If you're able to reduce the scope with a working set, that will simplify finding which of the found places is the one you're looking for. 如果您能够通过工作集来缩小范围,那么将简化查找所要查找的地点的过程。

While debuging you can see the actual stack trace in the Debug perspective, just click back down the trace from where the execution is stopping at a break point till you get to the method where the instance of the anonymous class was created. 调试时,您可以在“调试”透视图中看到实际的堆栈跟踪,只需单击从断点处停止执行的跟踪,直到找到创建匿名类实例的方法。

在此处输入图片说明

here is the call that returned the instance 这是返回实例的调用

在此处输入图片说明

hit F3 to locate the method. F3定位方法。

在此处输入图片说明

You can temporarily modify your code to call this interface with any parameters (ie null ) at the place where you get it, and do a step into. 您可以临时修改代码,以在获取代码的地方使用任何参数(即null )调用此接口,然后逐步执行。

UPDATE UPDATE

java.lang.reflect.Type is a basic interface instances of which are loaded inside JVM. java.lang.reflect.Type是一个基本接口实例,已在JVM中加载。 Locating this place (most probably it is native code) where they are instantiated in most cases will give nothing. 在大多数情况下,将其实例化的位置(最有可能是本机代码)放置在该位置。

This type is used to describe generics in fields, methods, classes, etc. Ie for a field declaration 此类型用于描述字段,方法,类等中的泛型。即用于字段声明

List<? extends Foo> fooList = new ArrayList<Foo>();

it corresponds to <? extends Foo> 它对应于<? extends Foo> <? extends Foo> . <? extends Foo> To find this declaration, you might want to: 要找到此声明,您可能需要:

  • Call toString() in the debugger for this interface and search in your code for occurences 在调试器中为此接口调用toString()并在代码中搜索是否发生
  • Inspect this Type instance find out or to show us more info 检查此Type实例以查找或向我们显示更多信息
  • Use debugger to find out how it's being get with reflection (ie Field.getType or Method.getGenericParameterTypes() ) 使用调试器来找出它如何被使用反射得到(即Field.getTypeMethod.getGenericParameterTypes()

i didn't track this prolbem further, because i used an other solution. 我没有进一步追踪这个问题,因为我使用了其他解决方案。 But what me helped was this piece of code where the variable "genericType" was the anonymous-instantiated-interface: 但是我帮助的是这段代码,其中的变量“ genericType”是匿名实例化接口:

// Ensure that we're handling only List<MediaEntity> objects.
boolean isWritable;
if (List.class.isAssignableFrom(type) && genericType instanceof ParameterizedType) {
    ParameterizedType parameterizedType = (ParameterizedType) genericType;
    Type[] actualTypeArgs = (parameterizedType.getActualTypeArguments());
    isWritable = (actualTypeArgs.length == 1 && actualTypeArgs[0].equals(MediaEntity.class));
} else {
    isWritable = false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM