简体   繁体   English

Java:告诉 JVM 将所有字段/方法视为公共

[英]Java: Telling JVM to treat all fields/methods as public

I'm going through a java codebase and think that it would be much easier to see how the software works if I could temporarily make all fields and methods public.我正在浏览一个 Java 代码库,并认为如果我可以暂时公开所有字段和方法,那么了解软件的工作方式会容易得多。

The access specifiers are basically guidelines to be followed by the programmer while he is coding (ex. use setter instead of direct assignment to allow the setter to perform some other work associated with the field).访问说明符基本上是程序员while he is coding要遵循的准则(例如,使用 setter 而不是直接赋值以允许 setter 执行与该字段相关的一些其他工作)。 Is it possible to tell jvm to ignore the access limiters when running a java program?是否可以在运行 java 程序时告诉 jvm 忽略访问限制器?

Another possibility might be to use some IDE plugin which manually changes the visibility of everything to public .另一种可能性可能是使用一些 IDE 插件,手动将所有内容的可见性更改为public

What you are asking for won't work on a number of levels.你所要求的在很多层面上都行不通。

  1. You can't do this at the source code / compilation level.您不能在源代码/编译级别执行此操作。 It alters the Java language.它改变了 Java 语言。 And it wouldn't achieve anything either.它也不会取得任何成就。

  2. You can't do this in any useful fashion at runtime.您不能在运行时以任何有用的方式执行此操作。 You could use reflection to make the fields accessible.您可以使用反射使字段可访问。 However, it is impractical because:但是,这是不切实际的,因为:

    • This must be done one Field at a time, or one Class at a time;这必须一次完成一个Field ,或一次完成一个Class eg例如

       Field[] fields = clazz.getDeclaredFields(); AccessibleObject.setAccessible(fields, true);
    • You would need to retain all of the Field objects so "treated", because setting a Field object to be accessible doesn't affect other Field objects for the same field.您需要保留所有的Field对象,因此“治疗”,因为设置Field对象可访问不会影响到其他Field对象为同一领域。

    • You could only make use of this if you modified your source code to use reflection and the above Field objects to access the field values.如果您修改源代码以使用反射和上述Field对象来访问字段值,则只能使用它。

  3. If you are trying to understand how your code works, there are other better ways to do it.如果您想了解您的代码是如何工作的,还有其他更好的方法可以做到。

    • Read and analyze the source code.阅读并分析源代码。 Use an IDE to help you with this.使用 IDE 来帮助您解决这个问题。
    • Use a debugger.使用调试器。 A debugger allows you to see the state of a variable irrespective of the variable's access .调试器允许您查看变量的状态,而不管变量的访问权限如何 And it works for parameters and local variables as well as fields.它适用于参数和局部变量以及字段。
    • Add some debug logging or trace prints to your code.向您的代码添加一些调试日志记录或跟踪打印。 (And remove the changes when you are finished your investigation. Use version control to help you back out the changes.) (并在您完成调查后删除更改。使用版本控制来帮助您撤消更改。)

@ElliottFrisch Why is it a bad idea? @ElliottFrisch 为什么这是个坏主意?

Basically, because it won't help you.基本上,因为它不会帮助你。

What you're asking is definitely achievable but raises some questions as to why you might want or need to do it.您所要求的绝对是可以实现的,但会提出一些关于您为什么想要或需要这样做的问题。 There is almost always a reason behind setting access modifiers.设置访问修饰符几乎总是有原因的。 In fact it may help you to understand the software more if you have a think about why certain fields and methods are made public etc.事实上,如果您考虑一下为什么某些字段和方法被公开等,它可能会帮助您更多地了解该软件。

I recently started a new job where I had to go through a large codebase and try to understand what everything does.我最近开始了一份新工作,我必须浏览一个大型代码库并尝试了解所有内容的作用。 I found that using a Debugger was the best way to step through the code and have a look at the value of each field, variable etc at each point in the program.我发现使用调试器是单执行代码并查看程序中每个点的每个字段、变量等的值的最佳方式。 All good Java IDES have debuggers.所有好的 Java IDES 都有调试器。 I use IntelliJ IDEA which has a very useful debugger that even allows you to run custom evaluation statements at different points in the running of a program.我使用IntelliJ IDEA ,它有一个非常有用的调试器,它甚至允许您在程序运行的不同点运行自定义评估语句。

This is generally considered High Treason by the Style Guide.这通常被风格指南认为是叛国罪

Options:选项:

  • You can still use java reflection to set fields (private, final, or otherwise), but the downside is that you can't code as if it is public, you still have to call some method to set a field to bla.您仍然可以使用 java 反射来设置字段(私有、最终或其他),但缺点是您无法将其编码为公共,您仍然必须调用某些方法将字段设置为 bla。
  • Use JNI which to link C code to java, and C really doesn't really care about Java's field visibility.使用 JNI 将 C 代码链接到 java,而 C 真的不关心 Java 的字段可见性。

A compiler plugin would probably work, but by the time most programmers get to the point of knowing how, they also know what a bad idea it was.编译器插件可能会工作,但是当大多数程序员知道如何工作时,他们也知道这是一个多么糟糕的主意。 Granted, I am tempted to try now.当然,我现在很想尝试。




Note: I am reasonably sure java's SecurityManager would take issue with you trying to make it's data public.注意:我有理由确信 java 的SecurityManager会在您尝试将其数据公开时提出问题。

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

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