Search in sources :

Example 1 with TypeKind

use of javax.lang.model.type.TypeKind in project buck by facebook.

the class TreeBackedTypesTest method testIsSameTypePrimitiveType.

@Test
public void testIsSameTypePrimitiveType() throws IOException {
    initCompiler();
    for (TypeKind typeKind : TypeKind.values()) {
        if (typeKind.isPrimitive()) {
            PrimitiveType primitiveType = types.getPrimitiveType(typeKind);
            PrimitiveType primitiveType2 = types.getPrimitiveType(typeKind);
            assertSameType(primitiveType, primitiveType2);
        }
    }
}
Also used : TypeKind(javax.lang.model.type.TypeKind) PrimitiveType(javax.lang.model.type.PrimitiveType) Test(org.junit.Test)

Example 2 with TypeKind

use of javax.lang.model.type.TypeKind in project immutables by immutables.

the class TypeStringProvider method process.

void process() {
    if (startType.getKind().isPrimitive()) {
        // taking a shortcut for primitives
        String typeName = Ascii.toLowerCase(startType.getKind().name());
        this.rawTypeName = typeName;
        this.returnTypeName = typeName;
        List<? extends AnnotationMirror> annotations = AnnotationMirrors.from(startType);
        if (!annotations.isEmpty()) {
            returnTypeName = typeAnnotationsToBuffer(annotations).append(typeName).toString();
        }
    } else {
        this.buffer = new StringBuilder(100);
        caseType(startType);
        if (workaroundTypeString != null) {
            // to not mix the mess, we just replace buffer with workaround produced type string
            this.buffer = new StringBuilder(workaroundTypeString);
        }
        // It seems that array type annotations are not exposed in javac
        // Nested type argument's type annotations are not exposed as well (in javac)
        // So currently we instert only for top level, declared type (here),
        // and primitives (see above)
        TypeKind k = startType.getKind();
        if (k == TypeKind.DECLARED || k == TypeKind.ERROR) {
            insertTypeAnnotationsIfPresent(startType, 0, rawTypeName.length());
        }
        this.returnTypeName = buffer.toString();
    }
}
Also used : TypeKind(javax.lang.model.type.TypeKind)

Example 3 with TypeKind

use of javax.lang.model.type.TypeKind in project bazel by bazelbuild.

the class TypesUtils method widenedNumericType.

/**
     * Returns the widened numeric type for an arithmetic operation
     * performed on a value of the left type and the right type.
     * Defined in JLS 5.6.2.  We return a {@link TypeKind} because
     * creating a {@link TypeMirror} requires a {@link Types} object
     * from the {@link javax.annotation.processing.ProcessingEnvironment}.
     *
     * @return  the result of widening numeric conversion, or NONE when
     *          the conversion cannot be performed
     */
public static TypeKind widenedNumericType(TypeMirror left, TypeMirror right) {
    if (!isNumeric(left) || !isNumeric(right)) {
        return TypeKind.NONE;
    }
    TypeKind leftKind = left.getKind();
    TypeKind rightKind = right.getKind();
    if (leftKind == TypeKind.DOUBLE || rightKind == TypeKind.DOUBLE) {
        return TypeKind.DOUBLE;
    }
    if (leftKind == TypeKind.FLOAT || rightKind == TypeKind.FLOAT) {
        return TypeKind.FLOAT;
    }
    if (leftKind == TypeKind.LONG || rightKind == TypeKind.LONG) {
        return TypeKind.LONG;
    }
    return TypeKind.INT;
}
Also used : TypeKind(javax.lang.model.type.TypeKind)

Example 4 with TypeKind

use of javax.lang.model.type.TypeKind in project LoganSquare by bluelinelabs.

the class JsonFieldHolder method getGetter.

public static String getGetter(Element element, Elements elements) {
    TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();
    TypeKind elementTypeKind = element.asType().getKind();
    String elementName = element.getSimpleName().toString();
    String elementNameLowerCase = elementName.toLowerCase();
    List<String> possibleMethodNames = new ArrayList<>();
    possibleMethodNames.add("get" + elementNameLowerCase);
    if (elementTypeKind == TypeKind.BOOLEAN) {
        possibleMethodNames.add("is" + elementNameLowerCase);
        possibleMethodNames.add("has" + elementNameLowerCase);
        possibleMethodNames.add(elementNameLowerCase);
    }
    // Handle the case where variables are named in the form mVariableName instead of just variableName
    if (elementName.length() > 1 && elementName.charAt(0) == 'm' && (elementName.charAt(1) >= 'A' && elementName.charAt(1) <= 'Z')) {
        possibleMethodNames.add("get" + elementNameLowerCase.substring(1));
        if (elementTypeKind == TypeKind.BOOLEAN) {
            possibleMethodNames.add("is" + elementNameLowerCase.substring(1));
            possibleMethodNames.add("has" + elementNameLowerCase.substring(1));
            possibleMethodNames.add(elementNameLowerCase.substring(1));
        }
    }
    List<? extends Element> elementMembers = elements.getAllMembers(enclosingElement);
    List<ExecutableElement> elementMethods = ElementFilter.methodsIn(elementMembers);
    for (ExecutableElement methodElement : elementMethods) {
        if (methodElement.getParameters().size() == 0) {
            String methodNameString = methodElement.getSimpleName().toString();
            String methodNameLowerCase = methodNameString.toLowerCase();
            if (possibleMethodNames.contains(methodNameLowerCase)) {
                if (methodElement.getParameters().size() == 0) {
                    if (methodElement.getReturnType().toString().equals(element.asType().toString())) {
                        return methodNameString;
                    }
                }
            }
        }
    }
    return null;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) TypeKind(javax.lang.model.type.TypeKind) ArrayList(java.util.ArrayList)

Example 5 with TypeKind

use of javax.lang.model.type.TypeKind in project j2objc by google.

the class OperatorRewriter method getPromotionSuffix.

/**
   * Some operator functions are given a suffix indicating the promotion type of
   * the operands according to JLS 5.6.2.
   */
private static String getPromotionSuffix(Assignment node) {
    if (!needsPromotionSuffix(node.getOperator())) {
        return "";
    }
    TypeKind lhsKind = node.getLeftHandSide().getTypeMirror().getKind();
    TypeKind rhsKind = node.getRightHandSide().getTypeMirror().getKind();
    if (lhsKind == TypeKind.DOUBLE || rhsKind == TypeKind.DOUBLE) {
        return "D";
    }
    if (lhsKind == TypeKind.FLOAT || rhsKind == TypeKind.FLOAT) {
        return "F";
    }
    if (lhsKind == TypeKind.LONG || rhsKind == TypeKind.LONG) {
        return "J";
    }
    return "I";
}
Also used : 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