Search in sources :

Example 21 with TypeKind

use of javax.lang.model.type.TypeKind in project lwjgl by LWJGL.

the class Utils method getMethodReturnType.

public static String getMethodReturnType(ExecutableElement method, GLreturn return_annotation, boolean buffer) {
    VariableElement return_param = null;
    for (VariableElement param : method.getParameters()) {
        if (param.getSimpleName().toString().equals(return_annotation.value())) {
            return_param = param;
            break;
        }
    }
    if (return_param == null) {
        throw new RuntimeException("The @GLreturn parameter \"" + return_annotation.value() + "\" could not be found in method: " + method);
    }
    TypeKind kind = NativeTypeTranslator.getPrimitiveKindFromBufferClass(Utils.getJavaType(return_param.asType()));
    if (return_param.getAnnotation(GLboolean.class) != null) {
        kind = TypeKind.BOOLEAN;
    }
    if (kind == TypeKind.BYTE && (return_param.getAnnotation(GLchar.class) != null || return_param.getAnnotation(GLcharARB.class) != null)) {
        return "String";
    } else {
        final String type = JavaTypeTranslator.getPrimitiveClassFromKind(kind).getName();
        return buffer ? Character.toUpperCase(type.charAt(0)) + type.substring(1) : type;
    }
}
Also used : TypeKind(javax.lang.model.type.TypeKind) GLboolean(org.lwjgl.util.generator.opengl.GLboolean)

Example 22 with TypeKind

use of javax.lang.model.type.TypeKind in project lwjgl by LWJGL.

the class FieldsGenerator method validateField.

private static void validateField(VariableElement field) {
    // Check if field is "public static final"
    Set<Modifier> modifiers = field.getModifiers();
    if (modifiers.size() != 3 || !modifiers.contains(Modifier.PUBLIC) || !modifiers.contains(Modifier.STATIC) || !modifiers.contains(Modifier.FINAL)) {
        throw new RuntimeException("Field " + field.getSimpleName() + " is not declared public static final");
    }
    // Check suported types (int, long, float, String)
    TypeMirror field_type = field.asType();
    if ("java.lang.String".equals(field_type.toString())) {
    } else if (field_type instanceof PrimitiveType) {
        PrimitiveType field_type_prim = (PrimitiveType) field_type;
        TypeKind field_kind = field_type_prim.getKind();
        if (field_kind != TypeKind.INT && field_kind != TypeKind.LONG && field_kind != TypeKind.FLOAT && field_kind != TypeKind.BYTE) {
            throw new RuntimeException("Field " + field.getSimpleName() + " is not of type 'int', 'long', 'float' or 'byte' " + field_kind.toString());
        }
    } else {
        throw new RuntimeException("Field " + field.getSimpleName() + " is not a primitive type or String");
    }
    Object field_value = field.getConstantValue();
    if (field_value == null) {
        throw new RuntimeException("Field " + field.getSimpleName() + " has no initial value");
    }
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) TypeKind(javax.lang.model.type.TypeKind) PrimitiveType(javax.lang.model.type.PrimitiveType) Modifier(javax.lang.model.element.Modifier)

Example 23 with TypeKind

use of javax.lang.model.type.TypeKind in project ceylon-compiler by ceylon.

the class TestTypeKind method testIsPrimitive.

static int testIsPrimitive() {
    int failures = 0;
    // The eight primitive types
    Set<TypeKind> primitives = // 1
    EnumSet.of(// 1
    BOOLEAN, // 2
    BYTE, // 3
    CHAR, // 4
    DOUBLE, // 5
    FLOAT, // 6
    INT, // 7
    LONG, // 8
    SHORT);
    for (TypeKind tk : TypeKind.values()) {
        boolean primitiveness;
        if ((primitiveness = tk.isPrimitive()) != primitives.contains(tk)) {
            failures++;
            System.err.println("Unexpected isPrimitive value " + primitiveness + "for " + tk);
        }
    }
    return failures;
}
Also used : TypeKind(javax.lang.model.type.TypeKind)

Example 24 with TypeKind

use of javax.lang.model.type.TypeKind in project ngAndroid by davityle.

the class TypeUtils method getOperatorKind.

/**
     * rules for this method are found here
     *
     * http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.6.2
     *
     * If any operand is of a reference type, it is subjected to unboxing conversion (§5.1.8).
     * Widening primitive conversion (§5.1.2) is applied to convert either or both operands as specified by the following rules:
     * If either operand is of type double, the other is converted to double.
     * Otherwise, if either operand is of type float, the other is converted to float.
     * Otherwise, if either operand is of type long, the other is converted to long.
     * Otherwise, both operands are converted to type int.
     *
     * test this hardcore..
     *
     * @param leftMirror
     * @param rightMirror
     * @param operator
     * @return
     */
public TypeMirror getOperatorKind(TypeMirror leftMirror, TypeMirror rightMirror, TokenType.BinaryOperator operator) {
    if (leftMirror == null || rightMirror == null)
        return null;
    TypeKind leftKind = leftMirror.getKind();
    TypeKind rightKind = rightMirror.getKind();
    if (isString(leftMirror))
        return leftMirror;
    if (isString(rightMirror))
        return rightMirror;
    if (either(leftKind, rightKind, TypeKind.BOOLEAN)) {
        if (operator != null) {
            messageUtils.error(Option.<Element>absent(), "Cannot perform perform the operation '%s' with a boolean type. (%s %s %s)", operator.toString(), leftMirror, operator.toString(), rightMirror);
        }
        return null;
    }
    if (leftKind.isPrimitive() && rightKind.isPrimitive()) {
        if (typeUtils.isSameType(leftMirror, rightMirror))
            return typeUtils.getPrimitiveType(leftKind);
        if (either(leftKind, rightKind, TypeKind.DOUBLE))
            return typeUtils.getPrimitiveType(TypeKind.DOUBLE);
        if (either(leftKind, rightKind, TypeKind.FLOAT))
            return typeUtils.getPrimitiveType(TypeKind.FLOAT);
        if (either(leftKind, rightKind, TypeKind.LONG))
            return typeUtils.getPrimitiveType(TypeKind.LONG);
        if (either(leftKind, rightKind, TypeKind.INT) || either(leftKind, rightKind, TypeKind.CHAR) || either(leftKind, rightKind, TypeKind.SHORT) || either(leftKind, rightKind, TypeKind.BYTE)) {
            return typeUtils.getPrimitiveType(TypeKind.INT);
        }
    }
    TypeMirror intMirror = typeUtils.getPrimitiveType(TypeKind.INT);
    if (both(assignable(intMirror, leftMirror), assignable(intMirror, rightMirror)))
        return intMirror;
    TypeMirror longMirror = typeUtils.getPrimitiveType(TypeKind.LONG);
    if (both(assignable(longMirror, leftMirror), assignable(longMirror, rightMirror)))
        return longMirror;
    TypeMirror floatMirror = typeUtils.getPrimitiveType(TypeKind.FLOAT);
    if (both(assignable(floatMirror, leftMirror), assignable(floatMirror, rightMirror)))
        return floatMirror;
    TypeMirror doubleMirror = typeUtils.getPrimitiveType(TypeKind.DOUBLE);
    if (both(assignable(doubleMirror, leftMirror), assignable(doubleMirror, rightMirror)))
        return doubleMirror;
    return null;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) TypeKind(javax.lang.model.type.TypeKind)

Aggregations

TypeKind (javax.lang.model.type.TypeKind)24 TypeMirror (javax.lang.model.type.TypeMirror)8 TypeElement (javax.lang.model.element.TypeElement)6 VariableElement (javax.lang.model.element.VariableElement)4 DeclaredType (javax.lang.model.type.DeclaredType)4 Element (javax.lang.model.element.Element)3 ExecutableElement (javax.lang.model.element.ExecutableElement)3 PrimitiveType (javax.lang.model.type.PrimitiveType)3 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Set (java.util.Set)2 AnnotationMirror (javax.lang.model.element.AnnotationMirror)2 Test (org.junit.Test)2 Function (com.google.common.base.Function)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 ClassName (com.squareup.javapoet.ClassName)1 JavaFile (com.squareup.javapoet.JavaFile)1 MethodSpec (com.squareup.javapoet.MethodSpec)1 TypeSpec (com.squareup.javapoet.TypeSpec)1