简体   繁体   中英

Decompiled java classes have some special characters

I decompiled some java classes and I get a source with special characters:

this.val$b.dispose();
this.val$domainLabel.setText((String)domains_config.get("description"));

what does this mean: this.val$b ?

According to the Java spec (see http://docs.oracle.com/javase/specs/jls/se5.0/html/lexical.html#3.8 ) $ is a valid value in an identifier. However, note that "The $ character should be used only in mechanically generated source code or, rarely, to access preexisting names on legacy systems."

There are two common reasons for seeing dollar signs in a variable name in decompiled code:

1. Your source code contains an inner class (perhaps, but not necessarily an anonymous one), in which case things for the inner class will have variable and constructor names like outerclass$innerclass . (See, for example http://docstore.mik.ua/orelly/java/exp/ch05_09.htm in the section on how inner classes really work). If the class is anonymous, the names will have a naming scheme/form like outerclass$ followed by outerclass$1 and so forth

2. The code has been run through an obfuscator. An obfuscator meets the criterion of "mechanically generating" the source code, so it can use dollar signs in the ame. An example would be RetroGuard, which explains in an FAQ on their website , the criterion for using $ in variable and class names. Essentially, the obfuscator uses the $ as a disambiguator and will rename classes or variables with generated names (typically single character letters used when possible to minimize code size), and what is renamed and what isn't depends on the variable's scope, etc.

In your particular example, val$b looks to me like it might be a variable name that has been obfuscated.

Accordihng to http://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html a dollar sign in a variable name is valid, but not recommended, they mention that auto generated code might use it

below is copied from the link, I bolded parts for emphasis

Variable names are case-sensitive. A variable's name can be any legal identifier — an unlimited-length sequence of Unicode letters and digits, beginning with a letter, the dollar sign "$", or the underscore character " ". The convention, however, is to always begin your variable names with a letter, not "$" or " ". Additionally, the dollar sign character, by convention, is never used at all. You may find some situations where auto-generated names will contain the dollar sign , but your variable names should always avoid using it. A similar convention exists for the underscore character; while it's technically legal to begin your variable's name with "_", this practice is discouraged. White space is not permitted. Subsequent characters may be letters, digits, dollar signs, or underscore characters. Conventions (and common sense) apply to this rule as well. When choosing a name for your variables, use full words instead of cryptic abbreviations. Doing so will make your code easier to read and understand. In many cases it will also make your code self-documenting; fields named cadence, speed, and gear, for example, are much more intuitive than abbreviated versions, such as s, c, and g. Also keep in mind that the name you choose must not be a keyword or reserved word

(Eh, I guess it's actually the answer)

$ has no special meaning in Java, that's simply what the decompiler came up with for the variable name.

The '$' is valid character in java identifiers. So "val$b" is just name of the field. See http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.8

The valid java identifiers can contain letters( a - z , A - Z ), digits( 0 - 9 ), underscores( _ ) and the dollar( $ ). So the id names you see are just a valid name without any special meaning.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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