编译 Java 类时禁用编译时依赖性检查 [英] Disabling compile-time dependency checking when compiling Java classes

查看:27
本文介绍了编译 Java 类时禁用编译时依赖性检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下两个 Java 类:

Consider the following two Java classes:

a.) class Test { void foo(Object foobar) { } }

b.) class Test { void foo(pkg.not.in.classpath.FooBar foobar) { } }

此外,假设在类路径中找不到 pkg.not.in.classpath.FooBar.

Furthermore, assume that pkg.not.in.classpath.FooBar is not found in the classpath.

第一个类将使用标准 javac 编译良好.

The first class will compile fine using the standard javac.

然而,第二个类不会编译,javac 会给你错误信息package pkg.not.in.classpath does not exist".

However, the second class won't compile and javac will give you the error message "package pkg.not.in.classpath does not exist".

错误消息在一般情况下很好,因为检查您的依赖项允许编译器告诉您是否有一些方法参数错误等.

The error message is nice in the general case since checking your dependencies allows the compiler to tell you if you got some method argument wrong, etc.

虽然在编译时检查依赖关系很好而且很有帮助,但 AFAIK 并不是严格需要在上面的例子中生成 Java 类文件.

While nice and helpful this checking of dependencies at compile-time is AFAIK not strictly needed to generate the Java class file in the example above.

  1. 您能否举出任何示例,说明在不执行编译时依赖性检查的情况下从技术上讲不可能生成有效的 Java 类文件?

您知道有什么方法可以指示 javac 或任何其他 Java 编译器跳过编译时依赖性检查吗?

请确保您的回答解决了这两个问题.

Please make sure your answer addresses both questions.

推荐答案

您能否举出任何示例,说明在不执行编译时依赖性检查的情况下从技术上讲不可能生成有效的 Java 类文件?

Can you give any example for which it would be technically impossible to generate a valid Java class file without performing compile time dependency checking?

考虑这个代码:

public class GotDeps {
  public static void main(String[] args) {
    int i = 1;
    Dep.foo(i);
  }
}

如果目标方法具有签名public static void foo(int n),则将生成以下指令:

If the target method has the signature public static void foo(int n), then these instructions will be generated:

public static void main(java.lang.String[]);
  Code:
   0:   iconst_1
   1:   istore_1
   2:   iload_1
   3:   invokestatic    #16; //Method Dep.foo:(I)V
   6:   return

如果目标方法有签名public static void foo(long n),那么int将被提升为一个long之前到方法调用:

If the target method has the signature public static void foo(long n), then the int will be promoted to a long prior to the method invocation:

public static void main(java.lang.String[]);
  Code:
   0:   iconst_1
   1:   istore_1
   2:   iload_1
   3:   i2l
   4:   invokestatic    #16; //Method Dep.foo:(J)V
   7:   return

在这种情况下,将无法生成调用指令或如何填充类常量池中由数字 16 引用的 CONSTANT_Methodref_info 结构.请参阅 类文件格式 在 VM 规范中了解更多详细信息.

In this case, it would not be possible to generate the invocation instructions or how to populate the CONSTANT_Methodref_info structure referred to in the class constant pool by the number 16. See the class file format in the VM spec for more details.

这篇关于编译 Java 类时禁用编译时依赖性检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆