public static TestableContribution of(PojoInfo pojoInfo) { Naming naming = pojoInfo.naming(); TypeName superClassTypeName = naming.superClassTypeName(); ParameterizedTypeName testerTypeName = ParameterizedTypeName.get(TESTER, naming.superClass()); return superClassTypeName instanceof ParameterizedTypeName ? new Generic(pojoInfo, testerTypeName) : new TestableContribution(pojoInfo, testerTypeName); }
private TypeSpec type() { ClassName superClass = naming.superClass(); return TypeSpec.classBuilder("Abstract" + superClass.simpleName() + "Loader") .addAnnotation(GENERATED) .addModifiers(Modifier.ABSTRACT) .addSuperinterface(ParameterizedTypeName.get(ClassName.get(ResultSetLoader.class), superClass)) .addField(inject.fieldSpec()) .addMethods(pojoInfo.constructorContextList() .stream() .map(IsRelationalLoaderConstructor::of) .map(IsRelationalLoaderConstructor::execute) .collect(Collectors.toList())) .addMethod(load()) .addMethods(pojoInfo.propertyList() .stream() .flatMap(property -> property.adapt(this)) .collect(Collectors.toList())) .build(); }
private MethodSpec load() { CodeBlock.Builder body = CodeBlock.builder() .addStatement("$1T rs = new $1T($2S, resultSet)", ResultSetWrapper.class, tableSimpleName()) .add("return new $T(", naming.pojo()) .add("\n $L", inject.name()); pojoInfo.constructorContextList() .stream() .flatMap(ConstructorContext::parameterInfoStream) .map(ParameterInfo::name) .forEach(name -> body.add(",\n $L", name)); pojoInfo.propertyList() .stream() .sorted() .map(property -> property.adapt(IsRelationalLoaderLoadMethod.INSTANCE)) .forEach(body::add); body.add(");\n"); return MethodSpec.methodBuilder("load") .addAnnotation(Override.class) .addModifiers(Modifier.PUBLIC) .addParameter(ResultSet.class, "resultSet") .returns(naming.superClass()) .addCode(body.build()) .build(); }
ClassName superClassName = naming.superClass(); if (!returnTypeInfo.className().equals(superClassName)) { methodInfo.compilationError("@Setter method must return %s", superClassName.simpleName());
private CodeBlock fieldInitializer() { Naming naming = pojoInfo.naming(); CodeBlock.Builder init = CodeBlock.builder() .add("$T.of($T.class)\n", TESTER, naming.superClass()); pojoInfo.propertyStream() .filter(property -> !property.hasAnnotation(NotTestable.class)) .forEach(property -> init.add(" .add($S, o -> o.$L())\n", property.name(), property.accessorName())); return init .add(" .build()") .build(); }