Cofoja : error: error in contract: package com.google.java.contract does not exist

冷暖自知 提交于 2019-12-11 02:19:55

问题


I'm trying to use Cofoja, the Google library for contract programming. I've copied/pasted the build file wich I'm able to compile my library with succesfully.

But when I start adding one annotation to specify a simple contract rule, I cannot compile anymore and get this console output :

build:
[javac] C:\Users\Admin\test\Seismi\build.xml:24: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
[javac] Compiling 2 source files to C:\Users\Admin\test\Seismi\build\classes
[javac] C:\Users\Admin\test\Seismi\build.xml:31: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
[javac] Compiling 9 source files to C:\Users\Admin\test\Seismi\build\contracts
[javac] error: error in contract: package com.google.java.contract does not exist
[javac] error: error in contract: package com.google.java.contract does not exist
[javac] error: error in contract: package com.google.java.contract does not exist
[javac] error: error in contract: package com.google.java.contract does not exist
[javac] error: error in contract: package com.google.java.contract does not exist
[javac] error: error in contract: package com.google.java.contract does not exist
[javac] error: error in contract: package com.google.java.contract.core.agent does not exist
[javac] error: error in contract: package com.google.java.contract.core.agent does not exist
[javac] error: error in contract: package com.google.java.contract.core.agent does not exist
[javac] C:\Users\Admin\test\Seismi\src\com\backpapp\requestmanager\IResult.java:7: error: error in contract: package com.google.java.contract does not exist
[javac]     @Ensures({"result != null","!result.isEmpty()"})
[javac]     ^
[javac]   clause: result != null
[javac]                         ^
[javac] 13 errors

Here is the IResult.java :

import com.google.java.contract.Ensures;

public interface IResult {
    @Ensures({"result != null","!result.isEmpty()"})
    public String getId();
}

And my ant file :

<project name="Seismi" default="dist" basedir=".">

  <!-- Project directories. -->
  <property name="src.dir" location="src"/>
  <property name="dist.dir" location="dist"/>
  <property name="lib.dir" location="libs"/>
  <property name="build.dir" location="build"/>
  <property name="classes.dir" location="${build.dir}/classes"/>
  <property name="contracts.dir" location="${build.dir}/contracts"/>


  <property name="cofoja" value="cofoja.asm.jar"/>

   <path id="class.path">
     <pathelement location="${lib.dir}/${cofoja}"/>   
     <pathelement location="${lib.dir}/json-simple-1.1.1.jar"/>
   </path>

  <target name="build">
    <mkdir dir="${build.dir}"/>

    <!-- Compiles WITHOUT contracts. -->
    <mkdir dir="${classes.dir}"/>
    <javac srcdir="${src.dir}" destdir="${classes.dir}">
      <classpath refid="class.path"/>
      <compilerarg value="-proc:none"/>
    </javac>

    <!-- Compiles ONLY the contracts. -->
    <mkdir dir="${contracts.dir}"/>
    <javac srcdir="${src.dir}" destdir="${contracts.dir}">
      <classpath>
        <path refid="class.path"/>
        <!-- Add ${classes.dir} to the second compilation class path
             so that Cofoja finds your already compiled class files. -->
        <pathelement path="${classes.dir}"/>
      </classpath>
      <compilerarg value="-proc:only"/>
    </javac>
  </target>

  <target name="dist" depends="build">
    <mkdir dir="${dist.dir}"/>
    <jar destfile="${dist.dir}/${ant.project.name}.jar">
      <fileset dir="${classes.dir}"/>
      <fileset dir="${contracts.dir}"/>

    </jar>
  </target>


  <target name="clean">
    <delete dir="${build.dir}"/>
    <delete dir="${dist.dir}"/>
  </target>
</project>

Any clue is more than welcome, I've been struggling all day on this.


回答1:


After spending best part of a day on this issue, the following was the solution

Add the classpath, sourcepath and classoutput compiler args to your compile only for contracts (will look like this)

<!-- Compiles ONLY the contracts. -->
<mkdir dir="${contracts.dir}"/>
<javac srcdir="${src.dir}" destdir="${contracts.dir}">
  <classpath>
    <path refid="class.path"/>
    <!-- Add ${classes.dir} to the second compilation class path
         so that Cofoja finds your already compiled class files. -->
    <pathelement path="${classes.dir}"/>
  </classpath>
  <compilerarg value="-Acom.google.java.contract.classpath=${classpathBuild}" />
  <compilerarg value="-Acom.google.java.contract.sourcepath=${src.dir}" />
  <compilerarg value="-Acom.google.java.contract.classoutput=${classes.dir}"/>
  <compilerarg value="-proc:only" />
</javac>

Regards



来源:https://stackoverflow.com/questions/27702652/cofoja-error-error-in-contract-package-com-google-java-contract-does-not-exi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!