Checker Framework logo

Perhaps, there are other runtime exceptions you wish to prevent or maybe other properties of data that should always hold. In both these cases and others, you you might wish to write your own type-checker. This section of the tutorial is for those who are interested in writing their own type-checkers. If you do not wish to write a new type-checker, feel free to skip this section.

Suppose that you wish to only allow encrypted information to be sent over the internet. To do so, you can write an Encryption Checker.

1. Import encryption-checker project

This project has three source files: Encrypted.java and PossibleUnencrypted.java are the type qualifier definitions and EncryptedDemo.java is the the example program that use the Encrypted checker. You will need to setup your JRE to use the Checker Framework jars, see Issue 1 below. This will fix the "TYPE_USE cannot be resolved or is not a field" error. The import myqual.Encrypted is never used warning should be ignored. When building the encryption-checker project, set your -source and -target levels to at least 1.7 (1.8 is recommended). See Issue 2.

2. Set Up the Subtyping Checker

The Encryption Checker is built on top of the Subtyping Checker. The Subtyping Checker allows you to enforce a basic type systems by listing its qualifiers when type checking. To set up the Subtyping Checker to use the Encryption Checker's qualifiers, do the following:

  1. Navigate to Eclipse->Preferences->Checker Framework
  2. Under Additional compiler parameters, add -Aquals=myqual.Encrypted,myqual.PossibleUnencrypted
  3. Click Ok

3. Run the Encrypted Checker

Right Click on the src package, select Checker Framework->Run Built-in Checker->Subtyping Checker. You should see the following errors:

incompatible types in assignment.
		/*@Encrypted*/ int encryptInt = (character + OFFSET) % Character.MAX_VALUE ;
  found   : int
  required: @Encrypted int	EncryptionDemo.java	/encryption-checker/src/encrypted	
incompatible types in argument.
		sendOverInternet(password);
  found   : String
  required: @Encrypted String	EncryptionDemo.java	/encryption-checker/src/encrypted	

4. Suppress the First Error

The first error needs to be suppressed, because the string on the left is considered "encrypted" in this encryption scheme. All @SuppressWarnings should have a comment explaining why suppressing the warning is the correct action. The comment should also be initialed and dated. See the correction below.

// The SuppressWarnings is necessary because, the type system is not capable of
//validating that the return value is encrypted. -SOM 01/25/2013
@SuppressWarnings("encrypted")
private /*@Encrypted*/ char encryptCharacter(char character) {

      

5. Run the Encryption Checker

You will see the following error:

incompatible types in argument.
		sendOverInternet(password);
  found   : String
  required: @Encrypted String	EncryptionDemo.java	/encryption-checker/src/encrypted

This is a real error, because the programmer is trying to send a password over the internet without encrypting it first.

6. Correct the Second Error

The password should be encrypted before it is sent over the internet. The correction is below.

void sendPassword() {
    String password = getUserPassword();
    sendOverInternet(encrypt(password));
}

Writing a Type annotation requires ElementType.TYPE_USE and/or ElementType.TYPE_PARAMETER which are part of Java 8, but not yet supported by Eclipse. This means that Eclipse will give an error: "TYPE_USE cannot be resolved or is not a field."

Workaround
  1. Navigate to Eclipse->Preferences->Java->Installed JRES
  2. Highlight a preferred JRE and click Duplicate...
  3. (Optionally) Add Checker Framework to the JRE name
  4. Click Add External Jars
  5. Navigate to eclipse->plugins->checker.framework.eclipse.plugin_VERSION->lib
  6. Select javac.jar and either jdk7.jar or jdk8.jar depending on the version the JRE you are duplicating. Click Open
  7. Move the two jars to the top of the system libraries list. Click Finish

To update your -source and -target levels:

Workaround:
  1. Right click the encryption-checker project
  2. Select properties->Java Compiler
  3. Check the box labeled "Enable project specific settings"
  4. Update the compliance setting to 1.8 (recommended) or 1.7.