简体   繁体   English

如何在Cofoja中使用离线字节码编写器?

[英]How to use offline bytecode writer with Cofoja?

I'm trying to do offline instrumentation of my code using Cofoja (contracts for Java). 我正在尝试使用Cofoja (Java合同)对代码进行脱机检测。 I cannot seem to get the contracts in the compiled class file using the offline bytecode writer (this feature is briefly mentioned in the Invocation section of the GitHub page). 我似乎无法使用离线字节码编写器在已编译的类文件中获取合同(此功能在GitHub页面的“ 调用”部分中已作简要介绍)。 I execute the resulting class file and purposely fail a contract. 我执行生成的类文件,并故意使合同失败。 Nothing happens. 什么都没发生。

Here is my Java code... In my main I simply do something like: return divide(10, 0); 这是我的Java代码...在我的主代码中,我只是简单地执行以下操作: return divide(10, 0);

  @Requires("y != 0")
  public static int divide(int x, int y)
  {
    return x / y;
  }

Then I do the following: 然后,我执行以下操作:

I build the .java file via my IDE, Intellij and get the class file. 我通过我的IDE Intellij构建.java文件,并获取类文件。
Then execute the offline bytecode writer like this: 然后执行离线字节码编写器,如下所示:

java -Dcom.google.java.contract.classoutput=cofoja -cp cofoja.asm-1.2-20140817.jar com.google.java.contract.core.agent.PreMain JavaTest.class

This results in another "JavaTest.class" file being generated in the "cofoja" directory. 这导致在“ cofoja”目录中生成另一个“ JavaTest.class”文件。 However, when I execute it I don't see any contract errors. 但是,当我执行它时,我看不到任何合同错误。

Does anyone know the correct steps to use "com.google.java.contract.core.agent.PreMain" to generate class files with contracts weaved in? 有谁知道使用“ com.google.java.contract.core.agent.PreMain”生成带有已编入合同的类文件的正确步骤?

Just for future reference, I went and looked at the source code for com.google.java.contract.core.agent.PreMain. 仅供以后参考,我查看了com.google.java.contract.core.agent.PreMain的源代码。 It turns out that it expects the contract files and helper files to live in the same place as your regular java class files. 事实证明,它希望合同文件和帮助文件与常规Java类文件位于同一位置。 Once I include the class files along with the contract files (in the same directory) this started working. 一旦我将类文件和合同文件(位于同一目录中)一起包括在内,便开始工作。
So to recap: 总结一下:

java -d {output_dir} -cp {your_classpath} "-Acom.google.java.contract.classoutput={output_dir}" "-Acom.google.java.contract.classpath={your_classpath}" "-Acom.google.java.contract.sourcepath={your_sources_dir}" -processor com.google.java.contract.core.apt.AnnotationProcessor

to compile java classes and cofoja contract files. 编译Java类和cofoja合同文件。 Make sure the class files go in the same directory as the cofoja contract files. 确保类文件与cofoja合同文件位于同一目录中。
Then execute this: 然后执行以下命令:

java -Dcom.google.java.contract.classoutput={output_dir} -cp cofoja.asm-1.2-20140817.jar com.google.java.contract.core.agent.PreMain {all .class files separated by spaces}

This command will generate single .class files with contracts built in which you can then compile into a jar file. 此命令将生成带有合同的单个.class文件,您可以在其中将其编译为jar文件。 Note that you need to have a list of all of your original class files as the arguments to the last command and make sure the contract files and helper files are sitting next to the corresponding class file. 请注意,您需要拥有所有原始类文件的列表作为最后一个命令的参数,并确保合同文件和帮助文件位于相应的类文件旁边。

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

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