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

查看:248
本文介绍了编译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不存在

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 结构。请参阅类文件格式

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天全站免登陆