简体   繁体   English

带有子类的 Java 条件编译

[英]Java conditional compilation with subclasses

My apology if this was already asked somewhere else but I couldn't find it.如果已经在其他地方问过这个问题,但我找不到,我深表歉意。

Basically, I would like to know whether a Java class with conditional compilation (for the purpose of removing all traces of a snippet of code in class files) will work on its subclasses.基本上,我想知道具有条件编译的 Java 类(为了删除类文件中的代码片段的所有痕迹)是否可以在其子类上工作。

For example, let's have class A and its subclass B implemented as follows:例如,让我们按如下方式实现类A及其子类B

public class A {
    protected static final boolean ASSERTS = false;

    protected A() {
        if (ASSERTS) {
            System.out.println("A: ASSERTS enabled.");
        }
}

public class B extends A {
    protected B() {
        if (ASSERTS) {
            System.out.println("B: ASSERTS enabled.");
        }
}

It's clear that nothing will be printed out.很明显,什么都不会被打印出来。 But, will their class files include the compiled if-condition?但是,他们的类文件会包含编译的 if 条件吗?

Many thanks in advance!提前谢谢了!

Just compiled the class in IntelliJ, then decompiled the .class file to see if it contained the statement(s).刚刚在 IntelliJ 中编译了类,然后反编译了.class文件以查看它是否包含语句。 It did not.它没。

I suspect this is compiler dependent (ie the Java Language Specification makes no statement about whether or not these things must be included, so they are free to choose), and that you could probably find a compiler which would include these statements however.我怀疑这是编译器相关的(即Java语言规范中是没有这些东西是否必须包含语句,所以他们可自由选择),而你也许可以找到一个编译器但是,其中包括这些语句。 I was using Corretto Java 11.我使用的是 Corretto Java 11。

No it will not.不,不会。

You should try creating your .class files and then de-compile it using a decompiler or simply go to javadecompilers.com and upload your class file to confirm your answer.您应该尝试创建您的 .class 文件,然后使用反编译器对其进行反编译,或者直接访问 javadecompilers.com 并上传您的类文件以确认您的答案。

Thanks for all suggestions! 感谢所有建议! As suggested by @shivam-puri, I haved tried decompiled my class files. 如@ shivam-puri所建议,我曾尝试反编译我的类文件。 (Fyi, I'm using JDK1.8.0_212 and javadecompilers.com .) (仅供参考 ,我正在使用JDK1.8.0_212和javadecompilers.com 。)

Apparently, there is no trace of if-condition in the subclass B . 显然,在子类B中没有if条件的痕迹。 That is, conditional compilation works on subclasses too. 也就是说,条件编译也适用于子类。

if make sources based on your code如果根据您的代码制作来源

public class A {
protected static final boolean ASSERTS = false;

protected A() {
    if (ASSERTS) {
        System.out.println("A: ASSERTS enabled.");
    }
}

public static class B extends A {
  protected B() {
      if (ASSERTS) {
          System.out.println("B: ASSERTS enabled.");
        }
      }
    }
}

and compile with javac, then with FALSE we have byte-code并用 javac 编译,然后用 FALSE 我们有字节码

  public class A {
    protected static final boolean ASSERTS;
    protected A();
    Code:
       0: aload_0
       1: invokespecial #2                  // Method java/lang/Object."<init>":()V
       4: return
  }

with TRUE we get compiled byte-code below使用 TRUE 我们得到下面的编译字节码

public class A {
  protected static final boolean ASSERTS;

  protected A();
    Code:
       0: aload_0
       1: invokespecial #2                  // Method java/lang/Object."<init>":()V
       4: getstatic     #3                  // Field java/lang/System.out:Ljava/io/PrintStream;
       7: ldc           #4                  // String A: ASSERTS enabled.
       9: invokevirtual #5                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
      12: return
}

so that on javac level such optimization works but for guaranty it is better to remove code parts on level of sources before compilation for instance with preprocessor这样在 javac 级别这样的优化工作,但为了保证最好在编译之前删除源级别的代码部分,例如使用预处理器

Java 不支持像 c、c++ 这样的宏您可以在运行时使用标志来启用或禁用断言,但不能使用宏

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

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