-
Notifications
You must be signed in to change notification settings - Fork 7
Installation
BenRomberg edited this page Nov 5, 2012
·
5 revisions
-
Download the most recent c4j.zip and extract it somewhere safe, e.g. to
c:\tools\c4j
. - Add
-ea -javaagent:c:/tools/c4j/c4j-[version].jar
to your VM Arguments, matching the path in step 1. Make sure to use the correct version.
- If you want to use C4J with Eclipse, it is advisable to Duplicate your currently used JRE (Window -> Preferences, Java -> Installed JREs) and add the VM Argument to the "Default VM Arguments". You can then either select this VM for all of your projects, or configure the build path of the projects you want to use C4J with to use this JRE.
- Add
c:/tools/c4j/c4j-[version].jar
to the classpath of your project. - You're now ready to start using contracts in your project. Try adding the following class to your project and execute it using the javaagent-parameter:
import static de.vksi.c4j.Condition.preCondition;
import de.vksi.c4j.ContractReference;
public class C4JTest {
public static void main(String... args) {
DummyClass dummy = new DummyClass();
System.out.println("This should work:");
dummy.method(1);
System.out.println("This should throw an AssertionError:");
dummy.method(0);
}
@ContractReference(DummyContract.class)
public static class DummyClass {
public void method(int arg) {
}
}
public static class DummyContract extends DummyClass {
@Override
public void method(final int arg) {
if (preCondition()) {
assert arg > 0;
}
}
}
}
- It is advisable to add the following Type to your favorites: In the Eclipse Preferences, Java -> Editor -> Content Assist -> Favorites, click on "New Type..." and enter
de.vksi.c4j.Condition
. Now, when defining pre- and post-conditions, you can just typepre
and hit Ctrl+Space to automatically add the static import to your class file.
There are various configuration options that can be configured by declaring a c4j-local.xml
or c4j-global.xml
on the classpath. For a complete list of options, consult the XSD-Files included in the distribution JAR file.
Note that there can be multiple c4j-local.xml files on the classpath, but only a single c4j-global.xml.
For an example configuration, check out the configuration for the system tests.