Search in sources :

Example 1 with JavadocComment

use of org.springframework.roo.classpath.details.comments.JavadocComment in project spring-roo by spring-projects.

the class JpaDataOnDemandMetadata method getRandomPersistentEntityMethod.

/**
 * @return the "getRandomEntity():Entity" method (never returns null)
 */
private MethodMetadata getRandomPersistentEntityMethod() {
    // Method definition to find or build
    final JavaSymbolName methodName = new JavaSymbolName("getRandom" + this.entity.getSimpleTypeName());
    // Locate user-defined method
    final MethodMetadata userMethod = getGovernorMethod(methodName);
    if (userMethod != null) {
        Validate.isTrue(userMethod.getReturnType().equals(this.entity), "Method '%s' on '%s' must return '%s'", methodName, this.destination, this.entity.getSimpleTypeName());
        this.randomPersistentEntityMethod = userMethod;
        return userMethod;
    }
    // Create method
    final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // init();
    bodyBuilder.appendFormalLine("init();");
    // return data.get(rnd.nextInt(data.size()));
    bodyBuilder.appendFormalLine("return %1$s().get(%2$s().nextInt(%1$s().size()));", getAccessorMethod(getDataField().build()).getMethodName(), getAccessorMethod(getRndField().build()).getMethodName());
    final MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, this.entity, bodyBuilder);
    CommentStructure comment = new CommentStructure();
    JavadocComment javadocComment = new JavadocComment(String.format("Returns a generated and persisted {@link %s} in a random index.", this.entity.getSimpleTypeName()), null, String.format("%1$s a random {@link %1$s}", this.entity.getSimpleTypeName()), null);
    comment.addComment(javadocComment, CommentLocation.BEGINNING);
    methodBuilder.setCommentStructure(comment);
    this.randomPersistentEntityMethod = methodBuilder.build();
    return this.randomPersistentEntityMethod;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) JavadocComment(org.springframework.roo.classpath.details.comments.JavadocComment) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) CommentStructure(org.springframework.roo.classpath.details.comments.CommentStructure)

Example 2 with JavadocComment

use of org.springframework.roo.classpath.details.comments.JavadocComment in project spring-roo by spring-projects.

the class JpaDataOnDemandMetadata method getConstructorWithEntityManagerAndSize.

/**
 * Builds constructor with EntityManager and size arguments.
 *
 * @return {@link ConstructorMetadataBuilder} for adding constructor to ITD
 */
private ConstructorMetadataBuilder getConstructorWithEntityManagerAndSize() {
    ConstructorMetadataBuilder constructorBuilder = new ConstructorMetadataBuilder(getId());
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    constructorBuilder.addParameter(ENTITY_MANAGER_VAR, JpaJavaType.ENTITY_MANAGER);
    constructorBuilder.addParameter(SIZE_VAR, JavaType.INT_PRIMITIVE);
    bodyBuilder.appendFormalLine(String.format("%1$s(%2$s);", getMutatorMethod(getEntityManagerField().build()).getMethodName(), ENTITY_MANAGER_VAR));
    bodyBuilder.appendFormalLine(String.format("%1$s(%2$s);", getMutatorMethod(getSizeField()).getMethodName(), SIZE_VAR));
    constructorBuilder.setModifier(Modifier.PUBLIC);
    constructorBuilder.setBodyBuilder(bodyBuilder);
    CommentStructure comment = new CommentStructure();
    List<String> paramsInfo = new ArrayList<String>();
    paramsInfo.add(ENTITY_MANAGER_VAR + " to persist entities");
    paramsInfo.add(SIZE_VAR + " the number of entities to create and persist initially.");
    JavadocComment javadocComment = new JavadocComment(String.format("Creates a new {@link %s}.", this.governorPhysicalTypeMetadata.getType().getSimpleTypeName()), paramsInfo, null, null);
    comment.addComment(javadocComment, CommentLocation.BEGINNING);
    constructorBuilder.setCommentStructure(comment);
    return constructorBuilder;
}
Also used : JavadocComment(org.springframework.roo.classpath.details.comments.JavadocComment) ConstructorMetadataBuilder(org.springframework.roo.classpath.details.ConstructorMetadataBuilder) ArrayList(java.util.ArrayList) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) CommentStructure(org.springframework.roo.classpath.details.comments.CommentStructure)

Example 3 with JavadocComment

use of org.springframework.roo.classpath.details.comments.JavadocComment in project spring-roo by spring-projects.

the class JpaDataOnDemandMetadata method getNewRandomTransientEntityMethod.

private MethodMetadata getNewRandomTransientEntityMethod() {
    // Method definition to find or build
    final JavaSymbolName methodName = new JavaSymbolName("getNewRandomTransient" + this.entity.getSimpleTypeName());
    // Locate user-defined method
    final MethodMetadata userMethod = getGovernorMethod(methodName);
    if (userMethod != null) {
        Validate.isTrue(userMethod.getReturnType().equals(entity), "Method '%s' on '%s' must return '%s'", methodName, this.destination, this.entity.getSimpleTypeName());
        this.newTransientEntityMethod = userMethod;
        return userMethod;
    }
    // Create method
    final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // int randomIndex = getSize() + rnd.nextInt(Integer.MAX_VALUE - getSize());
    bodyBuilder.appendFormalLine("int randomIndex = %1$s() + %2$s().nextInt(Integer.MAX_VALUE - %1$s());", this.sizeAccesorName, getAccessorMethod(getRndField().build()).getMethodName());
    // return factory.create(randomIndex);
    bodyBuilder.appendFormalLine("return %s().%s(randomIndex);", getAccessorMethod(getEntityFactoryField().build()).getMethodName(), this.entityFactoryMetadata.getCreateFactoryMethodName());
    final MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, this.entity, bodyBuilder);
    CommentStructure comment = new CommentStructure();
    JavadocComment javadocComment = new JavadocComment(String.format("Creates a new transient %s in a random index out of " + "the initial list of the created entities,".concat(IOUtils.LINE_SEPARATOR).concat("with an index greater than {@link %s#getSize()} - 1."), this.entity.getSimpleTypeName(), this.governorPhysicalTypeMetadata.getType().getSimpleTypeName()), null, String.format("%1$s the generated transient {@link %1$s}", this.entity.getSimpleTypeName()), null);
    comment.addComment(javadocComment, CommentLocation.BEGINNING);
    methodBuilder.setCommentStructure(comment);
    this.newTransientEntityMethod = methodBuilder.build();
    return this.newTransientEntityMethod;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) JavadocComment(org.springframework.roo.classpath.details.comments.JavadocComment) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) CommentStructure(org.springframework.roo.classpath.details.comments.CommentStructure)

Example 4 with JavadocComment

use of org.springframework.roo.classpath.details.comments.JavadocComment in project spring-roo by spring-projects.

the class JpaDataOnDemandMetadata method getDataField.

/**
 * @return the "data" field to use, which is either provided by the user or
 *         produced on demand (never returns null)
 */
private FieldMetadataBuilder getDataField() {
    final List<JavaType> parameterTypes = Arrays.asList(entity);
    final JavaType listType = new JavaType(LIST.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, parameterTypes);
    int index = -1;
    while (true) {
        // Compute the required field name
        index++;
        // The type parameters to be used by the field type
        final JavaSymbolName fieldName = new JavaSymbolName("data" + StringUtils.repeat("_", index));
        dataFieldName = fieldName;
        final FieldMetadata candidate = governorTypeDetails.getField(fieldName);
        if (candidate != null) {
            // Verify if candidate is suitable
            if (!Modifier.isPrivate(candidate.getModifier())) {
                // next possible name)
                continue;
            }
            if (!candidate.getFieldType().equals(listType)) {
                // The equals method also verifies type params are present
                continue;
            }
            // we assume the user knows what they're doing and have made one
            return new FieldMetadataBuilder(candidate);
        }
        // Candidate not found, so let's create one
        FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(getId(), Modifier.PRIVATE, new ArrayList<AnnotationMetadataBuilder>(), fieldName, listType);
        CommentStructure comment = new CommentStructure();
        comment.addComment(new JavadocComment("List of created entities."), CommentLocation.BEGINNING);
        fieldBuilder.setCommentStructure(comment);
        return fieldBuilder;
    }
}
Also used : AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) Jsr303JavaType(org.springframework.roo.model.Jsr303JavaType) JdkJavaType(org.springframework.roo.model.JdkJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) JavadocComment(org.springframework.roo.classpath.details.comments.JavadocComment) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) CommentStructure(org.springframework.roo.classpath.details.comments.CommentStructure) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 5 with JavadocComment

use of org.springframework.roo.classpath.details.comments.JavadocComment in project spring-roo by spring-projects.

the class JpaDataOnDemandMetadata method getSizeField.

/**
 * Creates size field.
 *
 * @return {@link FieldMetadataBuilder} for building field into ITD.
 */
private FieldMetadata getSizeField() {
    // Create field
    FieldMetadataBuilder sizeField = new FieldMetadataBuilder(getId(), Modifier.PRIVATE, new ArrayList<AnnotationMetadataBuilder>(), new JavaSymbolName(SIZE_VAR), JavaType.INT_PRIMITIVE);
    CommentStructure comment = new CommentStructure();
    comment.addComment(new JavadocComment("Number of elements to create and persist."), CommentLocation.BEGINNING);
    sizeField.setCommentStructure(comment);
    return sizeField.build();
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) JavadocComment(org.springframework.roo.classpath.details.comments.JavadocComment) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) CommentStructure(org.springframework.roo.classpath.details.comments.CommentStructure) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Aggregations

CommentStructure (org.springframework.roo.classpath.details.comments.CommentStructure)28 JavadocComment (org.springframework.roo.classpath.details.comments.JavadocComment)28 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)23 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)16 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)14 ArrayList (java.util.ArrayList)13 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)12 JavaType (org.springframework.roo.model.JavaType)12 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)11 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)10 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)7 JdkJavaType (org.springframework.roo.model.JdkJavaType)7 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)6 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)6 FieldMetadataBuilder (org.springframework.roo.classpath.details.FieldMetadataBuilder)5 BodyDeclaration (com.github.antlrjavaparser.api.body.BodyDeclaration)4 TypeDeclaration (com.github.antlrjavaparser.api.body.TypeDeclaration)4 AnnotationExpr (com.github.antlrjavaparser.api.expr.AnnotationExpr)4 ClassOrInterfaceType (com.github.antlrjavaparser.api.type.ClassOrInterfaceType)4 ParseException (com.github.antlrjavaparser.ParseException)3