Search in sources :

Example 1 with EnumDetails

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

the class IdentifierMetadata method setDateAnnotations.

private void setDateAnnotations(final String columnDefinition, final List<AnnotationMetadataBuilder> annotations) {
    // Add JSR 220 @Temporal annotation to date fields
    String temporalType = StringUtils.defaultIfEmpty(StringUtils.upperCase(columnDefinition), "DATE");
    if ("DATETIME".equals(temporalType)) {
        // ROO-2606
        temporalType = "TIMESTAMP";
    }
    final AnnotationMetadataBuilder temporalBuilder = new AnnotationMetadataBuilder(TEMPORAL);
    temporalBuilder.addEnumAttribute("value", new EnumDetails(TEMPORAL_TYPE, new JavaSymbolName(temporalType)));
    annotations.add(temporalBuilder);
    final AnnotationMetadataBuilder dateTimeFormatBuilder = new AnnotationMetadataBuilder(DATE_TIME_FORMAT);
    dateTimeFormatBuilder.addStringAttribute("style", "M-");
    annotations.add(dateTimeFormatBuilder);
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) EnumDetails(org.springframework.roo.model.EnumDetails) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 2 with EnumDetails

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

the class JavaParserAnnotationMetadataBuilder method convert.

private AnnotationAttributeValue<?> convert(JavaSymbolName annotationName, final Expression expression, final CompilationUnitServices compilationUnitServices) {
    if (annotationName == null) {
        annotationName = new JavaSymbolName("__ARRAY_ELEMENT__");
    }
    if (expression instanceof AnnotationExpr) {
        final AnnotationExpr annotationExpr = (AnnotationExpr) expression;
        final AnnotationMetadata value = getInstance(annotationExpr, compilationUnitServices).build();
        return new NestedAnnotationAttributeValue(annotationName, value);
    }
    if (expression instanceof BooleanLiteralExpr) {
        final boolean value = ((BooleanLiteralExpr) expression).getValue();
        return new BooleanAttributeValue(annotationName, value);
    }
    if (expression instanceof CharLiteralExpr) {
        final String value = ((CharLiteralExpr) expression).getValue();
        Validate.isTrue(value.length() == 1, "Expected a char expression, but instead received '%s' for attribute '%s'", value, annotationName);
        final char c = value.charAt(0);
        return new CharAttributeValue(annotationName, c);
    }
    if (expression instanceof LongLiteralExpr) {
        String value = ((LongLiteralExpr) expression).getValue();
        Validate.isTrue(value.toUpperCase().endsWith("L"), "Expected long literal expression '%s' to end in 'l' or 'L'", value);
        value = value.substring(0, value.length() - 1);
        final long l = new Long(value);
        return new LongAttributeValue(annotationName, l);
    }
    if (expression instanceof IntegerLiteralExpr) {
        final String value = ((IntegerLiteralExpr) expression).getValue();
        final int i = new Integer(value);
        return new IntegerAttributeValue(annotationName, i);
    }
    if (expression instanceof DoubleLiteralExpr) {
        String value = ((DoubleLiteralExpr) expression).getValue();
        boolean floatingPrecisionOnly = false;
        if (value.toUpperCase().endsWith("F")) {
            value = value.substring(0, value.length() - 1);
            floatingPrecisionOnly = true;
        }
        if (value.toUpperCase().endsWith("D")) {
            value = value.substring(0, value.length() - 1);
        }
        final double d = new Double(value);
        return new DoubleAttributeValue(annotationName, d, floatingPrecisionOnly);
    }
    if (expression instanceof BinaryExpr) {
        String result = "";
        BinaryExpr current = (BinaryExpr) expression;
        while (current != null) {
            String right = "";
            if (current.getRight() instanceof StringLiteralExpr) {
                right = ((StringLiteralExpr) current.getRight()).getValue();
            } else if (current.getRight() instanceof NameExpr) {
                right = ((NameExpr) current.getRight()).getName();
            }
            result = right + result;
            if (current.getLeft() instanceof StringLiteralExpr) {
                final String left = ((StringLiteralExpr) current.getLeft()).getValue();
                result = left + result;
            }
            if (current.getLeft() instanceof BinaryExpr) {
                current = (BinaryExpr) current.getLeft();
            } else {
                current = null;
            }
        }
        return new StringAttributeValue(annotationName, result);
    }
    if (expression instanceof StringLiteralExpr) {
        final String value = ((StringLiteralExpr) expression).getValue();
        return new StringAttributeValue(annotationName, value);
    }
    if (expression instanceof FieldAccessExpr) {
        final FieldAccessExpr field = (FieldAccessExpr) expression;
        final String fieldName = field.getField();
        // Determine the type
        final Expression scope = field.getScope();
        NameExpr nameToFind = null;
        if (scope instanceof FieldAccessExpr) {
            final FieldAccessExpr fScope = (FieldAccessExpr) scope;
            nameToFind = JavaParserUtils.getNameExpr(fScope.toString());
        } else if (scope instanceof NameExpr) {
            nameToFind = (NameExpr) scope;
        } else {
            throw new UnsupportedOperationException("A FieldAccessExpr for '" + field.getScope() + "' should return a NameExpr or FieldAccessExpr (was " + field.getScope().getClass().getName() + ")");
        }
        final JavaType fieldType = JavaParserUtils.getJavaType(compilationUnitServices, nameToFind, null);
        final EnumDetails enumDetails = new EnumDetails(fieldType, new JavaSymbolName(fieldName));
        return new EnumAttributeValue(annotationName, enumDetails);
    }
    if (expression instanceof NameExpr) {
        final NameExpr field = (NameExpr) expression;
        final String name = field.getName();
        // As we have no way of finding out the real type
        final JavaType fieldType = new JavaType("unknown.Object");
        final EnumDetails enumDetails = new EnumDetails(fieldType, new JavaSymbolName(name));
        return new EnumAttributeValue(annotationName, enumDetails);
    }
    if (expression instanceof ClassExpr) {
        final ClassExpr clazz = (ClassExpr) expression;
        final Type nameToFind = clazz.getType();
        final JavaType javaType = JavaParserUtils.getJavaType(compilationUnitServices, nameToFind, null);
        return new ClassAttributeValue(annotationName, javaType);
    }
    if (expression instanceof ArrayInitializerExpr) {
        final ArrayInitializerExpr castExp = (ArrayInitializerExpr) expression;
        final List<AnnotationAttributeValue<?>> arrayElements = new ArrayList<AnnotationAttributeValue<?>>();
        for (final Expression e : castExp.getValues()) {
            arrayElements.add(convert(null, e, compilationUnitServices));
        }
        return new ArrayAttributeValue<AnnotationAttributeValue<?>>(annotationName, arrayElements);
    }
    if (expression instanceof UnaryExpr) {
        final UnaryExpr castExp = (UnaryExpr) expression;
        if (castExp.getOperator() == Operator.negative) {
            String value = castExp.toString();
            value = value.toUpperCase().endsWith("L") ? value.substring(0, value.length() - 1) : value;
            final long l = new Long(value);
            return new LongAttributeValue(annotationName, l);
        } else {
            throw new UnsupportedOperationException("Only negative operator in UnaryExpr is supported");
        }
    }
    throw new UnsupportedOperationException("Unable to parse annotation attribute '" + annotationName + "' due to unsupported annotation expression '" + expression.getClass().getName() + "'");
}
Also used : IntegerLiteralExpr(com.github.antlrjavaparser.api.expr.IntegerLiteralExpr) ClassAttributeValue(org.springframework.roo.classpath.details.annotations.ClassAttributeValue) AnnotationExpr(com.github.antlrjavaparser.api.expr.AnnotationExpr) MarkerAnnotationExpr(com.github.antlrjavaparser.api.expr.MarkerAnnotationExpr) SingleMemberAnnotationExpr(com.github.antlrjavaparser.api.expr.SingleMemberAnnotationExpr) NormalAnnotationExpr(com.github.antlrjavaparser.api.expr.NormalAnnotationExpr) DoubleAttributeValue(org.springframework.roo.classpath.details.annotations.DoubleAttributeValue) StringLiteralExpr(com.github.antlrjavaparser.api.expr.StringLiteralExpr) NameExpr(com.github.antlrjavaparser.api.expr.NameExpr) ArrayList(java.util.ArrayList) CharAttributeValue(org.springframework.roo.classpath.details.annotations.CharAttributeValue) EnumDetails(org.springframework.roo.model.EnumDetails) EnumAttributeValue(org.springframework.roo.classpath.details.annotations.EnumAttributeValue) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ArrayInitializerExpr(com.github.antlrjavaparser.api.expr.ArrayInitializerExpr) BooleanLiteralExpr(com.github.antlrjavaparser.api.expr.BooleanLiteralExpr) LongLiteralExpr(com.github.antlrjavaparser.api.expr.LongLiteralExpr) FieldAccessExpr(com.github.antlrjavaparser.api.expr.FieldAccessExpr) StringAttributeValue(org.springframework.roo.classpath.details.annotations.StringAttributeValue) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue) ArrayAttributeValue(org.springframework.roo.classpath.details.annotations.ArrayAttributeValue) AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue) BinaryExpr(com.github.antlrjavaparser.api.expr.BinaryExpr) IntegerAttributeValue(org.springframework.roo.classpath.details.annotations.IntegerAttributeValue) CharLiteralExpr(com.github.antlrjavaparser.api.expr.CharLiteralExpr) UnaryExpr(com.github.antlrjavaparser.api.expr.UnaryExpr) BooleanAttributeValue(org.springframework.roo.classpath.details.annotations.BooleanAttributeValue) JavaType(org.springframework.roo.model.JavaType) JavaType(org.springframework.roo.model.JavaType) Type(com.github.antlrjavaparser.api.type.Type) LongAttributeValue(org.springframework.roo.classpath.details.annotations.LongAttributeValue) DoubleLiteralExpr(com.github.antlrjavaparser.api.expr.DoubleLiteralExpr) Expression(com.github.antlrjavaparser.api.expr.Expression) ClassExpr(com.github.antlrjavaparser.api.expr.ClassExpr)

Example 3 with EnumDetails

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

the class ControllerOperationsImpl method getRooControllerAnnotation.

/**
 * Method that returns @RooController annotation
 *
 * @param entity
 *            Entity over which create the controller
 * @param pathPrefix
 *            Prefix to use in RequestMapping
 * @param controllerType
 *            Indicates the controller type
 * @return
 */
private AnnotationMetadataBuilder getRooControllerAnnotation(final JavaType entity, final String pathPrefix, final ControllerType controllerType) {
    final List<AnnotationAttributeValue<?>> rooControllerAttributes = new ArrayList<AnnotationAttributeValue<?>>();
    rooControllerAttributes.add(new ClassAttributeValue(new JavaSymbolName("entity"), entity));
    if (StringUtils.isNotEmpty(pathPrefix)) {
        rooControllerAttributes.add(new StringAttributeValue(new JavaSymbolName("pathPrefix"), pathPrefix));
    }
    rooControllerAttributes.add(new EnumAttributeValue(new JavaSymbolName("type"), new EnumDetails(RooJavaType.ROO_ENUM_CONTROLLER_TYPE, new JavaSymbolName(controllerType.name()))));
    return new AnnotationMetadataBuilder(RooJavaType.ROO_CONTROLLER, rooControllerAttributes);
}
Also used : AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ClassAttributeValue(org.springframework.roo.classpath.details.annotations.ClassAttributeValue) ArrayList(java.util.ArrayList) EnumAttributeValue(org.springframework.roo.classpath.details.annotations.EnumAttributeValue) EnumDetails(org.springframework.roo.model.EnumDetails) StringAttributeValue(org.springframework.roo.classpath.details.annotations.StringAttributeValue) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 4 with EnumDetails

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

the class DbreMetadata method getField.

private FieldMetadataBuilder getField(final JavaSymbolName fieldName, final Column column, final String tableName, final boolean includeNonPortable) {
    JavaType fieldType = column.getJavaType();
    Validate.notNull(fieldType, "Field type for column '%s' in table '%s' is null", column.getName(), tableName);
    // boolean primitive
    if (fieldType.equals(JavaType.BOOLEAN_OBJECT) && column.isRequired()) {
        fieldType = JavaType.BOOLEAN_PRIMITIVE;
    }
    // Add annotations to field
    final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
    // Add @Column annotation
    final AnnotationMetadataBuilder columnBuilder = new AnnotationMetadataBuilder(COLUMN);
    columnBuilder.addStringAttribute(NAME, column.getEscapedName());
    if (includeNonPortable) {
        columnBuilder.addStringAttribute("columnDefinition", column.getTypeName());
    }
    // Add length attribute for Strings
    int columnSize = column.getColumnSize();
    if (columnSize < 4000 && fieldType.equals(JavaType.STRING)) {
        columnBuilder.addIntegerAttribute("length", columnSize);
    }
    // Add precision and scale attributes for numeric fields
    if (columnSize > 0 && JdkJavaType.isDecimalType(fieldType)) {
        columnBuilder.addIntegerAttribute("precision", columnSize);
        int scale = column.getScale();
        if (scale > 0) {
            columnBuilder.addIntegerAttribute("scale", scale);
        }
    }
    // Add unique = true to @Column if applicable
    if (column.isUnique()) {
        columnBuilder.addBooleanAttribute("unique", true);
    }
    annotations.add(columnBuilder);
    // Add @NotNull if applicable
    if (column.isRequired()) {
        annotations.add(new AnnotationMetadataBuilder(NOT_NULL));
    }
    // Add JSR 220 @Temporal annotation to date fields
    if (fieldType.equals(DATE) || fieldType.equals(CALENDAR)) {
        final AnnotationMetadataBuilder temporalBuilder = new AnnotationMetadataBuilder(TEMPORAL);
        temporalBuilder.addEnumAttribute(VALUE, new EnumDetails(TEMPORAL_TYPE, new JavaSymbolName(column.getJdbcType())));
        annotations.add(temporalBuilder);
        final AnnotationMetadataBuilder dateTimeFormatBuilder = new AnnotationMetadataBuilder(DATE_TIME_FORMAT);
        if (fieldType.equals(DATE)) {
            dateTimeFormatBuilder.addStringAttribute("style", "M-");
        } else {
            dateTimeFormatBuilder.addStringAttribute("style", "MM");
        }
        if (fieldName.getSymbolName().equals(CREATED)) {
            columnBuilder.addBooleanAttribute("updatable", false);
        }
        annotations.add(dateTimeFormatBuilder);
    }
    // Add @Lob for CLOB fields if applicable
    if (column.getJdbcType().equals("CLOB")) {
        annotations.add(new AnnotationMetadataBuilder(LOB));
    }
    final FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(getId(), Modifier.PRIVATE, annotations, fieldName, fieldType);
    if (fieldName.getSymbolName().equals(CREATED)) {
        if (fieldType.equals(DATE)) {
            fieldBuilder.setFieldInitializer("new java.util.Date()");
        } else {
            fieldBuilder.setFieldInitializer("java.util.Calendar.getInstance()");
        }
    }
    return fieldBuilder;
}
Also used : JdkJavaType(org.springframework.roo.model.JdkJavaType) JavaType(org.springframework.roo.model.JavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) ArrayList(java.util.ArrayList) EnumDetails(org.springframework.roo.model.EnumDetails) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 5 with EnumDetails

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

the class DtoFieldCreatorProvider method createStringField.

@Override
public void createStringField(ClassOrInterfaceTypeDetails cid, JavaSymbolName fieldName, boolean notNull, boolean nullRequired, String decimalMin, String decimalMax, Integer sizeMin, Integer sizeMax, String regexp, String column, String comment, boolean unique, String value, boolean lob, boolean permitReservedWords, boolean transientModifier, List<AnnotationMetadataBuilder> extraAnnotations) {
    final String physicalTypeIdentifier = cid.getDeclaredByMetadataId();
    final StringField fieldDetails = new StringField(physicalTypeIdentifier, fieldName);
    fieldDetails.setNotNull(notNull);
    fieldDetails.setNullRequired(nullRequired);
    if (decimalMin != null) {
        fieldDetails.setDecimalMin(decimalMin);
    }
    if (decimalMax != null) {
        fieldDetails.setDecimalMax(decimalMax);
    }
    if (sizeMin != null) {
        fieldDetails.setSizeMin(sizeMin);
    }
    if (sizeMax != null) {
        fieldDetails.setSizeMax(sizeMax);
    }
    if (regexp != null) {
        fieldDetails.setRegexp(regexp.replace("\\", "\\\\"));
    }
    if (column != null) {
        fieldDetails.setColumn(column);
    }
    if (comment != null) {
        fieldDetails.setComment(comment);
    }
    if (unique) {
        fieldDetails.setUnique(true);
    }
    if (value != null) {
        fieldDetails.setValue(value);
    }
    if (lob) {
        fieldDetails.getInitedAnnotations().add(new AnnotationMetadataBuilder("javax.persistence.Lob"));
        // ROO-3722: Add LAZY load in @Lob fields using @Basic
        AnnotationMetadataBuilder basicAnnotation = new AnnotationMetadataBuilder("javax.persistence.Basic");
        basicAnnotation.addEnumAttribute("fetch", new EnumDetails(new JavaType("javax.persistence.FetchType"), new JavaSymbolName("LAZY")));
        fieldDetails.getInitedAnnotations().add(basicAnnotation);
    }
    if (extraAnnotations != null && !extraAnnotations.isEmpty()) {
        fieldDetails.addAnnotations(extraAnnotations);
    }
    insertField(fieldDetails, permitReservedWords, false);
}
Also used : JdkJavaType(org.springframework.roo.model.JdkJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) StringField(org.springframework.roo.classpath.operations.jsr303.StringField) EnumDetails(org.springframework.roo.model.EnumDetails) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Aggregations

EnumDetails (org.springframework.roo.model.EnumDetails)26 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)25 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)17 ArrayList (java.util.ArrayList)16 JavaType (org.springframework.roo.model.JavaType)12 AnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue)11 EnumAttributeValue (org.springframework.roo.classpath.details.annotations.EnumAttributeValue)11 StringAttributeValue (org.springframework.roo.classpath.details.annotations.StringAttributeValue)9 NestedAnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)7 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)6 RooJavaType (org.springframework.roo.model.RooJavaType)6 ArrayAttributeValue (org.springframework.roo.classpath.details.annotations.ArrayAttributeValue)5 JdkJavaType (org.springframework.roo.model.JdkJavaType)5 List (java.util.List)4 FieldMetadataBuilder (org.springframework.roo.classpath.details.FieldMetadataBuilder)4 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)3 FieldDetails (org.springframework.roo.classpath.details.FieldDetails)3 BooleanAttributeValue (org.springframework.roo.classpath.details.annotations.BooleanAttributeValue)3 ClassAttributeValue (org.springframework.roo.classpath.details.annotations.ClassAttributeValue)3 StringField (org.springframework.roo.classpath.operations.jsr303.StringField)3