Search in sources :

Example 21 with FieldMetadataBuilder

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

the class UpdateCompilationUnitTest method testSimpleClass3AddField.

@Test
public void testSimpleClass3AddField() throws Exception {
    // Set up
    final File file = getResource(SIMPLE_CLASS3_FILE_PATH);
    final String fileContents = getResourceContents(file);
    final ClassOrInterfaceTypeDetails simpleInterfaceDetails = typeParsingService.getTypeFromString(fileContents, SIMPLE_CLASS3_DECLARED_BY_MID, SIMPLE_CLASS3_TYPE);
    final SetField fieldDetails = new SetField(SIMPLE_CLASS3_DECLARED_BY_MID, new JavaType(SET.getFullyQualifiedTypeName(), 0, DataType.TYPE, null, Arrays.asList(SIMPLE_CLASS3_TYPE)), new JavaSymbolName("children"), SIMPLE_CLASS3_TYPE, Cardinality.ONE_TO_MANY, new Cascade[] { Cascade.REMOVE }, false);
    final FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(fieldDetails.getPhysicalTypeIdentifier(), Modifier.PRIVATE, new ArrayList<AnnotationMetadataBuilder>(), fieldDetails.getFieldName(), fieldDetails.getFieldType());
    fieldBuilder.setFieldInitializer("new HashSet<SimpleClass3>()");
    final ClassOrInterfaceTypeDetails newClassDetails = addField(simpleInterfaceDetails, fieldBuilder.build());
    // Invoke
    final String result = typeParsingService.getCompilationUnitContents(newClassDetails);
    saveResult(file, result, "-addField");
    checkSimple3Class(result);
    assertTrue(result.contains("private Set<SimpleClass3> children = new HashSet<SimpleClass3>();"));
    // Add another
    final ClassOrInterfaceTypeDetails simpleInterfaceDetails2 = typeParsingService.getTypeFromString(result, SIMPLE_CLASS3_DECLARED_BY_MID, SIMPLE_CLASS3_TYPE);
    final ReferenceField fieldDetails2 = new ReferenceField(SIMPLE_CLASS3_DECLARED_BY_MID, SIMPLE_CLASS2_TYPE, new JavaSymbolName("referenceField"), Cardinality.MANY_TO_ONE, new Cascade[] { Cascade.REFRESH });
    final FieldMetadataBuilder fieldBuilder2 = new FieldMetadataBuilder(fieldDetails2.getPhysicalTypeIdentifier(), Modifier.PRIVATE, new ArrayList<AnnotationMetadataBuilder>(), fieldDetails2.getFieldName(), fieldDetails2.getFieldType());
    final ClassOrInterfaceTypeDetails newClassDetails2 = addField(simpleInterfaceDetails2, fieldBuilder2.build());
    // Invoke
    final String result2 = typeParsingService.getCompilationUnitContents(newClassDetails2);
    // Save to file for debug
    saveResult(file, result2, "-addField2");
    checkSimple3Class(result2);
    assertTrue(result.contains("private Set<SimpleClass3> children = new HashSet<SimpleClass3>();"));
    assertTrue(result2.contains("private SimpleClass2 referenceField;"));
}
Also used : JavaType(org.springframework.roo.model.JavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) SetField(org.springframework.roo.classpath.operations.jsr303.SetField) File(java.io.File) ReferenceField(org.springframework.roo.classpath.operations.jsr303.ReferenceField) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder) Test(org.junit.Test)

Example 22 with FieldMetadataBuilder

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

the class LinkFactoryMetadata method getConstantForMethodName.

/**
 * Builds and returns a private static final field with provided field name and initializer
 *
 * @param methodName
 * @return
 */
private FieldMetadataBuilder getConstantForMethodName(String methodName) {
    // If already exists, return the existing one
    if (constantForMethods.get(methodName) != null) {
        return constantForMethods.get(methodName);
    }
    // Create a new one and cache it
    FieldMetadataBuilder constant = new FieldMetadataBuilder(getId(), Modifier.PUBLIC + Modifier.STATIC + Modifier.FINAL, new JavaSymbolName(methodName.toUpperCase()), JavaType.STRING, "\"" + methodName + "\"");
    constantForMethods.put(methodName, constant);
    return constant;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 23 with FieldMetadataBuilder

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

the class JsonControllerIntegrationTestMetadata method getEntityServiceField.

/**
 * Builds and returns the entity service field, annotated with @MockBean
 *
 * @return {@link FieldMetadataBuilder}
 */
private FieldMetadataBuilder getEntityServiceField() {
    FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(this.getId(), Modifier.PRIVATE, this.serviceFieldName, this.entityService, null);
    // Add @Autowired
    fieldBuilder.addAnnotation(new AnnotationMetadataBuilder(SpringJavaType.MOCK_BEAN));
    return fieldBuilder;
}
Also used : AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 24 with FieldMetadataBuilder

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

the class JsonControllerIntegrationTestMetadata method getMockMvcField.

/**
 * Builds and returns a {@link MockMvc} field, annotated with @Autowired
 *
 * @return {@link FieldMetadataBuilder}
 */
private FieldMetadataBuilder getMockMvcField() {
    FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(this.getId(), Modifier.PRIVATE, MOCK_MVC_FIELD_NAME, SpringJavaType.MOCK_MVC, null);
    // Add @Autowired
    fieldBuilder.addAnnotation(new AnnotationMetadataBuilder(SpringJavaType.AUTOWIRED));
    return fieldBuilder;
}
Also used : AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 25 with FieldMetadataBuilder

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

the class RepositoryJpaIntegrationTestMetadata method getDodField.

/**
 * Builds and returns a `private` data-on-demand field.
 *
 * @return {@link FieldMetadataBuilder}
 */
private FieldMetadataBuilder getDodField() {
    FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(this.getId(), Modifier.PRIVATE, DATA_ON_DEMAND_FIELD_NAME, this.annotationValues.getDodClass(), null);
    // Add @Autowired
    fieldBuilder.addAnnotation(new AnnotationMetadataBuilder(SpringJavaType.AUTOWIRED));
    return fieldBuilder;
}
Also used : AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Aggregations

FieldMetadataBuilder (org.springframework.roo.classpath.details.FieldMetadataBuilder)66 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)48 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)40 ArrayList (java.util.ArrayList)21 JavaType (org.springframework.roo.model.JavaType)19 JdkJavaType (org.springframework.roo.model.JdkJavaType)10 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)8 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)7 FieldDetails (org.springframework.roo.classpath.details.FieldDetails)7 ForeignKey (org.springframework.roo.addon.dbre.addon.model.ForeignKey)5 CommentStructure (org.springframework.roo.classpath.details.comments.CommentStructure)5 JavadocComment (org.springframework.roo.classpath.details.comments.JavadocComment)5 LinkedHashMap (java.util.LinkedHashMap)4 Reference (org.springframework.roo.addon.dbre.addon.model.Reference)4 Table (org.springframework.roo.addon.dbre.addon.model.Table)4 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)4 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)4 CollectionField (org.springframework.roo.classpath.operations.jsr303.CollectionField)4 DateField (org.springframework.roo.classpath.operations.jsr303.DateField)4 EnumDetails (org.springframework.roo.model.EnumDetails)4