/** * Base template for a resource representation. * Covers the resource attributes * * @param index * @param plan * @return */ public JavaType create(ClassIndex index, ClassPlan plan) { // base class JavaInterfaceSource type = Roaster.parse( JavaInterfaceSource.class, "public interface " + plan.getClassName() + "Supplier<T extends " + plan.getClassName() + "> {}" ); type.setPackage(plan.getPackageName()); type.addAnnotation(FunctionalInterface.class); addGet( type, plan ); return type; }
protected void addAndThen(JavaInterfaceSource type, ClassPlan plan) { /* final MethodSource<JavaInterfaceSource> method = type.addMethod(); method.addParameter( plan.getClassName() + "Consumer<T>", "after" ); method.setName("andThen") .setBody( "return (c)->{ this.accept(c); after.accept(c); };") .setReturnType( plan.getClassName() +"Consumer<T>" ) .setDefault(true); */ type.addMethod( "default " + plan.getClassName() + "Consumer<T> andThen(" + plan.getClassName() + "Consumer<T> after) {\n" + " return (c)->{ this.accept(c); after.accept(c);};\n" + "}\n"); }
public void visit(JavaInterfaceSource interfaceSource) { for (AnnotationSource annoSource : interfaceSource.getAnnotations()) { visit(annoSource); } for (FieldSource<JavaInterfaceSource> fieldSource : interfaceSource.getFields()) { visit(fieldSource); } for (MethodSource methodSource : interfaceSource.getMethods()) { visit(methodSource); } }
@Override public String generate(final Class beanDef, JavaInterfaceSource javaInterface) { // final JavaInterfaceSource javaInterface = Roaster.create(JavaInterfaceSource.class); manageGenerics(beanDef, javaInterface); javaInterface.setPackage(beanDef._package().qualifiedName()).setName(beanDef.name()); // Manage extends beanDef.implementedTypes().stream().forEach(i -> javaInterface.addInterface(i.qualifiedNameWithParameter())); beanDef.properties().stream().forEach(propDef -> { writeField(javaInterface, propDef); }); beanDef.operations().stream().forEach(o -> { writeOperation(javaInterface, o); }); // return formatClass(javaInterface); return javaInterface.toString(); }
/** * Base template for a resource representation. * Covers the resource attributes * * @param index * @param plan * @return */ public JavaType create(ClassIndex index, ClassPlan plan) { // base class JavaInterfaceSource type = Roaster.parse( JavaInterfaceSource.class, "public interface " + plan.getClassName() + "Consumer<T extends " + plan.getClassName() + "<T>> {}" ); type.setPackage(plan.getPackageName()); type.addImport(plan.getPackageName() + "." + plan.getClassName()); type.addAnnotation(FunctionalInterface.class); addAccept(type, plan); addAndThen(type, plan); return type; }
private void createCRUDRepository(JavaClassSource entitySource, Project project) { // build interface declaration final String name = entitySource.getName(); final String idFieldType = entitySource.getFields().stream() .filter(field -> field.hasAnnotation(Id.class)) .findFirst() .orElseThrow(IllegalArgumentException::new) .getType() .getSimpleName(); final String repoInterfaceDeclaration = "public interface " + name + "Repository extends CrudRepository<" + name + "," + idFieldType + "> {}"; JavaInterfaceSource repoSource = Roaster.parse(JavaInterfaceSource.class, repoInterfaceDeclaration) .setPackage(entitySource.getPackage()); repoSource.addImport("org.springframework.data.repository.CrudRepository"); Roaster.format(repoSource.toString()); // Create Java Classes Greeting and GreetingProperties JavaSourceFacet facet = project.getFacet(JavaSourceFacet.class); facet.saveJavaSource(repoSource); }
for (MethodSource<JavaInterfaceSource> method : interfaceSource.getMethods()) usedImports.add(interfaceSource.getImport(method.getReturnType().getQualifiedName())); usedImports.add(interfaceSource.getImport(parameter.getType().getQualifiedName())); for (Import imprt : interfaceSource.getImports())
/** * Base template for a resource representation. * Covers the resource attributes * * @param index * @param plan * @return */ public JavaType create(ClassIndex index, ClassPlan plan) { // base class JavaInterfaceSource type = Roaster.parse( JavaInterfaceSource.class, "public interface " + plan.getClassName() + "Consumer<T extends " + plan.getClassName() + "<T>> {}" ); type.setPackage(plan.getPackageName()); type.addImport(plan.getPackageName() + "." + plan.getClassName()); type.addAnnotation(FunctionalInterface.class); addAccept(type, plan); addAndThen(type, plan); return type; }
for (MethodSource<JavaInterfaceSource> method : interfaceSource.getMethods()) usedImports.add(interfaceSource.getImport(method.getReturnType().getQualifiedName())); usedImports.add(interfaceSource.getImport(parameter.getType().getQualifiedName())); for (Import imprt : interfaceSource.getImports())
/** * Base template for a resource representation. * Covers the resource attributes * * @param index * @param plan * @return */ public JavaType create(ClassIndex index, ClassPlan plan) { // base class JavaInterfaceSource type = Roaster.parse( JavaInterfaceSource.class, "public interface " + plan.getClassName() + "Supplier<T extends " + plan.getClassName() + "> {}" ); type.setPackage(plan.getPackageName()); type.addAnnotation(FunctionalInterface.class); addGet( type, plan ); return type; }
protected void addAndThen(JavaInterfaceSource type, ClassPlan plan) { /* final MethodSource<JavaInterfaceSource> method = type.addMethod(); method.addParameter( plan.getClassName() + "Consumer<T>", "after" ); method.setName("andThen") .setBody( "return (c)->{ this.accept(c); after.accept(c); };") .setReturnType( plan.getClassName() +"Consumer<T>" ) .setDefault(true); */ type.addMethod( "default " + plan.getClassName() + "Consumer<T> andThen(" + plan.getClassName() + "Consumer<T> after) {\n" + " return (c)->{ this.accept(c); after.accept(c);};\n" + "}\n"); }
protected void addGet(JavaInterfaceSource type, ClassPlan plan) { final MethodSource<JavaInterfaceSource> method = type.addMethod(); method.getJavaDoc() .setText("Constructed instance of " + plan.getClassName() + " resource") .addTagValue("@return", "The instance"); method.setPublic() .setName("get") .setReturnType(plan.getClassName()); }
protected void addGet(JavaInterfaceSource type, ClassPlan plan) { final MethodSource<JavaInterfaceSource> method = type.addMethod(); method.getJavaDoc() .setText("Constructed instance of " + plan.getClassName() + " resource") .addTagValue("@return", "The instance"); method.setPublic() .setName("get") .setReturnType(plan.getClassName()); }
protected void addAccept(JavaInterfaceSource type, ClassPlan plan) { final MethodSource<JavaInterfaceSource> method = type.addMethod(); method.getJavaDoc() .setText("Configure a pre-constructed instance of " + plan.getClassName() + " resource") .addTagValue("@parameter", "Instance of " + plan.getClassName() + " to configure") .addTagValue("@return", "nothing"); method.addParameter("T", "value"); method.setName("accept") .setReturnType("void"); }
protected void addAccept(JavaInterfaceSource type, ClassPlan plan) { final MethodSource<JavaInterfaceSource> method = type.addMethod(); method.getJavaDoc() .setText("Configure a pre-constructed instance of " + plan.getClassName() + " resource") .addTagValue("@parameter", "Instance of " + plan.getClassName() + " to configure") .addTagValue("@return", "nothing"); method.addParameter("T", "value"); method.setName("accept") .setReturnType("void"); }