Search in sources :

Example 46 with ClassOrInterfaceTypeDetailsBuilder

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());
}
Also used : ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)

Example 47 with ClassOrInterfaceTypeDetailsBuilder

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());
}
Also used : LinkedHashSet(java.util.LinkedHashSet) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) JdkJavaType(org.springframework.roo.model.JdkJavaType) JavaType(org.springframework.roo.model.JavaType) ArrayList(java.util.ArrayList) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 48 with ClassOrInterfaceTypeDetailsBuilder

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());
}
Also used : ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)

Example 49 with ClassOrInterfaceTypeDetailsBuilder

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());
}
Also used : ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)

Example 50 with ClassOrInterfaceTypeDetailsBuilder

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());
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) ConstructorMetadataBuilder(org.springframework.roo.classpath.details.ConstructorMetadataBuilder) ArrayList(java.util.ArrayList) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) JdkJavaType(org.springframework.roo.model.JdkJavaType) JavaType(org.springframework.roo.model.JavaType) ConstructorMetadata(org.springframework.roo.classpath.details.ConstructorMetadata) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)

Aggregations

ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)68 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)54 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)43 JavaType (org.springframework.roo.model.JavaType)30 ArrayList (java.util.ArrayList)29 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)29 RooJavaType (org.springframework.roo.model.RooJavaType)26 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)19 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)14 SpringJavaType (org.springframework.roo.model.SpringJavaType)12 AnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue)10 ClassAttributeValue (org.springframework.roo.classpath.details.annotations.ClassAttributeValue)10 JpaJavaType (org.springframework.roo.model.JpaJavaType)10 Dependency (org.springframework.roo.project.Dependency)10 LogicalPath (org.springframework.roo.project.LogicalPath)10 Pom (org.springframework.roo.project.maven.Pom)9 List (java.util.List)8 NestedAnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)7 StringAttributeValue (org.springframework.roo.classpath.details.annotations.StringAttributeValue)7 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)7