反映方法范围变量的Java [英] Java reflecting on method scope variables

查看:126
本文介绍了反映方法范围变量的Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用反射,您可以获得与课程相关的所有内容。你可以获得所有声明的方法,字段和类(甚至可能更多),但我找不到一种方法来反映一个方法,所以我可以找出该方法可能使用的类。

Using reflection you can get pretty much everything relating to a class. You can get all the declared methods, fields and classes (and possibly even more), but i couldn't find a way to reflect on a method so i could find out what classes that method might be using.

基本上我想找出给定类具有的其他类的所有依赖项。

Essentially i would like to find out all dependencies to other classes that a given class has.

示例:
给出以下代码:

Example: Given the following code:

import com.yada.yada.yada.SomeClass 

public class MyClass
{
  public MyClass
  {
    new SomeClass();
  }
}

我怎样才能发现MyClass正在使用SomeClass它的构造函数?

How can i find out that MyClass is using SomeClass in its constructor?

我试图想办法在类文件中定义所有导入语句,但我也找不到任何方法。但是,假设有一种方法可以以某种方式挖掘类文件中定义的所有import语句,那么如何找到在同一个包中定义的类,这些类不需要import语句?

I was trying to think of a way to get all import statements defined in a class file but i couldn't find anything that way either. But, assuming there's a way to somehow dig up all import statements defined in a class file, how would one find out about classes defined in the same package, which do not require an import statement?

编辑:

场景:目标是将此类(MyClass)的字节码发送到另一个进程。然后,这个其他进程接受字节码并使用类加载器加载类(MyClass),依此类推。问题是,当我尝试在另一个进程中创建并运行MyClass的实例时,它会失败,因为它无法找到SomeClass的定义。

Scenario: The goal is to send the bytecode of this class (MyClass) to another process. This other process then takes in the bytecode and loads the class (MyClass) using class loaders, and so on. The problem is that when i try to create and run an instance of MyClass in the other process it fails because it cannot find a definition for SomeClass.

如果SomeClass是MyClass的成员,那不会有问题,但由于对它的唯一引用在于一个方法,所以没有办法通过反射来实现它?

If SomeClass were a member of MyClass it wouldn't be a problem but since the only reference to it lies in a method, there's no way to get to it via reflection?

推荐答案

我认为最接近所有类的依赖关系是通过挂钩类加载器机制并记录什么当您正在检查的类被实例化并调用其方法时,将加载类。在yourse中,你也可以传递它间接所依赖的所有类,但是根据你想要对信息做什么,这可能是你真正需要的。

I think the closest you can come to getting all of a class's dependencies is by hooking into the class loader mechanism and recording what classes get loaded when the class you're examining is instantiated and its methods are called. Of yourse, you'd transitively also get all the classes that it indirectly depends on, but depending on what you want to do with the information, that may be what you actually need.

但是对于所有情况都不可能做到(想象一下使用 Class.forName()的方法来要求一个随机的类名每次被调用)。

But it's impossible to do for all cases (just imagine a method that uses Class.forName() to ask for a random class name every time it's called).


如何找到同一包中定义的类

how would one find out about classes defined in the same package

一般来说这实际上是不可能的,因为类加载器概念实际上只允许要求一个完全限定的类名,并获得该类或 ClassNotFoundException的。可以从Web服务器(在applet的情况下)加载类或者动态生成类,因此除了要求它之外,您无法知道特定的类是否存在。

That's actually impossible to do in general, since the class loader concept really only allows asking for a fully qualified class name, and either getting that class or a ClassNotFoundException. Classes can be loaded from a webserver (in the case of applets) or generated on the fly, so you cannot know whether a specific class exists except by asking for it.

这篇关于反映方法范围变量的Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆