use of org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder in project spring-roo by spring-projects.
the class TypeManagementServiceImpl method addEnumConstant.
public void addEnumConstant(final String physicalTypeIdentifier, final JavaSymbolName constantName) {
Validate.notBlank(physicalTypeIdentifier, "Type identifier not provided");
Validate.notNull(constantName, "Constant name required");
// Obtain the physical type and itd mutable details
final PhysicalTypeMetadata ptm = (PhysicalTypeMetadata) metadataService.get(physicalTypeIdentifier);
Validate.notNull(ptm, "Java source code unavailable for type %s", PhysicalTypeIdentifier.getFriendlyName(physicalTypeIdentifier));
final PhysicalTypeDetails ptd = ptm.getMemberHoldingTypeDetails();
Validate.notNull(ptd, "Java source code details unavailable for type %s", PhysicalTypeIdentifier.getFriendlyName(physicalTypeIdentifier));
final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder((ClassOrInterfaceTypeDetails) ptd);
// Ensure it's an enum
Validate.isTrue(cidBuilder.getPhysicalTypeCategory() == PhysicalTypeCategory.ENUMERATION, "%s is not an enum", PhysicalTypeIdentifier.getFriendlyName(physicalTypeIdentifier));
cidBuilder.addEnumConstant(constantName);
createOrUpdateTypeOnDisk(cidBuilder.build());
}
use of org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder in project spring-roo by spring-projects.
the class ClasspathOperationsImpl method createClass.
@Override
public void createClass(final JavaType name, final boolean rooAnnotations, final LogicalPath path, final JavaType superclass, final JavaType implementsType, final boolean createAbstract, final boolean permitReservedWords) {
if (!permitReservedWords) {
ReservedWords.verifyReservedWordsNotPresent(name);
}
Validate.isTrue(!JdkJavaType.isPartOfJavaLang(name.getSimpleTypeName()), "Class name '%s' is part of java.lang", name.getSimpleTypeName());
int modifier = Modifier.PUBLIC;
if (createAbstract) {
modifier |= Modifier.ABSTRACT;
}
final String declaredByMetadataId = PhysicalTypeIdentifier.createIdentifier(name, path);
final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(declaredByMetadataId, modifier, name, PhysicalTypeCategory.CLASS);
if (!superclass.equals(OBJECT)) {
final ClassOrInterfaceTypeDetails superclassClassOrInterfaceTypeDetails = typeLocationService.getTypeDetails(superclass);
if (superclassClassOrInterfaceTypeDetails != null) {
cidBuilder.setSuperclass(new ClassOrInterfaceTypeDetailsBuilder(superclassClassOrInterfaceTypeDetails));
}
}
final List<JavaType> extendsTypes = new ArrayList<JavaType>();
extendsTypes.add(superclass);
cidBuilder.setExtendsTypes(extendsTypes);
if (implementsType != null) {
final Set<JavaType> implementsTypes = new LinkedHashSet<JavaType>();
final ClassOrInterfaceTypeDetails typeDetails = typeLocationService.getTypeDetails(declaredByMetadataId);
if (typeDetails != null) {
implementsTypes.addAll(typeDetails.getImplementsTypes());
}
implementsTypes.add(implementsType);
cidBuilder.setImplementsTypes(implementsTypes);
}
if (rooAnnotations) {
final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
annotations.add(new AnnotationMetadataBuilder(ROO_JAVA_BEAN));
annotations.add(new AnnotationMetadataBuilder(ROO_TO_STRING));
annotations.add(new AnnotationMetadataBuilder(ROO_EQUALS));
annotations.add(new AnnotationMetadataBuilder(ROO_SERIALIZABLE));
cidBuilder.setAnnotations(annotations);
}
typeManagementService.createOrUpdateTypeOnDisk(cidBuilder.build());
}
use of org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder in project spring-roo by spring-projects.
the class ClasspathOperationsImpl method createInterface.
@Override
public void createInterface(final JavaType name, final LogicalPath path, final boolean permitReservedWords) {
if (!permitReservedWords) {
ReservedWords.verifyReservedWordsNotPresent(name);
}
final String declaredByMetadataId = PhysicalTypeIdentifier.createIdentifier(name, path);
final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(declaredByMetadataId, Modifier.PUBLIC, name, PhysicalTypeCategory.INTERFACE);
typeManagementService.createOrUpdateTypeOnDisk(cidBuilder.build());
}
use of org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder in project spring-roo by spring-projects.
the class ClasspathOperationsImpl method createEnum.
@Override
public void createEnum(final JavaType name, final LogicalPath path, final boolean permitReservedWords) {
if (!permitReservedWords) {
ReservedWords.verifyReservedWordsNotPresent(name);
}
final String physicalTypeId = PhysicalTypeIdentifier.createIdentifier(name, path);
final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(physicalTypeId, Modifier.PUBLIC, name, PhysicalTypeCategory.ENUMERATION);
typeManagementService.createOrUpdateTypeOnDisk(cidBuilder.build());
}
use of org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder in project spring-roo by spring-projects.
the class ClasspathOperationsImpl method createConstructor.
@Override
public void createConstructor(final JavaType name, final Set<String> fields) {
final ClassOrInterfaceTypeDetails javaTypeDetails = typeLocationService.getTypeDetails(name);
Validate.notNull(javaTypeDetails, "The type specified, '%s', doesn't exist", name.getFullyQualifiedTypeName());
final String declaredByMetadataId = PhysicalTypeIdentifier.createIdentifier(name, pathResolver.getFocusedPath(Path.SRC_MAIN_JAVA));
final List<FieldMetadata> constructorFields = new ArrayList<FieldMetadata>();
final List<? extends FieldMetadata> declaredFields = javaTypeDetails.getDeclaredFields();
if (fields != null) {
for (final String field : fields) {
declared: for (final FieldMetadata declaredField : declaredFields) {
if (field.equals(declaredField.getFieldName().getSymbolName())) {
constructorFields.add(declaredField);
break declared;
}
}
}
if (constructorFields.isEmpty()) {
// class, so return without creating any constructor
throw new IllegalArgumentException(String.format("The set of fields provided for the constructor does not exist in the class '%s'", name));
}
}
// Search for an existing constructor
final List<JavaType> parameterTypes = new ArrayList<JavaType>();
for (final FieldMetadata fieldMetadata : constructorFields) {
parameterTypes.add(fieldMetadata.getFieldType());
}
final ConstructorMetadata result = javaTypeDetails.getDeclaredConstructor(parameterTypes);
if (result != null) {
// Found an existing constructor on this class
throw new IllegalArgumentException(String.format("The class '%s' already has a constructor method with the same arguments and it cannot " + "be created. Use '--force' parameter to overrite it.", name));
}
final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
bodyBuilder.appendFormalLine("super();");
for (final FieldMetadata field : constructorFields) {
final String fieldName = field.getFieldName().getSymbolName();
bodyBuilder.appendFormalLine("this." + fieldName + " = " + fieldName + ";");
parameterNames.add(field.getFieldName());
}
// Create the constructor
final ConstructorMetadataBuilder constructorBuilder = new ConstructorMetadataBuilder(declaredByMetadataId);
constructorBuilder.setModifier(Modifier.PUBLIC);
constructorBuilder.setParameterTypes(AnnotatedJavaType.convertFromJavaTypes(parameterTypes));
constructorBuilder.setParameterNames(parameterNames);
constructorBuilder.setBodyBuilder(bodyBuilder);
final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(javaTypeDetails);
cidBuilder.addConstructor(constructorBuilder);
typeManagementService.createOrUpdateTypeOnDisk(cidBuilder.build());
}
Aggregations