开发者

Does reflection require that literal strings are written among the bytecode?

When Java (or any language capable of reflection) outputs a program, does it keep the nam开发者_JS百科es of the methods as strings within the bytecode?

I'm wondering how, for example, the following is possible without this being the case:

Class c = Class.forName("foo");
Method m[] = c.getDeclaredMethods();

Will the bytecode then contain Strings of each method field and name within the class? I'm wondering because I want to make sure all parts of my code are not easily read, without having to obfuscate everything. If there is something fundamentally wrong with my understanding of reflection, please let me know.


First, Method, fields and class names are stored as clear text in .class file.

Second, additionally to reflection there are java decompilers that can restore the almost-readable code from byte code.

Third if you want to protect your code use obfuscation. There are several popular java obfuscatotors available.


Yes, every field will contain the full name. If you want to obfuscate your code, you need to use an obfuscation program (although that's not totally secure either, they can easily be read at run-time).


As mentioned by previous answers, when Java is translated / compiled to Java Bytecode (a "Java program"), the ids. are kept.

Since you mention "other languages":

If they target the same Java Virtual Machine, then those languages will also generate Java bytecode with names.

I not sure, but, I believe ".NET" languages doesn't generate id's directly in their programs, and there are proposals or prototypes of new versions of Java and J.V.M. (Java 7, Java 8), to use numbers or codes instead of plain names, both for security and to make the program faster and optimized.

There is a (digital / printed) book: "Java Virtual Machine Specification" where bytecode mentions class ids. If you are interested, I think you can still can download free from Sun-Oracle, or shop in Amazon.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜