use of org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails in project spring-roo by spring-projects.
the class UpdateCompilationUnitTest method testSimpleClassAddField.
@Test
public void testSimpleClassAddField() throws Exception {
// Set up
final File file = getResource(SIMPLE_CLASS_FILE_PATH);
final String fileContents = getResourceContents(file);
final ClassOrInterfaceTypeDetails simpleInterfaceDetails = typeParsingService.getTypeFromString(fileContents, SIMPLE_CLASS_DECLARED_BY_MID, SIMPLE_CLASS_TYPE);
final FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(SIMPLE_CLASS_DECLARED_BY_MID, Modifier.PRIVATE, new JavaSymbolName("newFieldAddedByCode"), new JavaType(String.class), "\"Create by code\"");
final ClassOrInterfaceTypeDetails newSimpleInterfaceDetails = addField(simpleInterfaceDetails, fieldBuilder.build());
// Invoke
final String result = typeParsingService.getCompilationUnitContents(newSimpleInterfaceDetails);
// save to file for debug
saveResult(file, result, "-addedField");
checkSimpleClass(result);
assertTrue(result.contains("private String newFieldAddedByCode = \"Create by code\";"));
}
use of org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails in project spring-roo by spring-projects.
the class UpdateCompilationUnitTest method testRegresion_ROO_1505.
@Test
public void testRegresion_ROO_1505() throws Exception {
// Set up
final File file = getResource(ROO1505_CLASS_FILE_PATH);
final String fileContents = getResourceContents(file);
final ClassOrInterfaceTypeDetails simpleInterfaceDetails = typeParsingService.getTypeFromString(fileContents, ROO1505_CLASS_DECLARED_BY_MID, ROO1505_CLASS_TYPE);
// Invoke
final String result = typeParsingService.getCompilationUnitContents(simpleInterfaceDetails);
// save to file for debug
saveResult(file, result);
check_ROO_1505_Class(result);
}
use of org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails in project spring-roo by spring-projects.
the class UpdateCompilationUnitTest method testSimpleClass3NoChanges.
@Test
public void testSimpleClass3NoChanges() 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);
// Invoke
final String result = typeParsingService.getCompilationUnitContents(simpleInterfaceDetails);
// Save to file for debug
saveResult(file, result);
checkSimple3Class(result);
}
use of org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails 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;"));
}
use of org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails in project spring-roo by spring-projects.
the class DefaultPhysicalTypeMetadataProvider method get.
public MetadataItem get(final String metadataIdentificationString) {
if (fileManager == null) {
fileManager = getFileManager();
}
Validate.notNull(fileManager, "FileManager is required");
if (metadataDependencyRegistry == null) {
metadataDependencyRegistry = getMetadataDependencyRegistry();
}
Validate.notNull(metadataDependencyRegistry, "MetadataDependencyRegistry is required");
if (projectOperations == null) {
projectOperations = getProjectOperations();
}
Validate.notNull(projectOperations, "ProjectOperations is required");
if (typeLocationService == null) {
typeLocationService = getTypeLocationService();
}
Validate.notNull(typeLocationService, "TypeLocationService is required");
if (typeParsingService == null) {
typeParsingService = getTypeParsingService();
}
Validate.notNull(typeParsingService, "TypeParsingService is required");
Validate.isTrue(PhysicalTypeIdentifier.isValid(metadataIdentificationString), "Metadata id '%s' is not valid for this metadata provider", metadataIdentificationString);
final String canonicalPath = typeLocationService.getPhysicalTypeCanonicalPath(metadataIdentificationString);
if (StringUtils.isBlank(canonicalPath)) {
return null;
}
metadataDependencyRegistry.deregisterDependencies(metadataIdentificationString);
if (!fileManager.exists(canonicalPath)) {
// that was found but could not be parsed
return null;
}
final JavaType javaType = PhysicalTypeIdentifier.getJavaType(metadataIdentificationString);
final ClassOrInterfaceTypeDetails typeDetails = typeParsingService.getTypeAtLocation(canonicalPath, metadataIdentificationString, javaType);
if (typeDetails == null) {
return null;
}
final PhysicalTypeMetadata result = new DefaultPhysicalTypeMetadata(metadataIdentificationString, canonicalPath, typeDetails);
final ClassOrInterfaceTypeDetails details = result.getMemberHoldingTypeDetails();
if (details != null && details.getPhysicalTypeCategory() == PhysicalTypeCategory.CLASS && details.getExtendsTypes().size() == 1) {
// This is a class, and it extends another class
if (details.getSuperclass() != null) {
// We have a dependency on the superclass, and there is metadata
// available for the superclass
// We won't implement the full MetadataNotificationListener
// here, but rely on MetadataService's fallback
// (which is to evict from cache and call get again given
// JavaParserMetadataProvider doesn't implement
// MetadataNotificationListener, then notify everyone we've
// changed)
final String superclassId = details.getSuperclass().getDeclaredByMetadataId();
metadataDependencyRegistry.registerDependency(superclassId, result.getId());
} else {
// type change, in the hope we discover our parent someday
for (final LogicalPath sourcePath : projectOperations.getPathResolver().getSourcePaths()) {
final String possibleSuperclass = PhysicalTypeIdentifier.createIdentifier(details.getExtendsTypes().get(0), sourcePath);
metadataDependencyRegistry.registerDependency(possibleSuperclass, result.getId());
}
}
}
MemberDetails memberDetails = new MemberDetailsBuilder(Arrays.asList(details)).build();
// Loop until such time as we complete a full loop where no changes are
// made to the result
boolean additionalLoopRequired = true;
while (additionalLoopRequired) {
additionalLoopRequired = false;
for (final MemberDetailsDecorator decorator : decorators) {
final MemberDetails newResult = decorator.decorateTypes(DefaultPhysicalTypeMetadataProvider.class.getName(), memberDetails);
Validate.isTrue(newResult != null, "Decorator '%s' returned an illegal result", decorator.getClass().getName());
if (!newResult.equals(memberDetails)) {
additionalLoopRequired = true;
memberDetails = newResult;
}
}
}
return new DefaultPhysicalTypeMetadata(metadataIdentificationString, canonicalPath, (ClassOrInterfaceTypeDetails) memberDetails.getDetails().get(0));
}
Aggregations