Search in sources :

Example 36 with ClassOrInterfaceTypeDetails

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

the class NewUpdateCompilationUnitTest 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.updateAndGetCompilationUnitContents(file.getCanonicalPath(), newSimpleInterfaceDetails);
    saveResult(file, result, "-addedField");
    checkSimpleClass(result);
    assertTrue(result.contains("private String newFieldAddedByCode = \"Create by code\";"));
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) JavaType(org.springframework.roo.model.JavaType) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) File(java.io.File) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder) Test(org.junit.Test)

Example 37 with ClassOrInterfaceTypeDetails

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

the class SeleniumOperationsImpl method generateTest.

/**
 * Creates a new Selenium testcase
 *
 * @param controller the JavaType of the controller under test (required)
 * @param name the name of the test case (optional)
 * @param serverURL the URL of the Selenium server (optional)
 */
@Override
public void generateTest(final JavaType controller, String name, String serverURL) {
    Validate.notNull(controller, "Controller type required");
    final ClassOrInterfaceTypeDetails controllerTypeDetails = typeLocationService.getTypeDetails(controller);
    Validate.notNull(controllerTypeDetails, "Class or interface type details for type '%s' could not be resolved", controller);
    final LogicalPath path = PhysicalTypeIdentifier.getPath(controllerTypeDetails.getDeclaredByMetadataId());
    final String webScaffoldMetadataIdentifier = WebScaffoldMetadata.createIdentifier(controller, path);
    final WebScaffoldMetadata webScaffoldMetadata = (WebScaffoldMetadata) metadataService.get(webScaffoldMetadataIdentifier);
    Validate.notNull(webScaffoldMetadata, "Web controller '%s' does not appear to be an automatic, scaffolded controller", controller.getFullyQualifiedTypeName());
    // allow the creation of new instances for the form backing object
    if (!webScaffoldMetadata.getAnnotationValues().isCreate()) {
        LOGGER.warning("The controller you specified does not allow the creation of new instances of the form backing object. No Selenium tests created.");
        return;
    }
    if (!serverURL.endsWith("/")) {
        serverURL = serverURL + "/";
    }
    final JavaType formBackingType = webScaffoldMetadata.getAnnotationValues().getFormBackingObject();
    final String relativeTestFilePath = "selenium/test-" + formBackingType.getSimpleTypeName().toLowerCase() + ".xhtml";
    final String seleniumPath = pathResolver.getFocusedIdentifier(Path.SRC_MAIN_WEBAPP, relativeTestFilePath);
    final InputStream templateInputStream = FileUtils.getInputStream(getClass(), "selenium-template.xhtml");
    Validate.notNull(templateInputStream, "Could not acquire selenium.xhtml template");
    final Document document = XmlUtils.readXml(templateInputStream);
    final Element root = (Element) document.getLastChild();
    if (root == null || !"html".equals(root.getNodeName())) {
        throw new IllegalArgumentException("Could not parse selenium test case template file!");
    }
    name = name != null ? name : "Selenium test for " + controller.getSimpleTypeName();
    XmlUtils.findRequiredElement("/html/head/title", root).setTextContent(name);
    XmlUtils.findRequiredElement("/html/body/table/thead/tr/td", root).setTextContent(name);
    final Element tbody = XmlUtils.findRequiredElement("/html/body/table/tbody", root);
    tbody.appendChild(openCommand(document, serverURL + projectOperations.getProjectName(projectOperations.getFocusedModuleName()) + "/" + webScaffoldMetadata.getAnnotationValues().getPath() + "?form"));
    final ClassOrInterfaceTypeDetails formBackingTypeDetails = typeLocationService.getTypeDetails(formBackingType);
    Validate.notNull(formBackingType, "Class or interface type details for type '%s' could not be resolved", formBackingType);
    final MemberDetails memberDetails = memberDetailsScanner.getMemberDetails(getClass().getName(), formBackingTypeDetails);
    // Add composite PK identifier fields if needed
    for (final FieldMetadata field : persistenceMemberLocator.getEmbeddedIdentifierFields(formBackingType)) {
        final JavaType fieldType = field.getFieldType();
        if (!fieldType.isCommonCollectionType() && !isSpecialType(fieldType)) {
            final FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(field);
            final String fieldName = field.getFieldName().getSymbolName();
            fieldBuilder.setFieldName(new JavaSymbolName(fieldName + "." + fieldName));
            tbody.appendChild(typeCommand(document, fieldBuilder.build()));
        }
    }
    // Add all other fields
    final List<FieldMetadata> fields = webMetadataService.getScaffoldEligibleFieldMetadata(formBackingType, memberDetails, null);
    for (final FieldMetadata field : fields) {
        final JavaType fieldType = field.getFieldType();
        if (!fieldType.isCommonCollectionType() && !isSpecialType(fieldType)) {
            tbody.appendChild(typeCommand(document, field));
        }
    }
    tbody.appendChild(clickAndWaitCommand(document, "//input[@id = 'proceed']"));
    // Add verifications for all other fields
    for (final FieldMetadata field : fields) {
        final JavaType fieldType = field.getFieldType();
        if (!fieldType.isCommonCollectionType() && !isSpecialType(fieldType)) {
            tbody.appendChild(verifyTextCommand(document, formBackingType, field));
        }
    }
    fileManager.createOrUpdateTextFileIfRequired(seleniumPath, XmlUtils.nodeToString(document), false);
    manageTestSuite(relativeTestFilePath, name, serverURL);
    // Install selenium-maven-plugin
    installMavenPlugin();
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) InputStream(java.io.InputStream) Element(org.w3c.dom.Element) LogicalPath(org.springframework.roo.project.LogicalPath) Document(org.w3c.dom.Document) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder) SpringJavaType(org.springframework.roo.model.SpringJavaType) JavaType(org.springframework.roo.model.JavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) WebScaffoldMetadata(org.springframework.roo.addon.web.mvc.controller.addon.scaffold.WebScaffoldMetadata) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails)

Example 38 with ClassOrInterfaceTypeDetails

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

the class WebFlowCommands method getClassPossibleValues.

@CliOptionAutocompleteIndicator(command = "web flow", param = "class", help = "You should specify an existing and serializable class for option " + "'--class'.", validate = false)
public List<String> getClassPossibleValues(ShellContext shellContext) {
    // Get current value of class
    String currentText = shellContext.getParameters().get("class");
    List<String> allPossibleValues = new ArrayList<String>();
    // Getting all existing entities
    Set<ClassOrInterfaceTypeDetails> domainClassesInProject = typeLocationService.findClassesOrInterfaceDetailsWithAnnotation(RooJavaType.ROO_JPA_ENTITY, RooJavaType.ROO_DTO);
    for (ClassOrInterfaceTypeDetails classDetails : domainClassesInProject) {
        // Check if class implements serializable (needed for WebFlow)
        boolean isSerializable = false;
        // First, chech for @RooSerializable
        if (classDetails.getAnnotation(RooJavaType.ROO_SERIALIZABLE) != null) {
            isSerializable = true;
        }
        // Check for the explicit 'implements Serializable'
        if (!isSerializable) {
            List<JavaType> implementsTypes = classDetails.getImplementsTypes();
            for (JavaType type : implementsTypes) {
                if (type.equals(JdkJavaType.SERIALIZABLE)) {
                    isSerializable = true;
                    break;
                }
            }
        }
        if (isSerializable) {
            // Add to possible values
            String name = replaceTopLevelPackageString(classDetails, currentText);
            if (!allPossibleValues.contains(name)) {
                allPossibleValues.add(name);
            }
        }
    }
    if (allPossibleValues.isEmpty()) {
        // Any entity or DTO in project is serializable
        LOGGER.info("Any auto-complete value offered because the project hasn't any entity " + "or DTO which implement Serializable");
    }
    return allPossibleValues;
}
Also used : JdkJavaType(org.springframework.roo.model.JdkJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) ArrayList(java.util.ArrayList) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) CliOptionAutocompleteIndicator(org.springframework.roo.shell.CliOptionAutocompleteIndicator)

Example 39 with ClassOrInterfaceTypeDetails

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

the class WebFlowOperationsImpl method installWebFlow.

/**
 * See {@link WebFlowOperations#installWebFlow(String, String)}.
 */
@Override
public void installWebFlow(final String flowName, final String moduleName, JavaType klass) {
    this.flowName = flowName.toLowerCase();
    // Add WebFlow project configuration
    installWebFlowConfiguration(moduleName);
    String targetDirectory = pathResolver.getIdentifier(moduleName, Path.SRC_MAIN_RESOURCES, "/templates/".concat(this.flowName));
    if (fileManager.exists(targetDirectory)) {
        throw new IllegalStateException("Flow directory already exists: " + targetDirectory);
    }
    // Copy Web Flow template views and *-flow.xml to project
    Map<String, String> replacements = new HashMap<String, String>();
    replacements.put("__WEBFLOW-ID__", this.flowName);
    copyDirectoryContents("*.html", targetDirectory, replacements, klass);
    createWebFlowFromTemplate(targetDirectory, replacements, klass);
    // Add localized messages for Web Flow labels
    addLocalizedMessages(moduleName);
    // Getting all thymeleaf controllers
    Set<ClassOrInterfaceTypeDetails> thymeleafControllers = getTypeLocationService().findClassesOrInterfaceDetailsWithAnnotation(RooJavaType.ROO_THYMELEAF);
    if (thymeleafControllers.isEmpty()) {
        LOGGER.log(Level.INFO, "WARNING: Menu view has not been updated because doesn't exists any Thymeleaf controller.");
        return;
    }
    // Update menu calling to the thymeleaf Metadata of the annotated @RooThymeleaf
    Iterator<ClassOrInterfaceTypeDetails> it = thymeleafControllers.iterator();
    ClassOrInterfaceTypeDetails thymeleafController = it.next();
    String controllerMetadataKey = ThymeleafMetadata.createIdentifier(thymeleafController);
    getMetadataService().evictAndGet(controllerMetadataKey);
}
Also used : HashMap(java.util.HashMap) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)

Example 40 with ClassOrInterfaceTypeDetails

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

the class SeiImplMetadataProviderImpl method getMetadata.

@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
    // Getting annotated class details
    ClassOrInterfaceTypeDetails endpoint = governorPhysicalTypeMetadata.getMemberHoldingTypeDetails();
    AnnotationMetadata seiImplAnnotation = endpoint.getAnnotation(ROO_SEI_IMPL);
    AnnotationAttributeValue<?> seiAttr = seiImplAnnotation.getAttribute(new JavaSymbolName("sei"));
    Validate.notNull(seiAttr, "ERROR: You must provide a valid SEI to be able to generate a Endpoint.");
    // Getting SEI from annotation
    JavaType seiType = (JavaType) seiAttr.getValue();
    // Getting SEI details
    ClassOrInterfaceTypeDetails seiTypeDetails = getTypeLocationService().getTypeDetails(seiType);
    // Getting SEI Metadata
    final String seiMetadataId = SeiMetadata.createIdentifier(seiTypeDetails.getType(), PhysicalTypeIdentifier.getPath(seiTypeDetails.getDeclaredByMetadataId()));
    final SeiMetadata seiMetadata = (SeiMetadata) getMetadataService().get(seiMetadataId);
    // Getting SEI methods from service and save it
    Map<MethodMetadata, MethodMetadata> seiMethods = seiMetadata.getSeiMethods();
    // Registering dependency between SeiMetadata and this one, to be able to
    // update Endpoint if SEI changes
    final String seiMetadataKey = SeiMetadata.createIdentifier(seiTypeDetails);
    registerDependency(seiMetadataKey, metadataIdentificationString);
    return new SeiImplMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, getProjectOperations().getTopLevelPackage(""), endpoint, seiType, seiMetadata.getService(), seiMethods);
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Aggregations

ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)232 JavaType (org.springframework.roo.model.JavaType)114 ArrayList (java.util.ArrayList)87 RooJavaType (org.springframework.roo.model.RooJavaType)86 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)52 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)43 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)42 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)40 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)39 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)36 JpaJavaType (org.springframework.roo.model.JpaJavaType)34 SpringJavaType (org.springframework.roo.model.SpringJavaType)28 CliOptionAutocompleteIndicator (org.springframework.roo.shell.CliOptionAutocompleteIndicator)24 JpaEntityMetadata (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata)23 JdkJavaType (org.springframework.roo.model.JdkJavaType)22 Test (org.junit.Test)20 File (java.io.File)19 List (java.util.List)19 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)19 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)17