Search in sources :

Example 1 with PrimitiveType

use of com.github.antlrjavaparser.api.type.PrimitiveType in project spring-roo by spring-projects.

the class JavaParserUtils method getType.

/**
 * Given a primitive type, computes the corresponding Java Parser type.
 * <p>
 * Presenting a non-primitive type to this method will throw an exception.
 * If you have a non-primitive type, use
 * {@link #importTypeIfRequired(JavaType, List, JavaType)} and then present
 * the {@link NameExpr} it returns to
 * {@link #getClassOrInterfaceType(NameExpr)}.
 *
 * @param javaType a primitive type (required, and must be primitive)
 * @return the equivalent Java Parser {@link Type}
 */
public static Type getType(final JavaType javaType) {
    Validate.notNull(javaType, "Java type required");
    Validate.isTrue(javaType.isPrimitive(), "Java type must be primitive to be presented to this method");
    if (javaType.equals(JavaType.VOID_PRIMITIVE)) {
        return new VoidType();
    } else if (javaType.equals(JavaType.BOOLEAN_PRIMITIVE)) {
        return new PrimitiveType(Primitive.Boolean);
    } else if (javaType.equals(JavaType.BYTE_PRIMITIVE)) {
        return new PrimitiveType(Primitive.Byte);
    } else if (javaType.equals(JavaType.CHAR_PRIMITIVE)) {
        return new PrimitiveType(Primitive.Char);
    } else if (javaType.equals(JavaType.DOUBLE_PRIMITIVE)) {
        return new PrimitiveType(Primitive.Double);
    } else if (javaType.equals(JavaType.FLOAT_PRIMITIVE)) {
        return new PrimitiveType(Primitive.Float);
    } else if (javaType.equals(JavaType.INT_PRIMITIVE)) {
        return new PrimitiveType(Primitive.Int);
    } else if (javaType.equals(JavaType.LONG_PRIMITIVE)) {
        return new PrimitiveType(Primitive.Long);
    } else if (javaType.equals(JavaType.SHORT_PRIMITIVE)) {
        return new PrimitiveType(Primitive.Short);
    }
    throw new IllegalStateException("Unknown primitive " + javaType);
}
Also used : VoidType(com.github.antlrjavaparser.api.type.VoidType) PrimitiveType(com.github.antlrjavaparser.api.type.PrimitiveType)

Example 2 with PrimitiveType

use of com.github.antlrjavaparser.api.type.PrimitiveType in project spring-roo by spring-projects.

the class JavaParserUtils method getJavaType.

/**
 * Resolves the effective {@link JavaType} a {@link Type} represents. A
 * {@link Type} includes low-level types such as void, arrays and
 * primitives.
 *
 * @param compilationUnitServices to use for package resolution (required)
 * @param type to locate (required)
 * @param typeParameters names to consider type parameters (can be null if
 *            there are none)
 * @return the {@link JavaType}, with proper indication of primitive and
 *         array status (never null)
 */
public static JavaType getJavaType(final CompilationUnitServices compilationUnitServices, final Type type, final Set<JavaSymbolName> typeParameters) {
    Validate.notNull(compilationUnitServices, "Compilation unit services required");
    Validate.notNull(type, "The reference type must be provided");
    if (type instanceof VoidType) {
        return JavaType.VOID_PRIMITIVE;
    }
    int array = 0;
    Type internalType = type;
    if (internalType instanceof ReferenceType) {
        array = ((ReferenceType) internalType).getArrayCount();
        if (array > 0) {
            internalType = ((ReferenceType) internalType).getType();
        }
    }
    if (internalType instanceof PrimitiveType) {
        final PrimitiveType pt = (PrimitiveType) internalType;
        if (pt.getType().equals(Primitive.Boolean)) {
            return new JavaType(Boolean.class.getName(), array, DataType.PRIMITIVE, null, null);
        }
        if (pt.getType().equals(Primitive.Char)) {
            return new JavaType(Character.class.getName(), array, DataType.PRIMITIVE, null, null);
        }
        if (pt.getType().equals(Primitive.Byte)) {
            return new JavaType(Byte.class.getName(), array, DataType.PRIMITIVE, null, null);
        }
        if (pt.getType().equals(Primitive.Short)) {
            return new JavaType(Short.class.getName(), array, DataType.PRIMITIVE, null, null);
        }
        if (pt.getType().equals(Primitive.Int)) {
            return new JavaType(Integer.class.getName(), array, DataType.PRIMITIVE, null, null);
        }
        if (pt.getType().equals(Primitive.Long)) {
            return new JavaType(Long.class.getName(), array, DataType.PRIMITIVE, null, null);
        }
        if (pt.getType().equals(Primitive.Float)) {
            return new JavaType(Float.class.getName(), array, DataType.PRIMITIVE, null, null);
        }
        if (pt.getType().equals(Primitive.Double)) {
            return new JavaType(Double.class.getName(), array, DataType.PRIMITIVE, null, null);
        }
        throw new IllegalStateException("Unsupported primitive '" + pt.getType() + "'");
    }
    if (internalType instanceof WildcardType) {
        // We only provide very primitive support for wildcard types; Roo
        // only needs metadata at the end of the day,
        // not complete binding support from an AST
        final WildcardType wt = (WildcardType) internalType;
        if (wt.getSuper() != null) {
            final ReferenceType rt = wt.getSuper();
            final ClassOrInterfaceType cit = (ClassOrInterfaceType) rt.getType();
            final JavaType effectiveType = getJavaTypeNow(compilationUnitServices, cit, typeParameters);
            return new JavaType(effectiveType.getFullyQualifiedTypeName(), rt.getArrayCount(), effectiveType.getDataType(), JavaType.WILDCARD_SUPER_ARG, effectiveType.getParameters());
        } else if (wt.getExtends() != null) {
            final ReferenceType rt = wt.getExtends();
            final ClassOrInterfaceType cit = (ClassOrInterfaceType) rt.getType();
            final JavaType effectiveType = getJavaTypeNow(compilationUnitServices, cit, typeParameters);
            return new JavaType(effectiveType.getFullyQualifiedTypeName(), rt.getArrayCount(), effectiveType.getDataType(), JavaType.WILDCARD_EXTENDS_ARG, effectiveType.getParameters());
        } else {
            return new JavaType(OBJECT.getFullyQualifiedTypeName(), 0, DataType.TYPE, JavaType.WILDCARD_NEITHER_ARG, null);
        }
    }
    ClassOrInterfaceType cit;
    if (internalType instanceof ClassOrInterfaceType) {
        cit = (ClassOrInterfaceType) internalType;
    } else if (internalType instanceof ReferenceType) {
        cit = (ClassOrInterfaceType) ((ReferenceType) type).getType();
    } else {
        throw new IllegalStateException("The presented type '" + internalType.getClass() + "' with value '" + internalType + "' is unsupported by JavaParserUtils");
    }
    final JavaType effectiveType = getJavaTypeNow(compilationUnitServices, cit, typeParameters);
    if (array > 0) {
        return new JavaType(effectiveType.getFullyQualifiedTypeName(), array, effectiveType.getDataType(), effectiveType.getArgName(), effectiveType.getParameters());
    }
    return effectiveType;
}
Also used : VoidType(com.github.antlrjavaparser.api.type.VoidType) ClassOrInterfaceType(com.github.antlrjavaparser.api.type.ClassOrInterfaceType) ReferenceType(com.github.antlrjavaparser.api.type.ReferenceType) WildcardType(com.github.antlrjavaparser.api.type.WildcardType) ClassOrInterfaceType(com.github.antlrjavaparser.api.type.ClassOrInterfaceType) DataType(org.springframework.roo.model.DataType) JdkJavaType(org.springframework.roo.model.JdkJavaType) VoidType(com.github.antlrjavaparser.api.type.VoidType) JavaType(org.springframework.roo.model.JavaType) PrimitiveType(com.github.antlrjavaparser.api.type.PrimitiveType) ReferenceType(com.github.antlrjavaparser.api.type.ReferenceType) Type(com.github.antlrjavaparser.api.type.Type) JdkJavaType(org.springframework.roo.model.JdkJavaType) JavaType(org.springframework.roo.model.JavaType) WildcardType(com.github.antlrjavaparser.api.type.WildcardType) PrimitiveType(com.github.antlrjavaparser.api.type.PrimitiveType)

Example 3 with PrimitiveType

use of com.github.antlrjavaparser.api.type.PrimitiveType in project spring-roo by spring-projects.

the class UpdateCompilationUnitUtils method equals.

/**
 * Compare two {@link Type}
 *
 * @param type
 * @param type2
 * @return
 */
private static boolean equals(final Type type, final Type type2) {
    if (ObjectUtils.equals(type, type2)) {
        return true;
    }
    if (type.getClass() != type2.getClass()) {
        return false;
    }
    if (type instanceof ClassOrInterfaceType) {
        final ClassOrInterfaceType cType = (ClassOrInterfaceType) type;
        final ClassOrInterfaceType cType2 = (ClassOrInterfaceType) type2;
        return cType.getName().equals(cType2.getName());
    } else if (type instanceof PrimitiveType) {
        final PrimitiveType pType = (PrimitiveType) type;
        final PrimitiveType pType2 = (PrimitiveType) type2;
        return pType.getType() == pType2.getType();
    } else if (type instanceof VoidType) {
        return true;
    } else if (type instanceof WildcardType) {
        final WildcardType wType = (WildcardType) type;
        final WildcardType wType2 = (WildcardType) type2;
        return equals(wType.getSuper(), wType2.getSuper()) && equals(wType.getExtends(), wType2.getExtends());
    }
    return false;
}
Also used : VoidType(com.github.antlrjavaparser.api.type.VoidType) WildcardType(com.github.antlrjavaparser.api.type.WildcardType) PrimitiveType(com.github.antlrjavaparser.api.type.PrimitiveType) ClassOrInterfaceType(com.github.antlrjavaparser.api.type.ClassOrInterfaceType)

Aggregations

PrimitiveType (com.github.antlrjavaparser.api.type.PrimitiveType)3 VoidType (com.github.antlrjavaparser.api.type.VoidType)3 ClassOrInterfaceType (com.github.antlrjavaparser.api.type.ClassOrInterfaceType)2 WildcardType (com.github.antlrjavaparser.api.type.WildcardType)2 ReferenceType (com.github.antlrjavaparser.api.type.ReferenceType)1 Type (com.github.antlrjavaparser.api.type.Type)1 DataType (org.springframework.roo.model.DataType)1 JavaType (org.springframework.roo.model.JavaType)1 JdkJavaType (org.springframework.roo.model.JdkJavaType)1