Search in sources :

Example 56 with JavaSymbolName

use of org.springframework.roo.model.JavaSymbolName in project spring-roo by spring-projects.

the class JspOperationsImpl method getIndexMethod.

private MethodMetadataBuilder getIndexMethod(final String folderName, final String declaredByMetadataId) {
    final List<AnnotationMetadataBuilder> indexMethodAnnotations = new ArrayList<AnnotationMetadataBuilder>();
    indexMethodAnnotations.add(new AnnotationMetadataBuilder(REQUEST_MAPPING, new ArrayList<AnnotationAttributeValue<?>>()));
    final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    bodyBuilder.appendFormalLine("return \"" + folderName + "/index\";");
    final MethodMetadataBuilder indexMethodBuilder = new MethodMetadataBuilder(declaredByMetadataId, Modifier.PUBLIC, new JavaSymbolName("index"), JavaType.STRING, new ArrayList<AnnotatedJavaType>(), new ArrayList<JavaSymbolName>(), bodyBuilder);
    indexMethodBuilder.setAnnotations(indexMethodAnnotations);
    return indexMethodBuilder;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 57 with JavaSymbolName

use of org.springframework.roo.model.JavaSymbolName in project spring-roo by spring-projects.

the class JspOperationsImpl method createManualController.

/**
 * Creates a new Spring MVC controller.
 * <p>
 * Request mappings assigned by this method will always commence with "/"
 * and end with "/**". You may present this prefix and/or this suffix if you
 * wish, although it will automatically be added should it not be provided.
 *
 * @param controller the controller class to create (required)
 * @param preferredMapping the mapping this controller should adopt
 *            (optional; if unspecified it will be based on the controller
 *            name)
 */
public void createManualController(final JavaType controller, final String preferredMapping, final LogicalPath webappPath) {
    Validate.notNull(controller, "Controller Java Type required");
    // Create annotation @RequestMapping("/myobject/**")
    final ImmutablePair<String, String> folderAndMapping = getFolderAndMapping(preferredMapping, controller);
    final String folderName = folderAndMapping.getKey();
    final String resourceIdentifier = getPathResolver().getFocusedCanonicalPath(Path.SRC_MAIN_JAVA, controller);
    final String declaredByMetadataId = PhysicalTypeIdentifier.createIdentifier(controller, getProjectOperations().getPathResolver().getPath(resourceIdentifier));
    final List<MethodMetadataBuilder> methods = new ArrayList<MethodMetadataBuilder>();
    // Add HTTP post method
    methods.add(getHttpPostMethod(declaredByMetadataId));
    // Add index method
    methods.add(getIndexMethod(folderName, declaredByMetadataId));
    // Create Type definition
    final List<AnnotationMetadataBuilder> typeAnnotations = new ArrayList<AnnotationMetadataBuilder>();
    final List<AnnotationAttributeValue<?>> requestMappingAttributes = new ArrayList<AnnotationAttributeValue<?>>();
    requestMappingAttributes.add(new StringAttributeValue(new JavaSymbolName("value"), folderAndMapping.getValue()));
    final AnnotationMetadataBuilder requestMapping = new AnnotationMetadataBuilder(REQUEST_MAPPING, requestMappingAttributes);
    typeAnnotations.add(requestMapping);
    // Create annotation @Controller
    final List<AnnotationAttributeValue<?>> controllerAttributes = new ArrayList<AnnotationAttributeValue<?>>();
    final AnnotationMetadataBuilder controllerAnnotation = new AnnotationMetadataBuilder(CONTROLLER, controllerAttributes);
    typeAnnotations.add(controllerAnnotation);
    final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(declaredByMetadataId, Modifier.PUBLIC, controller, PhysicalTypeCategory.CLASS);
    cidBuilder.setAnnotations(typeAnnotations);
    cidBuilder.setDeclaredMethods(methods);
    getTypeManagementService().createOrUpdateTypeOnDisk(cidBuilder.build());
    installView(folderName, "/index", new JavaSymbolName(controller.getSimpleTypeName()).getReadableSymbolName() + " View", "Controller", null, false, webappPath);
}
Also used : AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) ArrayList(java.util.ArrayList) ClassOrInterfaceTypeDetailsBuilder(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder) StringAttributeValue(org.springframework.roo.classpath.details.annotations.StringAttributeValue) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 58 with JavaSymbolName

use of org.springframework.roo.model.JavaSymbolName in project spring-roo by spring-projects.

the class LinkFactoryMetadata method getToUriMethod.

/**
 * Generates a `toUri` method which generates URI's for the *Collection*
 * controller methods which are called from views.
 *
 * @param finderName
 * @param serviceFinderMethod
 * @return
 */
private MethodMetadata getToUriMethod() {
    // Define methodName
    final JavaSymbolName toUriMethodName = TO_URI_METHOD_NAME;
    // Define method argument types
    List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
    parameterTypes.add(stringArgument);
    parameterTypes.add(objectArrayArgument);
    parameterTypes.add(mapStringObjectArgument);
    // Return method if already exists
    MethodMetadata existingMethod = getGovernorMethod(toUriMethodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
    if (existingMethod != null) {
        return existingMethod;
    }
    // Define method argument names
    final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
    parameterNames.add(METHOD_NAME_ARGUMENT_NAME);
    parameterNames.add(PARAMETERS_ARGUMENT_NAME);
    parameterNames.add(PATH_VARIABLES_ARGUMENT_NAME);
    // Generate body
    final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // Include a conditional for every method
    for (MethodMetadata method : this.controllerMethods) {
        // Getting methodName
        String methodName = method.getMethodName().getSymbolName();
        // Getting methodParams
        List<String> methodParamsToNull = new ArrayList<String>();
        for (AnnotatedJavaType paramType : method.getParameterTypes()) {
            JavaType javaType = paramType.getJavaType();
            if (javaType.isPrimitive() && !javaType.isArray()) {
                if (JavaType.BOOLEAN_PRIMITIVE.equals(javaType)) {
                    methodParamsToNull.add("false");
                } else if (JavaType.INT_PRIMITIVE.equals(javaType)) {
                    methodParamsToNull.add("0");
                } else if (JavaType.LONG_PRIMITIVE.equals(javaType)) {
                    methodParamsToNull.add("0l");
                } else if (JavaType.FLOAT_PRIMITIVE.equals(javaType)) {
                    methodParamsToNull.add("0.0f");
                } else if (JavaType.DOUBLE_PRIMITIVE.equals(javaType)) {
                    methodParamsToNull.add("0.0");
                }
            } else {
                // Include a null declaration for every parameter
                methodParamsToNull.add("null");
            }
        }
        // if (METHOD_NAME_ARGUMENT_NAME.equals(methodNameConstant)) {
        bodyBuilder.appendFormalLine("if (%s.equals(%s)) {", METHOD_NAME_ARGUMENT_NAME, getConstantForMethodName(methodName).getFieldName());
        bodyBuilder.indent();
        // return MvcUriComponentsBuilder.fromMethodCall(MvcUriComponentsBuilder.on(getControllerClass()).list(null)).build();
        bodyBuilder.appendFormalLine("return %1$s.fromMethodCall(%1$s.on(%2$s()).%3$s(%4$s)).buildAndExpand(%5$s);", getNameOfJavaType(SpringletsJavaType.SPRINGLETS_MVC_URI_COMPONENTS_BUILDER), this.getControllerClassMethod.getMethodName(), methodName, StringUtils.join(methodParamsToNull, ", "), PATH_VARIABLES_ARGUMENT_NAME);
        bodyBuilder.indentRemove();
        // }
        bodyBuilder.appendFormalLine("}");
    }
    // throw new IllegalArgumentException("Invalid method name: " + METHOD_NAME_ARGUMENT_NAME);
    bodyBuilder.appendFormalLine("throw new IllegalArgumentException(\"Invalid method name: \" + %s);", METHOD_NAME_ARGUMENT_NAME);
    // Build method builder
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, toUriMethodName, SpringJavaType.URI_COMPONENTS, parameterTypes, parameterNames, bodyBuilder);
    return methodBuilder.build();
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) JdkJavaType(org.springframework.roo.model.JdkJavaType) SpringJavaType(org.springframework.roo.model.SpringJavaType) SpringletsJavaType(org.springframework.roo.model.SpringletsJavaType) JavaType(org.springframework.roo.model.JavaType) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)

Example 59 with JavaSymbolName

use of org.springframework.roo.model.JavaSymbolName in project spring-roo by spring-projects.

the class LinkFactoryMetadata method getControllerClassMethod.

private MethodMetadata getControllerClassMethod() {
    // Define methodName
    final JavaSymbolName methodName = GET_CONTROLLER_CLASS_METHOD_NAME;
    // Define method argument types
    List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
    // Return method if already exists
    MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
    if (existingMethod != null) {
        return existingMethod;
    }
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    bodyBuilder.appendFormalLine("return %s.class;", getNameOfJavaType(this.controller));
    // return CONTROLLER_CLASS;
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, JavaType.wrapperOf(JavaType.CLASS, this.controller), null);
    // Set method body
    methodBuilder.setBodyBuilder(bodyBuilder);
    return methodBuilder.build();
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)

Example 60 with JavaSymbolName

use of org.springframework.roo.model.JavaSymbolName 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)

Aggregations

JavaSymbolName (org.springframework.roo.model.JavaSymbolName)317 ArrayList (java.util.ArrayList)186 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)155 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)143 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)142 JavaType (org.springframework.roo.model.JavaType)133 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)121 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)119 SpringJavaType (org.springframework.roo.model.SpringJavaType)61 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)59 JdkJavaType (org.springframework.roo.model.JdkJavaType)59 FieldMetadataBuilder (org.springframework.roo.classpath.details.FieldMetadataBuilder)48 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)47 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)45 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)39 AnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue)36 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)29 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)29 StringAttributeValue (org.springframework.roo.classpath.details.annotations.StringAttributeValue)27 EnumDetails (org.springframework.roo.model.EnumDetails)25