-1

Some code I'm maintaining uses unfamiliar syntax. I haven't been able to find examples of this syntax in the Java docs.

public static void main(String[] args){ ... javax.swing.SwingUtilities.invokeLater(new MyClass.1()); ... }

and

public MyClass(a,m){ ... javax.swing.myJButton.addActionListener(new MyClass.5(this)); ... }

Q1. What do '.1' and '.5' mean and do?

Q2. What does '(this)' do? Is it shorthand for (this.param1, this.param2,...)?

Q3. Is this syntax especially for anonymous object instantiation, javax.swing components, Runnables, multithreading, etc., or is it general use?

Q4. Another version of this code uses more familiar syntax. Are these statements syntactically equivalent to the ones above (notwithstanding the different constructor call and event behavior)?

javax.swing.SwingUtilities.invokeLater(
    new Runnable(){
        public void run(){
            new MyClass(a,m);
        }
    }
);

and

javax.swing.myJButton.addActionListener(
    new ActionListener(){
        public void actionPerformed(ActionEvent e){
            myJTextField.grabFocus();
        }
    }
);
KevinP
  • 35
  • 8
  • Welcome to Stack Overflow! Please take the tour and read through the help center, in particular how to ask. Your best bet here is to do your research, search for related topics on SO, and give it a go. After doing more research and searching, post a Minimal, Complete, and Verifiable example of your attempt and say specifically where you're stuck, which can help you get better answers! – Jonathan Gagne Oct 06 '18 at 01:23
  • 1
    Looks like you decompiled some java code. See also [this question](https://stackoverflow.com/questions/49992084/how-to-reintegrate-anonymous-classes-into-java-code-produced-by-the-ecd-decompil/50002096#50002096) – Johannes Kuhn Oct 06 '18 at 02:52
  • @JohannesKuhn - While I agree with your conclusion, class names and method names like `P$1` are actually valid Java identifiers. (The JLS says so!) – Stephen C Oct 06 '18 at 04:33
  • Yes, but on the other hand decompilers often don't produce such consistent code and mix between inner class and binary name. – Johannes Kuhn Oct 06 '18 at 04:39
  • Thanks Jonathan. Can you please provide examples of how my question failed to follow the guidelines? It's hard to be constructive if you don't start with a concrete foundation! :) – KevinP Oct 06 '18 at 06:45

1 Answers1

1

That is simply invalid Java syntax. You can confirm this for yourself by reading the JLS.

  • At that position, an <identifier> is required.
  • An <identifier> cannot start with a digit.

So, basically, the stuff you are trying to maintain is not valid Java source code.

My guess is that this is output from a decompiler, and the decompiler has encountered some bytecodes that it doesn't know how to decompile to Java. If you are "maintaining" decompiled code .... good luck to you! You will need to figure out what the code means from the context and / or by reverse engineering the bytecodes the hard way.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • At the JVM level, class names can have characters not valid in Java eg whitespace, most symbols and numbers. – Peter Lawrey Oct 06 '18 at 04:06
  • @PeterLawrey - I wasn't aware of that. It implies that these bytecodes have been obfuscated or minified. – Stephen C Oct 06 '18 at 04:33
  • Thanks Stephen, that explains a lot! The project was not properly version controlled, and was deployed to dozens of production environments with unpredictable, undocumented variation. A major part of my job has been to determine which version is used in each prod env, and synthesize portions of versions as needed to replace missing source code of other deployed versions. It's a real mess. Good to know some of it is just trash. – KevinP Oct 06 '18 at 06:52
  • Well ... I wouldn't say that the stuff you are decompiling is necessarily "trash". But clearly the decompiler is having problems figuring it out. – Stephen C Oct 06 '18 at 07:20
  • @StephenC That's my first guess, or it could have been compiled by a different language compiler. I assume if you use a groovy or scala compiler it creates bytecode which can't be decompiled into Java, though I would expect the code to be more obviously different. – Peter Lawrey Oct 06 '18 at 08:58