What is the use of the forward slash in Java?

huangapple go评论95阅读模式
英文:

What is the use of the forward slash in Java?

问题

I'm updating an old Java application to work on modern operating systems, and a very common error throughout this program is the use of a forward slash in class names. It's almost like it's trying to be used as a way to differentiate between subclasses or something. I can't seem to find much information on this online, as this is either written in Java 3 or 4 and I've never programmed in Java before.

A few examples of its usage in this program:

final class classname/a0 implements ActionListener { // <----------- these lines
private final t a;

public void actionPerformed(ActionEvent paramActionEvent) {
this.a.a(this.a.k.a, true, 0, 0);
}

final class classname/a1 implements ActionListener {
private final t a;

public void actionPerformed(ActionEvent paramActionEvent) {
this.a.a(this.a.k.a, true, 0, 0);
}

final class classname/a2 implements ActionListener {
private final t a;

public void actionPerformed(ActionEvent paramActionEvent) {
this.a.a(this.a.k.a, true, 0, 0);
}
}

They all throw the error - Syntax error on token "/", extends expected and - The type classname is already defined

Any info on what this means so that I can start work on a solution to this?

英文:

I'm updating an old Java application to work on modern operating systems, and a very common error throughout this program is the use of a forward slash in class names. It's almost like it's trying to be used as a way to differentiate between subclasses or something. I can't seem to find much information on this online, as this is either written in Java 3 or 4 and I've never programmed in Java before.

A few examples of its usage in this program:

final class classname/a0 implements ActionListener { // &lt;----------- these lines
  private final t a;
  
  public void actionPerformed(ActionEvent paramActionEvent) {
    this.a.a(this.a.k.a, true, 0, 0);
  }

final class classname/a1 implements ActionListener {
  private final t a;
  
  public void actionPerformed(ActionEvent paramActionEvent) {
    this.a.a(this.a.k.a, true, 0, 0);
  }

final class classname/a2 implements ActionListener {
  private final t a;
  
  public void actionPerformed(ActionEvent paramActionEvent) {
    this.a.a(this.a.k.a, true, 0, 0);
  }
}

They all throw the error - Syntax error on token &quot;/&quot;, extends expected and - The type classname is already defined

Any info on what this means so that I can start work on a solution to this?

答案1

得分: 1

以下是翻译好的部分:

这是无效的语法:

final class classname/a2 implements ActionListener

在Java中,你不能在标识符中使用 /

据我所记,Java标识符的语法规则如下:

  • 只能包含数字和字母
  • 不能以数字开头

此外,就我所见,没有足够的闭合括号来在语法上“关闭”每个类的代码定义。

应该像这样(假设不了解类型 t):

final class classnamea0 implements ActionListener {
  private final t a;
  
  public void actionPerformed(ActionEvent paramActionEvent) {
    this.a.a(this.a.k.a, true, 0, 0);
  }
}

final class classnamea1 implements ActionListener {
  private final t a;
  
  public void actionPerformed(ActionEvent paramActionEvent) {
    this.a.a(this.a.k.a, true, 0, 0);
  }
}

final class classnamea2 implements ActionListener {
  private final t a;
  
  public void actionPerformed(ActionEvent paramActionEvent) {
    this.a.a(this.a.k.a, true, 0, 0);
  }
}

如果我要猜测的话,看起来你似乎尝试使用工具将一些字节码逆向工程成Java可读源代码,并且它显示了源代码中接口的内部、编译时派生的匿名实现。

换句话说,以下是Kotlin代码:

interface Bar {
    fun bar()
}

class Foo {
    fun foo() {
        val bar = object : Bar() {
            override fun bar() { } 
        }   
    }
}

在编译时,这会被转换成字节码,其中有一个名为 Bar 的内部类(通常是类似于 Foo$Bar0 的东西),它映射到Kotlin源代码中 Foo 类中 Bar 的匿名实现。换句话说,在源代码中的匿名实现在编译后的Java字节码中变成了具名实现(它本身在运行时会被JIT编译)。

我所做的猜测基于以下事实:OP中的结果“Java”代码 A) 无效,B) 模糊不清,C) 显示出从原始源代码中的匿名接口/抽象类实现的字节码反编译的证据。

看起来原始工程师可能并没有打算让代码如此容易地反向工程化(你可能需要字节码到源代码的映射文件来正确解码字节码为可读源代码 - 这些通常是构建过程的副产品,可能仍然存在于某个地方的数据中)。

也就是说,我们可能需要更多的上下文来正确地帮助解决这个问题。

英文:

This is invalid syntax:

final class classname/a2 implements ActionListener 

You cannot use / in an identifier in Java.

As far as I can remember, the syntactical rules for Java indentifiers are as follows:

  • numbers and letters only
  • cannot start an identifier with a number

In addition, as far as I can see, there are not enough closing braces to syntactically "close" each of the classes code definitions.

It should be like so (assuming no knowledge of the type t):

final class classnamea0 implements ActionListener {
  private final t a;
  
  public void actionPerformed(ActionEvent paramActionEvent) {
    this.a.a(this.a.k.a, true, 0, 0);
  }
}

final class classnamea1 implements ActionListener {
  private final t a;
  
  public void actionPerformed(ActionEvent paramActionEvent) {
    this.a.a(this.a.k.a, true, 0, 0);
  }
}

final class classnamea2 implements ActionListener {
  private final t a;
  
  public void actionPerformed(ActionEvent paramActionEvent) {
    this.a.a(this.a.k.a, true, 0, 0);
  }
}

If I had to hazard a guess, it looks like you've tried to reverse engineer some byte code to Java readable source code using a tool and it's showing you inner, compile time derived, anonymous implemenations of interfaces from the source code.

In other words the following Kotlin code:

interface Bar {
    fun bar()
}

class Foo {
    fun foo() {
        val bar = object : Bar() {
            override fun bar() { } 
        }   
    }
}

At compile time, this is turned into byte code where there is a named inner class of Bar (usually something like Foo$Bar0) which maps to the anonymous implementation of Bar in the Kotlin source of Foo class. In other words, an anonymous implementation in source code is turned into a named implementation in the compiled java byte code (which itself is JIT compiled at run time).

This guess I am making is based of the fact the resultant "Java" code in the OP is A) not valid, B) obfuscated and C) showing evidence of decompilation from byte code of anonymous interface/abstract class implementations in the original source code.

Looks like the original engineer didn't intend for the code to be so easily reverse engineered (you may need mapping files from byte code -> source code to properly decode the byte code into readable source - these are usually produced as a side effect of a build process and may or may not still exist as data somewhere).

I.e. we may need more context to properly help out here.

huangapple
  • 本文由 发表于 2020年7月29日 05:03:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63142720.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定