NotNullValidator implements Validator { @Override public void addValueSpaceCheck(MethodSpec.Builder methodBuilder, ParameterSpec symbol, RDFSClazz range) { ClassName illegalArgEx = ClassName.get(IllegalArgumentException.class); methodBuilder.beginControlFlow("if($N == null)", symbol) .addStatement("throw new $T($S)", illegalArgEx, "Value must not be null") .endControlFlow(); }  @Override void addValueSpaceCheck(MethodSpec.Builder methodBuilder, ParameterSpec symbol, RDFSClazz range); @Override String getValueSpaceDefinition(RDFSClazz clazz); @Override boolean isValueSpaceConstrained(RDFSClazz clazz);  }
@Test public void addValueSpaceCheck() throws Exception { ParameterSpec paramS = ParameterSpec.builder(ClassName.get(String.class), "s") .build(); MethodSpec.Builder method = MethodSpec.methodBuilder("foo") .addModifiers(Modifier.PUBLIC) .addParameter(paramS) .returns(void.class); NotNullValidator validator = new NotNullValidator(); validator.addValueSpaceCheck(method, paramS, rdfsClazz); String code = method.build().code.toString(); Matcher matcher = Pattern.compile("if\\s*\\((s\\s*==\\s*null|null\\s*==\\s*s)\\)").matcher(code); assertTrue(matcher.find()); }